Compare commits

..

1 Commits

11 changed files with 420 additions and 156 deletions

3
.gitmodules vendored Normal file
View File

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

View File

@ -19,11 +19,22 @@ add_executable(Pico_Device)
# Sources
target_sources(Pico_Host PUBLIC
src_host/main.c
src_host/tuh_cb.c
src_host/cdc_app.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=1
)
# Include paths
@ -44,10 +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)

1
lib/Pico_USB_Host Submodule

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

View File

@ -5,22 +5,21 @@
*/
#include <stdlib.h>
#include <stdio.h>
#include <tusb.h>
#include <bsp/board.h>
#include <pico/stdio.h>
#include <pico/stdlib.h>
#define LED_BLINK_DELAY_MS 1000
#include <pico/usb_reset.h>
void recv_cb(void *args);
#define LED_BLINK_DELAY_MS 1000
int main(void)
{
stdio_init_all();
// Initialize TinyUSB stack
board_init();
tusb_init();
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;
@ -28,32 +27,42 @@ int main(void)
// main run loop
while(true)
{
if(us_to_ms(time_us_64()) - start_ms > LED_BLINK_DELAY_MS)
// TinyUSB device task | must be called regurlarly
tud_task();
if(board_millis() - start_ms > LED_BLINK_DELAY_MS)
{
gpio_put(PICO_DEFAULT_LED_PIN, led_state);
led_state = !led_state;
board_led_write(led_state);
led_state = ! led_state;
start_ms += LED_BLINK_DELAY_MS;
printf("Connected to CDC 1\n");
fflush(stdout);
if(tud_cdc_n_connected(0))
{
// 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);
}
}
}
}
void recv_cb(void *args __attribute__((unused)))
// callback when data is received on a CDC interface
void tud_cdc_rx_cb(uint8_t itf)
{
while(true)
{
int c = stdio_getchar_timeout_us(0);
// allocate buffer for the data in the stack
uint8_t buf[CFG_TUD_CDC_RX_BUFSIZE];
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));
if(putchar(c) == EOF)
{
break;
}
}
}
// process the received data
buf[count] = 0; // null-terminate the string
// echo back OK
tud_cdc_n_write_str(itf, buf);
tud_cdc_n_write_char(itf, '\n');
tud_cdc_n_write_flush(itf);
}

43
src_device/tusb_config.h Normal file
View File

@ -0,0 +1,43 @@
/**
* 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 // BOARD_TUD_RHPORT
//----------------------//
// DEVICE CONFIGURATION //
//----------------------//
// Enable CDC classes
#define CFG_TUD_CDC 1
// 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_ */

148
src_device/usb_descriptor.c Normal file
View File

@ -0,0 +1,148 @@
/**
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "usb_descriptor.h"
#include <tusb.h>
#include <bsp/board_api.h>
#include <assert.h>
// array of pointer to string descriptors
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
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 = 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
};
// 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 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),
// RPI Reset Interface
TUD_RPI_RESET_DESCRIPTOR(ITF_NUM_RPI_RESET, STR_ID_RPI_RESET)
};
// 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
};
// 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 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
(void)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;
size_t char_count;
// Determine which string descriptor to return
switch(index)
{
case STR_ID_LANGID:
memcpy(&_desc_str[1], string_desc_arr[STR_ID_LANGID], 2);
char_count = 1;
break;
case STR_ID_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

@ -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

113
src_host/cdc_app.c Normal file
View File

@ -0,0 +1,113 @@
/*
* 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,18 +26,23 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pico/stdlib.h>
#include <bsp/board_api.h>
#include <tusb.h>
#include <bsp/board.h>
#include <pico/stdlib.h>
#define LED_BLINK_DELAY_MS 1000
void cdc_app_task(void);
extern void cdc_app_task(void);
int main(void)
{
/*------------- MAIN -------------*/
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,
@ -50,9 +55,6 @@ 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;
@ -72,35 +74,13 @@ int main(void)
}
}
void cdc_app_task(void)
{
uint8_t buf[64 + 1]; // +1 for extra null character
uint32_t const bufsize = sizeof(buf) - 1;
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);
}
}
}
// 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 tuh_umount_cb(uint8_t dev_addr) {
// application tear-down
printf("A device with address %d is unmounted \r\n", dev_addr);
}

View File

@ -1,87 +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>
// 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,6 +113,7 @@ 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