Skip to content

CHCh Upgrade: Improved FTDI and CP210x support, add PL2303 support, bugfixes #2488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 60 commits into from
Jun 27, 2025
Merged
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
b9c44ee
improved tusb_config.h comment
heikokue Feb 10, 2024
ab6b9e3
Merge remote-tracking branch 'remotes/hathach/master' into cdch_upgrade
heikokue Feb 22, 2024
069c68a
sorted driver functions into Control Request, Driver API, Enumeration…
heikokue Feb 23, 2024
47777a6
improved TU_LOGs
heikokue Feb 20, 2024
829ea52
splitted cdch_internal_control_complete() into driver's _internal_con…
heikokue Feb 22, 2024
2f50f5a
changed to use of p_cdc->requested_line_coding
heikokue Feb 22, 2024
7dd435c
changed to use of p_cdc->requested_line_state
heikokue Feb 20, 2024
dcadf8c
created set_function_call()
heikokue Feb 22, 2024
ea86bbe
added continue enum after config fail
heikokue Feb 21, 2024
22a12c7
improved ACM checks
heikokue Feb 22, 2024
138567a
fixed #2448 CH34x ch34x_set_line_coding() callback bug
heikokue Feb 18, 2024
db511fb
fixed CFG_TUH_CDC_LINE_CONTROL_ON_ENUM handling. only set if defined.…
heikokue Feb 19, 2024
0b5f85e
created set_line_coding_sequence() and void set_line_coding_stage1_co…
heikokue Feb 21, 2024
7fef594
improved FTDI support
heikokue Feb 24, 2024
4547737
improved CP210x support
heikokue Feb 22, 2024
aabee25
added PL2303 support
heikokue Feb 22, 2024
ea175a7
updated contribution, readme and some comments
heikokue Feb 24, 2024
2b507db
small changes & code style
heikokue Feb 24, 2024
da93fcf
improved TU_LOGs
heikokue Feb 22, 2024
46a861b
improved PL2303 TU_LOGs
heikokue Feb 23, 2024
af5e75c
Merge remote-tracking branch 'remotes/hathach/master' into cdch_upgrade
heikokue Feb 24, 2024
7f7576f
Merge remote-tracking branch 'remotes/hathach/master' into cdch_upgrade
heikokue Feb 29, 2024
f97e312
FTDI fixed itf_num and some improvement
heikokue Feb 28, 2024
d3d61da
improved & fixed compiler warnings device descriptor handling
heikokue Feb 25, 2024
edf1320
removed expendable ACM check
heikokue Feb 28, 2024
3cf9cb9
small PL2303 improvements
heikokue Feb 28, 2024
e7308e3
improved TU_LOGs
heikokue Feb 28, 2024
e6d27b6
fixed IAR compile error
heikokue Feb 29, 2024
dea27d2
added explicite (uint16_t) casts inside tu_htole16()
heikokue Feb 29, 2024
e055104
added use of cdc_line_control_state_t type in CDCh
heikokue Mar 3, 2024
a9cc07f
added line control function using cdc_line_control_state_t
heikokue Mar 10, 2024
ee92e58
added defines CFG_TUH_CDC_DTR_CONTROL_ON_ENUM & CFG_TUH_CDC_RTS_CONTR…
heikokue Mar 3, 2024
2786a61
fixed FTDI set control line
heikokue Mar 3, 2024
cb69ed0
code style and clean up CDC serial header files
heikokue Mar 21, 2024
1bbd658
Merge remote-tracking branch 'remotes/hathach/master' into work
heikokue Mar 21, 2024
e2a5630
Merge remote-tracking branch 'remotes/hathach/master' into cdch_upgrade
heikokue Apr 4, 2024
5e67b92
fixed compile warnings
heikokue Apr 4, 2024
e07ee4a
CP210x removed baudrate check, fixed data bits check
heikokue Apr 4, 2024
a1b1c1f
foxed FTDI flow control config
heikokue Apr 4, 2024
e02a309
disable PL2303 flow control config
heikokue Apr 4, 2024
68602e4
small change process config complete
heikokue Apr 4, 2024
0c5e14c
updated doc
heikokue Apr 4, 2024
f00e698
Merge remote-tracking branch 'remotes/hathach/master' into cdch_upgrade
heikokue Apr 4, 2024
4a44dd5
Merge branch 'master' into fork/IngHK/cdch_upgrade
hathach Jun 16, 2025
d4abf43
add common cdch_process_set_config() to safely complete set_config() …
hathach Jun 17, 2025
506edc6
add get_itf_by_xfer() to better determine cdc interface from xfer com…
hathach Jun 18, 2025
9503883
usbh: add new API tuh_descriptor_get_device_local()
hathach Jun 18, 2025
2adb305
house keeping
hathach Jun 19, 2025
ec1a262
clean up pl2303 type data
hathach Jun 19, 2025
fa3ec44
revert CFG_TUH_CDC_DTR/RTS_CONTROL_ON_ENUM
hathach Jun 19, 2025
ce9140a
rename tuh_cdc_get_local_line_coding to tuh_cdc_get_line_coding_local
hathach Jun 19, 2025
221b528
union ftdi/pl2303/acm data to save memory.
hathach Jun 19, 2025
5c974ce
usbh make TU_API_SYNC() public, to implement sync() API, change retur…
hathach Jun 20, 2025
adf6cbf
cdch clean up and refactor, add explicit sync() API
hathach Jun 20, 2025
900d0d9
refactor change signature of serial driver's process_set_config
hathach Jun 20, 2025
470e12f
refactor, add cdch_internal_control_complete()
hathach Jun 20, 2025
8b5d703
major refactor to generalize cdch serial driver
hathach Jun 27, 2025
f4d049e
update acm and pl2303 to match the rest of drivers
hathach Jun 27, 2025
0194b84
use enum buf for process_set_config for ch34x and pl2303
hathach Jun 27, 2025
d863624
clean up
hathach Jun 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 47 additions & 61 deletions src/class/cdc/cdc_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,115 +421,101 @@ bool tuh_cdc_read_clear (uint8_t idx) {
// Control Endpoint API
//--------------------------------------------------------------------+

bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
Copy link
Author

@IngHK IngHK Feb 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the central flow code of tuh_cdc_set_control_line_state(), tuh_cdc_set_baudrate(), tuh_cdc_set_data_format() and tuh_cdc_set_line_coding() is identical and very tricky, it was outsourced to the common subfunction set_function_call(), which calls the regarding driver's function.
Here you see, why I moved (among other things) the requested line coding and line state into p_cdc->requested..., because it also simplifies the parameter transfer from Control Endpoint API to Driver API independent of the upper mentoined API functions

cdch_interface_t* p_cdc = get_itf(idx);
TU_VERIFY(p_cdc && p_cdc->serial_drid < SERIAL_DRIVER_COUNT);
TU_LOG_P_CDC("set control line state line_state = %u", line_state);
cdch_serial_driver_t const* driver = &serial_drivers[p_cdc->serial_drid];

p_cdc->requested_line_state = (uint8_t) line_state;

// call of (non-)blocking set-functions (to set line state, baudrate, ...)
static bool set_function_call (
cdch_interface_t * p_cdc,
bool (*set_function)(cdch_interface_t * p_cdc, tuh_xfer_cb_t complete_cb, uintptr_t user_data),
tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
if (complete_cb) {
return driver->set_control_line_state(p_cdc, complete_cb, user_data);
// non-blocking with call back
return set_function(p_cdc, complete_cb, user_data);
} else {
// blocking
xfer_result_t result = XFER_RESULT_INVALID;
bool ret = driver->set_control_line_state(p_cdc, complete_cb, (uintptr_t) &result);
xfer_result_t result = XFER_RESULT_INVALID; // use local result, because user_data ptr may be NULL
bool ret = set_function(p_cdc, NULL, (uintptr_t) &result);

if (user_data) {
// user_data is not NULL, return result via user_data
*((xfer_result_t*) user_data) = result;
*((xfer_result_t *) user_data) = result;
}

TU_VERIFY(ret && result == XFER_RESULT_SUCCESS);
return (ret && result == XFER_RESULT_SUCCESS);
}
}

bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
cdch_interface_t * p_cdc = get_itf(idx);
TU_VERIFY(p_cdc && p_cdc->serial_drid < SERIAL_DRIVER_COUNT);
TU_LOG_P_CDC("set control line state line_state = %u", line_state);
cdch_serial_driver_t const * driver = &serial_drivers[p_cdc->serial_drid];

p_cdc->requested_line_state = (uint8_t) line_state;

bool ret = set_function_call(p_cdc, driver->set_control_line_state, complete_cb, user_data);

if (ret && !complete_cb) {
p_cdc->line_state = (uint8_t) line_state;
return true;
}

return ret;
}

bool tuh_cdc_set_baudrate(uint8_t idx, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
cdch_interface_t* p_cdc = get_itf(idx);
cdch_interface_t * p_cdc = get_itf(idx);
TU_VERIFY(p_cdc && p_cdc->serial_drid < SERIAL_DRIVER_COUNT);
TU_LOG_P_CDC("set baudrate = %lu", baudrate);
cdch_serial_driver_t const* driver = &serial_drivers[p_cdc->serial_drid];
cdch_serial_driver_t const * driver = &serial_drivers[p_cdc->serial_drid];

p_cdc->requested_line_coding.bit_rate = baudrate;

if (complete_cb) {
return driver->set_baudrate(p_cdc, complete_cb, user_data);
} else {
// blocking
xfer_result_t result = XFER_RESULT_INVALID;
bool ret = driver->set_baudrate(p_cdc, complete_cb, (uintptr_t) &result);

if (user_data) {
// user_data is not NULL, return result via user_data
*((xfer_result_t*) user_data) = result;
}
bool ret = set_function_call(p_cdc, driver->set_baudrate, complete_cb, user_data);

TU_VERIFY(ret && result == XFER_RESULT_SUCCESS);
if (ret && !complete_cb) {
p_cdc->line_coding.bit_rate = baudrate;
return true;
}

return ret;
}

bool tuh_cdc_set_data_format(uint8_t idx, uint8_t stop_bits, uint8_t parity, uint8_t data_bits,
tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
cdch_interface_t* p_cdc = get_itf(idx);
cdch_interface_t * p_cdc = get_itf(idx);
TU_VERIFY(p_cdc && p_cdc->serial_drid < SERIAL_DRIVER_COUNT);
TU_LOG_P_CDC("set data format data_bits = %u parity = %u stop_bits = %u (indexes!)",
data_bits, parity, stop_bits);
cdch_serial_driver_t const* driver = &serial_drivers[p_cdc->serial_drid];
cdch_serial_driver_t const * driver = &serial_drivers[p_cdc->serial_drid];

p_cdc->requested_line_coding.stop_bits = stop_bits;
p_cdc->requested_line_coding.parity = parity;
p_cdc->requested_line_coding.data_bits = data_bits;

if (complete_cb) {
return driver->set_data_format(p_cdc, complete_cb, user_data);
} else {
// blocking
xfer_result_t result = XFER_RESULT_INVALID;
bool ret = driver->set_data_format(p_cdc, complete_cb, (uintptr_t) &result);

if (user_data) {
// user_data is not NULL, return result via user_data
*((xfer_result_t*) user_data) = result;
}
bool ret = set_function_call(p_cdc, driver->set_data_format, complete_cb, user_data);

TU_VERIFY(ret && result == XFER_RESULT_SUCCESS);
if (ret && !complete_cb) {
p_cdc->line_coding.stop_bits = stop_bits;
p_cdc->line_coding.parity = parity;
p_cdc->line_coding.data_bits = data_bits;
return true;
}

return ret;
}

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) {
cdch_interface_t* p_cdc = get_itf(idx);
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) {
cdch_interface_t * p_cdc = get_itf(idx);
TU_VERIFY(p_cdc && p_cdc->serial_drid < SERIAL_DRIVER_COUNT);
TU_LOG_P_CDC("set line coding baudrate = %lu data_bits = %u parity = %u stop_bits = %u (indexes!)",
line_coding->bit_rate, line_coding->data_bits, line_coding->parity, line_coding->stop_bits);
cdch_serial_driver_t const* driver = &serial_drivers[p_cdc->serial_drid];
cdch_serial_driver_t const * driver = &serial_drivers[p_cdc->serial_drid];

p_cdc->requested_line_coding = *line_coding;

if ( complete_cb ) {
return driver->set_line_coding(p_cdc, complete_cb, user_data);
} else {
// blocking
xfer_result_t result = XFER_RESULT_INVALID;
bool ret = driver->set_line_coding(p_cdc, complete_cb, (uintptr_t) &result);
bool ret = set_function_call(p_cdc, driver->set_line_coding, complete_cb, user_data);

if (user_data) {
// user_data is not NULL, return result via user_data
*((xfer_result_t*) user_data) = result;
}

TU_VERIFY(ret && result == XFER_RESULT_SUCCESS);
if (ret && !complete_cb) {
p_cdc->line_coding = *line_coding;
return true;
}

return ret;
}

//--------------------------------------------------------------------+
Expand Down