Skip to content

Commit 4d39474

Browse files
authored
Merge pull request arduino#18 from Seeed-Studio/feature-tinyusb-freertos
Make tinyusb work with freeRTOS
2 parents 91bfc64 + cfc52e9 commit 4d39474

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

cores/arduino/hooks.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717
*/
1818

19+
#include <stdint.h>
1920
/**
2021
* Empty yield() hook.
2122
*
@@ -25,23 +26,30 @@
2526
* Its defined as a weak symbol and it can be redefined to implement a
2627
* real cooperative scheduler.
2728
*/
28-
static void __empty() {
29-
// Empty
29+
extern void tud_task(void);
30+
extern uint32_t tud_cdc_n_write_flush (uint8_t itf);
31+
static void __empty()
32+
{
33+
// Empty
34+
#if defined(USE_TINYUSB)
35+
tud_task();
36+
tud_cdc_n_write_flush(0);
37+
#endif
3038
}
31-
32-
void yield(void) __attribute__ ((weak, alias("__empty")));
39+
void yield(void) __attribute__((weak, alias("__empty")));
3340

3441
/**
3542
* SysTick hook
3643
*
3744
* This function is called from SysTick handler, before the default
3845
* handler provided by Arduino.
3946
*/
40-
static int __false() {
41-
// Return false
42-
return 0;
47+
static int __false()
48+
{
49+
// Return false
50+
return 0;
4351
}
44-
int sysTickHook(void) __attribute__ ((weak, alias("__false")));
52+
int sysTickHook(void) __attribute__((weak, alias("__false")));
4553

4654
/**
4755
* SVC hook
@@ -50,10 +58,11 @@ int sysTickHook(void) __attribute__ ((weak, alias("__false")));
5058
* These functions are called from SVC handler, and PensSV handler.
5159
* Default action is halting.
5260
*/
53-
static void __halt() {
54-
// Halts
55-
while (1)
56-
;
61+
static void __halt()
62+
{
63+
// Halts
64+
while (1)
65+
;
5766
}
58-
void svcHook(void) __attribute__ ((weak, alias("__halt")));
59-
void pendSVHook(void) __attribute__ ((weak, alias("__halt")));
67+
void svcHook(void) __attribute__((weak, alias("__halt")));
68+
void pendSVHook(void) __attribute__((weak, alias("__halt")));

cores/arduino/main.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,33 @@
2222
// Weak empty variant initialization function.
2323
// May be redefined by variant files.
2424
void initVariant() __attribute__((weak));
25-
void initVariant() { }
25+
void initVariant() {}
26+
#if defined(USE_TINYUSB)
27+
// run TinyUSB background task auto This method will be overridden when running in FreeRTOS
28+
void __attribute__((weak)) tinyusb_task(void)
29+
{
30+
}
31+
#endif
2632

2733
/*
2834
* _real_body(),_wrap_body() is a compiler trick,
2935
* user could overwrite the arduino setup/loop function by re-implement _wrap_body(),
3036
* calling of _real_body() in _wrap_body() is optional.
3137
*/
32-
void _real_body(){
38+
void _real_body()
39+
{
3340
setup();
34-
3541
for (;;)
3642
{
3743
loop();
3844
yield(); // yield run usb background task
39-
40-
if (serialEventRun) serialEventRun();
45+
if (serialEventRun)
46+
serialEventRun();
4147
}
4248
}
4349

44-
void __attribute__((weak)) _wrap_body(){
50+
void __attribute__((weak)) _wrap_body()
51+
{
4552
_real_body();
4653
}
4754

@@ -51,7 +58,7 @@ extern "C" void __libc_init_array(void);
5158
/*
5259
* \brief Main entry point of Arduino application
5360
*/
54-
int main( void )
61+
int main(void)
5562
{
5663
init();
5764

@@ -63,21 +70,11 @@ int main( void )
6370

6471
#if defined(USE_TINYUSB)
6572
Adafruit_TinyUSB_Core_init();
73+
tinyusb_task();
6674
#elif defined(USBCON)
6775
USBDevice.init();
6876
USBDevice.attach();
6977
#endif
7078
_wrap_body();
7179
return 0;
7280
}
73-
74-
#if defined(USE_TINYUSB)
75-
76-
// run TinyUSB background task when yield()
77-
extern "C" void yield(void)
78-
{
79-
tud_task();
80-
tud_cdc_write_flush();
81-
}
82-
83-
#endif

0 commit comments

Comments
 (0)