Merge pull request #2834 from hathach/add-tusb_int_handler-update-tinyusb_init

add new tusb_int_handler(rhport, in_isr) and update tusb_init(rhport, role)
This commit is contained in:
Ha Thach 2024-10-10 17:00:40 +07:00 committed by GitHub
commit a4fb8354e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
59 changed files with 192 additions and 286 deletions

View File

@ -12,22 +12,19 @@ It is relatively simple to incorporate tinyusb to your project
* Add *your_project/tinyusb/src* to your include path. Also make sure your current include path also contains the configuration file tusb_config.h.
* Make sure all required macros are all defined properly in tusb_config.h (configure file in demo application is sufficient, but you need to add a few more such as CFG_TUSB_MCU, CFG_TUSB_OS since they are passed by IDE/compiler to maintain a unique configure for all boards).
* If you use the device stack, make sure you have created/modified usb descriptors for your own need. Ultimately you need to implement all **tud descriptor** callbacks for the stack to work.
* Add tusb_init() call to your reset initialization code.
* Call ``tud_int_handler()`` (device) and/or ``tuh_int_handler()`` (host) in your USB IRQ Handler
* Add tusb_init(rhport, role) call to your reset initialization code.
* Call ``tusb_int_handler(rhport, in_isr)`` in your USB IRQ Handler
* Implement all enabled classes's callbacks.
* If you don't use any RTOSes at all, you need to continuously and/or periodically call tud_task()/tuh_task() function. All of the callbacks and functionality are handled and invoked within the call of that task runner.
.. code-block::
int main(void)
{
int main(void) {
your_init_code();
tusb_init(); // initialize tinyusb stack
tusb_init(0, TUSB_ROLE_DEVICE); // initialize device stack on roothub port 0
while(1) // the mainloop
{
while(1) { // the mainloop
your_application_code();
tud_task(); // device task
tuh_task(); // host task
}

View File

@ -86,7 +86,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -209,7 +209,7 @@ void usb_device_task(void* param)
// init device stack on configured roothub port
// This should be called after scheduler/kernel is started.
// Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -79,7 +79,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -156,7 +156,7 @@ void usb_device_task(void* param)
// init device stack on configured roothub port
// This should be called after scheduler/kernel is started.
// Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -97,7 +97,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -30,11 +30,6 @@
extern "C" {
#endif
// board_test example is special example that doesn't enable device or host stack
// This can cause some TinyUSB API missing, this define hack to allow us to fill those API
// to pass the compilation process
#define tud_int_handler(x)
//--------------------------------------------------------------------
// COMMON CONFIGURATION
//--------------------------------------------------------------------

View File

@ -52,7 +52,7 @@ int main(void) {
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -51,7 +51,7 @@ int main(void) {
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -132,7 +132,7 @@ static void usb_device_task(void *param) {
// init device stack on configured roothub port
// This should be called after scheduler/kernel is started.
// Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -46,7 +46,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
#if (CFG_TUSB_MCU == OPT_MCU_RP2040)
stdio_init_all();

View File

@ -75,7 +75,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -70,7 +70,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -57,7 +57,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -57,7 +57,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -58,7 +58,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -136,7 +136,7 @@ void usb_device_task(void* param)
// init device stack on configured roothub port
// This should be called after scheduler/kernel is started.
// Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -81,7 +81,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -62,7 +62,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -63,7 +63,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -54,7 +54,7 @@ int main(void) {
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -216,7 +216,7 @@ int main(void) {
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -100,7 +100,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -106,7 +106,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -55,7 +55,7 @@ int main(void)
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -68,7 +68,7 @@ int main(void) {
freertos_init_task();
#else
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();
@ -319,7 +319,7 @@ void usb_device_task(void *param) {
// init device stack on configured roothub port
// This should be called after scheduler/kernel is started.
// Otherwise, it could cause kernel issue since USB IRQ handler does use RTOS queue API.
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -68,7 +68,7 @@ int main(void) {
freertos_init_task();
#else
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();
@ -327,7 +327,7 @@ void usb_device_task(void *param) {
// init device stack on configured roothub port
// This should be called after scheduler/kernel is started.
// Otherwise, it could cause kernel issue since USB IRQ handler does use RTOS queue API.
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -91,7 +91,7 @@ int main(void) {
board_init();
// init device stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -79,8 +79,8 @@ int main(void) {
printf("TinyUSB Host HID <-> Device CDC Example\r\n");
// init device and host stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tuh_init(BOARD_TUH_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -82,8 +82,8 @@ int main(void) {
printf("TinyUSB Host Information -> Device CDC Example\r\n");
// init device and host stack on configured roothub port
tud_init(BOARD_TUD_RHPORT);
tuh_init(BOARD_TUH_RHPORT);
tusb_init(BOARD_TUD_RHPORT, TUSB_ROLE_DEVICE);
tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -62,7 +62,7 @@ int main(void)
printf("TinyUSB Bare API Example\r\n");
// init host stack on configured roothub port
tuh_init(BOARD_TUH_RHPORT);
tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -50,7 +50,7 @@ int main(void) {
printf("TinyUSB Host CDC MSC HID Example\r\n");
// init host stack on configured roothub port
tuh_init(BOARD_TUH_RHPORT);
tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -126,7 +126,7 @@ static void usb_host_task(void *param) {
(void) param;
// init host stack on configured roothub port
if (!tuh_init(BOARD_TUH_RHPORT)) {
if (!tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST)) {
printf("Failed to init USB Host Stack\r\n");
vTaskSuspend(NULL);
}

View File

@ -19,13 +19,13 @@ add_executable(${PROJECT})
# Example source
target_sources(${PROJECT} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
)
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
)
# Example include
target_include_directories(${PROJECT} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src
)
${CMAKE_CURRENT_SOURCE_DIR}/src
)
# Configure compilation flags and libraries for the example without RTOS.
# See the corresponding function in hw/bsp/FAMILY/family.cmake for details.

View File

@ -4,11 +4,11 @@ mcu:LPC177X_8X
mcu:LPC18XX
mcu:LPC40XX
mcu:LPC43XX
mcu:MAX3421
mcu:MIMXRT1XXX
mcu:MIMXRT10XX
mcu:MIMXRT11XX
mcu:RP2040
mcu:MSP432E4
mcu:RP2040
mcu:RX65X
mcu:RAXXX
mcu:MAX3421

View File

@ -66,7 +66,7 @@ int main(void) {
printf("TinyUSB Device Info Example\r\n");
// init host stack on configured roothub port
tuh_init(BOARD_TUH_RHPORT);
tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -52,7 +52,7 @@ int main(void)
printf("Note: Events only displayed for explicit supported controllers\r\n");
// init host stack on configured roothub port
tuh_init(BOARD_TUH_RHPORT);
tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -78,7 +78,7 @@ int main(void) {
printf("TinyUSB Host MassStorage Explorer Example\r\n");
// init host stack on configured roothub port
tuh_init(BOARD_TUH_RHPORT);
tusb_init(BOARD_TUH_RHPORT, TUSB_ROLE_HOST);
if (board_init_after_tusb) {
board_init_after_tusb();

View File

@ -1,6 +1,7 @@
set(MCU_VARIANT MIMXRT1062)
set(JLINK_DEVICE MIMXRT1062xxx6A)
#set(JLINK_OPTION "-USB 000726129165")
set(PYOCD_TARGET mimxrt1060)
set(NXPLINK_DEVICE MIMXRT1062xxxxA:EVK-MIMXRT1060)

View File

@ -46,18 +46,6 @@
#pragma GCC diagnostic pop
#endif
#if defined(BOARD_TUD_RHPORT) && CFG_TUD_ENABLED
#define PORT_SUPPORT_DEVICE(_n) (BOARD_TUD_RHPORT == _n)
#else
#define PORT_SUPPORT_DEVICE(_n) 0
#endif
#if defined(BOARD_TUH_RHPORT) && CFG_TUH_ENABLED
#define PORT_SUPPORT_HOST(_n) (BOARD_TUH_RHPORT == _n)
#else
#define PORT_SUPPORT_HOST(_n) 0
#endif
// needed by fsl_flexspi_nor_boot
TU_ATTR_USED const uint8_t dcd_data[] = { 0x00 };
@ -156,23 +144,11 @@ void board_init(void)
// USB Interrupt Handler
//--------------------------------------------------------------------+
void USB_OTG1_IRQHandler(void) {
#if PORT_SUPPORT_DEVICE(0)
tud_int_handler(0);
#endif
#if PORT_SUPPORT_HOST(0)
tuh_int_handler(0, true);
#endif
tusb_int_handler(0, true);
}
void USB_OTG2_IRQHandler(void) {
#if PORT_SUPPORT_DEVICE(1)
tud_int_handler(1);
#endif
#if PORT_SUPPORT_HOST(1)
tuh_int_handler(1, true);
#endif
tusb_int_handler(1, true);
}
//--------------------------------------------------------------------+

View File

@ -28,39 +28,15 @@
#include "bsp/board_api.h"
#include "board.h"
#ifdef BOARD_TUD_RHPORT
#define PORT_SUPPORT_DEVICE(_n) (BOARD_TUD_RHPORT == _n)
#else
#define PORT_SUPPORT_DEVICE(_n) 0
#endif
#ifdef BOARD_TUH_RHPORT
#define PORT_SUPPORT_HOST(_n) (BOARD_TUH_RHPORT == _n)
#else
#define PORT_SUPPORT_HOST(_n) 0
#endif
//--------------------------------------------------------------------+
// USB Interrupt Handler
//--------------------------------------------------------------------+
void USB0_IRQHandler(void) {
#if PORT_SUPPORT_DEVICE(0)
tud_int_handler(0);
#endif
#if PORT_SUPPORT_HOST(0)
tuh_int_handler(0, true);
#endif
tusb_int_handler(0, true);
}
void USB1_IRQHandler(void) {
#if PORT_SUPPORT_DEVICE(1)
tud_int_handler(1);
#endif
#if PORT_SUPPORT_HOST(1)
tuh_int_handler(1, true);
#endif
tusb_int_handler(1, true);
}
//--------------------------------------------------------------------+
@ -118,13 +94,8 @@ void board_init(void) {
Chip_UART_TXEnable(UART_DEV);
//------------- USB -------------//
#if PORT_SUPPORT_DEVICE(0) || PORT_SUPPORT_HOST(0)
Chip_USB0_Init();
#endif
#if PORT_SUPPORT_DEVICE(1) || PORT_SUPPORT_HOST(1)
Chip_USB1_Init();
#endif
}
//--------------------------------------------------------------------+

View File

@ -39,18 +39,6 @@
#include "bsp/board_api.h"
#include "board.h"
#ifdef BOARD_TUD_RHPORT
#define PORT_SUPPORT_DEVICE(_n) (BOARD_TUD_RHPORT == _n)
#else
#define PORT_SUPPORT_DEVICE(_n) 0
#endif
#ifdef BOARD_TUH_RHPORT
#define PORT_SUPPORT_HOST(_n) (BOARD_TUH_RHPORT == _n)
#else
#define PORT_SUPPORT_HOST(_n) 0
#endif
/* System configuration variables used by chip driver */
const uint32_t OscRateIn = 12000000;
const uint32_t ExtRateIn = 0;
@ -161,9 +149,7 @@ void board_init(void)
* - Insert jumpers in position 2-3 in JP17/JP18/JP19
* - Insert jumpers in JP31 (OTG)
*/
#if PORT_SUPPORT_DEVICE(0) || PORT_SUPPORT_HOST(0)
Chip_USB0_Init();
#endif
/* From EA4357 user manual
*
@ -186,17 +172,15 @@ void board_init(void)
* - LED34 lights green when +5V is available on J20.
* - JP15 shall not be inserted. JP16 has no effect
*/
#if PORT_SUPPORT_DEVICE(1) || PORT_SUPPORT_HOST(1)
Chip_USB1_Init();
#endif
// USB0 Vbus Power: P2_3 on EA4357 channel B U20 GPIO26 active low (base board)
Chip_SCU_PinMuxSet(2, 3, SCU_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_FUNC7);
#if PORT_SUPPORT_DEVICE(0)
// P9_5 (GPIO5[18]) (GPIO28 on oem base) as USB connect, active low.
Chip_SCU_PinMuxSet(9, 5, SCU_MODE_PULLDOWN | SCU_MODE_FUNC4);
Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 5, 18);
#if defined(BOARD_TUD_RHPORT) && BOARD_TUD_RHPORT == 0
// P9_5 (GPIO5[18]) (GPIO28 on oem base) as USB connect, active low.
Chip_SCU_PinMuxSet(9, 5, SCU_MODE_PULLDOWN | SCU_MODE_FUNC4);
Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, 5, 18);
#endif
// USB1 Power: EA4357 channel A U20 is enabled by SJ5 connected to pad 1-2, no more action required
@ -206,26 +190,12 @@ void board_init(void)
//--------------------------------------------------------------------+
// USB Interrupt Handler
//--------------------------------------------------------------------+
void USB0_IRQHandler(void)
{
#if PORT_SUPPORT_DEVICE(0)
tud_int_handler(0);
#endif
#if PORT_SUPPORT_HOST(0)
tuh_int_handler(0, true);
#endif
void USB0_IRQHandler(void) {
tusb_int_handler(0, true);
}
void USB1_IRQHandler(void)
{
#if PORT_SUPPORT_DEVICE(1)
tud_int_handler(1);
#endif
#if PORT_SUPPORT_HOST(1)
tuh_int_handler(1, true);
#endif
void USB1_IRQHandler(void) {
tusb_int_handler(1, true);
}
//--------------------------------------------------------------------+

View File

@ -33,18 +33,6 @@
#include "bsp/board_api.h"
#include "board.h"
#ifdef BOARD_TUD_RHPORT
#define PORT_SUPPORT_DEVICE(_n) (BOARD_TUD_RHPORT == _n)
#else
#define PORT_SUPPORT_DEVICE(_n) 0
#endif
#ifdef BOARD_TUH_RHPORT
#define PORT_SUPPORT_HOST(_n) (BOARD_TUH_RHPORT == _n)
#else
#define PORT_SUPPORT_HOST(_n) 0
#endif
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM
//--------------------------------------------------------------------+
@ -71,11 +59,11 @@
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
void USB0_IRQHandler(void) {
tud_int_handler(0);
tusb_int_handler(0, true);
}
void USB1_IRQHandler(void) {
tud_int_handler(1);
tusb_int_handler(1, true);
}
/****************************************************************
@ -160,7 +148,7 @@ void board_init(void) {
#if defined(FSL_FEATURE_SOC_USBHSD_COUNT) && FSL_FEATURE_SOC_USBHSD_COUNT
// LPC546xx and LPC540xx has OTG 1 FS + 1 HS rhports
#if PORT_SUPPORT_DEVICE(0)
#if defined(BOARD_TUD_RHPORT) && BOARD_TUD_RHPORT == 0
// Port0 is Full Speed
POWER_DisablePD(kPDRUNCFG_PD_USB0_PHY); /*< Turn on USB Phy */
CLOCK_SetClkDiv(kCLOCK_DivUsb0Clk, 1, false);
@ -174,7 +162,7 @@ void board_init(void) {
CLOCK_EnableUsbfs0DeviceClock(kCLOCK_UsbSrcFro, CLOCK_GetFroHfFreq());
#endif
#if PORT_SUPPORT_DEVICE(1)
#if defined(BOARD_TUD_RHPORT) && BOARD_TUD_RHPORT == 1
// Port1 is High Speed
POWER_DisablePD(kPDRUNCFG_PD_USB1_PHY);

View File

@ -37,18 +37,6 @@
#include "sct_neopixel.h"
#endif
#ifdef BOARD_TUD_RHPORT
#define PORT_SUPPORT_DEVICE(_n) (BOARD_TUD_RHPORT == _n)
#else
#define PORT_SUPPORT_DEVICE(_n) 0
#endif
#ifdef BOARD_TUH_RHPORT
#define PORT_SUPPORT_HOST(_n) (BOARD_TUH_RHPORT == _n)
#else
#define PORT_SUPPORT_HOST(_n) 0
#endif
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM
//--------------------------------------------------------------------+
@ -73,11 +61,11 @@
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
void USB0_IRQHandler(void) {
tud_int_handler(0);
tusb_int_handler(0, true);
}
void USB1_IRQHandler(void) {
tud_int_handler(1);
tusb_int_handler(1, true);
}
/****************************************************************
@ -209,7 +197,7 @@ void board_init(void) {
/* PORT0 PIN22 configured as USB0_VBUS */
IOCON_PinMuxSet(IOCON, 0U, 22U, IOCON_PIO_DIG_FUNC7_EN);
#if PORT_SUPPORT_DEVICE(0)
#if defined(BOARD_TUD_RHPORT) && BOARD_TUD_RHPORT == 0
// Port0 is Full Speed
/* Turn on USB0 Phy */
@ -234,7 +222,7 @@ void board_init(void) {
CLOCK_EnableUsbfs0DeviceClock(kCLOCK_UsbfsSrcFro, CLOCK_GetFreq(kCLOCK_FroHf));
#endif
#if PORT_SUPPORT_DEVICE(1)
#if defined(BOARD_TUD_RHPORT) && BOARD_TUD_RHPORT == 1
// Port1 is High Speed
/* Turn on USB1 Phy */

View File

@ -33,39 +33,23 @@
#include "pin_mux.h"
#include "clock_config.h"
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM
//--------------------------------------------------------------------+
#ifdef BOARD_TUD_RHPORT
#define PORT_SUPPORT_DEVICE(_n) (BOARD_TUD_RHPORT == _n)
#else
#define PORT_SUPPORT_DEVICE(_n) 0
#endif
#ifdef BOARD_TUH_RHPORT
#define PORT_SUPPORT_HOST(_n) (BOARD_TUH_RHPORT == _n)
#else
#define PORT_SUPPORT_HOST(_n) 0
#endif
//--------------------------------------------------------------------+
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
#if CFG_TUSB_MCU == OPT_MCU_MCXN9
void USB0_FS_IRQHandler(void) {
tud_int_handler(0);
tusb_int_handler(0, true);
}
void USB1_HS_IRQHandler(void) {
tud_int_handler(1);
tusb_int_handler(1, true);
}
#elif CFG_TUSB_MCU == OPT_MCU_MCXA15
void USB0_IRQHandler(void) {
tud_int_handler(0);
tusb_int_handler(0, true);
}
#endif
@ -133,7 +117,7 @@ void board_init(void) {
// USB VBUS
/* PORT0 PIN22 configured as USB0_VBUS */
#if PORT_SUPPORT_DEVICE(0)
#if defined(BOARD_TUD_RHPORT) && BOARD_TUD_RHPORT == 0
// Port0 is Full Speed
#if CFG_TUSB_MCU == OPT_MCU_MCXA15
@ -147,7 +131,7 @@ void board_init(void) {
CLOCK_EnableUsbfsClock();
#endif
#if PORT_SUPPORT_DEVICE(1) && (CFG_TUSB_MCU == OPT_MCU_MCXN9)
#if defined(BOARD_TUD_RHPORT) && BOARD_TUD_RHPORT == 1 && (CFG_TUSB_MCU == OPT_MCU_MCXN9)
// Port1 is High Speed
// Power

View File

@ -182,43 +182,19 @@ uint32_t board_millis(void) {
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
#if CFG_TUD_ENABLED && defined(BOARD_TUD_RHPORT)
#define PORT_SUPPORT_DEVICE(_n) (BOARD_TUD_RHPORT == _n)
#else
#define PORT_SUPPORT_DEVICE(_n) 0
#endif
#if CFG_TUH_ENABLED && defined(BOARD_TUH_RHPORT)
#define PORT_SUPPORT_HOST(_n) (BOARD_TUH_RHPORT == _n)
#else
#define PORT_SUPPORT_HOST(_n) 0
#endif
//------------- USB0 FullSpeed -------------//
void usbfs_interrupt_handler(void) {
IRQn_Type irq = R_FSP_CurrentIrqGet();
R_BSP_IrqStatusClear(irq);
#if PORT_SUPPORT_HOST(0)
tuh_int_handler(0, true);
#endif
#if PORT_SUPPORT_DEVICE(0)
tud_int_handler(0);
#endif
tusb_int_handler(0, true);
}
void usbfs_resume_handler(void) {
IRQn_Type irq = R_FSP_CurrentIrqGet();
R_BSP_IrqStatusClear(irq);
#if PORT_SUPPORT_HOST(0)
tuh_int_handler(0, true);
#endif
#if PORT_SUPPORT_DEVICE(0)
tud_int_handler(0);
#endif
tusb_int_handler(0, true);
}
void usbfs_d0fifo_handler(void) {
@ -240,13 +216,7 @@ void usbhs_interrupt_handler(void) {
IRQn_Type irq = R_FSP_CurrentIrqGet();
R_BSP_IrqStatusClear(irq);
#if PORT_SUPPORT_HOST(1)
tuh_int_handler(1, true);
#endif
#if PORT_SUPPORT_DEVICE(1)
tud_int_handler(1);
#endif
tusb_int_handler(1, true);
}
void usbhs_d0fifo_handler(void) {

View File

@ -205,7 +205,9 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) {
pico_get_unique_board_id(&pico_id);
size_t len = PICO_UNIQUE_BOARD_ID_SIZE_BYTES;
if (len > max_len) len = max_len;
if (len > max_len) {
len = max_len;
}
memcpy(id, pico_id.id, len);
return len;

View File

@ -1,5 +1,6 @@
set(MCU_VARIANT stm32f412zx)
set(JLINK_DEVICE stm32f412zg)
# set(JLINK_OPTION "-USB 000771775987")
set(LD_FILE_GNU ${CMAKE_CURRENT_LIST_DIR}/STM32F412ZGTx_FLASH.ld)

View File

@ -32,11 +32,11 @@
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
void OTG_FS_IRQHandler(void) {
tud_int_handler(0);
tusb_int_handler(0, true);
}
void OTG_HS_IRQHandler(void) {
tud_int_handler(1);
tusb_int_handler(1, true);
}
//--------------------------------------------------------------------+

View File

@ -35,23 +35,6 @@ TU_ATTR_UNUSED static void Error_Handler(void) {
#include "board.h"
//--------------------------------------------------------------------+
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
// Despite being call USB2_OTG_FS on some MCUs
// OTG_FS is marked as RHPort0 by TinyUSB to be consistent across stm32 port
void OTG_FS_IRQHandler(void) {
tud_int_handler(0);
}
// Despite being call USB1_OTG_HS on some MCUs
// OTG_HS is marked as RHPort1 by TinyUSB to be consistent across stm32 port
void OTG_HS_IRQHandler(void) {
tud_int_handler(1);
}
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM
//--------------------------------------------------------------------+
@ -59,9 +42,21 @@ void OTG_HS_IRQHandler(void) {
UART_HandleTypeDef UartHandle;
//--------------------------------------------------------------------+
//
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
// Despite being call USB2_OTG_FS on some MCUs
// OTG_FS is marked as RHPort0 by TinyUSB to be consistent across stm32 port
void OTG_FS_IRQHandler(void) {
tusb_int_handler(0, true);
}
// Despite being call USB1_OTG_HS on some MCUs
// OTG_HS is marked as RHPort1 by TinyUSB to be consistent across stm32 port
void OTG_HS_IRQHandler(void) {
tusb_int_handler(1, true);
}
#ifdef TRACE_ETM
void trace_etm_init(void) {
// H7 trace pin is PE2 to PE6
@ -111,9 +106,10 @@ void board_init(void) {
SysTick->CTRL &= ~1U;
// If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
#ifdef USB_OTG_FS_PERIPH_BASE
#ifdef USB_OTG_FS_PERIPH_BASE
NVIC_SetPriority(OTG_FS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
#endif
#endif
NVIC_SetPriority(OTG_HS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
#endif
@ -206,13 +202,11 @@ void board_init(void) {
struct {
GPIO_TypeDef* port;
uint32_t pin;
} const ulpi_pins[] =
{
} const ulpi_pins[] = {
ULPI_PINS
};
for (uint8_t i=0; i < sizeof(ulpi_pins)/sizeof(ulpi_pins[0]); i++)
{
for (uint8_t i=0; i < sizeof(ulpi_pins)/sizeof(ulpi_pins[0]); i++) {
GPIO_InitStruct.Pin = ulpi_pins[i].pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;

View File

@ -39,6 +39,12 @@
/* CONSTANTS
*------------------------------------------------------------------*/
typedef enum {
TUSB_ROLE_INVALID = 0,
TUSB_ROLE_DEVICE,
TUSB_ROLE_HOST,
} tusb_role_t;
/// defined base on EHCI specs value for Endpoint Speed
typedef enum {
TUSB_SPEED_FULL = 0,

View File

@ -94,7 +94,7 @@
#endif
// Helper to implement optional parameter for TU_VERIFY Macro family
#define _GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
/*------------------------------------------------------------------*/
/* TU_VERIFY
@ -109,7 +109,7 @@
#define TU_VERIFY_1ARGS(_cond) TU_VERIFY_DEFINE(_cond, false)
#define TU_VERIFY_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, _ret)
#define TU_VERIFY(...) _GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
#define TU_VERIFY(...) TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
/*------------------------------------------------------------------*/
/* ASSERT
@ -126,7 +126,7 @@
#define TU_ASSERT_2ARGS(_cond, _ret) TU_ASSERT_DEFINE(_cond, _ret)
#ifndef TU_ASSERT
#define TU_ASSERT(...) _GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
#define TU_ASSERT(...) TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
#endif
#ifdef __cplusplus

View File

@ -557,7 +557,7 @@ bool tud_task_event_ready(void) {
*
int main(void) {
application_init();
tusb_init();
tusb_init(0, TUSB_ROLE_DEVICE);
while(1) { // the mainloop
application_code();

View File

@ -459,7 +459,7 @@ bool tuh_task_event_ready(void) {
int main(void)
{
application_init();
tusb_init();
tusb_init(0, TUSB_ROLE_HOST);
while(1) // the mainloop
{

View File

@ -149,12 +149,9 @@ extern void hcd_int_handler(uint8_t rhport, bool in_isr);
#endif
// Interrupt handler alias to HCD with in_isr as optional parameter
// - tuh_int_handler(rhport) --> hcd_int_handler(rhport, true)
// - tuh_int_handler(rhport, in_isr) --> hcd_int_handler(rhport, in_isr)
// Note: this is similar to TU_VERIFY(), _GET_3RD_ARG() is defined in tusb_verify.h
#define _tuh_int_handler_1arg(_rhport) hcd_int_handler(_rhport, true)
#define _tuh_int_hanlder_2arg(_rhport, _in_isr) hcd_int_handler(_rhport, _in_isr)
#define tuh_int_handler(...) _GET_3RD_ARG(__VA_ARGS__, _tuh_int_hanlder_2arg, _tuh_int_handler_1arg, _dummy)(__VA_ARGS__)
#define tuh_int_handler(...) TU_GET_3RD_ARG(__VA_ARGS__, _tuh_int_hanlder_2arg, _tuh_int_handler_1arg, _dummy)(__VA_ARGS__)
// Check if roothub port is initialized and active as a host
bool tuh_rhport_is_active(uint8_t rhport);

View File

@ -39,21 +39,50 @@
#include "host/usbh_pvt.h"
#endif
#define TUP_USBIP_CONTROLLER_NUM 2
static tusb_role_t _rhport_role[TUP_USBIP_CONTROLLER_NUM] = { 0 };
//--------------------------------------------------------------------+
// Public API
//--------------------------------------------------------------------+
bool tusb_init(void) {
#if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT)
// init device stack CFG_TUSB_RHPORTx_MODE must be defined
TU_ASSERT ( tud_init(TUD_OPT_RHPORT) );
bool _tusb_rhport_init(uint8_t rhport, tusb_role_t role) {
// backward compatible called with tusb_init(void)
#if defined(TUD_OPT_RHPORT) || defined(TUH_OPT_RHPORT)
if (rhport == 0xff || role == TUSB_ROLE_INVALID) {
#if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT)
// init device stack CFG_TUSB_RHPORTx_MODE must be defined
TU_ASSERT ( tud_init(TUD_OPT_RHPORT) );
_rhport_role[TUD_OPT_RHPORT] = TUSB_ROLE_DEVICE;
#endif
#if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT)
// init host stack CFG_TUSB_RHPORTx_MODE must be defined
TU_ASSERT( tuh_init(TUH_OPT_RHPORT) );
_rhport_role[TUH_OPT_RHPORT] = TUSB_ROLE_HOST;
#endif
return true;
}
#endif
#if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT)
// init host stack CFG_TUSB_RHPORTx_MODE must be defined
TU_ASSERT( tuh_init(TUH_OPT_RHPORT) );
// new API with explicit rhport and role
TU_ASSERT(rhport < TUP_USBIP_CONTROLLER_NUM && role != TUSB_ROLE_INVALID);
#if CFG_TUD_ENABLED
if (role == TUSB_ROLE_DEVICE) {
TU_ASSERT( tud_init(rhport) );
}
#endif
#if CFG_TUH_ENABLED
if (role == TUSB_ROLE_HOST) {
TU_ASSERT( tuh_init(rhport) );
}
#endif
_rhport_role[rhport] = role;
return true;
}
@ -71,6 +100,23 @@ bool tusb_inited(void) {
return ret;
}
void tusb_int_handler(uint8_t rhport, bool in_isr) {
TU_VERIFY(rhport < TUP_USBIP_CONTROLLER_NUM,);
#if CFG_TUD_ENABLED
if (_rhport_role[rhport] == TUSB_ROLE_DEVICE) {
(void) in_isr;
tud_int_handler(rhport);
}
#endif
#if CFG_TUH_ENABLED
if (_rhport_role[rhport] == TUSB_ROLE_HOST) {
tuh_int_handler(rhport, in_isr);
}
#endif
}
//--------------------------------------------------------------------+
// Descriptor helper
//--------------------------------------------------------------------+

View File

@ -129,18 +129,38 @@
//--------------------------------------------------------------------+
// APPLICATION API
//--------------------------------------------------------------------+
#if CFG_TUH_ENABLED || CFG_TUD_ENABLED
// Initialize device/host stack
// Internal helper for backward compatible with tusb_init(void)
bool _tusb_rhport_init(uint8_t rhport, tusb_role_t role);
// Initialize roothub port with device/host role
// Note: when using with RTOS, this should be called after scheduler/kernel is started.
// Otherwise it could cause kernel issue since USB IRQ handler does use RTOS queue API.
bool tusb_init(void);
// Otherwise, it could cause kernel issue since USB IRQ handler does use RTOS queue API.
// Note2: defined as macro for backward compatible with tusb_init(void), can be changed to function in the future.
#define _tusb_init_0arg() _tusb_rhport_init(0xff, TUSB_ROLE_INVALID)
#define _tusb_init_1arg(_rhport) _tusb_rhport_init(_rhport, TUSB_ROLE_INVALID)
#define _tusb_init_2arg(_rhport, _role) _tusb_rhport_init(_rhport, _role)
#define tusb_init(...) TU_GET_3RD_ARG(__VA_ARGS__, _tusb_init_2arg, _tusb_init_1arg, _tusb_init_0arg)(__VA_ARGS__)
// Check if stack is initialized
bool tusb_inited(void);
// Called to handle usb interrupt/event. tusb_init(rhport, role) must be called before
void tusb_int_handler(uint8_t rhport, bool in_isr);
// TODO
// bool tusb_teardown(void);
#else
#define tusb_init(...) (false)
#define tusb_int_handler(...) do {}while(0)
#define tusb_inited() (false)
#endif
#ifdef __cplusplus
}
#endif

View File

@ -200,7 +200,7 @@ void setUp(void)
if ( !tud_inited() )
{
dcd_init_Expect(rhport);
tusb_init();
tusb_init(0, TUSB_ROLE_DEVICE);
}
dcd_event_bus_reset(rhport, TUSB_SPEED_HIGH, false);

View File

@ -128,7 +128,7 @@ void setUp(void)
{
mscd_init_Expect();
dcd_init_Expect(rhport);
tusb_init();
tusb_init(0, TUSB_ROLE_DEVICE);
}
}