Skip to content

Commit a771042

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

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 I2c = I2cMaster1;
20+
using Scl = GpioB8; // D15
21+
using Sda = GpioB9; // D14
22+
23+
using AccInt1 = GpioC6;
24+
using GyroInt3 = GpioB15;
25+
26+
using Transport = modm::Bmi088I2cTransport<I2c>;
27+
using Imu = modm::Bmi088<Transport>;
28+
29+
constexpr uint8_t AccAddress = 0x18;
30+
constexpr uint8_t GyroAddress = 0x68;
31+
Imu imu{AccAddress, GyroAddress};
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+
I2c::connect<Scl::Scl, Sda::Sda>(I2c::PullUps::Internal);
66+
I2c::initialize<Board::SystemClock, 100_kHz, 10_pct>();
67+
68+
MODM_LOG_INFO << "BMI088 I2C Test\n";
69+
initializeImu();
70+
71+
std::atomic_bool accReady = false;
72+
std::atomic_bool gyroReady = false;
73+
74+
Exti::connect<AccInt1>(Exti::Trigger::RisingEdge, [&accReady](auto){
75+
accReady = true;
76+
});
77+
78+
Exti::connect<GyroInt3>(Exti::Trigger::RisingEdge, [&gyroReady](auto){
79+
gyroReady = true;
80+
});
81+
82+
while (true)
83+
{
84+
while(!accReady or !gyroReady);
85+
86+
const std::optional accResult = imu.readAccData();
87+
accReady = false;
88+
const std::optional gyroResult = imu.readGyroData();
89+
gyroReady = false;
90+
91+
if (accResult) {
92+
const modm::Vector3f data = accResult->getFloat();
93+
MODM_LOG_INFO.printf("Acc [mg]\tx:\t%5.1f\ty: %5.1f\tz: %5.1f\n", data[0], data[1], data[2]);
94+
}
95+
if (gyroResult) {
96+
const modm::Vector3f data = gyroResult->getFloat();
97+
MODM_LOG_INFO.printf("Gyro [deg/s]\tx:\t%5.2f\ty: %5.2f\tz: %5.2f\n", data[0], data[1], data[2]);
98+
}
99+
}
100+
101+
return 0;
102+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<library>
2+
<extends>modm:nucleo-h723zg</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../../build/nucleo_h723zg/bmi088_i2c</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:exti</module>
11+
<module>modm:platform:i2c:1</module>
12+
<module>modm:driver:bmi088</module>
13+
</modules>
14+
</library>

0 commit comments

Comments
 (0)