@@ -56,6 +56,7 @@ static void ifaceSetupHook(unsigned, void*);
56
56
#define USB_TIMEOUT 50
57
57
#if BOARD_HAVE_SERIALUSB
58
58
bool USBSerial::_hasBegun = false ;
59
+ bool USBSerial::_isBlocking = false ;
59
60
#endif
60
61
61
62
USBSerial::USBSerial (void ) {
@@ -105,30 +106,37 @@ void USBSerial::end(void) {
105
106
}
106
107
107
108
size_t USBSerial::write (uint8 ch) {
108
- size_t n = 0 ;
109
- this ->write (&ch, 1 );
110
- return n;
109
+
110
+ return this ->write (&ch, 1 );
111
111
}
112
112
113
113
size_t USBSerial::write (const char *str) {
114
- size_t n = 0 ;
115
- this ->write ((const uint8*)str, strlen (str));
116
- return n;
114
+ return this ->write ((const uint8*)str, strlen (str));
117
115
}
118
116
119
117
size_t USBSerial::write (const uint8 *buf, uint32 len)
120
118
{
121
- size_t n = 0 ;
122
- if (!(bool ) *this || !buf) {
119
+ #ifdef USB_SERIAL_REQUIRE_DTR
120
+ if (!(bool ) *this || !buf) {
121
+ return 0 ;
122
+ }
123
+ #else
124
+ if (!buf || !(usb_is_connected (USBLIB) && usb_is_configured (USBLIB))) {
123
125
return 0 ;
124
126
}
127
+ #endif
125
128
126
129
uint32 txed = 0 ;
127
- while (txed < len) {
128
- txed += usb_cdcacm_tx ((const uint8*)buf + txed, len - txed);
129
- }
130
+ if (!_isBlocking) {
131
+ txed = usb_cdcacm_tx ((const uint8*)buf + txed, len - txed);
132
+ }
133
+ else {
134
+ while (txed < len) {
135
+ txed += usb_cdcacm_tx ((const uint8*)buf + txed, len - txed);
136
+ }
137
+ }
130
138
131
- return n ;
139
+ return txed ;
132
140
}
133
141
134
142
int USBSerial::available (void ) {
@@ -186,10 +194,6 @@ size_t USBSerial::readBytes(char *buf, const size_t& len)
186
194
/* Blocks forever until 1 byte is received */
187
195
int USBSerial::read (void ) {
188
196
uint8 b;
189
- /*
190
- this->read(&b, 1);
191
- return b;
192
- */
193
197
194
198
if (usb_cdcacm_rx (&b, 1 )==0 )
195
199
{
@@ -217,6 +221,16 @@ USBSerial::operator bool() {
217
221
return usb_is_connected (USBLIB) && usb_is_configured (USBLIB) && usb_cdcacm_get_dtr ();
218
222
}
219
223
224
+ void USBSerial::enableBlockingTx (void )
225
+ {
226
+ _isBlocking=true ;
227
+ }
228
+ void USBSerial::disableBlockingTx (void )
229
+ {
230
+ _isBlocking=false ;
231
+ }
232
+
233
+
220
234
#if BOARD_HAVE_SERIALUSB
221
235
#ifdef SERIAL_USB
222
236
USBSerial Serial;
@@ -265,12 +279,7 @@ static void ifaceSetupHook(unsigned hook __attribute__((unused)), void *requestv
265
279
break ;
266
280
}
267
281
#endif
268
- #if false
269
- if ((usb_cdcacm_get_baud () == 1200 ) && (reset_state == DTR_NEGEDGE)) {
270
- iwdg_init (IWDG_PRE_4, 10 );
271
- while (1 );
272
- }
273
- #endif
282
+
274
283
}
275
284
276
285
#define RESET_DELAY 100000
0 commit comments