From 3f3ff3da3e3b51ea6e6c62719128e12408156554 Mon Sep 17 00:00:00 2001 From: Raphael Lehmann Date: Fri, 28 Jan 2022 18:41:49 +0100 Subject: [PATCH] [examples] Add example with TinyUSB and FreeRTOS (for Nucleo-F429ZI) --- examples/nucleo_f429zi/usb_freertos/main.cpp | 101 ++++++++++++++++++ .../nucleo_f429zi/usb_freertos/project.xml | 17 +++ 2 files changed, 118 insertions(+) create mode 100644 examples/nucleo_f429zi/usb_freertos/main.cpp create mode 100644 examples/nucleo_f429zi/usb_freertos/project.xml diff --git a/examples/nucleo_f429zi/usb_freertos/main.cpp b/examples/nucleo_f429zi/usb_freertos/main.cpp new file mode 100644 index 0000000000..ae77518fd9 --- /dev/null +++ b/examples/nucleo_f429zi/usb_freertos/main.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2022, Raphael Lehmann + * + * This file is part of the modm project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +// ---------------------------------------------------------------------------- + +#include + +#include +#include +#include + +using namespace Board; + +class SomeTask : modm::rtos::Thread +{ +public: + SomeTask() : Thread(2, 2048, "some_task") {} + + void + run() + { + modm::PeriodicTimer tmr{2.5s}; + uint8_t counter{0}; + + while (1) + { + if (tmr.execute()) + { + MODM_LOG_INFO << "SomeTask: Hello via Uart, counter=" << (counter++) << modm::endl; + } + + vTaskDelay(1); + } + + vTaskDelete(0); + } +}; + +class UsbTask : modm::rtos::Thread +{ +public: + UsbTask() : Thread(1, 2048, "usb_task"), usb_io_device0{}, usb_stream0{usb_io_device0} {} + + void + run() + { + MODM_LOG_INFO << "USBTask: Calling Board::initializeUsbFs() ..." << modm::endl; + Board::initializeUsbFs(4); + MODM_LOG_INFO << "USBTask: Calling tusb_init() ..." << modm::endl; + tusb_init(); + MODM_LOG_INFO << "... done!" << modm::endl; + + modm::PeriodicTimer tmr{2.5s}; + uint8_t counter{0}; + + while (1) + { + tud_task(); + if (tmr.execute()) + { + MODM_LOG_INFO << "UsbTask: Hello via Uart, counter=" << counter << modm::endl; + usb_stream0 << "UsbTask: Hello via USB CDC, counter=" << counter << modm::endl; + counter++; + } + + vTaskDelay(1); + } + + vTaskDelete(0); + } + +private: + modm::IODeviceWrapper usb_io_device0; + modm::IOStream usb_stream0; +}; + +SomeTask someTask; +UsbTask usbTask; + +int +main() +{ + Board::initialize(); + Leds::setOutput(); + MODM_LOG_INFO << "Nucleo-F429ZI: TinyUSB & FreeRTOS example" << modm::endl; + + MODM_LOG_INFO << "WARNING!" << modm::endl; + MODM_LOG_INFO << "TinyUSB in modm does not currently use the FreeRTOS abstraction layer"; + MODM_LOG_INFO << " and is not thread-safe with FreeRTOS threads." << modm::endl; + + modm::rtos::Scheduler::schedule(); + + // we should never get here + return 0; +} diff --git a/examples/nucleo_f429zi/usb_freertos/project.xml b/examples/nucleo_f429zi/usb_freertos/project.xml new file mode 100644 index 0000000000..1fca26d0fc --- /dev/null +++ b/examples/nucleo_f429zi/usb_freertos/project.xml @@ -0,0 +1,17 @@ + + modm:nucleo-f429zi + + + + + + modm:build:scons + modm:freertos + modm:processing:rtos + modm:processing:timer + modm:tinyusb + + + CFG_TUSB_DEBUG=2 + +