From a771042eb5d503acdae5ac18d08c61932b7c3618 Mon Sep 17 00:00:00 2001 From: Christopher Durand Date: Wed, 4 Oct 2023 22:44:50 +0200 Subject: [PATCH] [example] Add BMI088 I2C example for Nucleo H723ZG --- examples/nucleo_h723zg/bmi088/i2c/main.cpp | 102 ++++++++++++++++++ examples/nucleo_h723zg/bmi088/i2c/project.xml | 14 +++ 2 files changed, 116 insertions(+) create mode 100644 examples/nucleo_h723zg/bmi088/i2c/main.cpp create mode 100644 examples/nucleo_h723zg/bmi088/i2c/project.xml diff --git a/examples/nucleo_h723zg/bmi088/i2c/main.cpp b/examples/nucleo_h723zg/bmi088/i2c/main.cpp new file mode 100644 index 0000000000..17b6847d17 --- /dev/null +++ b/examples/nucleo_h723zg/bmi088/i2c/main.cpp @@ -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 + +#include +#include + +using namespace Board; + +using I2c = I2cMaster1; +using Scl = GpioB8; // D15 +using Sda = GpioB9; // D14 + +using AccInt1 = GpioC6; +using GyroInt3 = GpioB15; + +using Transport = modm::Bmi088I2cTransport; +using Imu = modm::Bmi088; + +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(I2c::PullUps::Internal); + I2c::initialize(); + + MODM_LOG_INFO << "BMI088 I2C Test\n"; + initializeImu(); + + std::atomic_bool accReady = false; + std::atomic_bool gyroReady = false; + + Exti::connect(Exti::Trigger::RisingEdge, [&accReady](auto){ + accReady = true; + }); + + Exti::connect(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; +} diff --git a/examples/nucleo_h723zg/bmi088/i2c/project.xml b/examples/nucleo_h723zg/bmi088/i2c/project.xml new file mode 100644 index 0000000000..797c04a836 --- /dev/null +++ b/examples/nucleo_h723zg/bmi088/i2c/project.xml @@ -0,0 +1,14 @@ + + modm:nucleo-h723zg + + + + + + modm:build:scons + modm:processing:fiber + modm:platform:exti + modm:platform:i2c:1 + modm:driver:bmi088 + +