合并TinyUSB仓库

This commit is contained in:
2025-10-10 15:24:53 +08:00
parent 12f3c23968
commit 99a4282aca
371 changed files with 151941 additions and 0 deletions

View File

@@ -0,0 +1,171 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 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.
*/
#ifdef __RTTHREAD__
#include <tusb.h>
#include <stdint.h>
#include <board.h>
#include <rtdevice.h>
static bool ejected = false;
static rt_device_t flash_device;
static struct rt_device_blk_geometry blk_geom;
#ifdef __CC_ARM
uint16_t __builtin_bswap16(uint16_t x)
{
return (x << 8) | (x >> 8);
}
#endif
void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
{
(void) lun;
const char vid[] = PKG_TINYUSB_DEVICE_MSC_VID;
const char pid[] = PKG_TINYUSB_DEVICE_MSC_PID;
const char rev[] = PKG_TINYUSB_DEVICE_MSC_REV;
memcpy(vendor_id, vid, strlen(vid));
memcpy(product_id, pid, strlen(pid));
memcpy(product_rev, rev, strlen(rev));
}
bool tud_msc_test_unit_ready_cb(uint8_t lun)
{
(void) lun;
if (ejected)
{
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
return false;
}
if (flash_device == NULL)
{
flash_device = rt_device_find(PKG_TINYUSB_DEVICE_MSC_NAME);
}
if (flash_device != NULL)
{
static uint8_t open_flg = 0;
if (!open_flg)
{
open_flg = 1;
rt_device_open(flash_device, 0);
}
rt_device_control(flash_device, RT_DEVICE_CTRL_BLK_GETGEOME, &blk_geom);
return true;
}
return false;
}
void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size)
{
(void) lun;
*block_count = blk_geom.sector_count;
*block_size = blk_geom.bytes_per_sector;
}
bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
{
(void) lun;
(void) power_condition;
if (load_eject)
{
if (start)
{
ejected = false;
} else {
// unload disk storage
ejected = true;
}
}
return true;
}
int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize)
{
(void) lun;
(void) offset;
(void) bufsize;
return (int32_t) rt_device_read(flash_device, (rt_off_t) lba, buffer, 1) * blk_geom.bytes_per_sector;
}
int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize)
{
(void) lun;
(void) offset;
(void) bufsize;
return (int32_t) rt_device_write(flash_device, (rt_off_t) lba, buffer, 1) * blk_geom.bytes_per_sector;
}
int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize)
{
void const *response = NULL;
uint16_t resplen = 0;
// most scsi handled is input
bool in_xfer = true;
switch (scsi_cmd[0])
{
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
// Host is about to read/write etc ... better not to disconnect disk
resplen = 0;
break;
default:
// Set Sense = Invalid Command Operation
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
// negative means error -> tinyusb could stall and/or response with failed status
resplen = -1;
break;
}
// return resplen must not larger than bufsize
if (resplen > bufsize) resplen = bufsize;
if (response && (resplen > 0))
{
if (in_xfer)
{
memcpy(buffer, response, resplen);
} else {
// SCSI output
}
}
return resplen;
}
#endif /*__RTTHREAD__*/

View File

@@ -0,0 +1,223 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 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.
*/
#ifndef _TUSB_CONFIG_H_
#define _TUSB_CONFIG_H_
#ifdef __RTTHREAD__
#include <rtconfig.h>
#ifdef __cplusplus
extern "C" {
#endif
//--------------------------------------------------------------------
// COMMON CONFIGURATION
//--------------------------------------------------------------------
#if defined(SOC_SERIES_STM32F0)
#define CFG_TUSB_MCU OPT_MCU_STM32F0
#elif defined(SOC_SERIES_STM32F1)
#define CFG_TUSB_MCU OPT_MCU_STM32F1
#elif defined(SOC_SERIES_STM32F2)
#define CFG_TUSB_MCU OPT_MCU_STM32F2
#elif defined(SOC_SERIES_STM32F3)
#define CFG_TUSB_MCU OPT_MCU_STM32F3
#elif defined(SOC_SERIES_STM32F4)
#define CFG_TUSB_MCU OPT_MCU_STM32F4
#elif defined(SOC_SERIES_STM32F7)
#define CFG_TUSB_MCU OPT_MCU_STM32F7
#elif defined(SOC_SERIES_STM32H7)
#define CFG_TUSB_MCU OPT_MCU_STM32H7
#elif defined(SOC_SERIES_STM32L0)
#define CFG_TUSB_MCU OPT_MCU_STM32L0
#elif defined(SOC_SERIES_STM32L1)
#define CFG_TUSB_MCU OPT_MCU_STM32L1
#elif defined(SOC_SERIES_STM32L4)
#define CFG_TUSB_MCU OPT_MCU_STM32L4
#elif defined(SOC_NRF52840)
#define CFG_TUSB_MCU OPT_MCU_NRF5X
#elif defined(SOC_HPM6000)
#define CFG_TUSB_MCU OPT_MCU_HPM
#elif defined(SOC_RP2040)
#define CFG_TUSB_MCU OPT_MCU_RP2040
#elif defined(SOC_FAMILY_RENESAS)
#define CFG_TUSB_MCU OPT_MCU_RAXXX
#else
#error "Not support for current MCU"
#endif
#define CFG_TUSB_OS OPT_OS_RTTHREAD
//--------------------------------------------------------------------
// DEBUG CONFIGURATION
//--------------------------------------------------------------------
#define CFG_TUSB_DEBUG 3
#ifdef CFG_TUSB_DEBUG
#define CFG_TUSB_DEBUG_PRINTF rt_kprintf
#endif /* CFG_TUSB_DEBUG */
// fs is 0; hs is 1
#ifndef BOARD_DEVICE_RHPORT_NUM
#define BOARD_DEVICE_RHPORT_NUM 1
#endif
#ifndef BOARD_DEVICE_RHPORT_SPEED
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED
#endif
#if BOARD_DEVICE_RHPORT_NUM == 0
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
#elif BOARD_DEVICE_RHPORT_NUM == 1
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
#else
#error "Incorrect RHPort configuration"
#endif
/* 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
* into those specific section.
* e.g
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
*/
#ifndef CFG_TUSB_MEM_SECTION
#define CFG_TUSB_MEM_SECTION
#endif
#ifndef CFG_TUSB_MEM_ALIGN
#define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4)))
#endif
//--------------------------------------------------------------------
// DEVICE CONFIGURATION
//--------------------------------------------------------------------
#if defined(PKG_TINYUSB_DEVICE_ENABLE)
#define CFG_TUD_ENABLED (1)
#else
#define CFG_TUD_ENABLED (0)
#endif
#ifndef CFG_TUD_ENDPOINT0_SIZE
#define CFG_TUD_ENDPOINT0_SIZE 8
#endif
#ifndef CFG_TUD_CDC
#define CFG_TUD_CDC 1
#endif
// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE 128
#define CFG_TUD_CDC_TX_BUFSIZE 128
#define CFG_TUD_MSC_EP_BUFSIZE 32
#define CFG_TUD_HID_EP_BUFSIZE 32
#ifndef PKG_TINYUSB_DEVICE_CDC_STRING
#define PKG_TINYUSB_DEVICE_CDC_STRING ""
#endif
#ifndef PKG_TINYUSB_DEVICE_MSC_STRING
#define PKG_TINYUSB_DEVICE_MSC_STRING ""
#endif
#ifndef PKG_TINYUSB_DEVICE_HID_STRING
#define PKG_TINYUSB_DEVICE_HID_STRING ""
#endif
//--------------------------------------------------------------------
// HOST CONFIGURATION
//--------------------------------------------------------------------
#if defined(PKG_TINYUSB_HOST_ENABLE)
#define CFG_TUH_ENABLED (1)
#else
#define CFG_TUH_ENABLED (0)
#endif
#if (PKG_TINYUSB_HOST_PORT == 0) && defined(PKG_TINYUSB_HOST_ENABLE)
#undef CFG_TUSB_RHPORT0_MODE
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | PKG_TINYUSB_HOST_PORT_SPEED)
#endif
#if (PKG_TINYUSB_HOST_PORT == 1) && defined(PKG_TINYUSB_HOST_ENABLE)
#undef CFG_TUSB_RHPORT1_MODE
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_HOST | PKG_TINYUSB_HOST_PORT_SPEED)
#endif
#define BOARD_TUH_RHPORT PKG_TINYUSB_HOST_PORT// FULL SPEED
#define BOARD_TUH_MAX_SPEED PKG_TINYUSB_HOST_PORT_SPEED
// 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
// Size of buffer to hold descriptors and other data used for enumeration
#define CFG_TUH_ENUMERATION_BUFSIZE 256
#define CFG_TUH_HUB 2 // number of supported hubs
#define CFG_TUH_CDC 0 // CDC ACM
#define CFG_TUH_CDC_FTDI 0 // FTDI Serial. FTDI is not part of CDC class, only to re-use CDC driver API
#define CFG_TUH_CDC_CP210X 0// CP210x Serial. CP210X is not part of CDC class, only to re-use CDC driver API
#define CFG_TUH_CDC_CH34X 0 // CH340 or CH341 Serial. CH34X is not part of CDC class, only to re-use CDC driver API
#define CFG_TUH_HID 0 // typical keyboard + mouse device can have 3-4 HID interfaces
#define CFG_TUH_MSC 0
//#define CFG_TUH_VENDOR 3
// max device support (excluding hub device): 1 hub typically has 4 ports
#define CFG_TUH_DEVICE_MAX (3 * CFG_TUH_HUB + 1)
//------------- HID -------------//
#define CFG_TUH_HID_EPIN_BUFSIZE 64
#define CFG_TUH_HID_EPOUT_BUFSIZE 64
//------------- CDC -------------//
// Set Line Control state on enumeration/mounted:
// DTR ( bit 0), RTS (bit 1)
#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0x03
// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t
// 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
#endif /*__RTTHREAD__*/
#endif /* _TUSB_CONFIG_H_ */

View File

@@ -0,0 +1,201 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 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.
*/
#ifdef __RTTHREAD__
#include <rtthread.h>
#include "bsp_init.h"
#include "stm32f4xx.h"
#include "dcd.h"
#include <ctype.h>
#define DBG_TAG "TinyUSB"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#include <tusb.h>
#ifndef RT_USING_HEAP
/* if there is not enable heap, we should use static thread and stack. */
static rt_uint8_t tusb_stack[PKG_TINYUSB_STACK_SIZE];
static struct rt_thread tusb_thread;
#endif /* RT_USING_HEAP */
struct rt_semaphore g_dcd_sem;
int tusb_board_init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_OTG_HS, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_OTG2_FS);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_OTG2_FS);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// no need enable irq, it will be enabled in dwc2_dcd_int_enable()
return 0;
}
void usb_irq_enable(){
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = OTG_HS_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void tusb_thread_entry(void *parameter)
{
(void) parameter;
while (1)
{
#if CFG_TUH_ENABLED
tuh_task();
#endif
#if CFG_TUD_ENABLED
tud_task();
#endif
}
}
static void cdc_task(void *parameter);
static int init_tinyusb(void)
{
rt_thread_t tid;
RCC_ClocksTypeDef rcc = {0};
RCC_GetClocksFreq(&rcc);
rt_kprintf("TinyUSB init... \r\n");
rt_kprintf("rcc.SYSCLK_Frequency=%d Hz\r\n", rcc.SYSCLK_Frequency);
rt_kprintf("rcc.HCLK_Frequency=%d Hz\r\n", rcc.HCLK_Frequency);
rt_kprintf("rcc.PCLK1_Frequency=%d Hz\r\n", rcc.PCLK1_Frequency);
rt_kprintf("rcc.PCLK2_Frequency=%d Hz\r\n", rcc.PCLK2_Frequency);
rt_kprintf("rcc.USBCLK_Frequency=%d Hz\r\n", rcc.USBCLK_Frequency);
tusb_board_init();
rt_sem_init(&g_dcd_sem, "tusb_cdc", 0, RT_IPC_FLAG_PRIO);
//call tusb_rhport_init()
tusb_init();
// dcd_int_enable(TUD_OPT_RHPORT);
usb_irq_enable();
#ifdef RT_USING_HEAP
tid = rt_thread_create("tusb", tusb_thread_entry, RT_NULL,
2048,
1, 10);
if (tid == RT_NULL)
#else
rt_err_t result;
tid = &tusb_thread;
result = rt_thread_init(tid, "tusb", tusb_thread_entry, RT_NULL,
tusb_stack, sizeof(tusb_stack), 4, 10);
if (result != RT_EOK)
#endif /* RT_USING_HEAP */
{
LOG_E("Fail to create TinyUSB thread");
return -1;
}
rt_thread_startup(tid);
tid = rt_thread_create("tusb_cdc", cdc_task, RT_NULL,
2048,
6, 10);
rt_thread_startup(tid);
return 0;
}
// INIT_APP_EXPORT(init_tinyusb);
extern_init(tinyusb, init_tinyusb);
static void echo_serial_port(uint8_t itf, uint8_t buf[], uint32_t count) {
uint8_t const case_diff = 'a' - 'A';
for (uint32_t i = 0; i < count; i++) {
if (itf == 0) {
// echo back 1st port as lower case
if (isupper(buf[i])) buf[i] += case_diff;
} else {
// echo back 2nd port as upper case
if (islower(buf[i])) buf[i] -= case_diff;
}
tud_cdc_n_write_char(itf, buf[i]);
}
tud_cdc_n_write_flush(itf);
}
// notify cdc task recv data
void tud_cdc_rx_cb(uint8_t itf){
TU_LOG(2, "tud_cdc_rx_cb\r\n");
rt_sem_release(&g_dcd_sem);
}
static void cdc_task(void *parameter) {
TU_LOG(2, "CDC task started\r\n");
while(1){
rt_sem_take(&g_dcd_sem, RT_WAITING_FOREVER);
TU_LOG(2, "tud_cdc_rx_take\r\n");
for (uint8_t itf = 0; itf < CFG_TUD_CDC; itf++) {
// connected() check for DTR bit
// Most but not all terminal client set this when making connection
// if ( tud_cdc_n_connected(itf) )
{
if (tud_cdc_n_available(itf)) {
uint8_t buf[64];
uint32_t count = tud_cdc_n_read(itf, buf, sizeof(buf));
// echo back to both serial ports
echo_serial_port(0, buf, count);
echo_serial_port(1, buf, count);
}
}
}
}
}
#endif /*__RTTHREAD__*/