@@ -59,7 +59,7 @@ typedef struct {
59
59
// Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send)
60
60
uint8_t line_state ;
61
61
62
- // Notify host of flow control bits: CTS, DSR, DCD, RI, and some error flags.
62
+ // Notify host of flow control bits: DSR, DCD, RI, and some error flags.
63
63
cdc_serial_state_t serial_state ;
64
64
bool serial_state_changed ;
65
65
@@ -120,6 +120,29 @@ static bool _prep_out_transaction (cdcd_interface_t* p_cdc) {
120
120
}
121
121
}
122
122
123
+ bool _send_serial_state_notification (cdcd_interface_t * p_cdc ) {
124
+ const uint8_t rhport = 0 ;
125
+
126
+ if (!p_cdc -> serial_state_changed ) {
127
+ // Nothing to do.
128
+ return true;
129
+ }
130
+
131
+ if (!usbd_edpt_claim (rhport , p_cdc -> ep_notif )) {
132
+ // If claim failed, we're already in the middle of a transaction.
133
+ // cdcd_xfer_cb() will pick up this change.
134
+ return true;
135
+ }
136
+
137
+ // We have the end point. Build and send the notification.
138
+ p_cdc -> serial_state_changed = false;
139
+
140
+ p_cdc -> epnotif_buf = cdc_notify_serial_status ;
141
+ p_cdc -> epnotif_buf .header .wIndex = p_cdc -> itf_num ;
142
+ p_cdc -> epnotif_buf .serial_state = p_cdc -> serial_state ;
143
+ return usbd_edpt_xfer (rhport , p_cdc -> ep_notif , (uint8_t * ) & (p_cdc -> epnotif_buf ), sizeof (p_cdc -> epnotif_buf ));
144
+ }
145
+
123
146
//--------------------------------------------------------------------+
124
147
// APPLICATION API
125
148
//--------------------------------------------------------------------+
@@ -148,11 +171,12 @@ cdc_serial_state_t tud_cdc_n_get_serial_state(uint8_t itf) {
148
171
}
149
172
150
173
void tud_cdc_n_set_serial_state (uint8_t itf , cdc_serial_state_t serial_state ) {
151
- if (memcmp (& (_cdcd_itf [itf ].serial_state ), & serial_state , sizeof (serial_state )) != 0 ) {
152
- TU_LOG_DRV (" Serial State Changed: %x -> %x\r\n" , _cdcd_itf [itf ].serial_state , serial_state );
153
- _cdcd_itf [itf ].serial_state_changed = true;
174
+ cdcd_interface_t * p_cdc = & _cdcd_itf [itf ];
175
+ if (p_cdc -> serial_state .state != serial_state .state ) {
176
+ p_cdc -> serial_state_changed = true;
177
+ p_cdc -> serial_state = serial_state ;
178
+ _send_serial_state_notification (p_cdc );
154
179
}
155
- _cdcd_itf [itf ].serial_state = serial_state ;
156
180
}
157
181
158
182
void tud_cdc_n_get_line_coding (uint8_t itf , cdc_line_coding_t * coding ) {
@@ -343,13 +367,10 @@ uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
343
367
344
368
if (TUSB_DESC_ENDPOINT == tu_desc_type (p_desc )) {
345
369
// notification endpoint
346
- tusb_desc_endpoint_t desc_ep = * (tusb_desc_endpoint_t const * ) p_desc ;
347
- TU_LOG_DRV (" Interval before: %d\r\n" , desc_ep .bInterval );
348
-
349
- desc_ep .bInterval = 1 ; // Query every frame, 1ms at Full Speed.
370
+ tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const * ) p_desc ;
350
371
351
- TU_ASSERT (usbd_edpt_open (rhport , & desc_ep ), 0 );
352
- p_cdc -> ep_notif = desc_ep . bEndpointAddress ;
372
+ TU_ASSERT (usbd_edpt_open (rhport , desc_ep ), 0 );
373
+ p_cdc -> ep_notif = desc_ep -> bEndpointAddress ;
353
374
354
375
drv_len += tu_desc_len (p_desc );
355
376
p_desc = tu_desc_next (p_desc );
@@ -460,10 +481,9 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
460
481
if ((ep_addr == p_cdc -> ep_out ) || (ep_addr == p_cdc -> ep_in ) || (ep_addr == p_cdc -> ep_notif )) break ;
461
482
}
462
483
TU_ASSERT (itf < CFG_TUD_CDC );
463
-
484
+
464
485
// Received new data
465
486
if (ep_addr == p_cdc -> ep_out ) {
466
- TU_LOG_DRV (" XFer Out\r\n" );
467
487
tu_fifo_write_n (& p_cdc -> rx_ff , p_cdc -> epout_buf , (uint16_t ) xferred_bytes );
468
488
469
489
// Check for wanted char and invoke callback if needed
@@ -486,7 +506,6 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
486
506
// Note: This will cause incorrect baudrate set in line coding.
487
507
// Though maybe the baudrate is not really important !!!
488
508
if (ep_addr == p_cdc -> ep_in ) {
489
- TU_LOG_DRV (" XFer In\r\n" );
490
509
// invoke transmit callback to possibly refill tx fifo
491
510
if (tud_cdc_tx_complete_cb ) tud_cdc_tx_complete_cb (itf );
492
511
@@ -503,27 +522,8 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
503
522
504
523
// Notifications
505
524
if (ep_addr == p_cdc -> ep_notif ) {
506
- TU_LOG_DRV (" XFer Notification\r\n" );
507
- uint8_t const rhport = 0 ;
508
-
509
- // SERIAL_STATE notification. Send flow control signals.
510
- if (p_cdc -> serial_state_changed ) {
511
- p_cdc -> serial_state_changed = false;
512
-
513
- // Build the notification
514
- p_cdc -> epnotif_buf = cdc_notify_serial_status ;
515
- p_cdc -> epnotif_buf .header .wIndex = p_cdc -> itf_num ;
516
- p_cdc -> epnotif_buf .serial_state = p_cdc -> serial_state ;
517
-
518
- // claim endpoint
519
- TU_VERIFY (usbd_edpt_claim (rhport , p_cdc -> ep_notif ), 0 );
520
-
521
- // Send notification
522
- return usbd_edpt_xfer (rhport , p_cdc -> ep_notif , (uint8_t * ) & (p_cdc -> epnotif_buf ), sizeof (p_cdc -> epnotif_buf ));
523
- }
524
- else {
525
- // Send a NAK?
526
- }
525
+ // Send any changes that may have come in while sending the previous change.
526
+ return _send_serial_state_notification (p_cdc );
527
527
}
528
528
529
529
return true;
0 commit comments