23
23
#if defined(USBCON)
24
24
#ifdef CDC_ENABLED
25
25
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
-
41
26
typedef struct
42
27
{
43
28
u32 dwDTERate;
@@ -129,7 +114,6 @@ bool WEAK CDC_Setup(Setup& setup)
129
114
}
130
115
131
116
132
- int _serialPeek = -1 ;
133
117
void Serial_::begin (unsigned long baud_count)
134
118
{
135
119
}
@@ -142,55 +126,29 @@ void Serial_::end(void)
142
126
{
143
127
}
144
128
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
-
167
129
int Serial_::available (void )
168
130
{
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);
171
135
}
172
136
173
137
int Serial_::peek (void )
174
138
{
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;
181
142
}
182
143
183
144
int Serial_::read (void )
184
145
{
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 ;
192
149
return c;
193
- }
150
+ }
151
+ return USB_Recv (CDC_RX);
194
152
}
195
153
196
154
void Serial_::flush (void )
0 commit comments