From 2381c614a7836f80be4b19e4afefcde3e531543c Mon Sep 17 00:00:00 2001 From: cajt Date: Fri, 5 Jan 2024 19:30:16 +0100 Subject: [PATCH] [example] Add STM32F401 Discovery examples --- .github/workflows/linux.yml | 2 +- .../accelerometer/main.cpp | 87 ++++++++++++++++++ .../accelerometer/project.xml | 17 ++++ examples/stm32f401_discovery/blink/main.cpp | 40 ++++++++ .../stm32f401_discovery/blink/project.xml | 9 ++ .../stm32f401_discovery/gyroscope/main.cpp | 91 +++++++++++++++++++ .../stm32f401_discovery/gyroscope/project.xml | 14 +++ examples/stm32f401_discovery/uart/main.cpp | 50 ++++++++++ examples/stm32f401_discovery/uart/project.xml | 11 +++ 9 files changed, 320 insertions(+), 1 deletion(-) create mode 100644 examples/stm32f401_discovery/accelerometer/main.cpp create mode 100644 examples/stm32f401_discovery/accelerometer/project.xml create mode 100644 examples/stm32f401_discovery/blink/main.cpp create mode 100644 examples/stm32f401_discovery/blink/project.xml create mode 100644 examples/stm32f401_discovery/gyroscope/main.cpp create mode 100644 examples/stm32f401_discovery/gyroscope/project.xml create mode 100644 examples/stm32f401_discovery/uart/main.cpp create mode 100644 examples/stm32f401_discovery/uart/project.xml diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index d6e4765bc7..9de02e20ef 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -201,7 +201,7 @@ jobs: - name: Examples STM32F4 Only Discovery Board if: always() run: | - (cd examples && ../tools/scripts/examples_compile.py stm32f4_discovery stm32f429_discovery stm32f469_discovery) + (cd examples && ../tools/scripts/examples_compile.py stm32f4_discovery stm32f429_discovery stm32f469_discovery stm32f401_discovery) stm32f4-examples-2: runs-on: ubuntu-22.04 diff --git a/examples/stm32f401_discovery/accelerometer/main.cpp b/examples/stm32f401_discovery/accelerometer/main.cpp new file mode 100644 index 0000000000..7260dc6124 --- /dev/null +++ b/examples/stm32f401_discovery/accelerometer/main.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2015, Kevin Läufer + * Copyright (c) 2015-2018, Niklas Hauser + * Copyright (c) 2024, Carl Treudler + * + * 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 + +using namespace Board; + +// create the data object +Board::lsm3::Accelerometer::Data data; +// and hand it to the sensor driver +Board::lsm3::Accelerometer accelerometer(data); + + +class ReaderThread : public modm::pt::Protothread +{ +public: + bool + update() + { + PT_BEGIN(); + + // initialize with limited range of ±2g + PT_CALL(accelerometer.configure(accelerometer.Scale::G2)); + + while (true) + { + // read out the sensor + PT_CALL(accelerometer.readAcceleration()); + + averageX.update(accelerometer.getData().getX()); + averageY.update(accelerometer.getData().getY()); + + { + bool xs = averageX.getValue() < -0.2f; + bool xn = averageX.getValue() > 0.2f; + + bool xe = averageY.getValue() < -0.2f; + bool xw = averageY.getValue() > 0.2f; + + + LedBlue::set(xs); // South + LedGreen::set(xw); //West + LedOrange::set(xn); // North + LedRed::set(xe); // East + } + + // repeat every 5 ms + timeout.restart(5ms); + PT_WAIT_UNTIL(timeout.isExpired()); + } + + PT_END(); + } + +private: + modm::ShortTimeout timeout; + modm::filter::MovingAverage averageX; + modm::filter::MovingAverage averageY; +}; + +ReaderThread reader; + +int +main() +{ + Board::initialize(); + Board::initializeLsm3(); + + Leds::set(); + modm::delay(42ms); + + modm::fiber::Scheduler::run(); + + return 0; +} diff --git a/examples/stm32f401_discovery/accelerometer/project.xml b/examples/stm32f401_discovery/accelerometer/project.xml new file mode 100644 index 0000000000..bd149df959 --- /dev/null +++ b/examples/stm32f401_discovery/accelerometer/project.xml @@ -0,0 +1,17 @@ + + modm:disco-f401vc + + + + + + modm:driver:lsm303a + modm:math:filter + modm:platform:gpio + modm:platform:i2c + modm:platform:i2c.bitbang + modm:processing:timer + modm:processing:protothread + modm:build:scons + + diff --git a/examples/stm32f401_discovery/blink/main.cpp b/examples/stm32f401_discovery/blink/main.cpp new file mode 100644 index 0000000000..0e0046fa3a --- /dev/null +++ b/examples/stm32f401_discovery/blink/main.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2011, Georgi Grinshpun + * Copyright (c) 2011-2012, Fabian Greif + * Copyright (c) 2012, 2014, Sascha Schade + * Copyright (c) 2013, Kevin Läufer + * Copyright (c) 2013, 2015-2017, Niklas Hauser + * Copyright (c) 2024, Carl Treudler + * + * 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 + +using namespace Board; + +// ---------------------------------------------------------------------------- +int +main() +{ + initialize(); + + LedOrange::set(); + LedRed::set(); + + while (true) + { + LedBlue::toggle(); + LedGreen::toggle(); + LedOrange::toggle(); + LedRed::toggle(); + modm::delay(Button::read() ? 250ms : 500ms); + } + + return 0; +} diff --git a/examples/stm32f401_discovery/blink/project.xml b/examples/stm32f401_discovery/blink/project.xml new file mode 100644 index 0000000000..d56c28e21d --- /dev/null +++ b/examples/stm32f401_discovery/blink/project.xml @@ -0,0 +1,9 @@ + + modm:disco-f401vc + + + + + modm:build:scons + + diff --git a/examples/stm32f401_discovery/gyroscope/main.cpp b/examples/stm32f401_discovery/gyroscope/main.cpp new file mode 100644 index 0000000000..233aaa42df --- /dev/null +++ b/examples/stm32f401_discovery/gyroscope/main.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2014-2018, Niklas Hauser + * Copyright (c) 2015, Kevin Läufer + * Copyright (c) 2015, Martin Esser + * Copyright (c) 2018, Christopher Durand + * Copyright (c) 2024, Carl Treudler + * + * 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 + +// maps arbitrary gpios to a bit +using LedRing = SoftwareGpioPort< + Board::LedOrange, // 3 + Board::LedRed, // 2 + Board::LedBlue, // 1 + Board::LedGreen // 0 + >; + +// create the data object +Board::l3g::Gyroscope::Data data; +// and hand it to the sensor driver +Board::l3g::Gyroscope gyro(data); + + +class ReaderThread : public modm::pt::Protothread +{ +public: + bool + update() + { + PT_BEGIN(); + + // initialize with limited range of 250 degrees per second + PT_CALL(gyro.configure(gyro.Scale::Dps250)); + + while (true) + { + // read out the sensor + PT_CALL(gyro.readRotation()); + + // update the moving average + averageZ.update(gyro.getData().getZ()); + + { + float value = averageZ.getValue(); + // normalize rotation and scale by 5 leds + uint16_t leds = abs(value / 200 * 5); + leds = (1ul << leds) - 1; + + LedRing::write(leds); + } + + // repeat every 5 ms + timeout.restart(5ms); + PT_WAIT_UNTIL(timeout.isExpired()); + } + + PT_END(); + } + +private: + modm::ShortTimeout timeout; + modm::filter::MovingAverage averageZ; +}; + +ReaderThread reader; + + +int +main() +{ + Board::initialize(); + Board::initializeL3g(); + + while (true) + { + reader.update(); + } + + return 0; +} diff --git a/examples/stm32f401_discovery/gyroscope/project.xml b/examples/stm32f401_discovery/gyroscope/project.xml new file mode 100644 index 0000000000..f175575740 --- /dev/null +++ b/examples/stm32f401_discovery/gyroscope/project.xml @@ -0,0 +1,14 @@ + + modm:disco-f401vc + + + + + modm:driver:l3gd20 + modm:math:filter + modm:platform:spi:1 + modm:processing:timer + modm:processing:protothread + modm:build:scons + + diff --git a/examples/stm32f401_discovery/uart/main.cpp b/examples/stm32f401_discovery/uart/main.cpp new file mode 100644 index 0000000000..02594edcb7 --- /dev/null +++ b/examples/stm32f401_discovery/uart/main.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2011, Georgi Grinshpun + * Copyright (c) 2011-2012, Fabian Greif + * Copyright (c) 2012, 2014, Sascha Schade + * Copyright (c) 2013, Kevin Läufer + * Copyright (c) 2013, 2015-2017, Niklas Hauser + * Copyright (c) 2024, Carl Treudler + * + * 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 + +// ---------------------------------------------------------------------------- +/** + * Very basic example of USART usage. + * The ASCII sequence 'A', 'B', 'C', ... , 'Z', 'A', 'B', 'C', ... + * is printed with 9600 baud, 8N1 at pin PA3. + */ +int +main() +{ + Board::initialize(); + + Board::LedRed::set(); + + // Enable USART 2 + Usart2::connect(); + Usart2::initialize(); + + while (true) + { + static uint8_t c = 'A'; + Board::LedRed::toggle(); + Board::LedGreen::toggle(); + Usart2::write(c); + ++c; + if (c > 'Z') { + c = 'A'; + } + modm::delay(500ms); + } + + return 0; +} diff --git a/examples/stm32f401_discovery/uart/project.xml b/examples/stm32f401_discovery/uart/project.xml new file mode 100644 index 0000000000..6f1b628e06 --- /dev/null +++ b/examples/stm32f401_discovery/uart/project.xml @@ -0,0 +1,11 @@ + + modm:disco-f401vc + + + + + modm:platform:gpio + modm:platform:uart:2 + modm:build:scons + +