Skip to content

Commit 05c119c

Browse files
committed
cdc host, add set line coding API
1 parent 11233e4 commit 05c119c

File tree

4 files changed

+143
-41
lines changed

4 files changed

+143
-41
lines changed

examples/host/cdc_msc_hid/src/tusb_config.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,15 @@
109109
#define CFG_TUH_HID_EPOUT_BUFSIZE 64
110110

111111
//------------- CDC -------------//
112-
// Set both DTR ( bit 0), RTS (bit 1) on enumeration/mounted
113-
#define CFG_TUH_CDC_SET_DTRRTS_ON_ENUM 0x03
112+
113+
// Set Line Control state on enumeration/mounted:
114+
// DTR ( bit 0), RTS (bit 1)
115+
#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0x03
116+
117+
// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t
118+
// bit rate = 115200, 1 stop bit, no parity, 8 bit data width
119+
#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
120+
114121

115122
#ifdef __cplusplus
116123
}

src/class/cdc/cdc.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,22 @@ enum
198198
CDC_CONTROL_LINE_STATE_RTS = 0x02,
199199
};
200200

201+
enum
202+
{
203+
CDC_LINE_CONDING_STOP_BITS_1 = 0, // 1 bit
204+
CDC_LINE_CONDING_STOP_BITS_1_5 = 1, // 1.5 bits
205+
CDC_LINE_CONDING_STOP_BITS_2 = 2, // 2 bits
206+
};
207+
208+
enum
209+
{
210+
CDC_LINE_CODING_PARITY_NONE = 0,
211+
CDC_LINE_CODING_PARITY_ODD = 1,
212+
CDC_LINE_CODING_PARITY_EVEN = 2,
213+
CDC_LINE_CODING_PARITY_MARK = 3,
214+
CDC_LINE_CODING_PARITY_SPACE = 4,
215+
};
216+
201217
//--------------------------------------------------------------------+
202218
// Management Element Notification (Notification Endpoint)
203219
//--------------------------------------------------------------------+
@@ -207,13 +223,13 @@ typedef enum
207223
{
208224
CDC_NOTIF_NETWORK_CONNECTION = 0x00, ///< This notification allows the device to notify the host about network connection status.
209225
CDC_NOTIF_RESPONSE_AVAILABLE = 0x01, ///< This notification allows the device to notify the hostthat a response is available. This response can be retrieved with a subsequent \ref CDC_REQUEST_GET_ENCAPSULATED_RESPONSE request.
210-
CDC_NOTIF_AUX_JACK_HOOK_STATE = 0x08,
211-
CDC_NOTIF_RING_DETECT = 0x09,
212-
CDC_NOTIF_SERIAL_STATE = 0x20,
213-
CDC_NOTIF_CALL_STATE_CHANGE = 0x28,
214-
CDC_NOTIF_LINE_STATE_CHANGE = 0x29,
226+
CDC_NOTIF_AUX_JACK_HOOK_STATE = 0x08,///< CDC_NOTIF_AUX_JACK_HOOK_STATE
227+
CDC_NOTIF_RING_DETECT = 0x09,///< CDC_NOTIF_RING_DETECT
228+
CDC_NOTIF_SERIAL_STATE = 0x20,///< CDC_NOTIF_SERIAL_STATE
229+
CDC_NOTIF_CALL_STATE_CHANGE = 0x28,///< CDC_NOTIF_CALL_STATE_CHANGE
230+
CDC_NOTIF_LINE_STATE_CHANGE = 0x29,///< CDC_NOTIF_LINE_STATE_CHANGE
215231
CDC_NOTIF_CONNECTION_SPEED_CHANGE = 0x2A, ///< This notification allows the device to inform the host-networking driver that a change in either the upstream or the downstream bit rate of the connection has occurred
216-
CDC_NOTIF_MDLM_SEMANTIC_MODEL_NOTIFICATION = 0x40,
232+
CDC_NOTIF_MDLM_SEMANTIC_MODEL_NOTIFICATION = 0x40,///< CDC_NOTIF_MDLM_SEMANTIC_MODEL_NOTIFICATION
217233
}cdc_notification_request_t;
218234

219235
//--------------------------------------------------------------------+

src/class/cdc/cdc_host.c

Lines changed: 100 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ typedef struct {
5252
cdc_acm_capability_t acm_capability;
5353
uint8_t ep_notif;
5454

55-
// Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send)
56-
uint8_t line_state;
55+
cdc_line_coding_t line_coding; // Baudrate, stop bits, parity, data width
56+
uint8_t line_state; // DTR (bit0), RTS (bit1)
5757

5858
tuh_xfer_cb_t user_control_cb;
5959

@@ -240,6 +240,13 @@ static void cdch_internal_control_complete(tuh_xfer_t* xfer)
240240
p_cdc->line_state = (uint8_t) tu_le16toh(xfer->setup->wValue);
241241
break;
242242

243+
case CDC_REQUEST_SET_LINE_CODING:
244+
{
245+
uint16_t const len = tu_min16(sizeof(cdc_line_coding_t), tu_le16toh(xfer->setup->wLength));
246+
memcpy(&p_cdc->line_coding, xfer->buffer, len);
247+
}
248+
break;
249+
243250
default: break;
244251
}
245252
}
@@ -265,7 +272,7 @@ bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_c
265272
},
266273
.bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE,
267274
.wValue = tu_htole16(line_state),
268-
.wIndex = tu_htole16(p_cdc->bInterfaceNumber),
275+
.wIndex = tu_htole16((uint16_t) p_cdc->bInterfaceNumber),
269276
.wLength = 0
270277
};
271278

@@ -283,6 +290,46 @@ bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_c
283290
return tuh_control_xfer(&xfer);
284291
}
285292

293+
bool tuh_cdc_set_line_coding(uint8_t idx, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
294+
{
295+
cdch_interface_t* p_cdc = get_itf(idx);
296+
TU_VERIFY(p_cdc && p_cdc->acm_capability.support_line_request);
297+
298+
TU_LOG_CDCH("CDC Set Line Conding\r\n");
299+
300+
tusb_control_request_t const request =
301+
{
302+
.bmRequestType_bit =
303+
{
304+
.recipient = TUSB_REQ_RCPT_INTERFACE,
305+
.type = TUSB_REQ_TYPE_CLASS,
306+
.direction = TUSB_DIR_OUT
307+
},
308+
.bRequest = CDC_REQUEST_SET_LINE_CODING,
309+
.wValue = 0,
310+
.wIndex = tu_htole16(p_cdc->bInterfaceNumber),
311+
.wLength = tu_htole16(sizeof(cdc_line_coding_t))
312+
};
313+
314+
// use usbh enum buf to hold line coding since user line_coding variable may not live long enough
315+
// for the transfer to complete
316+
uint8_t* enum_buf = usbh_get_enum_buf();
317+
memcpy(enum_buf, line_coding, sizeof(cdc_line_coding_t));
318+
319+
p_cdc->user_control_cb = complete_cb;
320+
tuh_xfer_t xfer =
321+
{
322+
.daddr = p_cdc->daddr,
323+
.ep_addr = 0,
324+
.setup = &request,
325+
.buffer = enum_buf,
326+
.complete_cb = cdch_internal_control_complete,
327+
.user_data = user_data
328+
};
329+
330+
return tuh_control_xfer(&xfer);
331+
}
332+
286333
//--------------------------------------------------------------------+
287334
// CLASS-USBH API
288335
//--------------------------------------------------------------------+
@@ -448,46 +495,70 @@ bool cdch_open(uint8_t rhport, uint8_t daddr, tusb_desc_interface_t const *itf_d
448495
return true;
449496
}
450497

451-
static void config_cdc_complete(uint8_t daddr, uint8_t itf_num)
498+
enum
452499
{
453-
uint8_t const idx = tuh_cdc_itf_get_index(daddr, itf_num);
500+
CONFIG_SET_CONTROL_LINE_STATE,
501+
CONFIG_SET_LINE_CODING,
502+
CONFIG_COMPLETE
503+
};
454504

455-
if (idx != TUSB_INDEX_INVALID)
505+
static void process_cdc_config(tuh_xfer_t* xfer)
506+
{
507+
uintptr_t const state = xfer->user_data;
508+
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
509+
uint8_t const idx = tuh_cdc_itf_get_index(xfer->daddr, itf_num);
510+
TU_ASSERT(idx != TUSB_INDEX_INVALID, );
511+
512+
switch(state)
456513
{
457-
if (tuh_cdc_mount_cb) tuh_cdc_mount_cb(idx);
514+
case CONFIG_SET_CONTROL_LINE_STATE:
515+
#if CFG_TUH_CDC_LINE_CONTROL_ON_ENUM
516+
TU_ASSERT( tuh_cdc_set_control_line_state(idx, CFG_TUH_CDC_LINE_CONTROL_ON_ENUM, process_cdc_config, CONFIG_SET_LINE_CODING), );
517+
break;
518+
#endif
519+
TU_ATTR_FALLTHROUGH;
458520

459-
// Prepare for incoming data
460-
cdch_interface_t* p_cdc = get_itf(idx);
461-
tu_edpt_stream_read_xfer(&p_cdc->stream.rx);
462-
}
521+
case CONFIG_SET_LINE_CODING:
522+
#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
523+
{
524+
cdc_line_coding_t line_coding = CFG_TUH_CDC_LINE_CODING_ON_ENUM;
525+
TU_ASSERT( tuh_cdc_set_line_coding(idx, &line_coding, process_cdc_config, 0), );
526+
break;
527+
}
528+
#endif
529+
TU_ATTR_FALLTHROUGH;
463530

464-
// notify usbh that driver enumeration is complete
465-
// itf_num+1 to account for data interface as well
466-
usbh_driver_set_config_complete(daddr, itf_num+1);
467-
}
531+
case CONFIG_COMPLETE:
532+
if (tuh_cdc_mount_cb) tuh_cdc_mount_cb(idx);
468533

469-
#if CFG_TUH_CDC_SET_DTRRTS_ON_ENUM
534+
// Prepare for incoming data
535+
cdch_interface_t* p_cdc = get_itf(idx);
536+
tu_edpt_stream_read_xfer(&p_cdc->stream.rx);
470537

471-
static void config_set_dtr_rts_complete (tuh_xfer_t* xfer)
472-
{
473-
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
474-
config_cdc_complete(xfer->daddr, itf_num);
538+
// notify usbh that driver enumeration is complete
539+
// itf_num+1 to account for data interface as well
540+
usbh_driver_set_config_complete(xfer->daddr, itf_num+1);
541+
break;
542+
543+
default: break;
544+
}
475545
}
476546

477547
bool cdch_set_config(uint8_t daddr, uint8_t itf_num)
478548
{
479-
uint8_t const idx = tuh_cdc_itf_get_index(daddr, itf_num);
480-
return tuh_cdc_set_control_line_state(idx, CFG_TUH_CDC_SET_DTRRTS_ON_ENUM, config_set_dtr_rts_complete, 0);
481-
}
549+
// fake transfer to kick-off process
550+
tusb_control_request_t request;
551+
request.wIndex = tu_htole16((uint16_t) itf_num);
482552

483-
#else
553+
tuh_xfer_t xfer;
554+
xfer.daddr = daddr;
555+
xfer.result = XFER_RESULT_SUCCESS;
556+
xfer.setup = &request;
557+
xfer.user_data = CONFIG_SET_CONTROL_LINE_STATE;
558+
559+
process_cdc_config(&xfer);
484560

485-
bool cdch_set_config(uint8_t daddr, uint8_t itf_num)
486-
{
487-
config_cdc_complete(daddr, itf_num);
488561
return true;
489562
}
490563

491564
#endif
492-
493-
#endif

src/class/cdc/cdc_host.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@
3737
// Class Driver Configuration
3838
//--------------------------------------------------------------------+
3939

40-
// Set DTR ( bit 0), RTS (bit 1) on enumeration/mounted
41-
#ifndef CFG_TUH_CDC_SET_DTRRTS_ON_ENUM
42-
#define CFG_TUH_CDC_SET_DTRRTS_ON_ENUM 0
40+
// Set Line Control state on enumeration/mounted: DTR ( bit 0), RTS (bit 1)
41+
#ifndef CFG_TUH_CDC_LINE_CONTROL_ON_ENUM
42+
#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0
4343
#endif
4444

45+
// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t
46+
//#ifndef CFG_TUH_CDC_LINE_CODING_ON_ENUM
47+
//#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
48+
//#endif
49+
4550
// RX FIFO size
4651
#ifndef CFG_TUH_CDC_RX_BUFSIZE
4752
#define CFG_TUH_CDC_RX_BUFSIZE USBH_EPSIZE_BULK_MAX
@@ -130,9 +135,12 @@ bool tuh_cdc_read_clear (uint8_t idx);
130135
// Control Endpoint (Request) API
131136
//--------------------------------------------------------------------+
132137

133-
// Send control request to Set Control Line State: DTR (bit 0), RTS (bit 1)
138+
// Request to Set Control Line State: DTR (bit 0), RTS (bit 1)
134139
bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data);
135140

141+
// Request to Set Line Coding
142+
bool tuh_cdc_set_line_coding(uint8_t idx, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data);
143+
136144
// Connect by set both DTR, RTS
137145
static inline bool tuh_cdc_connect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
138146
{

0 commit comments

Comments
 (0)