Skip to content

Commit

Permalink
[example] Add STM32F401 Discovery examples
Browse files Browse the repository at this point in the history
  • Loading branch information
cajt authored and salkinium committed Jan 5, 2024
1 parent 2da6c2f commit 2381c61
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Expand Up @@ -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
Expand Down
87 changes: 87 additions & 0 deletions 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 <modm/board.hpp>
#include <modm/processing.hpp>
#include <modm/math/filter.hpp>

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<float, 25> averageX;
modm::filter::MovingAverage<float, 25> averageY;
};

ReaderThread reader;

int
main()
{
Board::initialize();
Board::initializeLsm3();

Leds::set();
modm::delay(42ms);

modm::fiber::Scheduler::run();

return 0;
}
17 changes: 17 additions & 0 deletions examples/stm32f401_discovery/accelerometer/project.xml
@@ -0,0 +1,17 @@
<library>
<extends>modm:disco-f401vc</extends>
<options>
<option name="modm:build:build.path">../../../build/stm32f401_discovery/accelerometer</option>
<option name="modm:processing:protothread:use_fiber">yes</option>
</options>
<modules>
<module>modm:driver:lsm303a</module>
<module>modm:math:filter</module>
<module>modm:platform:gpio</module>
<module>modm:platform:i2c</module>
<module>modm:platform:i2c.bitbang</module>
<module>modm:processing:timer</module>
<module>modm:processing:protothread</module>
<module>modm:build:scons</module>
</modules>
</library>
40 changes: 40 additions & 0 deletions 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 <modm/board.hpp>

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;
}
9 changes: 9 additions & 0 deletions examples/stm32f401_discovery/blink/project.xml
@@ -0,0 +1,9 @@
<library>
<extends>modm:disco-f401vc</extends>
<options>
<option name="modm:build:build.path">../../../build/stm32f401_discovery/blink</option>
</options>
<modules>
<module>modm:build:scons</module>
</modules>
</library>
91 changes: 91 additions & 0 deletions 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 <modm/board.hpp>
#include <modm/driver/inertial/l3gd20.hpp>
#include <modm/processing.hpp>
#include <modm/math/filter.hpp>

// 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<float, 25> averageZ;
};

ReaderThread reader;


int
main()
{
Board::initialize();
Board::initializeL3g();

while (true)
{
reader.update();
}

return 0;
}
14 changes: 14 additions & 0 deletions examples/stm32f401_discovery/gyroscope/project.xml
@@ -0,0 +1,14 @@
<library>
<extends>modm:disco-f401vc</extends>
<options>
<option name="modm:build:build.path">../../../build/stm32f401_discovery/gyroscope</option>
</options>
<modules>
<module>modm:driver:l3gd20</module>
<module>modm:math:filter</module>
<module>modm:platform:spi:1</module>
<module>modm:processing:timer</module>
<module>modm:processing:protothread</module>
<module>modm:build:scons</module>
</modules>
</library>
50 changes: 50 additions & 0 deletions 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 <modm/board.hpp>

// ----------------------------------------------------------------------------
/**
* 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<GpioA2::Tx>();
Usart2::initialize<Board::SystemClock, 9600_Bd>();

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;
}
11 changes: 11 additions & 0 deletions examples/stm32f401_discovery/uart/project.xml
@@ -0,0 +1,11 @@
<library>
<extends>modm:disco-f401vc</extends>
<options>
<option name="modm:build:build.path">../../../build/stm32f401_discovery/uart</option>
</options>
<modules>
<module>modm:platform:gpio</module>
<module>modm:platform:uart:2</module>
<module>modm:build:scons</module>
</modules>
</library>

0 comments on commit 2381c61

Please sign in to comment.