From 933f931dd93d27262d71ae5fd8fb87bad34a0394 Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Fri, 16 Oct 2026 16:26:10 +0200 Subject: [PATCH] Trying stuff to make the pico reset interface work in cutom cdc mode. --- CMakeLists.txt | 14 ++--- src_device/main.c | 33 ++++------ src_device/tusb_config.h | 25 ++++---- src_device/usb_descriptor.c | 119 +++++++++--------------------------- src_device/usb_descriptor.h | 40 ++++++++++++ 5 files changed, 100 insertions(+), 131 deletions(-) create mode 100644 src_device/usb_descriptor.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 08340ab..9267aae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,11 +30,11 @@ target_sources(Pico_Device PUBLIC # Flags # Enable the reset interface target_compile_definitions(Pico_Device PUBLIC - PICO_ENABLE_USB_RESET_VIA_VENDOR_INTERFACE=1 - # use SDK MS_OS_20 descriptor, as this doesn't have a custom one - PICO_USB_RESET_SUPPORT_MS_OS_20_DESCRIPTOR=1 - # pass the interface number to the SDK MS_OS_20 descriptor - PICO_USB_RESET_MS_OS_20_DESCRIPTOR_ITF=4 + PICO_ENABLE_USB_RESET_VIA_VENDOR_INTERFACE=1 + # use SDK MS_OS_20 descriptor, as this doesn't have a custom one + PICO_USB_RESET_SUPPORT_MS_OS_20_DESCRIPTOR=1 + # pass the interface number to the SDK MS_OS_20 descriptor + PICO_USB_RESET_MS_OS_20_DESCRIPTOR_ITF=1 ) # Include paths @@ -55,12 +55,12 @@ target_link_libraries(Pico_Host target_link_libraries(Pico_Device pico_stdlib + pico_usb_reset + pico_unique_id tinyusb_board tinyusb_device ) -pico_enable_stdio_usb(Pico_Device 1) - # Outputs pico_add_extra_outputs(Pico_Host) pico_add_extra_outputs(Pico_Device) diff --git a/src_device/main.c b/src_device/main.c index 7c98be8..eb6776c 100644 --- a/src_device/main.c +++ b/src_device/main.c @@ -10,6 +10,8 @@ #include #include +#include + #define LED_BLINK_DELAY_MS 1000 int main(void) @@ -18,8 +20,6 @@ int main(void) board_init(); tusb_init(); - // let pico sdk use the first cdc interface for std io - stdio_init_all(); uint32_t start_ms = 0; bool led_state = false; @@ -38,12 +38,9 @@ int main(void) if(tud_cdc_n_connected(0)) { - printf("Connected to CDC 0\n"); - } - - if(tud_cdc_n_connected(1)) - { - printf("Connected to CDC 1\n"); + // and echo back OK on CDC 1 + tud_cdc_n_write(0, (uint8_t const *) "OK\r\n", 4); + tud_cdc_n_write_flush(0); } } } @@ -55,23 +52,17 @@ void tud_cdc_rx_cb(uint8_t itf) // allocate buffer for the data in the stack uint8_t buf[CFG_TUD_CDC_RX_BUFSIZE]; - printf("RX CDC %d\n", itf); - // read the available data // | IMPORTANT: also do this for CDC0 because otherwise // | you won't be able to print anymore to CDC0 // | next time this function is called uint32_t count = tud_cdc_n_read(itf, buf, sizeof(buf)); - // check if the data was received on the second cdc interface - if (itf == 1) { - // process the received data - buf[count] = 0; // null-terminate the string - // now echo data back to the console on CDC 0 - printf("Received on CDC 1: %s\n", buf); + // process the received data + buf[count] = 0; // null-terminate the string - // and echo back OK on CDC 1 - tud_cdc_n_write(itf, (uint8_t const *) "OK\r\n", 4); - tud_cdc_n_write_flush(itf); - } -} \ No newline at end of file + // echo back OK + tud_cdc_n_write_str(itf, buf); + tud_cdc_n_write_char(itf, '\n'); + tud_cdc_n_write_flush(itf); +} diff --git a/src_device/tusb_config.h b/src_device/tusb_config.h index 9f4ea70..2cb460c 100644 --- a/src_device/tusb_config.h +++ b/src_device/tusb_config.h @@ -11,28 +11,29 @@ extern "C" { #endif -#define CFG_TUD_ENABLED (1) +#define CFG_TUD_ENABLED 1 // Legacy RHPORT configuration #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED) + #ifndef BOARD_TUD_RHPORT -#define BOARD_TUD_RHPORT (0) -#endif -// end legacy RHPORT +#define BOARD_TUD_RHPORT 0 +#endif // BOARD_TUD_RHPORT -//------------------------ +//----------------------// // DEVICE CONFIGURATION // -//------------------------ +//----------------------// + +// Enable CDC classes +#define CFG_TUD_CDC 1 -// Enable 2 CDC classes -#define CFG_TUD_CDC (2) // Set CDC FIFO buffer sizes -#define CFG_TUD_CDC_RX_BUFSIZE (64) -#define CFG_TUD_CDC_TX_BUFSIZE (64) -#define CFG_TUD_CDC_EP_BUFSIZE (64) +#define CFG_TUD_CDC_RX_BUFSIZE 64 +#define CFG_TUD_CDC_TX_BUFSIZE 64 +#define CFG_TUD_CDC_EP_BUFSIZE 64 #ifndef CFG_TUD_ENDPOINT0_SIZE -#define CFG_TUD_ENDPOINT0_SIZE (64) +#define CFG_TUD_ENDPOINT0_SIZE 64 #endif #ifdef __cplusplus diff --git a/src_device/usb_descriptor.c b/src_device/usb_descriptor.c index 32c947b..42471eb 100644 --- a/src_device/usb_descriptor.c +++ b/src_device/usb_descriptor.c @@ -4,43 +4,20 @@ * SPDX-License-Identifier: BSD-3-Clause */ +#include "usb_descriptor.h" + #include #include #include -#include "pico/usb_reset.h" - -// set some example Vendor and Product ID -// the board will use to identify at the host -#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) -#define CDC_EXAMPLE_VID 0xCafe -// use _PID_MAP to generate unique PID for each interface -#define CDC_EXAMPLE_PID (0x4100 | _PID_MAP(CDC, 0)) -// set to 2.10 so the MS_OS_20_DESCRIPTOR works -#define CDC_EXAMPLE_BCD 0x0210 - -// String descriptors referenced with .i... in the descriptor tables - -enum { - STRID_LANGID = 0, // 0: supported language ID - STRID_MANUFACTURER, // 1: Manufacturer - STRID_PRODUCT, // 2: Product - STRID_SERIAL, // 3: Serials - STRID_CDC_0, // 4: CDC Interface 0 - STRID_CDC_1, // 5: CDC Interface 1 - STRID_RPI_RESET, // 6: Reset Interface -}; - // array of pointer to string descriptors -char const *string_desc_arr[] = { - // switched because board is little endian - (const char[]) { 0x09, 0x04 }, // 0: supported language is English (0x0409) - "Raspberry Pi", // 1: Manufacturer - "Pico (2)", // 2: Product - NULL, // 3: Serials (null so it uses unique ID if available) - "Pico SDK stdio", // 4: CDC Interface 0 - "Custom CDC", // 5: CDC Interface 1, - "Reset" // 6: Reset Interface +const char const *string_desc_arr[] = { + [STR_ID_LANGID] = (const char[]) { 0x09, 0x04 }, // Supported language is English (0x0409), switched because board is little endian + [STR_ID_MANUFACTURER] = "Raspberry Pi", // Manufacturer + [STR_ID_PRODUCT] = "Pico (0)", // Product + [STR_ID_SERIAL] = NULL, // Serials (null so it uses unique ID if available) + [STR_ID_CDC] = "Custom CDC Test", // CDC Interface name + [STR_ID_RPI_RESET] = "Reset" // RPI Reset Interface name }; // defines a descriptor that will be communicated to the host @@ -59,61 +36,25 @@ tusb_desc_device_t const desc_device = { .idProduct = CDC_EXAMPLE_PID, .bcdDevice = 0x0100, // Device release number - .iManufacturer = STRID_MANUFACTURER, // Index of manufacturer string - .iProduct = STRID_PRODUCT, // Index of product string - .iSerialNumber = STRID_SERIAL, // Index of serial number string + .iManufacturer = STR_ID_MANUFACTURER, // Index of manufacturer string + .iProduct = STR_ID_PRODUCT, // Index of product string + .iSerialNumber = STR_ID_SERIAL, // Index of serial number string .bNumConfigurations = 0x01 // 1 configuration }; -// called when host requests to get device descriptor -uint8_t const *tud_descriptor_device_cb(void); - -enum { - ITF_NUM_CDC_0 = 0, - ITF_NUM_CDC_0_DATA, - ITF_NUM_CDC_1, - ITF_NUM_CDC_1_DATA, - ITF_NUM_RPI_RESET, - ITF_NUM_TOTAL -}; - -static_assert(ITF_NUM_RPI_RESET == PICO_USB_RESET_MS_OS_20_DESCRIPTOR_ITF, "ITF_NUM_RPI_RESET must be equal to the PICO_USB_RESET_MS_OS_20_DESCRIPTOR_ITF set in CMakeLists.txt"); - -// total length of configuration descriptor -#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_CDC * TUD_CDC_DESC_LEN + TUD_RPI_RESET_DESC_LEN) - -// define endpoint numbers -#define EPNUM_CDC_0_NOTIF 0x81 // notification endpoint for CDC 0 -#define EPNUM_CDC_0_OUT 0x02 // out endpoint for CDC 0 -#define EPNUM_CDC_0_IN 0x82 // in endpoint for CDC 0 - -#define EPNUM_CDC_1_NOTIF 0x84 // notification endpoint for CDC 1 -#define EPNUM_CDC_1_OUT 0x05 // out endpoint for CDC 1 -#define EPNUM_CDC_1_IN 0x85 // in endpoint for CDC 1 - -// configure descriptor (for 2 CDC interfaces) +// configure descriptor uint8_t const desc_configuration[] = { // config descriptor | how much power in mA, count of interfaces, ... TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x80, 100), - // CDC 0: Communication Interface - TODO: get 64 from tusb_config.h - TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_0, STRID_CDC_0, EPNUM_CDC_0_NOTIF, 8, EPNUM_CDC_0_OUT, EPNUM_CDC_0_IN, 64), - // CDC 0: Data Interface - //TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_0_DATA, 4, 0x01, 0x02), + // CDC Data Interface + TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, STR_ID_CDC, EPNUM_CDC_0_NOTIF, 8, EPNUM_CDC_0_OUT, EPNUM_CDC_0_IN, CFG_TUD_CDC_EP_BUFSIZE), - // CDC 1: Communication Interface - TODO: get 64 from tusb_config.h - TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_1, STRID_CDC_1, EPNUM_CDC_1_NOTIF, 8, EPNUM_CDC_1_OUT, EPNUM_CDC_1_IN, 64), - // CDC 1: Data Interface - //TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_1_DATA, 4, 0x03, 0x04), - - // RPi Reset Interface - TUD_RPI_RESET_DESCRIPTOR(ITF_NUM_RPI_RESET, STRID_RPI_RESET), + // RPI Reset Interface + TUD_RPI_RESET_DESCRIPTOR(ITF_NUM_RPI_RESET, STR_ID_RPI_RESET) }; -// called when host requests to get configuration descriptor -uint8_t const * tud_descriptor_configuration_cb(uint8_t index); - // more device descriptor this time the qualifier tusb_desc_device_qualifier_t const desc_device_qualifier = { .bLength = sizeof(tusb_desc_device_t), @@ -129,29 +70,22 @@ tusb_desc_device_qualifier_t const desc_device_qualifier = { .bReserved = 0x00 }; -// called when host requests to get device qualifier descriptor -uint8_t const* tud_descriptor_device_qualifier_cb(void); - // buffer to hold the string descriptor during the request | plus 1 for the null terminator static uint16_t _desc_str[32 + 1]; -// called when host request to get string descriptor -uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid); - -// --------------------------------------------------------------------+ -// IMPLEMENTATION -// --------------------------------------------------------------------+ - +// called when host requests to get device descriptor uint8_t const *tud_descriptor_device_cb(void) { return (uint8_t const *)&desc_device; } +// called when host requests to get device qualifier descriptor uint8_t const* tud_descriptor_device_qualifier_cb(void) { return (uint8_t const *)&desc_device_qualifier; } +// called when host requests to get configuration descriptor uint8_t const * tud_descriptor_configuration_cb(uint8_t index) { // avoid unused parameter warning and keep function signature consistent @@ -160,20 +94,23 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index) return desc_configuration; } +// called when host request to get string descriptor uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { // TODO: check lang id - (void) langid; + (void)langid; + size_t char_count; // Determine which string descriptor to return - switch (index) { - case STRID_LANGID: - memcpy(&_desc_str[1], string_desc_arr[STRID_LANGID], 2); + switch(index) + { + case STR_ID_LANGID: + memcpy(&_desc_str[1], string_desc_arr[STR_ID_LANGID], 2); char_count = 1; break; - case STRID_SERIAL: + case STR_ID_SERIAL: // try to read the serial from the board char_count = board_usb_get_serial(_desc_str + 1, 32); break; diff --git a/src_device/usb_descriptor.h b/src_device/usb_descriptor.h new file mode 100644 index 0000000..85dba44 --- /dev/null +++ b/src_device/usb_descriptor.h @@ -0,0 +1,40 @@ +#ifndef _USB_DESCRIPTOR_H +#define _USB_DESCRIPTOR_H + +#include +#include + +// set some example Vendor and Product ID +// the board will use to identify at the host +#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) +#define CDC_EXAMPLE_VID 0xCafe +// use _PID_MAP to generate unique PID for each interface +#define CDC_EXAMPLE_PID (0x4100 | _PID_MAP(CDC, 0)) +// set to 2.10 so the MS_OS_20_DESCRIPTOR works +#define CDC_EXAMPLE_BCD 0x0210 + +// String descriptors referenced with .i... in the descriptor tables +enum { + STR_ID_LANGID = 0, // Supported language ID + STR_ID_MANUFACTURER, // Manufacturer + STR_ID_PRODUCT, // Product + STR_ID_SERIAL, // Serials + STR_ID_CDC, // CDC Interface 0 + STR_ID_RPI_RESET // RPI Reset interface +}; + +enum { + ITF_NUM_CDC = 0, + ITF_NUM_RPI_RESET, + ITF_NUM_TOTAL +}; + +// total length of configuration descriptor +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_CDC * TUD_CDC_DESC_LEN) + +// define endpoint numbers +#define EPNUM_CDC_0_NOTIF 0x81 // notification endpoint for CDC 0 +#define EPNUM_CDC_0_OUT 0x02 // out endpoint for CDC 0 +#define EPNUM_CDC_0_IN 0x82 // in endpoint for CDC 0 + +#endif // _USB_DESCRIPTOR_H \ No newline at end of file