@@ -106,7 +106,7 @@ size_t USBSerial::write(uint8_t ch) {
106106 if (((UserTxBufPtrIn + 1 ) % APP_TX_DATA_SIZE) == UserTxBufPtrOut)
107107 {
108108 // Buffer full!!! Force a flush to not loose data and go on
109- flush ();
109+ CDC_flush ();
110110 }
111111 UserTxBufferFS[UserTxBufPtrIn] = ch;
112112 UserTxBufPtrIn = ((UserTxBufPtrIn + 1 ) % APP_TX_DATA_SIZE);
@@ -117,36 +117,56 @@ size_t USBSerial::write(uint8_t ch) {
117117}
118118
119119int USBSerial::available (void ) {
120- return ((APP_RX_DATA_SIZE + (UserRxBufPtrIn - UserRxBufPtrOut)) % APP_RX_DATA_SIZE);
120+ int ret;
121+
122+ CDC_disable_TIM_Interrupt ();
123+ ret = ((APP_RX_DATA_SIZE + (UserRxBufPtrIn - UserRxBufPtrOut)) % APP_RX_DATA_SIZE);
124+ CDC_enable_TIM_Interrupt ();
125+
126+ return ret;
121127}
122128
123129int USBSerial::read (void ) {
124- if (UserRxBufPtrOut == UserRxBufPtrIn)
125- {
126- return -1 ;
130+ /* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
131+ /* value that we read is correct, we need to disable TIM Interrupt. */
132+ CDC_disable_TIM_Interrupt ();
133+ if (UserRxBufPtrOut == UserRxBufPtrIn)
134+ {
135+ CDC_enable_TIM_Interrupt ();
136+ return -1 ;
127137 } else
128- {
129- unsigned char c = UserRxBufferFS[UserRxBufPtrOut];
138+ {
139+ unsigned char c = UserRxBufferFS[UserRxBufPtrOut];
130140 UserRxBufPtrOut = ((UserRxBufPtrOut + 1 ) % APP_RX_DATA_SIZE);
141+ CDC_enable_TIM_Interrupt ();
131142 return c;
132- }
143+ }
133144}
134145
135146int USBSerial::peek (void )
136147{
137- if (UserRxBufPtrOut == UserRxBufPtrIn)
138- {
139- return -1 ;
148+ /* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
149+ /* value that we read is correct, we need to disable TIM Interrupt. */
150+ CDC_disable_TIM_Interrupt ();
151+ if (UserRxBufPtrOut == UserRxBufPtrIn)
152+ {
153+ CDC_enable_TIM_Interrupt ();
154+ return -1 ;
140155 } else
141- {
142- unsigned char c = UserRxBufferFS[UserRxBufPtrOut];
156+ {
157+ unsigned char c = UserRxBufferFS[UserRxBufPtrOut];
158+ CDC_enable_TIM_Interrupt ();
143159 return c;
144- }
160+ }
145161}
146162
147163void USBSerial::flush (void )
148164{
165+ /* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
166+ /* value that we read is correct, we need to disable TIM Interrupt. */
167+ CDC_disable_TIM_Interrupt ();
149168 CDC_flush ();
169+ CDC_enable_TIM_Interrupt ();
150170
151171#if 0
152172 /* Flush EP1 for data IN */
0 commit comments