Skip to content

Commit 2381c61

Browse files
cajtsalkinium
authored andcommitted
[example] Add STM32F401 Discovery examples
1 parent 2da6c2f commit 2381c61

File tree

9 files changed

+320
-1
lines changed

9 files changed

+320
-1
lines changed

.github/workflows/linux.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ jobs:
201201
- name: Examples STM32F4 Only Discovery Board
202202
if: always()
203203
run: |
204-
(cd examples && ../tools/scripts/examples_compile.py stm32f4_discovery stm32f429_discovery stm32f469_discovery)
204+
(cd examples && ../tools/scripts/examples_compile.py stm32f4_discovery stm32f429_discovery stm32f469_discovery stm32f401_discovery)
205205
206206
stm32f4-examples-2:
207207
runs-on: ubuntu-22.04
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2015, Kevin Läufer
3+
* Copyright (c) 2015-2018, Niklas Hauser
4+
* Copyright (c) 2024, Carl Treudler
5+
*
6+
* This file is part of the modm project.
7+
*
8+
* This Source Code Form is subject to the terms of the Mozilla Public
9+
* License, v. 2.0. If a copy of the MPL was not distributed with this
10+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
*/
12+
// ----------------------------------------------------------------------------
13+
14+
#include <modm/board.hpp>
15+
#include <modm/processing.hpp>
16+
#include <modm/math/filter.hpp>
17+
18+
using namespace Board;
19+
20+
// create the data object
21+
Board::lsm3::Accelerometer::Data data;
22+
// and hand it to the sensor driver
23+
Board::lsm3::Accelerometer accelerometer(data);
24+
25+
26+
class ReaderThread : public modm::pt::Protothread
27+
{
28+
public:
29+
bool
30+
update()
31+
{
32+
PT_BEGIN();
33+
34+
// initialize with limited range of ±2g
35+
PT_CALL(accelerometer.configure(accelerometer.Scale::G2));
36+
37+
while (true)
38+
{
39+
// read out the sensor
40+
PT_CALL(accelerometer.readAcceleration());
41+
42+
averageX.update(accelerometer.getData().getX());
43+
averageY.update(accelerometer.getData().getY());
44+
45+
{
46+
bool xs = averageX.getValue() < -0.2f;
47+
bool xn = averageX.getValue() > 0.2f;
48+
49+
bool xe = averageY.getValue() < -0.2f;
50+
bool xw = averageY.getValue() > 0.2f;
51+
52+
53+
LedBlue::set(xs); // South
54+
LedGreen::set(xw); //West
55+
LedOrange::set(xn); // North
56+
LedRed::set(xe); // East
57+
}
58+
59+
// repeat every 5 ms
60+
timeout.restart(5ms);
61+
PT_WAIT_UNTIL(timeout.isExpired());
62+
}
63+
64+
PT_END();
65+
}
66+
67+
private:
68+
modm::ShortTimeout timeout;
69+
modm::filter::MovingAverage<float, 25> averageX;
70+
modm::filter::MovingAverage<float, 25> averageY;
71+
};
72+
73+
ReaderThread reader;
74+
75+
int
76+
main()
77+
{
78+
Board::initialize();
79+
Board::initializeLsm3();
80+
81+
Leds::set();
82+
modm::delay(42ms);
83+
84+
modm::fiber::Scheduler::run();
85+
86+
return 0;
87+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<library>
2+
<extends>modm:disco-f401vc</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/stm32f401_discovery/accelerometer</option>
5+
<option name="modm:processing:protothread:use_fiber">yes</option>
6+
</options>
7+
<modules>
8+
<module>modm:driver:lsm303a</module>
9+
<module>modm:math:filter</module>
10+
<module>modm:platform:gpio</module>
11+
<module>modm:platform:i2c</module>
12+
<module>modm:platform:i2c.bitbang</module>
13+
<module>modm:processing:timer</module>
14+
<module>modm:processing:protothread</module>
15+
<module>modm:build:scons</module>
16+
</modules>
17+
</library>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2011, Georgi Grinshpun
3+
* Copyright (c) 2011-2012, Fabian Greif
4+
* Copyright (c) 2012, 2014, Sascha Schade
5+
* Copyright (c) 2013, Kevin Läufer
6+
* Copyright (c) 2013, 2015-2017, Niklas Hauser
7+
* Copyright (c) 2024, Carl Treudler
8+
*
9+
* This file is part of the modm project.
10+
*
11+
* This Source Code Form is subject to the terms of the Mozilla Public
12+
* License, v. 2.0. If a copy of the MPL was not distributed with this
13+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
14+
*/
15+
// ----------------------------------------------------------------------------
16+
17+
#include <modm/board.hpp>
18+
19+
using namespace Board;
20+
21+
// ----------------------------------------------------------------------------
22+
int
23+
main()
24+
{
25+
initialize();
26+
27+
LedOrange::set();
28+
LedRed::set();
29+
30+
while (true)
31+
{
32+
LedBlue::toggle();
33+
LedGreen::toggle();
34+
LedOrange::toggle();
35+
LedRed::toggle();
36+
modm::delay(Button::read() ? 250ms : 500ms);
37+
}
38+
39+
return 0;
40+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<library>
2+
<extends>modm:disco-f401vc</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/stm32f401_discovery/blink</option>
5+
</options>
6+
<modules>
7+
<module>modm:build:scons</module>
8+
</modules>
9+
</library>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2014-2018, Niklas Hauser
3+
* Copyright (c) 2015, Kevin Läufer
4+
* Copyright (c) 2015, Martin Esser
5+
* Copyright (c) 2018, Christopher Durand
6+
* Copyright (c) 2024, Carl Treudler
7+
*
8+
* This file is part of the modm project.
9+
*
10+
* This Source Code Form is subject to the terms of the Mozilla Public
11+
* License, v. 2.0. If a copy of the MPL was not distributed with this
12+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
13+
*/
14+
// ----------------------------------------------------------------------------
15+
16+
#include <modm/board.hpp>
17+
#include <modm/driver/inertial/l3gd20.hpp>
18+
#include <modm/processing.hpp>
19+
#include <modm/math/filter.hpp>
20+
21+
// maps arbitrary gpios to a bit
22+
using LedRing = SoftwareGpioPort<
23+
Board::LedOrange, // 3
24+
Board::LedRed, // 2
25+
Board::LedBlue, // 1
26+
Board::LedGreen // 0
27+
>;
28+
29+
// create the data object
30+
Board::l3g::Gyroscope::Data data;
31+
// and hand it to the sensor driver
32+
Board::l3g::Gyroscope gyro(data);
33+
34+
35+
class ReaderThread : public modm::pt::Protothread
36+
{
37+
public:
38+
bool
39+
update()
40+
{
41+
PT_BEGIN();
42+
43+
// initialize with limited range of 250 degrees per second
44+
PT_CALL(gyro.configure(gyro.Scale::Dps250));
45+
46+
while (true)
47+
{
48+
// read out the sensor
49+
PT_CALL(gyro.readRotation());
50+
51+
// update the moving average
52+
averageZ.update(gyro.getData().getZ());
53+
54+
{
55+
float value = averageZ.getValue();
56+
// normalize rotation and scale by 5 leds
57+
uint16_t leds = abs(value / 200 * 5);
58+
leds = (1ul << leds) - 1;
59+
60+
LedRing::write(leds);
61+
}
62+
63+
// repeat every 5 ms
64+
timeout.restart(5ms);
65+
PT_WAIT_UNTIL(timeout.isExpired());
66+
}
67+
68+
PT_END();
69+
}
70+
71+
private:
72+
modm::ShortTimeout timeout;
73+
modm::filter::MovingAverage<float, 25> averageZ;
74+
};
75+
76+
ReaderThread reader;
77+
78+
79+
int
80+
main()
81+
{
82+
Board::initialize();
83+
Board::initializeL3g();
84+
85+
while (true)
86+
{
87+
reader.update();
88+
}
89+
90+
return 0;
91+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<library>
2+
<extends>modm:disco-f401vc</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/stm32f401_discovery/gyroscope</option>
5+
</options>
6+
<modules>
7+
<module>modm:driver:l3gd20</module>
8+
<module>modm:math:filter</module>
9+
<module>modm:platform:spi:1</module>
10+
<module>modm:processing:timer</module>
11+
<module>modm:processing:protothread</module>
12+
<module>modm:build:scons</module>
13+
</modules>
14+
</library>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2011, Georgi Grinshpun
3+
* Copyright (c) 2011-2012, Fabian Greif
4+
* Copyright (c) 2012, 2014, Sascha Schade
5+
* Copyright (c) 2013, Kevin Läufer
6+
* Copyright (c) 2013, 2015-2017, Niklas Hauser
7+
* Copyright (c) 2024, Carl Treudler
8+
*
9+
* This file is part of the modm project.
10+
*
11+
* This Source Code Form is subject to the terms of the Mozilla Public
12+
* License, v. 2.0. If a copy of the MPL was not distributed with this
13+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
14+
*/
15+
// ----------------------------------------------------------------------------
16+
17+
#include <modm/board.hpp>
18+
19+
// ----------------------------------------------------------------------------
20+
/**
21+
* Very basic example of USART usage.
22+
* The ASCII sequence 'A', 'B', 'C', ... , 'Z', 'A', 'B', 'C', ...
23+
* is printed with 9600 baud, 8N1 at pin PA3.
24+
*/
25+
int
26+
main()
27+
{
28+
Board::initialize();
29+
30+
Board::LedRed::set();
31+
32+
// Enable USART 2
33+
Usart2::connect<GpioA2::Tx>();
34+
Usart2::initialize<Board::SystemClock, 9600_Bd>();
35+
36+
while (true)
37+
{
38+
static uint8_t c = 'A';
39+
Board::LedRed::toggle();
40+
Board::LedGreen::toggle();
41+
Usart2::write(c);
42+
++c;
43+
if (c > 'Z') {
44+
c = 'A';
45+
}
46+
modm::delay(500ms);
47+
}
48+
49+
return 0;
50+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<library>
2+
<extends>modm:disco-f401vc</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/stm32f401_discovery/uart</option>
5+
</options>
6+
<modules>
7+
<module>modm:platform:gpio</module>
8+
<module>modm:platform:uart:2</module>
9+
<module>modm:build:scons</module>
10+
</modules>
11+
</library>

0 commit comments

Comments
 (0)