@@ -56,6 +56,7 @@ static void ifaceSetupHook(unsigned, void*);
5656#define USB_TIMEOUT 50
5757#if BOARD_HAVE_SERIALUSB
5858bool USBSerial::_hasBegun = false ;
59+ bool USBSerial::_isBlocking = false ;
5960#endif
6061
6162USBSerial::USBSerial (void ) {
@@ -105,30 +106,37 @@ void USBSerial::end(void) {
105106}
106107
107108size_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 );
111111}
112112
113113size_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));
117115}
118116
119117size_t USBSerial::write (const uint8 *buf, uint32 len)
120118{
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))) {
123125 return 0 ;
124126 }
127+ #endif
125128
126129 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+ }
130138
131- return n ;
139+ return txed ;
132140}
133141
134142int USBSerial::available (void ) {
@@ -186,10 +194,6 @@ size_t USBSerial::readBytes(char *buf, const size_t& len)
186194/* Blocks forever until 1 byte is received */
187195int USBSerial::read (void ) {
188196 uint8 b;
189- /*
190- this->read(&b, 1);
191- return b;
192- */
193197
194198 if (usb_cdcacm_rx (&b, 1 )==0 )
195199 {
@@ -217,6 +221,16 @@ USBSerial::operator bool() {
217221 return usb_is_connected (USBLIB) && usb_is_configured (USBLIB) && usb_cdcacm_get_dtr ();
218222}
219223
224+ void USBSerial::enableBlockingTx (void )
225+ {
226+ _isBlocking=true ;
227+ }
228+ void USBSerial::disableBlockingTx (void )
229+ {
230+ _isBlocking=false ;
231+ }
232+
233+
220234#if BOARD_HAVE_SERIALUSB
221235 #ifdef SERIAL_USB
222236 USBSerial Serial;
@@ -265,12 +279,7 @@ static void ifaceSetupHook(unsigned hook __attribute__((unused)), void *requestv
265279 break ;
266280 }
267281#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+
274283}
275284
276285#define RESET_DELAY 100000
0 commit comments