Skip to content

Commit 3f3ff3d

Browse files
committed
[examples] Add example with TinyUSB and FreeRTOS (for Nucleo-F429ZI)
1 parent aa24f4a commit 3f3ff3d

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (c) 2022, Raphael Lehmann
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <tusb.h>
13+
14+
#include <modm/board.hpp>
15+
#include <modm/processing/rtos.hpp>
16+
#include <modm/processing/timer/periodic_timer.hpp>
17+
18+
using namespace Board;
19+
20+
class SomeTask : modm::rtos::Thread
21+
{
22+
public:
23+
SomeTask() : Thread(2, 2048, "some_task") {}
24+
25+
void
26+
run()
27+
{
28+
modm::PeriodicTimer tmr{2.5s};
29+
uint8_t counter{0};
30+
31+
while (1)
32+
{
33+
if (tmr.execute())
34+
{
35+
MODM_LOG_INFO << "SomeTask: Hello via Uart, counter=" << (counter++) << modm::endl;
36+
}
37+
38+
vTaskDelay(1);
39+
}
40+
41+
vTaskDelete(0);
42+
}
43+
};
44+
45+
class UsbTask : modm::rtos::Thread
46+
{
47+
public:
48+
UsbTask() : Thread(1, 2048, "usb_task"), usb_io_device0{}, usb_stream0{usb_io_device0} {}
49+
50+
void
51+
run()
52+
{
53+
MODM_LOG_INFO << "USBTask: Calling Board::initializeUsbFs() ..." << modm::endl;
54+
Board::initializeUsbFs(4);
55+
MODM_LOG_INFO << "USBTask: Calling tusb_init() ..." << modm::endl;
56+
tusb_init();
57+
MODM_LOG_INFO << "... done!" << modm::endl;
58+
59+
modm::PeriodicTimer tmr{2.5s};
60+
uint8_t counter{0};
61+
62+
while (1)
63+
{
64+
tud_task();
65+
if (tmr.execute())
66+
{
67+
MODM_LOG_INFO << "UsbTask: Hello via Uart, counter=" << counter << modm::endl;
68+
usb_stream0 << "UsbTask: Hello via USB CDC, counter=" << counter << modm::endl;
69+
counter++;
70+
}
71+
72+
vTaskDelay(1);
73+
}
74+
75+
vTaskDelete(0);
76+
}
77+
78+
private:
79+
modm::IODeviceWrapper<UsbUart0, modm::IOBuffer::DiscardIfFull> usb_io_device0;
80+
modm::IOStream usb_stream0;
81+
};
82+
83+
SomeTask someTask;
84+
UsbTask usbTask;
85+
86+
int
87+
main()
88+
{
89+
Board::initialize();
90+
Leds::setOutput();
91+
MODM_LOG_INFO << "Nucleo-F429ZI: TinyUSB & FreeRTOS example" << modm::endl;
92+
93+
MODM_LOG_INFO << "WARNING!" << modm::endl;
94+
MODM_LOG_INFO << "TinyUSB in modm does not currently use the FreeRTOS abstraction layer";
95+
MODM_LOG_INFO << " and is not thread-safe with FreeRTOS threads." << modm::endl;
96+
97+
modm::rtos::Scheduler::schedule();
98+
99+
// we should never get here
100+
return 0;
101+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<library>
2+
<extends>modm:nucleo-f429zi</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_f429zi/usb_freertos</option>
5+
<option name="modm:tinyusb:config">device.cdc</option>
6+
</options>
7+
<modules>
8+
<module>modm:build:scons</module>
9+
<module>modm:freertos</module>
10+
<module>modm:processing:rtos</module>
11+
<module>modm:processing:timer</module>
12+
<module>modm:tinyusb</module>
13+
</modules>
14+
<collectors>
15+
<collect name="modm:build:cppdefines">CFG_TUSB_DEBUG=2</collect>
16+
</collectors>
17+
</library>

0 commit comments

Comments
 (0)