Skip to content

Commit 06c0700

Browse files
committed
[example] Add BMI088 SPI example for Nucleo H723ZG
1 parent f1f4f86 commit 06c0700

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 2023, Christopher Durand
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <modm/driver/inertial/bmi088.hpp>
13+
14+
#include <modm/board.hpp>
15+
#include <atomic>
16+
17+
using namespace Board;
18+
19+
using Spi = SpiMaster2_Dma<Dma1::Channel0, Dma1::Channel1>;
20+
using CsGyro = GpioC0;
21+
using CsAcc = GpioD6;
22+
using Mosi = GpioC3;
23+
using Miso = GpioC2;
24+
using Sck = GpioD3;
25+
26+
using AccInt1 = GpioC8;
27+
using GyroInt3 = GpioC9;
28+
29+
using Transport = modm::Bmi088SpiTransport<Spi, CsAcc, CsGyro>;
30+
using Imu = modm::Bmi088<Transport>;
31+
Imu imu;
32+
33+
void initializeImu()
34+
{
35+
AccInt1::setInput(AccInt1::InputType::PullDown);
36+
GyroInt3::setInput(GyroInt3::InputType::PullDown);
37+
38+
constexpr bool selfTest = true;
39+
while (!imu.initialize(selfTest)) {
40+
MODM_LOG_ERROR << "Initialization failed, retrying ...\n";
41+
modm::delay(500ms);
42+
}
43+
44+
bool ok = imu.setAccRate(Imu::AccRate::Rate12Hz_Bw5Hz);
45+
ok &= imu.setAccRange(Imu::AccRange::Range3g);
46+
47+
const auto int1Config = (Imu::AccGpioConfig::ActiveHigh | Imu::AccGpioConfig::EnableOutput);
48+
ok &= imu.setAccInt1GpioConfig(int1Config);
49+
ok &= imu.setAccGpioMap(Imu::AccGpioMap::Int1DataReady);
50+
51+
ok &= imu.setGyroRate(Imu::GyroRate::Rate100Hz_Bw12Hz);
52+
ok &= imu.setGyroRange(Imu::GyroRange::Range250dps);
53+
ok &= imu.setGyroGpioConfig(Imu::GyroGpioConfig::Int3ActiveHigh);
54+
ok &= imu.setGyroGpioMap(Imu::GyroGpioMap::Int3DataReady);
55+
56+
if (!ok) {
57+
MODM_LOG_ERROR << "Configuration failed!\n";
58+
}
59+
}
60+
61+
int main()
62+
{
63+
Board::initialize();
64+
Leds::setOutput();
65+
Dma1::enable();
66+
Spi::connect<Sck::Sck, Mosi::Mosi, Miso::Miso>();
67+
Spi::initialize<Board::SystemClock, 9_MHz, 10_pct>();
68+
69+
MODM_LOG_INFO << "BMI088 SPI Test\n";
70+
initializeImu();
71+
72+
std::atomic_bool accReady = false;
73+
std::atomic_bool gyroReady = false;
74+
75+
Exti::connect<AccInt1>(Exti::Trigger::RisingEdge, [&accReady](auto){
76+
accReady = true;
77+
});
78+
79+
Exti::connect<GyroInt3>(Exti::Trigger::RisingEdge, [&gyroReady](auto){
80+
gyroReady = true;
81+
});
82+
83+
while (true)
84+
{
85+
while(!accReady or !gyroReady);
86+
87+
const std::optional accResult = imu.readAccData();
88+
accReady = false;
89+
const std::optional gyroResult = imu.readGyroData();
90+
gyroReady = false;
91+
92+
if (accResult) {
93+
const modm::Vector3f data = accResult->getFloat();
94+
MODM_LOG_INFO.printf("Acc [mg]\tx:\t%5.1f\ty: %5.1f\tz: %5.1f\n", data[0], data[1], data[2]);
95+
}
96+
if (gyroResult) {
97+
const modm::Vector3f data = gyroResult->getFloat();
98+
MODM_LOG_INFO.printf("Gyro [deg/s]\tx:\t%5.2f\ty: %5.2f\tz: %5.2f\n", data[0], data[1], data[2]);
99+
}
100+
}
101+
102+
return 0;
103+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<library>
2+
<extends>modm:nucleo-h723zg</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../../build/nucleo_h723zg/bmi088_spi</option>
5+
<option name="modm:processing:protothread:use_fiber">yes</option>
6+
</options>
7+
<modules>
8+
<module>modm:build:scons</module>
9+
<module>modm:processing:fiber</module>
10+
<module>modm:platform:dma</module>
11+
<module>modm:platform:exti</module>
12+
<module>modm:platform:spi:2</module>
13+
<module>modm:driver:bmi088</module>
14+
</modules>
15+
</library>

0 commit comments

Comments
 (0)