Skip to content

Commit 155a238

Browse files
committed
- Improve CDC performance by a factor of 20.
- Fixes receive buffer overruns by suspending rx if there is not enough buffer available. - Reduce StackRxBufferFS to USB_OTG_FS_MAX_PACKET_SIZE - Removed enable/disable timer interrupt on receive
1 parent efb919e commit 155a238

File tree

3 files changed

+251
-252
lines changed

3 files changed

+251
-252
lines changed

Diff for: libraries/USBSerial/src/USBSerial.cpp

+9-20
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ size_t USBSerial::write(uint8_t ch) {
149149
CDC_disable_TIM_Interrupt();
150150

151151
if(((UserTxBufPtrIn + 1) % APP_TX_DATA_SIZE) == UserTxBufPtrOut)
152-
{
153-
// Buffer full!!! Force a flush to not loose data and go on
154-
CDC_flush();
152+
{
153+
CDC_flush(); // Buffer full!!! Force a flush to not loose data and go on
155154
}
156155
UserTxBufferFS[UserTxBufPtrIn] = ch;
157156
UserTxBufPtrIn = ((UserTxBufPtrIn + 1) % APP_TX_DATA_SIZE);
@@ -162,28 +161,21 @@ size_t USBSerial::write(uint8_t ch) {
162161
}
163162

164163
int USBSerial::available(void) {
165-
int ret;
166-
167-
CDC_disable_TIM_Interrupt();
168-
ret = ((APP_RX_DATA_SIZE + (UserRxBufPtrIn - UserRxBufPtrOut)) % APP_RX_DATA_SIZE);
169-
CDC_enable_TIM_Interrupt();
170-
171-
return ret;
164+
return ((APP_RX_DATA_SIZE + (UserRxBufPtrIn - UserRxBufPtrOut)) % APP_RX_DATA_SIZE);
172165
}
173166

174-
int USBSerial::read(void) {
175-
/* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
176-
/* value that we read is correct, we need to disable TIM Interrupt. */
177-
CDC_disable_TIM_Interrupt();
167+
int USBSerial::read(void)
168+
{
178169
if(UserRxBufPtrOut == UserRxBufPtrIn)
179170
{
180-
CDC_enable_TIM_Interrupt();
181171
return -1;
182-
} else
172+
}
173+
else
183174
{
184175
unsigned char c = UserRxBufferFS[UserRxBufPtrOut];
185176
UserRxBufPtrOut = ((UserRxBufPtrOut + 1) % APP_RX_DATA_SIZE);
186-
CDC_enable_TIM_Interrupt();
177+
178+
CDC_resume_receive();
187179
return c;
188180
}
189181
}
@@ -192,15 +184,12 @@ int USBSerial::peek(void)
192184
{
193185
/* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
194186
/* value that we read is correct, we need to disable TIM Interrupt. */
195-
CDC_disable_TIM_Interrupt();
196187
if(UserRxBufPtrOut == UserRxBufPtrIn)
197188
{
198-
CDC_enable_TIM_Interrupt();
199189
return -1;
200190
} else
201191
{
202192
unsigned char c = UserRxBufferFS[UserRxBufPtrOut];
203-
CDC_enable_TIM_Interrupt();
204193
return c;
205194
}
206195
}

0 commit comments

Comments
 (0)