32
32
33
33
#if defined(USBCON)
34
34
35
- #define CDC_TX_PACKET_SIZE 64
36
-
37
35
stm32l0_usbd_cdc_t stm32l0_usbd_cdc;
38
36
39
37
extern int (*stm32l0_stdio_put)(char , FILE*);
@@ -60,6 +58,9 @@ CDC::CDC(struct _stm32l0_usbd_cdc_t *usbd_cdc, void (*serialEventRun)(void))
60
58
_tx_count = 0 ;
61
59
_tx_size = 0 ;
62
60
61
+ _tx_data2 = NULL ;
62
+ _tx_size2 = 0 ;
63
+
63
64
if (serialEventRun) {
64
65
g_serialEventRun = serialEventRun;
65
66
}
@@ -119,6 +120,10 @@ int CDC::availableForWrite(void)
119
120
return 0 ;
120
121
}
121
122
123
+ if (_tx_size2 != 0 ) {
124
+ return 0 ;
125
+ }
126
+
122
127
return CDC_TX_BUFFER_SIZE - _tx_count;
123
128
}
124
129
@@ -176,6 +181,16 @@ size_t CDC::write(const uint8_t *buffer, size_t size)
176
181
return 0 ;
177
182
}
178
183
184
+ if (_tx_size2 != 0 ) {
185
+ if (_nonblocking || (__get_IPSR () != 0 )) {
186
+ return 0 ;
187
+ }
188
+
189
+ while (_tx_size2 != 0 ) {
190
+ __WFE ();
191
+ }
192
+ }
193
+
179
194
count = 0 ;
180
195
181
196
while (count < size) {
@@ -195,7 +210,7 @@ size_t CDC::write(const uint8_t *buffer, size_t size)
195
210
if (tx_size > (CDC_TX_BUFFER_SIZE - tx_read)) {
196
211
tx_size = (CDC_TX_BUFFER_SIZE - tx_read);
197
212
}
198
-
213
+
199
214
if (tx_size > CDC_TX_PACKET_SIZE) {
200
215
tx_size = CDC_TX_PACKET_SIZE;
201
216
}
@@ -267,6 +282,52 @@ size_t CDC::write(const uint8_t *buffer, size_t size)
267
282
return count;
268
283
}
269
284
285
+ bool CDC::write (const uint8_t *buffer, size_t size, void (*callback)(void ))
286
+ {
287
+ return write (buffer, size, Callback (callback));
288
+ }
289
+
290
+ bool CDC::write (const uint8_t *buffer, size_t size, Callback callback)
291
+ {
292
+ if (!_enabled) {
293
+ return false ;
294
+ }
295
+
296
+ if (size == 0 ) {
297
+ return false ;
298
+ }
299
+
300
+ if (_tx_size2 != 0 ) {
301
+ return false ;
302
+ }
303
+
304
+ _completionCallback = callback;
305
+
306
+ _tx_data2 = buffer;
307
+ _tx_size2 = size;
308
+
309
+ if (!_tx_busy) {
310
+
311
+ _tx_busy = true ;
312
+
313
+ if (!stm32l0_usbd_cdc_transmit (_usbd_cdc, _tx_data2, _tx_size2, (stm32l0_usbd_cdc_done_callback_t )CDC::_doneCallback, (void *)this )) {
314
+ _tx_busy = false ;
315
+
316
+ _tx_size2 = 0 ;
317
+ _tx_data2 = NULL ;
318
+
319
+ return false ;
320
+ }
321
+ }
322
+
323
+ return true ;
324
+ }
325
+
326
+ bool CDC::done ()
327
+ {
328
+ return (_tx_size2 == 0 );
329
+ }
330
+
270
331
void CDC::setNonBlocking (bool enabled)
271
332
{
272
333
_nonblocking = enabled;
@@ -294,37 +355,73 @@ void CDC::disableWakeup()
294
355
295
356
CDC::operator bool ()
296
357
{
297
- return (_enabled && (stm32l0_usbd_cdc_info. lineState & 1 ));
358
+ return (_enabled && (stm32l0_usbd_cdc_line_state (_usbd_cdc) & USB_CDC_LINE_STATE_DTR ));
298
359
}
299
360
300
361
unsigned long CDC::baud ()
301
362
{
302
- return stm32l0_usbd_cdc_info.dwDTERate ;
363
+ stm32l0_usbd_cdc_line_coding_t line_coding;
364
+
365
+ if (_enabled)
366
+ {
367
+ stm32l0_usbd_cdc_line_coding (_usbd_cdc, &line_coding);
368
+
369
+ return line_coding.dwDTERate ;
370
+ }
371
+
372
+ return STM32L0_USBD_CDC_LINE_CODING_DTE_RATE;
303
373
}
304
374
305
375
uint8_t CDC::stopbits ()
306
376
{
307
- return stm32l0_usbd_cdc_info.bCharFormat ;
377
+ stm32l0_usbd_cdc_line_coding_t line_coding;
378
+
379
+ if (_enabled)
380
+ {
381
+ stm32l0_usbd_cdc_line_coding (_usbd_cdc, &line_coding);
382
+
383
+ return line_coding.bCharFormat ;
384
+ }
385
+
386
+ return STM32L0_USBD_CDC_LINE_CODING_CHAR_FORMAT;
308
387
}
309
388
310
389
uint8_t CDC::paritytype ()
311
390
{
312
- return stm32l0_usbd_cdc_info.bParityType ;
391
+ stm32l0_usbd_cdc_line_coding_t line_coding;
392
+
393
+ if (_enabled)
394
+ {
395
+ stm32l0_usbd_cdc_line_coding (_usbd_cdc, &line_coding);
396
+
397
+ return line_coding.bParityType ;
398
+ }
399
+
400
+ return STM32L0_USBD_CDC_LINE_CODING_PARITY_TYPE;
313
401
}
314
402
315
403
uint8_t CDC::numbits ()
316
404
{
317
- return stm32l0_usbd_cdc_info.bDataBits ;
405
+ stm32l0_usbd_cdc_line_coding_t line_coding;
406
+
407
+ if (_enabled)
408
+ {
409
+ stm32l0_usbd_cdc_line_coding (_usbd_cdc, &line_coding);
410
+
411
+ return line_coding.bDataBits ;
412
+ }
413
+
414
+ return STM32L0_USBD_CDC_LINE_CODING_DATA_BITS;
318
415
}
319
416
320
417
bool CDC::dtr ()
321
418
{
322
- return stm32l0_usbd_cdc_info. lineState & 1 ;
419
+ return (_enabled && ( stm32l0_usbd_cdc_line_state (_usbd_cdc) & USB_CDC_LINE_STATE_DTR)) ;
323
420
}
324
421
325
422
bool CDC::rts ()
326
423
{
327
- return stm32l0_usbd_cdc_info. lineState & 2 ;
424
+ return (_enabled && ( stm32l0_usbd_cdc_line_state (_usbd_cdc) & USB_CDC_LINE_STATE_RTS)) ;
328
425
}
329
426
330
427
void CDC::_eventCallback (class CDC *self, uint32_t events)
@@ -360,7 +457,7 @@ void CDC::_doneCallback(class CDC *self)
360
457
if (tx_size > CDC_TX_PACKET_SIZE) {
361
458
tx_size = CDC_TX_PACKET_SIZE;
362
459
}
363
-
460
+
364
461
self->_tx_size = tx_size;
365
462
self->_tx_busy = true ;
366
463
@@ -370,8 +467,33 @@ void CDC::_doneCallback(class CDC *self)
370
467
self->_tx_size = 0 ;
371
468
self->_tx_count = 0 ;
372
469
self->_tx_read = self->_tx_write ;
470
+
471
+ if (self->_tx_size2 != 0 ) {
472
+ self->_tx_size2 = 0 ;
473
+ self->_tx_data2 = NULL ;
474
+
475
+ self->_completionCallback .queue (self->_wakeup );
476
+ }
477
+ }
478
+ } else {
479
+ if (self->_tx_size2 != 0 ) {
480
+ self->_tx_busy = true ;
481
+
482
+ if (!stm32l0_usbd_cdc_transmit (self->_usbd_cdc , self->_tx_data2 , self->_tx_size2 , (stm32l0_usbd_cdc_done_callback_t )CDC::_doneCallback, (void *)self)) {
483
+ self->_tx_busy = false ;
484
+
485
+ self->_tx_size2 = 0 ;
486
+ self->_tx_data2 = NULL ;
487
+
488
+ self->_completionCallback .queue (self->_wakeup );
489
+ }
373
490
}
374
491
}
492
+ } else {
493
+ self->_tx_size2 = 0 ;
494
+ self->_tx_data2 = NULL ;
495
+
496
+ self->_completionCallback .queue (self->_wakeup );
375
497
}
376
498
}
377
499
0 commit comments