Compare commits

...

2 Commits

Author SHA1 Message Date
Ulysse Cura 8e9813bde3 Removed useless lib. 2026-10-16 17:13:02 +02:00
Ulysse Cura 1ac4e0fc75 Working pico host example. 2026-10-16 17:12:15 +02:00
10 changed files with 151 additions and 446 deletions

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "lib/Pico_USB_Host"]
path = lib/Pico_USB_Host
url = https://gitea.skadubpc.net/ulysse/Pico_USB_Host_Lib.git

View File

@ -19,22 +19,11 @@ add_executable(Pico_Device)
# Sources
target_sources(Pico_Host PUBLIC
src_host/main.c
src_host/cdc_app.c
src_host/tuh_cb.c
)
target_sources(Pico_Device PUBLIC
src_device/main.c
src_device/usb_descriptor.c
)
# 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
)
# Include paths
@ -55,8 +44,6 @@ target_link_libraries(Pico_Host
target_link_libraries(Pico_Device
pico_stdlib
tinyusb_board
tinyusb_device
)
pico_enable_stdio_usb(Pico_Device 1)

@ -1 +0,0 @@
Subproject commit 6e3be14cfbe61df4d9a90333765710f8ef711153

View File

@ -5,73 +5,55 @@
*/
#include <stdlib.h>
#include <tusb.h>
#include <bsp/board.h>
#include <stdio.h>
#include <pico/stdio.h>
#include <pico/stdlib.h>
#define LED_BLINK_DELAY_MS 1000
void recv_cb(void *args);
int main(void)
{
// Initialize TinyUSB stack
board_init();
tusb_init();
// let pico sdk use the first cdc interface for std io
stdio_init_all();
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
stdio_set_chars_available_callback(recv_cb, NULL);
uint32_t start_ms = 0;
bool led_state = false;
// main run loop
while(true)
{
// TinyUSB device task | must be called regurlarly
tud_task();
if(board_millis() - start_ms > LED_BLINK_DELAY_MS)
if(us_to_ms(time_us_64()) - start_ms > LED_BLINK_DELAY_MS)
{
board_led_write(led_state);
led_state = ! led_state;
gpio_put(PICO_DEFAULT_LED_PIN, led_state);
led_state = !led_state;
start_ms += LED_BLINK_DELAY_MS;
if(tud_cdc_n_connected(0))
{
printf("Connected to CDC 0\n");
}
if(tud_cdc_n_connected(1))
{
printf("Connected to CDC 1\n");
}
fflush(stdout);
}
}
}
// callback when data is received on a CDC interface
void tud_cdc_rx_cb(uint8_t itf)
void recv_cb(void *args __attribute__((unused)))
{
// allocate buffer for the data in the stack
uint8_t buf[CFG_TUD_CDC_RX_BUFSIZE];
while(true)
{
int c = stdio_getchar_timeout_us(0);
printf("RX CDC %d\n", itf);
if(c == PICO_ERROR_TIMEOUT)
{
break;
}
// 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);
// 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);
if(putchar(c) == EOF)
{
break;
}
}
}

View File

@ -1,42 +0,0 @@
/**
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _TUSB_CONFIG_H_
#define _TUSB_CONFIG_H_
#ifdef __cplusplus
extern "C" {
#endif
#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
//------------------------
// DEVICE CONFIGURATION //
//------------------------
// 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)
#ifndef CFG_TUD_ENDPOINT0_SIZE
#define CFG_TUD_ENDPOINT0_SIZE (64)
#endif
#ifdef __cplusplus
}
#endif
#endif /* _TUSB_CONFIG_H_ */

View File

@ -1,211 +0,0 @@
/**
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <tusb.h>
#include <bsp/board_api.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
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
};
// defines a descriptor that will be communicated to the host
tusb_desc_device_t const desc_device = {
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = CDC_EXAMPLE_BCD,
.bDeviceClass = TUSB_CLASS_MISC, // CDC is a subclass of misc
.bDeviceSubClass = MISC_SUBCLASS_COMMON, // CDC uses common subclass
.bDeviceProtocol = MISC_PROTOCOL_IAD, // CDC uses IAD
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, // 64 bytes
.idVendor = CDC_EXAMPLE_VID,
.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
.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)
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 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),
};
// 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),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = CDC_EXAMPLE_BCD,
.bDeviceClass = TUSB_CLASS_CDC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.bNumConfigurations = 0x01,
.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
// --------------------------------------------------------------------+
uint8_t const *tud_descriptor_device_cb(void)
{
return (uint8_t const *)&desc_device;
}
uint8_t const* tud_descriptor_device_qualifier_cb(void)
{
return (uint8_t const *)&desc_device_qualifier;
}
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
{
// avoid unused parameter warning and keep function signature consistent
(void)index;
return desc_configuration;
}
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
{
// TODO: check lang id
(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);
char_count = 1;
break;
case STRID_SERIAL:
// try to read the serial from the board
char_count = board_usb_get_serial(_desc_str + 1, 32);
break;
default:
// COPYRIGHT NOTE: Based on TinyUSB example
// Windows wants utf16le
// Determine which string descriptor to return
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) {
return NULL;
}
// Copy string descriptor into _desc_str
const char *str = string_desc_arr[index];
char_count = strlen(str);
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
// Cap at max char
if (char_count > max_count) {
char_count = max_count;
}
// Convert ASCII string into UTF-16
for (size_t i = 0; i < char_count; i++) {
_desc_str[1 + i] = str[i];
}
break;
}
// First byte is the length (including header), second byte is string type
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (char_count * 2 + 2));
return _desc_str;
}

View File

@ -1,113 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022, Ha Thach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/
#include <tusb.h>
#include <bsp/board_api.h>
size_t get_console_inputs(uint8_t* buf, size_t bufsize)
{
size_t count = 0;
while (count < bufsize) {
int ch = board_getchar();
if (ch <= 0) break;
buf[count] = (uint8_t) ch;
count++;
}
return count;
}
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))
{
// console --> cdc interfaces
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;
// forward cdc interfaces -> console
uint32_t count = tuh_cdc_read(idx, buf, bufsize);
buf[count] = 0;
printf("%s", (char*) buf);
}
// 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);
#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
// 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: %" PRIu32 ", 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);
}
#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
}
// 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);
}

View File

@ -26,23 +26,18 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <bsp/board_api.h>
#include <tusb.h>
#include <pico/stdlib.h>
#include <tusb.h>
#include <bsp/board.h>
#define LED_BLINK_DELAY_MS 1000
extern void cdc_app_task(void);
void cdc_app_task(void);
/*------------- MAIN -------------*/
int main(void) {
int main(void)
{
board_init();
gpio_set_function(12, UART_FUNCSEL_NUM(uart0, 12));
gpio_set_function(13, UART_FUNCSEL_NUM(uart0, 13));
printf("TinyUSB Host CDC Example\n");
// init host stack on configured roothub port
const tusb_rhport_init_t rh_init = {
.role = TUSB_ROLE_HOST,
@ -55,6 +50,9 @@ int main(void) {
return PICO_ERROR_GENERIC;
}
gpio_set_function(12, UART_FUNCSEL_NUM(uart0, 12));
gpio_set_function(13, UART_FUNCSEL_NUM(uart0, 13));
uint32_t start_ms = 0;
bool led_state = false;
@ -74,13 +72,35 @@ int main(void) {
}
}
// TinyUSB Callbacks
void tuh_mount_cb(uint8_t dev_addr) {
// application set-up
printf("A device with address %d is mounted\r\n", dev_addr);
}
void cdc_app_task(void)
{
uint8_t buf[64 + 1]; // +1 for extra null character
uint32_t const bufsize = sizeof(buf) - 1;
void tuh_umount_cb(uint8_t dev_addr) {
// application tear-down
printf("A device with address %d is unmounted \r\n", dev_addr);
size_t count = 0;
while(count < bufsize)
{
int ch = board_getchar();
if (ch <= 0) break;
buf[count] = (uint8_t) ch;
count++;
}
if(count)
{
buf[count] = 0;
// loop over all mounted interfaces
for(uint8_t idx = 0; idx < CFG_TUH_CDC; idx++)
{
if(tuh_cdc_mounted(idx))
{
// console --> cdc interfaces
tuh_cdc_write(idx, buf, count);
tuh_cdc_write_flush(idx);
}
}
}
}

87
src_host/tuh_cb.c Normal file
View File

@ -0,0 +1,87 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022, Ha Thach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/
#include <tusb.h>
#include <bsp/board_api.h>
// TinyUSB Callbacks
void tuh_mount_cb(uint8_t dev_addr)
{
// application set-up
printf("A device with address %d is mounted\n", dev_addr);
}
void tuh_umount_cb(uint8_t dev_addr)
{
// application tear-down
printf("A device with address %d is unmounted\n", dev_addr);
}
// 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;
// forward cdc interfaces -> console
uint32_t count = tuh_cdc_read(idx, buf, bufsize);
buf[count] = 0;
printf("%s", (char*) buf);
}
// 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);
#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
// 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: %" PRIu32 ", 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);
}
#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
}
// 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);
}

View File

@ -113,7 +113,6 @@ extern "C" {
// bit rate = 115200, 1 stop bit, no parity, 8 bit data width
#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CODING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
#ifdef __cplusplus
}
#endif