diff --git a/stmhal/usbd_cdc_interface.c b/stmhal/usbd_cdc_interface.c
index a62f8a801fe11095ff430c655593187c6ab7e67e..c441e9c61cb5224cf9bcc6c47a9059a4997bf8d7 100644
--- a/stmhal/usbd_cdc_interface.c
+++ b/stmhal/usbd_cdc_interface.c
@@ -363,7 +363,7 @@ void USBD_CDC_SetInterrupt(int chr, void *data) {
 void USBD_CDC_Tx(const char *str, uint32_t len) {
     for (int i = 0; i < len; i++) {
         uint timeout = 200;
-        while (UserTxBufPtrIn + 1 == UserTxBufPtrOut) {
+        while (((UserTxBufPtrIn + 1) & (APP_TX_DATA_SIZE - 1)) == UserTxBufPtrOut) {
             if (timeout-- == 0) {
                 break;
             }