This is an automated email from the ASF dual-hosted git repository. jerzy pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit e82d0609fb11919e9faca2f0a86c45d7f1191eea Author: Jerzy Kasenberg <[email protected]> AuthorDate: Thu Aug 18 13:38:24 2022 +0200 tinyusb: Allow USB console with other CDC interfaces This converts USB console to use common CDC code that allows co-existence of several CDC interfaces --- hw/usb/tinyusb/cdc_console/pkg.yml | 1 + hw/usb/tinyusb/cdc_console/src/cdc_console.c | 61 +++++++++++++++------- hw/usb/tinyusb/cdc_console/syscfg.yml | 8 +-- .../tinyusb/std_descriptors/include/tusb_config.h | 47 ++++++++++++++++- .../tinyusb/std_descriptors/src/std_descriptors.c | 26 +++++++-- hw/usb/tinyusb/std_descriptors/syscfg.yml | 5 +- 6 files changed, 120 insertions(+), 28 deletions(-) diff --git a/hw/usb/tinyusb/cdc_console/pkg.yml b/hw/usb/tinyusb/cdc_console/pkg.yml index 45800d7e1..9cdc5612e 100755 --- a/hw/usb/tinyusb/cdc_console/pkg.yml +++ b/hw/usb/tinyusb/cdc_console/pkg.yml @@ -29,6 +29,7 @@ pkg.deps: - "@apache-mynewt-core/hw/hal" - "@apache-mynewt-core/kernel/os" - "@apache-mynewt-core/hw/usb/tinyusb" + - "@apache-mynewt-core/hw/usb/tinyusb/cdc" - "@tinyusb/tinyusb" pkg.init: diff --git a/hw/usb/tinyusb/cdc_console/src/cdc_console.c b/hw/usb/tinyusb/cdc_console/src/cdc_console.c index be6490d32..e91a3cea0 100755 --- a/hw/usb/tinyusb/cdc_console/src/cdc_console.c +++ b/hw/usb/tinyusb/cdc_console/src/cdc_console.c @@ -20,6 +20,7 @@ #include <os/mynewt.h> #include <class/cdc/cdc_device.h> +#include <cdc/cdc.h> #include <console/console.h> #include <bsp/bsp.h> @@ -28,6 +29,12 @@ static struct os_event rx_receive_event; static struct os_event tx_flush_event; static bool connected; +static const struct cdc_callbacks console_cdc_callback; + +cdc_itf_t console_cdc_itf = { + .callbacks = &console_cdc_callback +}; + static void cdc_schedule_tx_flush(void) { @@ -39,11 +46,11 @@ cdc_write(int c) { uint32_t written; - written = tud_cdc_write_char(c); - if (tud_cdc_write_available() == 0) { - tud_cdc_write_flush(); + written = tud_cdc_n_write_char(console_cdc_itf.cdc_num, c); + if (tud_cdc_n_write_available(console_cdc_itf.cdc_num) == 0) { + tud_cdc_n_write_flush(console_cdc_itf.cdc_num); if (written == 0) { - tud_cdc_write_char(c); + tud_cdc_n_write_char(console_cdc_itf.cdc_num, c); } } } @@ -72,11 +79,11 @@ static void tx_flush_ev_cb(struct os_event *ev) { #if MYNEWT_VAL(USBD_CDC_TX_BUFSIZE) - if (connected && tud_cdc_write_available() < CFG_TUD_CDC_TX_BUFSIZE) { + if (connected && tud_cdc_n_write_available(console_cdc_itf.cdc_num) < CFG_TUD_CDC_TX_BUFSIZE) { #else - if (connected && tud_cdc_write_available() < USBD_CDC_DATA_EP_SIZE) { + if (connected && tud_cdc_n_write_available(console_cdc_itf.cdc_num) < USBD_CDC_DATA_EP_SIZE) { #endif - if (tud_cdc_write_flush() == 0) { + if (tud_cdc_n_write_flush(console_cdc_itf.cdc_num) == 0) { /* * Previous data not sent yet. * There is no data sent notification in tinyusb/cdc, retry flush later. @@ -100,8 +107,8 @@ rx_ev_cb(struct os_event *ev) } } - while (tud_cdc_available()) { - console_rejected_char = tud_cdc_read_char(); + while (tud_cdc_n_available(console_cdc_itf.cdc_num)) { + console_rejected_char = tud_cdc_n_read_char(console_cdc_itf.cdc_num); if (console_rejected_char >= 0) { ret = console_handle_char(console_rejected_char); if (ret < 0) { @@ -115,9 +122,12 @@ rx_ev_cb(struct os_event *ev) console_rejected_char = -1; } -void -tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) +static void +cdc_console_line_state_cb(cdc_itf_t *itf, bool dtr, bool rts) { + (void)itf; + (void)rts; + if (dtr != connected) { connected = dtr; cdc_schedule_tx_flush(); @@ -125,20 +135,23 @@ tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) } /* Invoked when CDC interface received data from host */ -void -tud_cdc_rx_cb(uint8_t itf) +static void +cdc_console_rx_cb(cdc_itf_t *itf) { + (void)itf; os_eventq_put(os_eventq_dflt_get(), &rx_receive_event); } -void -tud_cdc_line_coding_cb(uint8_t itf, const cdc_line_coding_t *p_line_coding) +static void +cdc_console_line_coding_cb(cdc_itf_t *itf, const cdc_line_coding_t *p_line_coding) { + (void)itf; } -void -tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) +static void +cdc_console_rx_wanted_cb(cdc_itf_t *itf, char wanted_char) { + (void)itf; } int @@ -147,11 +160,23 @@ usb_cdc_console_pkg_init(void) rx_receive_event.ev_cb = rx_ev_cb; tx_flush_event.ev_cb = tx_flush_ev_cb; + cdc_itf_add(&console_cdc_itf); + return 0; } int usb_cdc_console_is_init(void) { - return (int)tud_cdc_connected(); + return (int)tud_cdc_n_connected(console_cdc_itf.cdc_num); } + +static const struct cdc_callbacks console_cdc_callback = { + .cdc_rx_cb = cdc_console_rx_cb, + .cdc_line_coding_cb = cdc_console_line_coding_cb, + .cdc_line_state_cb = cdc_console_line_state_cb, + .cdc_rx_wanted_cb = cdc_console_rx_wanted_cb, + .cdc_send_break_cb = NULL, + .cdc_tx_complete_cb = NULL, +}; + diff --git a/hw/usb/tinyusb/cdc_console/syscfg.yml b/hw/usb/tinyusb/cdc_console/syscfg.yml index 0dc36b980..7c64373bd 100755 --- a/hw/usb/tinyusb/cdc_console/syscfg.yml +++ b/hw/usb/tinyusb/cdc_console/syscfg.yml @@ -18,12 +18,12 @@ # syscfg.defs: - USBD_CDC_DECRIPTOR_STRING: - description: String for CDC interface + USBD_CDC_CONSOLE_DECRIPTOR_STRING: + description: String for CDC/Console interface value: '"Mynewt console"' syscfg.vals: - USBD_CDC: 1 + USBD_CDC_CONSOLE: 1 syscfg.restrictions: - - "USBD_CDC" + - "USBD_CDC_CONSOLE" diff --git a/hw/usb/tinyusb/std_descriptors/include/tusb_config.h b/hw/usb/tinyusb/std_descriptors/include/tusb_config.h index dc98585ee..42cc716dd 100755 --- a/hw/usb/tinyusb/std_descriptors/include/tusb_config.h +++ b/hw/usb/tinyusb/std_descriptors/include/tusb_config.h @@ -72,11 +72,54 @@ extern "C" { #define CFG_TUD_ENDPOINT0_SIZE MYNEWT_VAL(USBD_EP0_SIZE) /* ------------- CLASS ------------- */ +/* + * If CDC_CONSOLE does not have specific values for endpoint configuration, + * use values for unspecified CDC + */ +#if defined(MYNEWT_VAL_USBD_CDC_CONSOLE_NOTIFY_EP_SIZE) +#define USBD_CDC_CONSOLE_NOTIFY_EP_SIZE MYNEWT_VAL(USBD_CDC_CONSOLE_NOTIFY_EP_SIZE) +#else +#define USBD_CDC_CONSOLE_NOTIFY_EP_SIZE USBD_CDC_NOTIFY_EP_SIZE +#endif + +#if defined(MYNEWT_VAL_USBD_CDC_CONSOLE_NOTIFY_EP) +#define USBD_CDC_CONSOLE_NOTIFY_EP MYNEWT_VAL(USBD_CDC_CONSOLE_NOTIFY_EP) +#else +#define USBD_CDC_CONSOLE_NOTIFY_EP USBD_CDC_NOTIFY_EP +#endif + +#if defined(MYNEWT_VAL_USBD_CDC_CONSOLE_DATA_OUT_EP) +#define USBD_CDC_CONSOLE_DATA_OUT_EP MYNEWT_VAL(USBD_CDC_CONSOLE_DATA_OUT_EP) +#else +#define USBD_CDC_CONSOLE_DATA_OUT_EP USBD_CDC_DATA_OUT_EP +#endif + +#if defined(MYNEWT_VAL_USBD_CDC_CONSOLE_DATA_IN_EP) +#define USBD_CDC_CONSOLE_DATA_IN_EP MYNEWT_VAL(USBD_CDC_CONSOLE_DATA_IN_EP) +#else +#define USBD_CDC_CONSOLE_DATA_IN_EP USBD_CDC_DATA_IN_EP +#endif + +#if defined(MYNEWT_VAL_USBD_CDC_CONSOLE_DATA_EP_SIZE) +#define USBD_CDC_CONSOLE_DATA_EP_SIZE MYNEWT_VAL(USBD_CDC_CONSOLE_DATA_EP_SIZE) +#else +#define USBD_CDC_CONSOLE_DATA_EP_SIZE USBD_CDC_DATA_EP_SIZE +#endif + #if MYNEWT_VAL(USBD_CDC) -#define CFG_TUD_CDC MYNEWT_VAL(USBD_CDC) +#define CFG_CDC MYNEWT_VAL(USBD_CDC) #else -#define CFG_TUD_CDC 0 +#define CFG_CDC 0 #endif + +#if MYNEWT_VAL(CONSOLE_USB) +#define CFG_CDC_CONSOLE MYNEWT_VAL(CONSOLE_USB) +#else +#define CFG_CDC_CONSOLE 0 +#endif + +#define CFG_TUD_CDC ((CFG_CDC) + (CFG_CDC_CONSOLE)) + #if MYNEWT_VAL(USBD_HID) #define CFG_TUD_HID MYNEWT_VAL(USBD_HID) #else diff --git a/hw/usb/tinyusb/std_descriptors/src/std_descriptors.c b/hw/usb/tinyusb/std_descriptors/src/std_descriptors.c index 311edde9d..515777bfc 100755 --- a/hw/usb/tinyusb/std_descriptors/src/std_descriptors.c +++ b/hw/usb/tinyusb/std_descriptors/src/std_descriptors.c @@ -48,6 +48,11 @@ enum usb_desc_ix { #else #define CDC_IF_STR_IX 0 #endif +#if defined MYNEWT_VAL_USBD_CDC_CONSOLE_DESCRIPTOR_STRING + CDC_CONSOLE_IF_STR_IX, +#else +#define CDC_CONSOLE_IF_STR_IX 0 +#endif #if defined MYNEWT_VAL_USBD_MSC_DESCRIPTOR_STRING MSC_IF_STR_IX, #else @@ -257,11 +262,16 @@ enum { #endif #endif -#if CFG_TUD_CDC +#if CFG_CDC ITF_NUM_CDC, ITF_NUM_CDC_DATA, #endif +#if CFG_CDC_CONSOLE + ITF_NUM_CDC_CONSOLE, + ITF_NUM_CDC_CONSOLE_DATA, +#endif + #if CFG_TUD_MSC ITF_NUM_MSC, #endif @@ -278,7 +288,8 @@ enum { }; #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + \ - CFG_TUD_CDC * TUD_CDC_DESC_LEN + \ + CFG_CDC * TUD_CDC_DESC_LEN + \ + CFG_CDC_CONSOLE * TUD_CDC_DESC_LEN + \ CFG_TUD_MSC * TUD_MSC_DESC_LEN + \ CFG_TUD_HID * TUD_HID_DESC_LEN + \ CFG_TUD_BTH * TUD_BTH_DESC_LEN + \ @@ -296,7 +307,13 @@ const uint8_t desc_configuration[] = { 0, 9, 17, 25, 33, 49), #endif -#if CFG_TUD_CDC +#if CFG_CDC_CONSOLE + TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_CONSOLE, CDC_CONSOLE_IF_STR_IX, USBD_CDC_CONSOLE_NOTIFY_EP, + USBD_CDC_CONSOLE_NOTIFY_EP_SIZE, USBD_CDC_CONSOLE_DATA_OUT_EP, USBD_CDC_CONSOLE_DATA_IN_EP, + (CFG_TUSB_RHPORT0_MODE & OPT_MODE_HIGH_SPEED) ? 512 : USBD_CDC_CONSOLE_DATA_EP_SIZE), +#endif + +#if CFG_CDC TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, CDC_IF_STR_IX, USBD_CDC_NOTIFY_EP, USBD_CDC_NOTIFY_EP_SIZE, USBD_CDC_DATA_OUT_EP, USBD_CDC_DATA_IN_EP, (CFG_TUSB_RHPORT0_MODE & OPT_MODE_HIGH_SPEED) ? 512 : USBD_CDC_DATA_EP_SIZE), @@ -338,6 +355,9 @@ const char *string_desc_arr[] = { #if defined MYNEWT_VAL_USBD_CDC_DESCRIPTOR_STRING MYNEWT_VAL(USBD_CDC_DESCRIPTOR_STRING), #endif +#if defined MYNEWT_VAL_USBD_CDC_CONSOLE_DESCRIPTOR_STRING + MYNEWT_VAL(USBD_CDC_CONSOLE_DESCRIPTOR_STRING), +#endif #if defined MYNEWT_VAL_USBD_MSC_DESCRIPTOR_STRING MYNEWT_VAL(USBD_MSC_DESCRIPTOR_STRING), #endif diff --git a/hw/usb/tinyusb/std_descriptors/syscfg.yml b/hw/usb/tinyusb/std_descriptors/syscfg.yml index 25f2e524a..1ab4916d7 100644 --- a/hw/usb/tinyusb/std_descriptors/syscfg.yml +++ b/hw/usb/tinyusb/std_descriptors/syscfg.yml @@ -62,7 +62,10 @@ syscfg.defs: description: Device friendly name value: '"Dev device"' USBD_CDC: - description: Enable CDC device function in TinyUSB stack. + description: Enable CDC device function in TinyUSB stack (other then console or hci). + value: + USBD_CDC_CONSOLE: + description: Enable CDC device function for console in TinyUSB stack. value: USBD_HID: description: Enable HID device function in TinyUSB stack.
