Compare commits
1 Commits
main
...
custom_cdc
| Author | SHA1 | Date |
|---|---|---|
|
|
933f931dd9 |
|
|
@ -30,11 +30,11 @@ target_sources(Pico_Device PUBLIC
|
||||||
# Flags
|
# Flags
|
||||||
# Enable the reset interface
|
# Enable the reset interface
|
||||||
target_compile_definitions(Pico_Device PUBLIC
|
target_compile_definitions(Pico_Device PUBLIC
|
||||||
PICO_ENABLE_USB_RESET_VIA_VENDOR_INTERFACE=1
|
PICO_ENABLE_USB_RESET_VIA_VENDOR_INTERFACE=1
|
||||||
# use SDK MS_OS_20 descriptor, as this doesn't have a custom one
|
# use SDK MS_OS_20 descriptor, as this doesn't have a custom one
|
||||||
PICO_USB_RESET_SUPPORT_MS_OS_20_DESCRIPTOR=1
|
PICO_USB_RESET_SUPPORT_MS_OS_20_DESCRIPTOR=1
|
||||||
# pass the interface number to the SDK MS_OS_20 descriptor
|
# pass the interface number to the SDK MS_OS_20 descriptor
|
||||||
PICO_USB_RESET_MS_OS_20_DESCRIPTOR_ITF=4
|
PICO_USB_RESET_MS_OS_20_DESCRIPTOR_ITF=1
|
||||||
)
|
)
|
||||||
|
|
||||||
# Include paths
|
# Include paths
|
||||||
|
|
@ -55,12 +55,12 @@ target_link_libraries(Pico_Host
|
||||||
|
|
||||||
target_link_libraries(Pico_Device
|
target_link_libraries(Pico_Device
|
||||||
pico_stdlib
|
pico_stdlib
|
||||||
|
pico_usb_reset
|
||||||
|
pico_unique_id
|
||||||
tinyusb_board
|
tinyusb_board
|
||||||
tinyusb_device
|
tinyusb_device
|
||||||
)
|
)
|
||||||
|
|
||||||
pico_enable_stdio_usb(Pico_Device 1)
|
|
||||||
|
|
||||||
# Outputs
|
# Outputs
|
||||||
pico_add_extra_outputs(Pico_Host)
|
pico_add_extra_outputs(Pico_Host)
|
||||||
pico_add_extra_outputs(Pico_Device)
|
pico_add_extra_outputs(Pico_Device)
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@
|
||||||
#include <pico/stdio.h>
|
#include <pico/stdio.h>
|
||||||
#include <pico/stdlib.h>
|
#include <pico/stdlib.h>
|
||||||
|
|
||||||
|
#include <pico/usb_reset.h>
|
||||||
|
|
||||||
#define LED_BLINK_DELAY_MS 1000
|
#define LED_BLINK_DELAY_MS 1000
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
|
|
@ -18,8 +20,6 @@ int main(void)
|
||||||
board_init();
|
board_init();
|
||||||
tusb_init();
|
tusb_init();
|
||||||
|
|
||||||
// let pico sdk use the first cdc interface for std io
|
|
||||||
stdio_init_all();
|
|
||||||
|
|
||||||
uint32_t start_ms = 0;
|
uint32_t start_ms = 0;
|
||||||
bool led_state = false;
|
bool led_state = false;
|
||||||
|
|
@ -38,12 +38,9 @@ int main(void)
|
||||||
|
|
||||||
if(tud_cdc_n_connected(0))
|
if(tud_cdc_n_connected(0))
|
||||||
{
|
{
|
||||||
printf("Connected to CDC 0\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);
|
||||||
if(tud_cdc_n_connected(1))
|
|
||||||
{
|
|
||||||
printf("Connected to CDC 1\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -55,23 +52,17 @@ void tud_cdc_rx_cb(uint8_t itf)
|
||||||
// allocate buffer for the data in the stack
|
// allocate buffer for the data in the stack
|
||||||
uint8_t buf[CFG_TUD_CDC_RX_BUFSIZE];
|
uint8_t buf[CFG_TUD_CDC_RX_BUFSIZE];
|
||||||
|
|
||||||
printf("RX CDC %d\n", itf);
|
|
||||||
|
|
||||||
// read the available data
|
// read the available data
|
||||||
// | IMPORTANT: also do this for CDC0 because otherwise
|
// | IMPORTANT: also do this for CDC0 because otherwise
|
||||||
// | you won't be able to print anymore to CDC0
|
// | you won't be able to print anymore to CDC0
|
||||||
// | next time this function is called
|
// | next time this function is called
|
||||||
uint32_t count = tud_cdc_n_read(itf, buf, sizeof(buf));
|
uint32_t count = tud_cdc_n_read(itf, buf, sizeof(buf));
|
||||||
|
|
||||||
// check if the data was received on the second cdc interface
|
// process the received data
|
||||||
if (itf == 1) {
|
buf[count] = 0; // null-terminate the string
|
||||||
// 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);
|
|
||||||
|
|
||||||
// and echo back OK on CDC 1
|
// echo back OK
|
||||||
tud_cdc_n_write(itf, (uint8_t const *) "OK\r\n", 4);
|
tud_cdc_n_write_str(itf, buf);
|
||||||
tud_cdc_n_write_flush(itf);
|
tud_cdc_n_write_char(itf, '\n');
|
||||||
}
|
tud_cdc_n_write_flush(itf);
|
||||||
}
|
}
|
||||||
|
|
@ -11,28 +11,29 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define CFG_TUD_ENABLED (1)
|
#define CFG_TUD_ENABLED 1
|
||||||
|
|
||||||
// Legacy RHPORT configuration
|
// Legacy RHPORT configuration
|
||||||
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED)
|
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED)
|
||||||
|
|
||||||
#ifndef BOARD_TUD_RHPORT
|
#ifndef BOARD_TUD_RHPORT
|
||||||
#define BOARD_TUD_RHPORT (0)
|
#define BOARD_TUD_RHPORT 0
|
||||||
#endif
|
#endif // BOARD_TUD_RHPORT
|
||||||
// end legacy RHPORT
|
|
||||||
|
|
||||||
//------------------------
|
//----------------------//
|
||||||
// DEVICE CONFIGURATION //
|
// DEVICE CONFIGURATION //
|
||||||
//------------------------
|
//----------------------//
|
||||||
|
|
||||||
|
// Enable CDC classes
|
||||||
|
#define CFG_TUD_CDC 1
|
||||||
|
|
||||||
// Enable 2 CDC classes
|
|
||||||
#define CFG_TUD_CDC (2)
|
|
||||||
// Set CDC FIFO buffer sizes
|
// Set CDC FIFO buffer sizes
|
||||||
#define CFG_TUD_CDC_RX_BUFSIZE (64)
|
#define CFG_TUD_CDC_RX_BUFSIZE 64
|
||||||
#define CFG_TUD_CDC_TX_BUFSIZE (64)
|
#define CFG_TUD_CDC_TX_BUFSIZE 64
|
||||||
#define CFG_TUD_CDC_EP_BUFSIZE (64)
|
#define CFG_TUD_CDC_EP_BUFSIZE 64
|
||||||
|
|
||||||
#ifndef CFG_TUD_ENDPOINT0_SIZE
|
#ifndef CFG_TUD_ENDPOINT0_SIZE
|
||||||
#define CFG_TUD_ENDPOINT0_SIZE (64)
|
#define CFG_TUD_ENDPOINT0_SIZE 64
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
||||||
|
|
@ -4,43 +4,20 @@
|
||||||
* SPDX-License-Identifier: BSD-3-Clause
|
* SPDX-License-Identifier: BSD-3-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "usb_descriptor.h"
|
||||||
|
|
||||||
#include <tusb.h>
|
#include <tusb.h>
|
||||||
#include <bsp/board_api.h>
|
#include <bsp/board_api.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#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
|
// array of pointer to string descriptors
|
||||||
char const *string_desc_arr[] = {
|
const char const *string_desc_arr[] = {
|
||||||
// switched because board is little endian
|
[STR_ID_LANGID] = (const char[]) { 0x09, 0x04 }, // Supported language is English (0x0409), switched because board is little endian
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: supported language is English (0x0409)
|
[STR_ID_MANUFACTURER] = "Raspberry Pi", // Manufacturer
|
||||||
"Raspberry Pi", // 1: Manufacturer
|
[STR_ID_PRODUCT] = "Pico (0)", // Product
|
||||||
"Pico (2)", // 2: Product
|
[STR_ID_SERIAL] = NULL, // Serials (null so it uses unique ID if available)
|
||||||
NULL, // 3: Serials (null so it uses unique ID if available)
|
[STR_ID_CDC] = "Custom CDC Test", // CDC Interface name
|
||||||
"Pico SDK stdio", // 4: CDC Interface 0
|
[STR_ID_RPI_RESET] = "Reset" // RPI Reset Interface name
|
||||||
"Custom CDC", // 5: CDC Interface 1,
|
|
||||||
"Reset" // 6: Reset Interface
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// defines a descriptor that will be communicated to the host
|
// 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,
|
.idProduct = CDC_EXAMPLE_PID,
|
||||||
.bcdDevice = 0x0100, // Device release number
|
.bcdDevice = 0x0100, // Device release number
|
||||||
|
|
||||||
.iManufacturer = STRID_MANUFACTURER, // Index of manufacturer string
|
.iManufacturer = STR_ID_MANUFACTURER, // Index of manufacturer string
|
||||||
.iProduct = STRID_PRODUCT, // Index of product string
|
.iProduct = STR_ID_PRODUCT, // Index of product string
|
||||||
.iSerialNumber = STRID_SERIAL, // Index of serial number string
|
.iSerialNumber = STR_ID_SERIAL, // Index of serial number string
|
||||||
|
|
||||||
.bNumConfigurations = 0x01 // 1 configuration
|
.bNumConfigurations = 0x01 // 1 configuration
|
||||||
};
|
};
|
||||||
|
|
||||||
// called when host requests to get device descriptor
|
// configure 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)
|
|
||||||
uint8_t const desc_configuration[] = {
|
uint8_t const desc_configuration[] = {
|
||||||
// config descriptor | how much power in mA, count of interfaces, ...
|
// config descriptor | how much power in mA, count of interfaces, ...
|
||||||
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x80, 100),
|
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x80, 100),
|
||||||
|
|
||||||
// CDC 0: Communication Interface - TODO: get 64 from tusb_config.h
|
// CDC Data Interface
|
||||||
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_0, STRID_CDC_0, EPNUM_CDC_0_NOTIF, 8, EPNUM_CDC_0_OUT, EPNUM_CDC_0_IN, 64),
|
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 0: Data Interface
|
|
||||||
//TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_0_DATA, 4, 0x01, 0x02),
|
|
||||||
|
|
||||||
// CDC 1: Communication Interface - TODO: get 64 from tusb_config.h
|
// RPI Reset Interface
|
||||||
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_1, STRID_CDC_1, EPNUM_CDC_1_NOTIF, 8, EPNUM_CDC_1_OUT, EPNUM_CDC_1_IN, 64),
|
TUD_RPI_RESET_DESCRIPTOR(ITF_NUM_RPI_RESET, STR_ID_RPI_RESET)
|
||||||
// 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),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 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
|
// more device descriptor this time the qualifier
|
||||||
tusb_desc_device_qualifier_t const desc_device_qualifier = {
|
tusb_desc_device_qualifier_t const desc_device_qualifier = {
|
||||||
.bLength = sizeof(tusb_desc_device_t),
|
.bLength = sizeof(tusb_desc_device_t),
|
||||||
|
|
@ -129,29 +70,22 @@ tusb_desc_device_qualifier_t const desc_device_qualifier = {
|
||||||
.bReserved = 0x00
|
.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
|
// buffer to hold the string descriptor during the request | plus 1 for the null terminator
|
||||||
static uint16_t _desc_str[32 + 1];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// called when host request to get string descriptor
|
// called when host requests to get device descriptor
|
||||||
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid);
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------+
|
|
||||||
// IMPLEMENTATION
|
|
||||||
// --------------------------------------------------------------------+
|
|
||||||
|
|
||||||
uint8_t const *tud_descriptor_device_cb(void)
|
uint8_t const *tud_descriptor_device_cb(void)
|
||||||
{
|
{
|
||||||
return (uint8_t const *)&desc_device;
|
return (uint8_t const *)&desc_device;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// called when host requests to get device qualifier descriptor
|
||||||
uint8_t const* tud_descriptor_device_qualifier_cb(void)
|
uint8_t const* tud_descriptor_device_qualifier_cb(void)
|
||||||
{
|
{
|
||||||
return (uint8_t const *)&desc_device_qualifier;
|
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)
|
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
||||||
{
|
{
|
||||||
// avoid unused parameter warning and keep function signature consistent
|
// 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;
|
return desc_configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// called when host request to get string descriptor
|
||||||
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
||||||
{
|
{
|
||||||
// TODO: check lang id
|
// TODO: check lang id
|
||||||
(void) langid;
|
(void)langid;
|
||||||
|
|
||||||
size_t char_count;
|
size_t char_count;
|
||||||
|
|
||||||
// Determine which string descriptor to return
|
// Determine which string descriptor to return
|
||||||
switch (index) {
|
switch(index)
|
||||||
case STRID_LANGID:
|
{
|
||||||
memcpy(&_desc_str[1], string_desc_arr[STRID_LANGID], 2);
|
case STR_ID_LANGID:
|
||||||
|
memcpy(&_desc_str[1], string_desc_arr[STR_ID_LANGID], 2);
|
||||||
char_count = 1;
|
char_count = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case STRID_SERIAL:
|
case STR_ID_SERIAL:
|
||||||
// try to read the serial from the board
|
// try to read the serial from the board
|
||||||
char_count = board_usb_get_serial(_desc_str + 1, 32);
|
char_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
#ifndef _USB_DESCRIPTOR_H
|
||||||
|
#define _USB_DESCRIPTOR_H
|
||||||
|
|
||||||
|
#include <tusb.h>
|
||||||
|
#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 {
|
||||||
|
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
|
||||||
Loading…
Reference in New Issue