Skip to content

Commit cd96277

Browse files
authored
Merge pull request #2488 from IngHK/cdch_upgrade
CHCh Upgrade: Improved FTDI and CP210x support, add PL2303 support, bugfixes
2 parents cc12306 + d863624 commit cd96277

File tree

16 files changed

+2410
-1246
lines changed

16 files changed

+2410
-1246
lines changed

CONTRIBUTORS.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ Notable contributors
3131
- Most features development
3232

3333

34+
`Heiko Kuester <https://github.com/IngHK>`__
35+
--------------------------------------------
36+
37+
- Add CH34x and PL2303 support (CDC host)
38+
- Improve FTDI and CP210x support (CDC host)
39+
40+
3441
`Hristo Gochkov <https://github.com/me-no-dev>`__
3542
-------------------------------------------------
3643

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Host Stack
7474
- Human Interface Device (HID): Keyboard, Mouse, Generic
7575
- Mass Storage Class (MSC)
7676
- Communication Device Class: CDC-ACM
77-
- Vendor serial over USB: FTDI, CP210x, CH34x
77+
- Vendor serial over USB: FTDI, CP210x, CH34x, PL2303
7878
- Hub with multiple-level support
7979

8080
Similar to the Device Stack, if you have a special requirement, ``usbh_app_driver_get_cb()`` can be used to write your own class driver without modifying the stack.

examples/host/cdc_msc_hid/src/cdc_app.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ static size_t get_console_inputs(uint8_t* buf, size_t bufsize) {
3131
size_t count = 0;
3232
while (count < bufsize) {
3333
int ch = board_getchar();
34-
if (ch <= 0) break;
35-
34+
if (ch <= 0) { break; }
3635
buf[count] = (uint8_t) ch;
3736
count++;
3837
}
@@ -69,10 +68,12 @@ void tuh_cdc_rx_cb(uint8_t idx) {
6968
uint32_t const bufsize = sizeof(buf) - 1;
7069

7170
// forward cdc interfaces -> console
72-
uint32_t count = tuh_cdc_read(idx, buf, bufsize);
73-
buf[count] = 0;
74-
75-
printf("%s", (char*) buf);
71+
const uint32_t count = tuh_cdc_read(idx, buf, bufsize);
72+
if (count) {
73+
buf[count] = 0;
74+
printf("%s", (char*) buf);
75+
fflush(stdout);
76+
}
7677
}
7778

7879
// Invoked when a device with CDC interface is mounted
@@ -88,7 +89,7 @@ void tuh_cdc_mount_cb(uint8_t idx) {
8889
// If CFG_TUH_CDC_LINE_CODING_ON_ENUM is defined, line coding will be set by tinyusb stack
8990
// while eneumerating new cdc device
9091
cdc_line_coding_t line_coding = {0};
91-
if (tuh_cdc_get_local_line_coding(idx, &line_coding)) {
92+
if (tuh_cdc_get_line_coding_local(idx, &line_coding)) {
9293
printf(" Baudrate: %" PRIu32 ", Stop Bits : %u\r\n", line_coding.bit_rate, line_coding.stop_bits);
9394
printf(" Parity : %u, Data Width: %u\r\n", line_coding.parity, line_coding.data_bits);
9495
}

examples/host/cdc_msc_hid/src/tusb_config.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,11 @@
103103
#define CFG_TUH_ENUMERATION_BUFSIZE 256
104104

105105
#define CFG_TUH_HUB 1 // number of supported hubs
106-
#define CFG_TUH_CDC 1 // CDC ACM
106+
#define CFG_TUH_CDC 2 // number of supported CDC devices. also activates CDC ACM
107107
#define CFG_TUH_CDC_FTDI 1 // FTDI Serial. FTDI is not part of CDC class, only to re-use CDC driver API
108108
#define CFG_TUH_CDC_CP210X 1 // CP210x Serial. CP210X is not part of CDC class, only to re-use CDC driver API
109109
#define CFG_TUH_CDC_CH34X 1 // CH340 or CH341 Serial. CH34X is not part of CDC class, only to re-use CDC driver API
110+
#define CFG_TUH_CDC_PL2303 1 // PL2303 Serial. PL2303 is not part of CDC class, only to re-use CDC driver API
110111
#define CFG_TUH_HID (3*CFG_TUH_DEVICE_MAX) // typical keyboard + mouse device can have 3-4 HID interfaces
111112
#define CFG_TUH_MSC 1
112113
#define CFG_TUH_VENDOR 0
@@ -122,7 +123,7 @@
122123

123124
// Set Line Control state on enumeration/mounted:
124125
// DTR ( bit 0), RTS (bit 1)
125-
#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0x03
126+
#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM (CDC_CONTROL_LINE_STATE_DTR | CDC_CONTROL_LINE_STATE_RTS)
126127

127128
// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t
128129
// bit rate = 115200, 1 stop bit, no parity, 8 bit data width

examples/host/cdc_msc_hid_freertos/src/tusb_config.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@
108108
#define CFG_TUH_ENUMERATION_BUFSIZE 256
109109

110110
#define CFG_TUH_HUB 1 // number of supported hubs
111-
#define CFG_TUH_CDC 1 // CDC ACM
111+
#define CFG_TUH_CDC 1 // number of supported CDC devices. also activates CDC ACM
112112
#define CFG_TUH_CDC_FTDI 1 // FTDI Serial. FTDI is not part of CDC class, only to re-use CDC driver API
113113
#define CFG_TUH_CDC_CP210X 1 // CP210x Serial. CP210X is not part of CDC class, only to re-use CDC driver API
114114
#define CFG_TUH_CDC_CH34X 1 // CH340 or CH341 Serial. CH34X is not part of CDC class, only to re-use CDC driver API
115+
#define CFG_TUH_CDC_PL2303 1 // PL2303 Serial. PL2303 is not part of CDC class, only to re-use CDC driver API
115116
#define CFG_TUH_HID (3*CFG_TUH_DEVICE_MAX) // typical keyboard + mouse device can have 3-4 HID interfaces
116117
#define CFG_TUH_MSC 1
117118
#define CFG_TUH_VENDOR 0
@@ -127,7 +128,7 @@
127128

128129
// Set Line Control state on enumeration/mounted:
129130
// DTR ( bit 0), RTS (bit 1)
130-
#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0x03
131+
#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM (CDC_CONTROL_LINE_STATE_DTR | CDC_CONTROL_LINE_STATE_RTS)
131132

132133
// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t
133134
// bit rate = 115200, 1 stop bit, no parity, 8 bit data width

src/class/cdc/cdc.h

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ typedef enum {
192192
CDC_LINE_CODING_STOP_BITS_2 = 2, // 2 bits
193193
} cdc_line_coding_stopbits_t;
194194

195+
#define CDC_LINE_CODING_STOP_BITS_TEXT(STOP_BITS) ( \
196+
STOP_BITS == CDC_LINE_CODING_STOP_BITS_1 ? "1" : \
197+
STOP_BITS == CDC_LINE_CODING_STOP_BITS_1_5 ? "1.5" : \
198+
STOP_BITS == CDC_LINE_CODING_STOP_BITS_2 ? "2" : "?" )
199+
195200
// TODO Backward compatible for typos. Maybe removed in the future release
196201
#define CDC_LINE_CONDING_STOP_BITS_1 CDC_LINE_CODING_STOP_BITS_1
197202
#define CDC_LINE_CONDING_STOP_BITS_1_5 CDC_LINE_CODING_STOP_BITS_1_5
@@ -205,6 +210,13 @@ typedef enum {
205210
CDC_LINE_CODING_PARITY_SPACE = 4,
206211
} cdc_line_coding_parity_t;
207212

213+
#define CDC_LINE_CODING_PARITY_CHAR(PARITY) ( \
214+
PARITY == CDC_LINE_CODING_PARITY_NONE ? 'N' : \
215+
PARITY == CDC_LINE_CODING_PARITY_ODD ? 'O' : \
216+
PARITY == CDC_LINE_CODING_PARITY_EVEN ? 'E' : \
217+
PARITY == CDC_LINE_CODING_PARITY_MARK ? 'M' : \
218+
PARITY == CDC_LINE_CODING_PARITY_SPACE ? 'S' : '?' )
219+
208220
//--------------------------------------------------------------------+
209221
// Management Element Notification (Notification Endpoint)
210222
//--------------------------------------------------------------------+
@@ -392,8 +404,7 @@ static inline uint8_t cdc_functional_desc_typeof(uint8_t const * p_desc)
392404
//--------------------------------------------------------------------+
393405
// Requests
394406
//--------------------------------------------------------------------+
395-
typedef struct TU_ATTR_PACKED
396-
{
407+
typedef struct TU_ATTR_PACKED {
397408
uint32_t bit_rate;
398409
uint8_t stop_bits; ///< 0: 1 stop bit - 1: 1.5 stop bits - 2: 2 stop bits
399410
uint8_t parity; ///< 0: None - 1: Odd - 2: Even - 3: Mark - 4: Space
@@ -402,15 +413,16 @@ typedef struct TU_ATTR_PACKED
402413

403414
TU_VERIFY_STATIC(sizeof(cdc_line_coding_t) == 7, "size is not correct");
404415

405-
typedef struct TU_ATTR_PACKED
406-
{
407-
uint16_t dtr : 1;
408-
uint16_t rts : 1;
409-
uint16_t : 6;
410-
uint16_t : 8;
416+
typedef union TU_ATTR_PACKED {
417+
struct {
418+
uint8_t dtr : 1;
419+
uint8_t rts : 1;
420+
uint8_t : 6;
421+
};
422+
uint8_t value;
411423
} cdc_line_control_state_t;
412424

413-
TU_VERIFY_STATIC(sizeof(cdc_line_control_state_t) == 2, "size is not correct");
425+
TU_VERIFY_STATIC(sizeof(cdc_line_control_state_t) == 1, "size is not correct");
414426

415427
TU_ATTR_PACKED_END // End of all packed definitions
416428
TU_ATTR_BIT_FIELD_ORDER_END

0 commit comments

Comments
 (0)