Mise à jour de TinyUSB

This commit is contained in:
Samuel 2025-06-07 12:12:22 +02:00
parent 6d7344f25b
commit d54b1167f8
5 changed files with 120 additions and 104 deletions

View File

@ -25,20 +25,11 @@
*/ */
#include "tusb.h" #include "tusb.h"
#include "bsp/board.h" #include "bsp/board_api.h"
//--------------------------------------------------------------------+ size_t get_console_inputs(uint8_t* buf, size_t bufsize) {
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+
//------------- IMPLEMENTATION -------------//
size_t get_console_inputs(uint8_t* buf, size_t bufsize)
{
size_t count = 0; size_t count = 0;
while (count < bufsize) while (count < bufsize) {
{
int ch = board_getchar(); int ch = board_getchar();
if (ch <= 0) break; if (ch <= 0) break;
@ -49,8 +40,7 @@ size_t get_console_inputs(uint8_t* buf, size_t bufsize)
return count; return count;
} }
void cdc_app_task(void) void cdc_app_task(void) {
{
uint8_t buf[64 + 1]; // +1 for extra null character uint8_t buf[64 + 1]; // +1 for extra null character
uint32_t const bufsize = sizeof(buf) - 1; uint32_t const bufsize = sizeof(buf) - 1;
@ -58,13 +48,10 @@ void cdc_app_task(void)
buf[count] = 0; buf[count] = 0;
// loop over all mounted interfaces // loop over all mounted interfaces
for(uint8_t idx=0; idx<CFG_TUH_CDC; idx++) for (uint8_t idx = 0; idx < CFG_TUH_CDC; idx++) {
{ if (tuh_cdc_mounted(idx)) {
if ( tuh_cdc_mounted(idx) )
{
// console --> cdc interfaces // console --> cdc interfaces
if (count) if (count) {
{
tuh_cdc_write(idx, buf, count); tuh_cdc_write(idx, buf, count);
tuh_cdc_write_flush(idx); tuh_cdc_write_flush(idx);
} }
@ -72,9 +59,12 @@ void cdc_app_task(void)
} }
} }
//--------------------------------------------------------------------+
// TinyUSB callbacks
//--------------------------------------------------------------------+
// Invoked when received new data // Invoked when received new data
void tuh_cdc_rx_cb(uint8_t idx) void tuh_cdc_rx_cb(uint8_t idx) {
{
uint8_t buf[64 + 1]; // +1 for extra null character uint8_t buf[64 + 1]; // +1 for extra null character
uint32_t const bufsize = sizeof(buf) - 1; uint32_t const bufsize = sizeof(buf) - 1;
@ -82,32 +72,38 @@ void tuh_cdc_rx_cb(uint8_t idx)
uint32_t count = tuh_cdc_read(idx, buf, bufsize); uint32_t count = tuh_cdc_read(idx, buf, bufsize);
buf[count] = 0; buf[count] = 0;
printf((char*) buf); printf("%s", (char*) buf);
} }
void tuh_cdc_mount_cb(uint8_t idx) // Invoked when a device with CDC interface is mounted
{ // idx is index of cdc interface in the internal pool.
tuh_cdc_itf_info_t itf_info = { 0 }; void tuh_cdc_mount_cb(uint8_t idx) {
tuh_itf_info_t itf_info = {0};
tuh_cdc_itf_get_info(idx, &itf_info); 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.bInterfaceNumber); 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 #ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
// CFG_TUH_CDC_LINE_CODING_ON_ENUM must be defined for line coding is set by tinyusb in enumeration // If CFG_TUH_CDC_LINE_CODING_ON_ENUM is defined, line coding will be set by tinyusb stack
// otherwise you need to call tuh_cdc_set_line_coding() first // while eneumerating new cdc device
cdc_line_coding_t line_coding = {0}; cdc_line_coding_t line_coding = {0};
if ( tuh_cdc_get_local_line_coding(idx, &line_coding) ) 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(" Baudrate: %lu, 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); 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 #endif
} }
void tuh_cdc_umount_cb(uint8_t idx) // Invoked when a device with CDC interface is unmounted
{ void tuh_cdc_umount_cb(uint8_t idx) {
tuh_cdc_itf_info_t itf_info = { 0 }; tuh_itf_info_t itf_info = {0};
tuh_cdc_itf_get_info(idx, &itf_info); 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.bInterfaceNumber); printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr,
itf_info.desc.bInterfaceNumber);
} }

View File

@ -23,7 +23,7 @@
* *
*/ */
#include "bsp/board.h" #include "bsp/board_api.h"
#include "tusb.h" #include "tusb.h"
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -160,7 +160,9 @@ static void process_kbd_report(hid_keyboard_report_t const *report)
putchar(ch); putchar(ch);
if ( ch == '\r' ) putchar('\n'); // added new line for enter key if ( ch == '\r' ) putchar('\n'); // added new line for enter key
#ifndef __ICCARM__ // TODO IAR doesn't support stream control ?
fflush(stdout); // flush right away, else nanolib will wait for newline fflush(stdout); // flush right away, else nanolib will wait for newline
#endif
} }
} }
// TODO example skips key released // TODO example skips key released
@ -263,7 +265,7 @@ static void process_generic_report(uint8_t dev_addr, uint8_t instance, uint8_t c
if (!rpt_info) if (!rpt_info)
{ {
printf("Couldn't find the report info for this report !\r\n"); printf("Couldn't find report info !\r\n");
return; return;
} }

36
main.c
View File

@ -27,20 +27,24 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "bsp/board.h" #include "bsp/board_api.h"
#include "tusb.h" #include "tusb.h"
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTYPES // MACRO CONSTANT TYPEDEF PROTYPES
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void led_blinking_task(void); void led_blinking_task(void);
extern void cdc_app_task(void); extern void cdc_app_task(void);
extern void hid_app_task(void); extern void hid_app_task(void);
#if CFG_TUH_ENABLED && CFG_TUH_MAX3421
// API to read/rite MAX3421's register. Implemented by TinyUSB
extern uint8_t tuh_max3421_reg_read(uint8_t rhport, uint8_t reg, bool in_isr);
extern bool tuh_max3421_reg_write(uint8_t rhport, uint8_t reg, uint8_t data, bool in_isr);
#endif
/*------------- MAIN -------------*/ /*------------- MAIN -------------*/
int main(void) int main(void) {
{
board_init(); board_init();
printf("TinyUSB Host CDC MSC HID Example\r\n"); printf("TinyUSB Host CDC MSC HID Example\r\n");
@ -48,8 +52,17 @@ int main(void)
// init host stack on configured roothub port // init host stack on configured roothub port
tuh_init(BOARD_TUH_RHPORT); tuh_init(BOARD_TUH_RHPORT);
while (1) if (board_init_after_tusb) {
{ board_init_after_tusb();
}
#if CFG_TUH_ENABLED && CFG_TUH_MAX3421
// FeatherWing MAX3421E use MAX3421E's GPIO0 for VBUS enable
enum { IOPINS1_ADDR = 20u << 3, /* 0xA0 */ };
tuh_max3421_reg_write(BOARD_TUH_RHPORT, IOPINS1_ADDR, 0x01, false);
#endif
while (1) {
// tinyusb host task // tinyusb host task
tuh_task(); tuh_task();
@ -57,22 +70,18 @@ int main(void)
cdc_app_task(); cdc_app_task();
hid_app_task(); hid_app_task();
} }
return 0;
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// TinyUSB Callbacks // TinyUSB Callbacks
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void tuh_mount_cb(uint8_t dev_addr) void tuh_mount_cb(uint8_t dev_addr) {
{
// application set-up // application set-up
printf("A device with address %d is mounted\r\n", dev_addr); printf("A device with address %d is mounted\r\n", dev_addr);
} }
void tuh_umount_cb(uint8_t dev_addr) void tuh_umount_cb(uint8_t dev_addr) {
{
// application tear-down // application tear-down
printf("A device with address %d is unmounted \r\n", dev_addr); printf("A device with address %d is unmounted \r\n", dev_addr);
} }
@ -81,8 +90,7 @@ void tuh_umount_cb(uint8_t dev_addr)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Blinking Task // Blinking Task
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void led_blinking_task(void) void led_blinking_task(void) {
{
const uint32_t interval_ms = 1000; const uint32_t interval_ms = 1000;
static uint32_t start_ms = 0; static uint32_t start_ms = 0;

View File

@ -25,6 +25,8 @@
#include "tusb.h" #include "tusb.h"
#include <inttypes.h>
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION // MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -48,8 +50,8 @@ bool inquiry_complete_cb(uint8_t dev_addr, tuh_msc_complete_data_t const * cb_da
uint32_t const block_count = tuh_msc_get_block_count(dev_addr, cbw->lun); uint32_t const block_count = tuh_msc_get_block_count(dev_addr, cbw->lun);
uint32_t const block_size = tuh_msc_get_block_size(dev_addr, cbw->lun); uint32_t const block_size = tuh_msc_get_block_size(dev_addr, cbw->lun);
printf("Disk Size: %lu MB\r\n", block_count / ((1024*1024)/block_size)); printf("Disk Size: %" PRIu32 " MB\r\n", block_count / ((1024*1024)/block_size));
printf("Block Count = %lu, Block Size: %lu\r\n", block_count, block_size); printf("Block Count = %" PRIu32 ", Block Size: %" PRIu32 "\r\n", block_count, block_size);
return true; return true;
} }
@ -68,4 +70,3 @@ void tuh_msc_umount_cb(uint8_t dev_addr)
(void) dev_addr; (void) dev_addr;
printf("A MassStorage device is unmounted\r\n"); printf("A MassStorage device is unmounted\r\n");
} }

View File

@ -30,28 +30,8 @@
extern "C" { extern "C" {
#endif #endif
//--------------------------------------------------------------------+
// Board Specific Configuration
//--------------------------------------------------------------------+
#if CFG_TUSB_MCU == OPT_MCU_RP2040
// change to 1 if using pico-pio-usb as host controller for raspberry rp2040
#define CFG_TUH_RPI_PIO_USB 0
#define BOARD_TUH_RHPORT CFG_TUH_RPI_PIO_USB
#endif
// RHPort number used for host can be defined by board.mk, default to port 0
#ifndef BOARD_TUH_RHPORT
#define BOARD_TUH_RHPORT 0
#endif
// RHPort max operational speed can defined by board.mk
#ifndef BOARD_TUH_MAX_SPEED
#define BOARD_TUH_MAX_SPEED OPT_MODE_DEFAULT_SPEED
#endif
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// COMMON CONFIGURATION // Common Configuration
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// defined by compiler flags for flexibility // defined by compiler flags for flexibility
@ -64,15 +44,9 @@
#endif #endif
#ifndef CFG_TUSB_DEBUG #ifndef CFG_TUSB_DEBUG
#define CFG_TUSB_DEBUG 1 #define CFG_TUSB_DEBUG 0
#endif #endif
// Enable Host stack
#define CFG_TUH_ENABLED 1
// Default is max speed that hardware controller could support with on-chip PHY
#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. /* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
* Tinyusb use follows macros to declare transferring memory so that they can be put * Tinyusb use follows macros to declare transferring memory so that they can be put
* into those specific section. * into those specific section.
@ -80,29 +54,64 @@
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
*/ */
#ifndef CFG_TUSB_MEM_SECTION #ifndef CFG_TUH_MEM_SECTION
#define CFG_TUSB_MEM_SECTION #define CFG_TUH_MEM_SECTION
#endif #endif
#ifndef CFG_TUSB_MEM_ALIGN #ifndef CFG_TUH_MEM_ALIGN
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) #define CFG_TUH_MEM_ALIGN __attribute__ ((aligned(4)))
#endif #endif
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// CONFIGURATION // Host Configuration
//--------------------------------------------------------------------
// Enable Host stack
#define CFG_TUH_ENABLED 1
#if CFG_TUSB_MCU == OPT_MCU_RP2040
// #define CFG_TUH_RPI_PIO_USB 1 // use pio-usb as host controller
// #define CFG_TUH_MAX3421 1 // use max3421 as host controller
// host roothub port is 1 if using either pio-usb or max3421
#if (defined(CFG_TUH_RPI_PIO_USB) && CFG_TUH_RPI_PIO_USB) || (defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421)
#define BOARD_TUH_RHPORT 1
#endif
#endif
// Default is max speed that hardware controller could support with on-chip PHY
#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED
//------------------------- Board Specific --------------------------
// RHPort number used for host can be defined by board.mk, default to port 0
#ifndef BOARD_TUH_RHPORT
#define BOARD_TUH_RHPORT 0
#endif
// RHPort max operational speed can defined by board.mk
#ifndef BOARD_TUH_MAX_SPEED
#define BOARD_TUH_MAX_SPEED OPT_MODE_DEFAULT_SPEED
#endif
//--------------------------------------------------------------------
// Driver Configuration
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// Size of buffer to hold descriptors and other data used for enumeration // Size of buffer to hold descriptors and other data used for enumeration
#define CFG_TUH_ENUMERATION_BUFSIZE 256 #define CFG_TUH_ENUMERATION_BUFSIZE 256
#define CFG_TUH_HUB 1 // number of supported hubs #define CFG_TUH_HUB 1 // number of supported hubs
#define CFG_TUH_CDC 1 #define CFG_TUH_CDC 1 // CDC ACM
#define CFG_TUH_HID 4 // typical keyboard + mouse device can have 3-4 HID interfaces #define CFG_TUH_CDC_FTDI 1 // FTDI Serial. FTDI is not part of CDC class, only to re-use CDC driver API
#define CFG_TUH_CDC_CP210X 1 // CP210x Serial. CP210X is not part of CDC class, only to re-use CDC driver API
#define CFG_TUH_CDC_CH34X 1 // CH340 or CH341 Serial. CH34X is not part of CDC class, only to re-use CDC driver API
#define CFG_TUH_HID (3*CFG_TUH_DEVICE_MAX) // typical keyboard + mouse device can have 3-4 HID interfaces
#define CFG_TUH_MSC 1 #define CFG_TUH_MSC 1
#define CFG_TUH_VENDOR 0 #define CFG_TUH_VENDOR 0
// max device support (excluding hub device) // max device support (excluding hub device): 1 hub typically has 4 ports
#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports #define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
//------------- HID -------------// //------------- HID -------------//
#define CFG_TUH_HID_EPIN_BUFSIZE 64 #define CFG_TUH_HID_EPIN_BUFSIZE 64
@ -116,7 +125,7 @@
// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t // Set Line Coding on enumeration/mounted, value for cdc_line_coding_t
// bit rate = 115200, 1 stop bit, no parity, 8 bit data width // bit rate = 115200, 1 stop bit, no parity, 8 bit data width
#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 } #define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CODING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
#ifdef __cplusplus #ifdef __cplusplus