Skip to content

Commit 57bbf3a

Browse files
authored
Merge pull request #2391 from IngHK/cdc_ch34x_support
initial support of CH34x CDC device
2 parents 938cae8 + 769a237 commit 57bbf3a

File tree

9 files changed

+850
-268
lines changed

9 files changed

+850
-268
lines changed

examples/host/cdc_msc_hid/src/cdc_app.c

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,11 @@
2727
#include "tusb.h"
2828
#include "bsp/board_api.h"
2929

30-
//--------------------------------------------------------------------+
31-
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
32-
//--------------------------------------------------------------------+
33-
34-
35-
//------------- IMPLEMENTATION -------------//
36-
37-
size_t get_console_inputs(uint8_t* buf, size_t bufsize)
38-
{
30+
size_t get_console_inputs(uint8_t* buf, size_t bufsize) {
3931
size_t count = 0;
40-
while (count < bufsize)
41-
{
32+
while (count < bufsize) {
4233
int ch = board_getchar();
43-
if ( ch <= 0 ) break;
34+
if (ch <= 0) break;
4435

4536
buf[count] = (uint8_t) ch;
4637
count++;
@@ -49,34 +40,33 @@ size_t get_console_inputs(uint8_t* buf, size_t bufsize)
4940
return count;
5041
}
5142

52-
void cdc_app_task(void)
53-
{
54-
uint8_t buf[64+1]; // +1 for extra null character
55-
uint32_t const bufsize = sizeof(buf)-1;
43+
void cdc_app_task(void) {
44+
uint8_t buf[64 + 1]; // +1 for extra null character
45+
uint32_t const bufsize = sizeof(buf) - 1;
5646

5747
uint32_t count = get_console_inputs(buf, bufsize);
5848
buf[count] = 0;
5949

6050
// loop over all mounted interfaces
61-
for(uint8_t idx=0; idx<CFG_TUH_CDC; idx++)
62-
{
63-
if ( tuh_cdc_mounted(idx) )
64-
{
51+
for (uint8_t idx = 0; idx < CFG_TUH_CDC; idx++) {
52+
if (tuh_cdc_mounted(idx)) {
6553
// console --> cdc interfaces
66-
if (count)
67-
{
54+
if (count) {
6855
tuh_cdc_write(idx, buf, count);
6956
tuh_cdc_write_flush(idx);
7057
}
7158
}
7259
}
7360
}
7461

62+
//--------------------------------------------------------------------+
63+
// TinyUSB callbacks
64+
//--------------------------------------------------------------------+
65+
7566
// Invoked when received new data
76-
void tuh_cdc_rx_cb(uint8_t idx)
77-
{
78-
uint8_t buf[64+1]; // +1 for extra null character
79-
uint32_t const bufsize = sizeof(buf)-1;
67+
void tuh_cdc_rx_cb(uint8_t idx) {
68+
uint8_t buf[64 + 1]; // +1 for extra null character
69+
uint32_t const bufsize = sizeof(buf) - 1;
8070

8171
// forward cdc interfaces -> console
8272
uint32_t count = tuh_cdc_read(idx, buf, bufsize);
@@ -85,29 +75,35 @@ void tuh_cdc_rx_cb(uint8_t idx)
8575
printf((char*) buf);
8676
}
8777

88-
void tuh_cdc_mount_cb(uint8_t idx)
89-
{
90-
tuh_itf_info_t itf_info = { 0 };
78+
// Invoked when a device with CDC interface is mounted
79+
// idx is index of cdc interface in the internal pool.
80+
void tuh_cdc_mount_cb(uint8_t idx) {
81+
tuh_itf_info_t itf_info = {0};
9182
tuh_cdc_itf_get_info(idx, &itf_info);
9283

93-
printf("CDC Interface is mounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.desc.bInterfaceNumber);
84+
printf("CDC Interface is mounted: address = %u, itf_num = %u\r\n", itf_info.daddr,
85+
itf_info.desc.bInterfaceNumber);
9486

9587
#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-
{
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
90+
cdc_line_coding_t line_coding = {0};
91+
if (tuh_cdc_get_local_line_coding(idx, &line_coding)) {
10192
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);
93+
printf(" Parity : %u, Data Width: %u\r\n", line_coding.parity, line_coding.data_bits);
10394
}
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);
10499
#endif
105100
}
106101

107-
void tuh_cdc_umount_cb(uint8_t idx)
108-
{
109-
tuh_itf_info_t itf_info = { 0 };
102+
// Invoked when a device with CDC interface is unmounted
103+
void tuh_cdc_umount_cb(uint8_t idx) {
104+
tuh_itf_info_t itf_info = {0};
110105
tuh_cdc_itf_get_info(idx, &itf_info);
111106

112-
printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.desc.bInterfaceNumber);
107+
printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr,
108+
itf_info.desc.bInterfaceNumber);
113109
}

examples/host/cdc_msc_hid/src/tusb_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
#define CFG_TUH_CDC 1 // CDC ACM
106106
#define CFG_TUH_CDC_FTDI 1 // FTDI Serial. FTDI is not part of CDC class, only to re-use CDC driver API
107107
#define CFG_TUH_CDC_CP210X 1 // CP210x Serial. CP210X is not part of CDC class, only to re-use CDC driver API
108+
#define CFG_TUH_CDC_CH34X 1 // CH340 or CH341 Serial. CH34X is not part of CDC class, only to re-use CDC driver API
108109
#define CFG_TUH_HID (3*CFG_TUH_DEVICE_MAX) // typical keyboard + mouse device can have 3-4 HID interfaces
109110
#define CFG_TUH_MSC 1
110111
#define CFG_TUH_VENDOR 0

examples/host/cdc_msc_hid_freertos/src/tusb_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
#define CFG_TUH_CDC 1 // CDC ACM
111111
#define CFG_TUH_CDC_FTDI 1 // FTDI Serial. FTDI is not part of CDC class, only to re-use CDC driver API
112112
#define CFG_TUH_CDC_CP210X 1 // CP210x Serial. CP210X is not part of CDC class, only to re-use CDC driver API
113+
#define CFG_TUH_CDC_CH34X 1 // CH340 or CH341 Serial. CH34X is not part of CDC class, only to re-use CDC driver API
113114
#define CFG_TUH_HID (3*CFG_TUH_DEVICE_MAX) // typical keyboard + mouse device can have 3-4 HID interfaces
114115
#define CFG_TUH_MSC 1
115116
#define CFG_TUH_VENDOR 0

src/class/cdc/cdc.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ typedef enum{
136136
//--------------------------------------------------------------------+
137137

138138
/// Communication Interface Management Element Request Codes
139-
typedef enum
140-
{
139+
typedef enum {
141140
CDC_REQUEST_SEND_ENCAPSULATED_COMMAND = 0x00, ///< is used to issue a command in the format of the supported control protocol of the Communications Class interface
142141
CDC_REQUEST_GET_ENCAPSULATED_RESPONSE = 0x01, ///< is used to request a response in the format of the supported control protocol of the Communications Class interface.
143142
CDC_REQUEST_SET_COMM_FEATURE = 0x02,
@@ -180,39 +179,38 @@ typedef enum
180179
CDC_REQUEST_GET_ATM_VC_STATISTICS = 0x53,
181180

182181
CDC_REQUEST_MDLM_SEMANTIC_MODEL = 0x60,
183-
}cdc_management_request_t;
182+
} cdc_management_request_t;
184183

185-
enum {
184+
typedef enum {
186185
CDC_CONTROL_LINE_STATE_DTR = 0x01,
187186
CDC_CONTROL_LINE_STATE_RTS = 0x02,
188-
};
187+
} cdc_control_line_state_t;
189188

190-
enum {
189+
typedef enum {
191190
CDC_LINE_CODING_STOP_BITS_1 = 0, // 1 bit
192191
CDC_LINE_CODING_STOP_BITS_1_5 = 1, // 1.5 bits
193192
CDC_LINE_CODING_STOP_BITS_2 = 2, // 2 bits
194-
};
193+
} cdc_line_coding_stopbits_t;
195194

196195
// TODO Backward compatible for typos. Maybe removed in the future release
197196
#define CDC_LINE_CONDING_STOP_BITS_1 CDC_LINE_CODING_STOP_BITS_1
198197
#define CDC_LINE_CONDING_STOP_BITS_1_5 CDC_LINE_CODING_STOP_BITS_1_5
199198
#define CDC_LINE_CONDING_STOP_BITS_2 CDC_LINE_CODING_STOP_BITS_2
200199

201-
enum {
200+
typedef enum {
202201
CDC_LINE_CODING_PARITY_NONE = 0,
203202
CDC_LINE_CODING_PARITY_ODD = 1,
204203
CDC_LINE_CODING_PARITY_EVEN = 2,
205204
CDC_LINE_CODING_PARITY_MARK = 3,
206205
CDC_LINE_CODING_PARITY_SPACE = 4,
207-
};
206+
} cdc_line_coding_parity_t;
208207

209208
//--------------------------------------------------------------------+
210209
// Management Element Notification (Notification Endpoint)
211210
//--------------------------------------------------------------------+
212211

213212
/// 6.3 Notification Codes
214-
typedef enum
215-
{
213+
typedef enum {
216214
CDC_NOTIF_NETWORK_CONNECTION = 0x00, ///< This notification allows the device to notify the host about network connection status.
217215
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.
218216
CDC_NOTIF_AUX_JACK_HOOK_STATE = 0x08,

0 commit comments

Comments
 (0)