-
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 BMI088 I2C example for Nucleo H723ZG
- Loading branch information
1 parent
06c0700
commit a771042
Showing
2 changed files
with
116 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (c) 2023, Christopher Durand | ||
* | ||
* 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/driver/inertial/bmi088.hpp> | ||
|
||
#include <modm/board.hpp> | ||
#include <atomic> | ||
|
||
using namespace Board; | ||
|
||
using I2c = I2cMaster1; | ||
using Scl = GpioB8; // D15 | ||
using Sda = GpioB9; // D14 | ||
|
||
using AccInt1 = GpioC6; | ||
using GyroInt3 = GpioB15; | ||
|
||
using Transport = modm::Bmi088I2cTransport<I2c>; | ||
using Imu = modm::Bmi088<Transport>; | ||
|
||
constexpr uint8_t AccAddress = 0x18; | ||
constexpr uint8_t GyroAddress = 0x68; | ||
Imu imu{AccAddress, GyroAddress}; | ||
|
||
void initializeImu() | ||
{ | ||
AccInt1::setInput(AccInt1::InputType::PullDown); | ||
GyroInt3::setInput(GyroInt3::InputType::PullDown); | ||
|
||
constexpr bool selfTest = true; | ||
while (!imu.initialize(selfTest)) { | ||
MODM_LOG_ERROR << "Initialization failed, retrying ...\n"; | ||
modm::delay(500ms); | ||
} | ||
|
||
bool ok = imu.setAccRate(Imu::AccRate::Rate12Hz_Bw5Hz); | ||
ok &= imu.setAccRange(Imu::AccRange::Range3g); | ||
|
||
const auto int1Config = (Imu::AccGpioConfig::ActiveHigh | Imu::AccGpioConfig::EnableOutput); | ||
ok &= imu.setAccInt1GpioConfig(int1Config); | ||
ok &= imu.setAccGpioMap(Imu::AccGpioMap::Int1DataReady); | ||
|
||
ok &= imu.setGyroRate(Imu::GyroRate::Rate100Hz_Bw12Hz); | ||
ok &= imu.setGyroRange(Imu::GyroRange::Range250dps); | ||
ok &= imu.setGyroGpioConfig(Imu::GyroGpioConfig::Int3ActiveHigh); | ||
ok &= imu.setGyroGpioMap(Imu::GyroGpioMap::Int3DataReady); | ||
|
||
if (!ok) { | ||
MODM_LOG_ERROR << "Configuration failed!\n"; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
Board::initialize(); | ||
Leds::setOutput(); | ||
I2c::connect<Scl::Scl, Sda::Sda>(I2c::PullUps::Internal); | ||
I2c::initialize<Board::SystemClock, 100_kHz, 10_pct>(); | ||
|
||
MODM_LOG_INFO << "BMI088 I2C Test\n"; | ||
initializeImu(); | ||
|
||
std::atomic_bool accReady = false; | ||
std::atomic_bool gyroReady = false; | ||
|
||
Exti::connect<AccInt1>(Exti::Trigger::RisingEdge, [&accReady](auto){ | ||
accReady = true; | ||
}); | ||
|
||
Exti::connect<GyroInt3>(Exti::Trigger::RisingEdge, [&gyroReady](auto){ | ||
gyroReady = true; | ||
}); | ||
|
||
while (true) | ||
{ | ||
while(!accReady or !gyroReady); | ||
|
||
const std::optional accResult = imu.readAccData(); | ||
accReady = false; | ||
const std::optional gyroResult = imu.readGyroData(); | ||
gyroReady = false; | ||
|
||
if (accResult) { | ||
const modm::Vector3f data = accResult->getFloat(); | ||
MODM_LOG_INFO.printf("Acc [mg]\tx:\t%5.1f\ty: %5.1f\tz: %5.1f\n", data[0], data[1], data[2]); | ||
} | ||
if (gyroResult) { | ||
const modm::Vector3f data = gyroResult->getFloat(); | ||
MODM_LOG_INFO.printf("Gyro [deg/s]\tx:\t%5.2f\ty: %5.2f\tz: %5.2f\n", data[0], data[1], data[2]); | ||
} | ||
} | ||
|
||
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:nucleo-h723zg</extends> | ||
<options> | ||
<option name="modm:build:build.path">../../../../build/nucleo_h723zg/bmi088_i2c</option> | ||
<option name="modm:processing:protothread:use_fiber">yes</option> | ||
</options> | ||
<modules> | ||
<module>modm:build:scons</module> | ||
<module>modm:processing:fiber</module> | ||
<module>modm:platform:exti</module> | ||
<module>modm:platform:i2c:1</module> | ||
<module>modm:driver:bmi088</module> | ||
</modules> | ||
</library> |