Skip to content

Commit 87d509d

Browse files
committed
make CFG_TUH_CDC_LINE_CODING_ON_ENUM optional for ch34x
1 parent 55cb713 commit 87d509d

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

examples/host/cdc_msc_hid/src/cdc_app.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,17 @@ void tuh_cdc_mount_cb(uint8_t idx) {
8585
itf_info.desc.bInterfaceNumber);
8686

8787
#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
88-
// CFG_TUH_CDC_LINE_CODING_ON_ENUM must be defined for line coding is set by tinyusb in enumeration
89-
// otherwise you need to call tuh_cdc_set_line_coding() first
88+
// If CFG_TUH_CDC_LINE_CODING_ON_ENUM is defined, line coding will be set by tinyusb stack
89+
// while eneumerating new cdc device
9090
cdc_line_coding_t line_coding = {0};
9191
if (tuh_cdc_get_local_line_coding(idx, &line_coding)) {
9292
printf(" Baudrate: %lu, Stop Bits : %u\r\n", line_coding.bit_rate, line_coding.stop_bits);
9393
printf(" Parity : %u, Data Width: %u\r\n", line_coding.parity, line_coding.data_bits);
9494
}
95+
#else
96+
// Set Line Coding upon mounted
97+
cdc_line_coding_t new_line_coding = { 115200, CDC_LINE_CODING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 };
98+
tuh_cdc_set_line_coding(idx, &new_line_coding, NULL, 0);
9599
#endif
96100
}
97101

src/class/cdc/cdc_host.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,10 +1510,11 @@ static void ch34x_process_config(tuh_xfer_t* xfer) {
15101510
uint8_t const idx = tuh_cdc_itf_get_index(xfer->daddr, itf_num);
15111511
cdch_interface_t* p_cdc = get_itf(idx);
15121512
uintptr_t const state = xfer->user_data;
1513-
cdc_line_coding_t const line_coding = CFG_TUH_CDC_LINE_CODING_ON_ENUM_CH34X;
15141513
uint8_t buffer[2]; // TODO remove
15151514
TU_ASSERT (p_cdc,);
15161515

1516+
// TODO check xfer->result
1517+
15171518
switch (state) {
15181519
case CONFIG_CH34X_READ_VERSION:
15191520
TU_LOG_DRV("[%u] CDCh CH34x attempt to read Chip Version\r\n", p_cdc->daddr);
@@ -1527,20 +1528,28 @@ static void ch34x_process_config(tuh_xfer_t* xfer) {
15271528
// only versions >= 0x30 are tested, below 0x30 seems having other programming, see drivers from WCH vendor, Linux kernel and FreeBSD
15281529
TU_ASSERT (version >= 0x30,);
15291530

1531+
#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
1532+
cdc_line_coding_t const line_coding = CFG_TUH_CDC_LINE_CODING_ON_ENUM;
1533+
uint8_t const lcr = ch34x_get_lcr(line_coding.stop_bits, line_coding.parity, line_coding.data_bits);
1534+
uint16_t const first_arg = tu_u16(lcr, 0x9c);
15301535
uint16_t const div_ps = ch34x_get_divisor_prescaler(line_coding.bit_rate);
15311536
TU_ASSERT(div_ps != 0, );
1532-
1533-
uint8_t const lcr = ch34x_get_lcr(line_coding.stop_bits, line_coding.parity, line_coding.data_bits);
1537+
#else
1538+
uint16_t const first_arg = 0;
1539+
uint16_t const div_ps = 0;
1540+
#endif
15341541

15351542
// Init CH34x with line coding
1536-
TU_ASSERT (ch34x_control_out(p_cdc, CH34X_REQ_SERIAL_INIT, tu_u16(lcr, 0x9c), div_ps,
1543+
TU_ASSERT (ch34x_control_out(p_cdc, CH34X_REQ_SERIAL_INIT, first_arg, div_ps,
15371544
ch34x_process_config, CONFIG_CH34X_SPECIAL_REG_WRITE),);
15381545
break;
15391546
}
15401547

15411548
case CONFIG_CH34X_SPECIAL_REG_WRITE:
15421549
// do special reg write, purpose unknown, overtaken from WCH driver
1543-
p_cdc->line_coding = line_coding;
1550+
#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
1551+
p_cdc->line_coding = ((cdc_line_coding_t) CFG_TUH_CDC_LINE_CODING_ON_ENUM);
1552+
#endif
15441553
TU_ASSERT (ch34x_write_reg(p_cdc, 0x0f2c, 0x0007, ch34x_process_config, CONFIG_CH34X_FLOW_CONTROL),);
15451554
break;
15461555

src/class/cdc/serial/ch34x.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,11 @@
2727
#ifndef _CH34X_H_
2828
#define _CH34X_H_
2929

30-
// For simplicity, only the name CH34X is used here,
31-
// but specifically only CH340 and CH341 are supported.
32-
33-
// There is no official documentation for the CH34x chips. Reference can be found
30+
// There is no official documentation for the CH34x (CH340, CH341) chips. Reference can be found
3431
// - https://github.com/WCHSoftGroup/ch341ser_linux
3532
// - https://github.com/torvalds/linux/blob/master/drivers/usb/serial/ch341.c
3633
// - https://github.com/freebsd/freebsd-src/blob/main/sys/dev/usb/serial/uchcom.c
3734

38-
// set line_coding @ enumeration
39-
#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
40-
#define CFG_TUH_CDC_LINE_CODING_ON_ENUM_CH34X CFG_TUH_CDC_LINE_CODING_ON_ENUM
41-
#else // this default is necessary to work properly
42-
#define CFG_TUH_CDC_LINE_CODING_ON_ENUM_CH34X { 9600, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
43-
#endif
44-
4535
// USB requests
4636
#define CH34X_REQ_READ_VERSION 0x5F // dec 95
4737
#define CH34X_REQ_WRITE_REG 0x9A // dec 154

0 commit comments

Comments
 (0)