Skip to content

Commit

Permalink
Merge pull request #2391 from IngHK/cdc_ch34x_support
Browse files Browse the repository at this point in the history
initial support of CH34x CDC device
  • Loading branch information
hathach authored Jan 22, 2024
2 parents 938cae8 + 769a237 commit 57bbf3a
Show file tree
Hide file tree
Showing 9 changed files with 850 additions and 268 deletions.
76 changes: 36 additions & 40 deletions examples/host/cdc_msc_hid/src/cdc_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,11 @@
#include "tusb.h"
#include "bsp/board_api.h"

//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+


//------------- IMPLEMENTATION -------------//

size_t get_console_inputs(uint8_t* buf, size_t bufsize)
{
size_t get_console_inputs(uint8_t* buf, size_t bufsize) {
size_t count = 0;
while (count < bufsize)
{
while (count < bufsize) {
int ch = board_getchar();
if ( ch <= 0 ) break;
if (ch <= 0) break;

buf[count] = (uint8_t) ch;
count++;
Expand All @@ -49,34 +40,33 @@ size_t get_console_inputs(uint8_t* buf, size_t bufsize)
return count;
}

void cdc_app_task(void)
{
uint8_t buf[64+1]; // +1 for extra null character
uint32_t const bufsize = sizeof(buf)-1;
void cdc_app_task(void) {
uint8_t buf[64 + 1]; // +1 for extra null character
uint32_t const bufsize = sizeof(buf) - 1;

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

// loop over all mounted interfaces
for(uint8_t idx=0; idx<CFG_TUH_CDC; idx++)
{
if ( tuh_cdc_mounted(idx) )
{
for (uint8_t idx = 0; idx < CFG_TUH_CDC; idx++) {
if (tuh_cdc_mounted(idx)) {
// console --> cdc interfaces
if (count)
{
if (count) {
tuh_cdc_write(idx, buf, count);
tuh_cdc_write_flush(idx);
}
}
}
}

//--------------------------------------------------------------------+
// TinyUSB callbacks
//--------------------------------------------------------------------+

// Invoked when received new data
void tuh_cdc_rx_cb(uint8_t idx)
{
uint8_t buf[64+1]; // +1 for extra null character
uint32_t const bufsize = sizeof(buf)-1;
void tuh_cdc_rx_cb(uint8_t idx) {
uint8_t buf[64 + 1]; // +1 for extra null character
uint32_t const bufsize = sizeof(buf) - 1;

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

void tuh_cdc_mount_cb(uint8_t idx)
{
tuh_itf_info_t itf_info = { 0 };
// Invoked when a device with CDC interface is mounted
// idx is index of cdc interface in the internal pool.
void tuh_cdc_mount_cb(uint8_t idx) {
tuh_itf_info_t itf_info = {0};
tuh_cdc_itf_get_info(idx, &itf_info);

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

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

void tuh_cdc_umount_cb(uint8_t idx)
{
tuh_itf_info_t itf_info = { 0 };
// Invoked when a device with CDC interface is unmounted
void tuh_cdc_umount_cb(uint8_t idx) {
tuh_itf_info_t itf_info = {0};
tuh_cdc_itf_get_info(idx, &itf_info);

printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.desc.bInterfaceNumber);
printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr,
itf_info.desc.bInterfaceNumber);
}
1 change: 1 addition & 0 deletions examples/host/cdc_msc_hid/src/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
#define CFG_TUH_CDC 1 // CDC ACM
#define CFG_TUH_CDC_FTDI 1 // FTDI Serial. FTDI is not part of CDC class, only to re-use CDC driver API
#define CFG_TUH_CDC_CP210X 1 // CP210x Serial. CP210X is not part of CDC class, only to re-use CDC driver API
#define CFG_TUH_CDC_CH34X 1 // CH340 or CH341 Serial. CH34X is not part of CDC class, only to re-use CDC driver API
#define CFG_TUH_HID (3*CFG_TUH_DEVICE_MAX) // typical keyboard + mouse device can have 3-4 HID interfaces
#define CFG_TUH_MSC 1
#define CFG_TUH_VENDOR 0
Expand Down
1 change: 1 addition & 0 deletions examples/host/cdc_msc_hid_freertos/src/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
#define CFG_TUH_CDC 1 // CDC ACM
#define CFG_TUH_CDC_FTDI 1 // FTDI Serial. FTDI is not part of CDC class, only to re-use CDC driver API
#define CFG_TUH_CDC_CP210X 1 // CP210x Serial. CP210X is not part of CDC class, only to re-use CDC driver API
#define CFG_TUH_CDC_CH34X 1 // CH340 or CH341 Serial. CH34X is not part of CDC class, only to re-use CDC driver API
#define CFG_TUH_HID (3*CFG_TUH_DEVICE_MAX) // typical keyboard + mouse device can have 3-4 HID interfaces
#define CFG_TUH_MSC 1
#define CFG_TUH_VENDOR 0
Expand Down
20 changes: 9 additions & 11 deletions src/class/cdc/cdc.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ typedef enum{
//--------------------------------------------------------------------+

/// Communication Interface Management Element Request Codes
typedef enum
{
typedef enum {
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
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.
CDC_REQUEST_SET_COMM_FEATURE = 0x02,
Expand Down Expand Up @@ -180,39 +179,38 @@ typedef enum
CDC_REQUEST_GET_ATM_VC_STATISTICS = 0x53,

CDC_REQUEST_MDLM_SEMANTIC_MODEL = 0x60,
}cdc_management_request_t;
} cdc_management_request_t;

enum {
typedef enum {
CDC_CONTROL_LINE_STATE_DTR = 0x01,
CDC_CONTROL_LINE_STATE_RTS = 0x02,
};
} cdc_control_line_state_t;

enum {
typedef enum {
CDC_LINE_CODING_STOP_BITS_1 = 0, // 1 bit
CDC_LINE_CODING_STOP_BITS_1_5 = 1, // 1.5 bits
CDC_LINE_CODING_STOP_BITS_2 = 2, // 2 bits
};
} cdc_line_coding_stopbits_t;

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

enum {
typedef enum {
CDC_LINE_CODING_PARITY_NONE = 0,
CDC_LINE_CODING_PARITY_ODD = 1,
CDC_LINE_CODING_PARITY_EVEN = 2,
CDC_LINE_CODING_PARITY_MARK = 3,
CDC_LINE_CODING_PARITY_SPACE = 4,
};
} cdc_line_coding_parity_t;

//--------------------------------------------------------------------+
// Management Element Notification (Notification Endpoint)
//--------------------------------------------------------------------+

/// 6.3 Notification Codes
typedef enum
{
typedef enum {
CDC_NOTIF_NETWORK_CONNECTION = 0x00, ///< This notification allows the device to notify the host about network connection status.
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.
CDC_NOTIF_AUX_JACK_HOOK_STATE = 0x08,
Expand Down
Loading

0 comments on commit 57bbf3a

Please sign in to comment.