Skip to content

Commit 212f89e

Browse files
committed
stmhal: Improve USB CDC write function (increase timeout).
1 parent 0e4ba25 commit 212f89e

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

stmhal/usbd_cdc_interface.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,10 @@ void USBD_CDC_SetInterrupt(int chr, void *data) {
362362

363363
void USBD_CDC_Tx(const char *str, uint32_t len) {
364364
for (int i = 0; i < len; i++) {
365-
uint timeout = 200;
366-
while (((UserTxBufPtrIn + 1) & (APP_TX_DATA_SIZE - 1)) == UserTxBufPtrOut) {
367-
if (timeout-- == 0) {
368-
break;
369-
}
370-
HAL_Delay(1);
365+
// if the buffer is full, wait until it gets drained, with a timeout of 1000ms (wraparound of tick is taken care of by 2's complement arithmetic)
366+
uint32_t start = HAL_GetTick();
367+
while (((UserTxBufPtrIn + 1) & (APP_TX_DATA_SIZE - 1)) == UserTxBufPtrOut && HAL_GetTick() - start <= 1000) {
368+
__WFI(); // enter sleep mode, waiting for interrupt
371369
}
372370
UserTxBuffer[UserTxBufPtrIn] = str[i];
373371
UserTxBufPtrIn = (UserTxBufPtrIn + 1) & (APP_TX_DATA_SIZE - 1);

0 commit comments

Comments
 (0)