-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[example] Add STM32F401 Discovery examples
- Loading branch information
Showing
9 changed files
with
320 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |