@@ -161,32 +161,21 @@ void Serial_::end(void)
161161{
162162}
163163
164- void Serial_::accept (void )
164+ void Serial_::accept (uint8_t *data, uint32_t size )
165165{
166- uint8_t buffer[CDC_SERIAL_BUFFER_SIZE];
167- uint32_t len = usb.recv (CDC_ENDPOINT_OUT, &buffer, CDC_SERIAL_BUFFER_SIZE);
168-
169- uint8_t enableInterrupts = ((__get_PRIMASK () & 0x1 ) == 0 );
170- __disable_irq ();
171-
172166 ring_buffer *ringBuffer = &cdc_rx_buffer;
173167 uint32_t i = ringBuffer->head ;
174-
175- // if we should be storing the received character into the location
176- // just before the tail (meaning that the head would advance to the
177- // current location of the tail), we're about to overflow the buffer
178- // and so we don't write the character or advance the head.
179- uint32_t k = 0 ;
180- while (len > 0 && !ringBuffer->full ) {
181- len--;
182- ringBuffer->buffer [i++] = buffer[k++];
168+ while (size--) {
169+ ringBuffer->buffer [i++] = *data;
170+ data++;
183171 i %= CDC_SERIAL_BUFFER_SIZE;
184- if (i == ringBuffer->tail )
185- ringBuffer->full = true ;
186172 }
187173 ringBuffer->head = i;
188- if (enableInterrupts) {
189- __enable_irq ();
174+ if (i == ringBuffer->tail ) ringBuffer->full = true ;
175+ if (availableForStore () < EPX_SIZE) {
176+ stalled = true ;
177+ } else {
178+ usb.epOut (CDC_ENDPOINT_OUT);
190179 }
191180}
192181
@@ -196,9 +185,6 @@ int Serial_::available(void)
196185 if (buffer->full ) {
197186 return CDC_SERIAL_BUFFER_SIZE;
198187 }
199- if (buffer->head == buffer->tail ) {
200- USB->DEVICE .DeviceEndpoint [CDC_ENDPOINT_OUT].EPINTENSET .reg = USB_DEVICE_EPINTENCLR_TRCPT (1 );
201- }
202188 return (uint32_t )(CDC_SERIAL_BUFFER_SIZE + buffer->head - buffer->tail ) % CDC_SERIAL_BUFFER_SIZE;
203189}
204190
@@ -228,11 +214,11 @@ int Serial_::read(void)
228214{
229215 ring_buffer *buffer = &cdc_rx_buffer;
230216
231- // if the head isn't ahead of the tail, we don't have any characters
232- if (buffer-> head == buffer-> tail && !buffer-> full )
217+ // if we have enough space enable OUT endpoint to receive more data
218+ if (stalled && availableForStore () >= EPX_SIZE )
233219 {
234- if (usb. available (CDC_ENDPOINT_OUT))
235- accept ( );
220+ stalled = false ;
221+ usb. epOut (CDC_ENDPOINT_OUT );
236222 }
237223 if (buffer->head == buffer->tail && !buffer->full )
238224 {
0 commit comments