2323#if defined(USBCON)
2424#ifdef CDC_ENABLED
2525
26- #if (RAMEND < 1000)
27- #define SERIAL_BUFFER_SIZE 16
28- #else
29- #define SERIAL_BUFFER_SIZE 64
30- #endif
31-
32- struct ring_buffer
33- {
34- unsigned char buffer[SERIAL_BUFFER_SIZE];
35- volatile int head;
36- volatile int tail;
37- };
38-
39- ring_buffer cdc_rx_buffer = { { 0 }, 0 , 0 };
40-
4126typedef struct
4227{
4328 u32 dwDTERate;
@@ -129,7 +114,6 @@ bool WEAK CDC_Setup(Setup& setup)
129114}
130115
131116
132- int _serialPeek = -1 ;
133117void Serial_::begin (unsigned long baud_count)
134118{
135119}
@@ -142,55 +126,29 @@ void Serial_::end(void)
142126{
143127}
144128
145- void Serial_::accept (void )
146- {
147- ring_buffer *buffer = &cdc_rx_buffer;
148- int i = (unsigned int )(buffer->head +1 ) % SERIAL_BUFFER_SIZE;
149-
150- // if we should be storing the received character into the location
151- // just before the tail (meaning that the head would advance to the
152- // current location of the tail), we're about to overflow the buffer
153- // and so we don't write the character or advance the head.
154-
155- // while we have room to store a byte
156- while (i != buffer->tail ) {
157- int c = USB_Recv (CDC_RX);
158- if (c == -1 )
159- break ; // no more data
160- buffer->buffer [buffer->head ] = c;
161- buffer->head = i;
162-
163- i = (unsigned int )(buffer->head +1 ) % SERIAL_BUFFER_SIZE;
164- }
165- }
166-
167129int Serial_::available (void )
168130{
169- ring_buffer *buffer = &cdc_rx_buffer;
170- return (unsigned int )(SERIAL_BUFFER_SIZE + buffer->head - buffer->tail ) % SERIAL_BUFFER_SIZE;
131+ if (peek_buffer >= 0 ) {
132+ return 1 ;
133+ }
134+ return USB_Available (CDC_RX);
171135}
172136
173137int Serial_::peek (void )
174138{
175- ring_buffer *buffer = &cdc_rx_buffer;
176- if (buffer->head == buffer->tail ) {
177- return -1 ;
178- } else {
179- return buffer->buffer [buffer->tail ];
180- }
139+ if (peek_buffer < 0 )
140+ peek_buffer = USB_Recv (CDC_RX);
141+ return peek_buffer;
181142}
182143
183144int Serial_::read (void )
184145{
185- ring_buffer *buffer = &cdc_rx_buffer;
186- // if the head isn't ahead of the tail, we don't have any characters
187- if (buffer->head == buffer->tail ) {
188- return -1 ;
189- } else {
190- unsigned char c = buffer->buffer [buffer->tail ];
191- buffer->tail = (unsigned int )(buffer->tail + 1 ) % SERIAL_BUFFER_SIZE;
146+ if (peek_buffer >= 0 ) {
147+ int c = peek_buffer;
148+ peek_buffer = -1 ;
192149 return c;
193- }
150+ }
151+ return USB_Recv (CDC_RX);
194152}
195153
196154void Serial_::flush (void )
0 commit comments