Skip to content

Commit f33883c

Browse files
committed
add tuh_cdc_get_local_line_coding()
1 parent 14d45b5 commit f33883c

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

examples/host/cdc_msc_hid/src/cdc_app.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,24 @@ void tuh_cdc_mount_cb(uint8_t idx)
9090
tuh_cdc_itf_info_t itf_info = { 0 };
9191
tuh_cdc_itf_get_info(idx, &itf_info);
9292

93-
printf("CDC Interface is mounted: device address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
93+
printf("CDC Interface is mounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
94+
95+
#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
96+
// CFG_TUH_CDC_LINE_CODING_ON_ENUM must be defined for line coding is set by tinyusb in enumeration
97+
// otherwise you need to call tuh_cdc_set_line_coding() first
98+
cdc_line_coding_t line_coding = { 0 };
99+
if ( tuh_cdc_get_local_line_coding(idx, &line_coding) )
100+
{
101+
printf(" Baudrate: %lu, Stop Bits : %u\r\n", line_coding.bit_rate, line_coding.stop_bits);
102+
printf(" Parity : %u, Data Width: %u\r\n", line_coding.parity , line_coding.data_bits);
103+
}
104+
#endif
94105
}
95106

96107
void tuh_cdc_umount_cb(uint8_t idx)
97108
{
98109
tuh_cdc_itf_info_t itf_info = { 0 };
99110
tuh_cdc_itf_get_info(idx, &itf_info);
100111

101-
printf("CDC Interface is unmounted: device address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
112+
printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
102113
}

src/class/cdc/cdc_host.c

Lines changed: 20 additions & 2 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-
cdc_line_coding_t line_coding; // Baudrate, stop bits, parity, data width
56-
uint8_t line_state; // DTR (bit0), RTS (bit1)
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

@@ -162,6 +162,20 @@ bool tuh_cdc_get_rts(uint8_t idx)
162162
return (p_cdc->line_state & CDC_CONTROL_LINE_STATE_RTS) ? true : false;
163163
}
164164

165+
bool tuh_cdc_get_local_line_coding(uint8_t idx, cdc_line_coding_t* line_coding)
166+
{
167+
cdch_interface_t* p_cdc = get_itf(idx);
168+
TU_VERIFY(p_cdc);
169+
170+
*line_coding = p_cdc->line_coding;
171+
172+
return true;
173+
}
174+
175+
//--------------------------------------------------------------------+
176+
// Write
177+
//--------------------------------------------------------------------+
178+
165179
uint32_t tuh_cdc_write(uint8_t idx, void const* buffer, uint32_t bufsize)
166180
{
167181
cdch_interface_t* p_cdc = get_itf(idx);
@@ -194,6 +208,10 @@ uint32_t tuh_cdc_write_available(uint8_t idx)
194208
return tu_edpt_stream_write_available(&p_cdc->stream.tx);
195209
}
196210

211+
//--------------------------------------------------------------------+
212+
// Read
213+
//--------------------------------------------------------------------+
214+
197215
uint32_t tuh_cdc_read (uint8_t idx, void* buffer, uint32_t bufsize)
198216
{
199217
cdch_interface_t* p_cdc = get_itf(idx);

src/class/cdc/cdc_host.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ TU_ATTR_ALWAYS_INLINE static inline bool tuh_cdc_connected(uint8_t idx)
102102
return tuh_cdc_get_dtr(idx);
103103
}
104104

105+
// Get local (saved/cached) version of line coding.
106+
// This function should return correct values if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding()
107+
// are invoked previously or CFG_TUH_CDC_LINE_CODING_ON_ENUM is defined.
108+
// NOTE: This function does not make any USB transfer request to device.
109+
bool tuh_cdc_get_local_line_coding(uint8_t idx, cdc_line_coding_t* line_coding);
110+
105111
//--------------------------------------------------------------------+
106112
// Write API
107113
//--------------------------------------------------------------------+
@@ -133,6 +139,7 @@ bool tuh_cdc_read_clear (uint8_t idx);
133139

134140
//--------------------------------------------------------------------+
135141
// Control Endpoint (Request) API
142+
// Each Function will make a USB transfer request to/from device
136143
//--------------------------------------------------------------------+
137144

138145
// Request to Set Control Line State: DTR (bit 0), RTS (bit 1)
@@ -141,6 +148,11 @@ bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_c
141148
// Request to Set Line Coding
142149
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);
143150

151+
// Request to Get Line Coding
152+
// Should only use if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding() never got invoked and
153+
// CFG_TUH_CDC_LINE_CODING_ON_ENUM is not defined
154+
// bool tuh_cdc_get_line_coding(uint8_t idx, cdc_line_coding_t* coding);
155+
144156
// Connect by set both DTR, RTS
145157
static inline bool tuh_cdc_connect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
146158
{

0 commit comments

Comments
 (0)