Skip to content

Commit cb6d216

Browse files
committed
move usb hardware init and tud_init into usb device thread
help to prevent USB IRQ when scheduler is not started yet
1 parent ca4f2bc commit cb6d216

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

cores/nRF5/TinyUSB/Adafruit_TinyUSB_nRF.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030
#include "Arduino.h"
3131
#include "Adafruit_TinyUSB_Core.h"
3232

33+
//--------------------------------------------------------------------+
34+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
35+
//--------------------------------------------------------------------+
36+
37+
#define USBD_STACK_SZ (200)
38+
39+
// tinyusb function that handles power event (detected, ready, removed)
40+
// We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled.
41+
extern "C" void tusb_hal_nrf_power_event(uint32_t event);
42+
3343
//--------------------------------------------------------------------+
3444
// Forward USB interrupt events to TinyUSB IRQ Handler
3545
//--------------------------------------------------------------------+
@@ -46,30 +56,6 @@ extern "C" void USBD_IRQHandler(void)
4656
#endif
4757
}
4858

49-
//--------------------------------------------------------------------+
50-
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
51-
//--------------------------------------------------------------------+
52-
53-
#define USBD_STACK_SZ (200)
54-
55-
// tinyusb function that handles power event (detected, ready, removed)
56-
// We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled.
57-
extern "C" void tusb_hal_nrf_power_event(uint32_t event);
58-
59-
// USB Device Driver task
60-
// This top level thread process all usb events and invoke callbacks
61-
static void usb_device_task(void* param)
62-
{
63-
(void) param;
64-
65-
// RTOS forever loop
66-
while (1)
67-
{
68-
// tinyusb device task
69-
tud_task();
70-
}
71-
}
72-
7359
//--------------------------------------------------------------------+
7460
// Core Init & Touch1200
7561
//--------------------------------------------------------------------+
@@ -99,8 +85,12 @@ static void usb_hardware_init(void)
9985
if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
10086
}
10187

102-
void Adafruit_TinyUSB_Core_init(void)
88+
// USB Device Driver task
89+
// This top level thread process all usb events and invoke callbacks
90+
static void usb_device_task(void* param)
10391
{
92+
(void) param;
93+
10494
USBDevice.addInterface( (Adafruit_USBD_Interface&) Serial);
10595
USBDevice.setID(USB_VID, USB_PID);
10696
USBDevice.begin();
@@ -110,6 +100,16 @@ void Adafruit_TinyUSB_Core_init(void)
110100
// Init tinyusb stack
111101
tusb_init();
112102

103+
// RTOS forever loop
104+
while (1)
105+
{
106+
// tinyusb device task
107+
tud_task();
108+
}
109+
}
110+
111+
void Adafruit_TinyUSB_Core_init(void)
112+
{
113113
// Create a task for tinyusb device stack
114114
xTaskCreate( usb_device_task, "usbd", USBD_STACK_SZ, NULL, TASK_PRIO_HIGH, NULL);
115115
}

cores/nRF5/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ static void loop_task(void* arg)
4444
{
4545
(void) arg;
4646

47-
#ifdef USE_TINYUSB
48-
Adafruit_TinyUSB_Core_init();
49-
#endif
50-
5147
#if CFG_DEBUG
5248
// If Serial is not begin(), call it to avoid hard fault
5349
if(!Serial) Serial.begin(115200);
@@ -80,6 +76,10 @@ int main( void )
8076
SEGGER_SYSVIEW_Conf();
8177
#endif
8278

79+
#ifdef USE_TINYUSB
80+
Adafruit_TinyUSB_Core_init();
81+
#endif
82+
8383
// Create a task for loop()
8484
xTaskCreate( loop_task, "loop", LOOP_STACK_SZ, NULL, TASK_PRIO_LOW, &_loopHandle);
8585

0 commit comments

Comments
 (0)