@@ -106,7 +106,7 @@ size_t USBSerial::write(uint8_t ch) {
106
106
if (((UserTxBufPtrIn + 1 ) % APP_TX_DATA_SIZE) == UserTxBufPtrOut)
107
107
{
108
108
// Buffer full!!! Force a flush to not loose data and go on
109
- flush ();
109
+ CDC_flush ();
110
110
}
111
111
UserTxBufferFS[UserTxBufPtrIn] = ch;
112
112
UserTxBufPtrIn = ((UserTxBufPtrIn + 1 ) % APP_TX_DATA_SIZE);
@@ -117,36 +117,56 @@ size_t USBSerial::write(uint8_t ch) {
117
117
}
118
118
119
119
int 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;
121
127
}
122
128
123
129
int 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 ;
127
137
} else
128
- {
129
- unsigned char c = UserRxBufferFS[UserRxBufPtrOut];
138
+ {
139
+ unsigned char c = UserRxBufferFS[UserRxBufPtrOut];
130
140
UserRxBufPtrOut = ((UserRxBufPtrOut + 1 ) % APP_RX_DATA_SIZE);
141
+ CDC_enable_TIM_Interrupt ();
131
142
return c;
132
- }
143
+ }
133
144
}
134
145
135
146
int USBSerial::peek (void )
136
147
{
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 ;
140
155
} else
141
- {
142
- unsigned char c = UserRxBufferFS[UserRxBufPtrOut];
156
+ {
157
+ unsigned char c = UserRxBufferFS[UserRxBufPtrOut];
158
+ CDC_enable_TIM_Interrupt ();
143
159
return c;
144
- }
160
+ }
145
161
}
146
162
147
163
void USBSerial::flush (void )
148
164
{
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 ();
149
168
CDC_flush ();
169
+ CDC_enable_TIM_Interrupt ();
150
170
151
171
#if 0
152
172
/* Flush EP1 for data IN */
0 commit comments