Skip to content

Commit

Permalink
[example] Add BMI088 SPI example for Nucleo H723ZG
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-durand committed Jul 20, 2023
1 parent 93aea14 commit f25619d
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
105 changes: 105 additions & 0 deletions examples/nucleo_h723zg/bmi088/spi/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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 Spi = SpiMaster2_Dma<Dma1::Channel0, Dma1::Channel1>;
using CsGyro = GpioC0;
using CsAcc = GpioD6;
using Mosi = GpioC3;
using Miso = GpioC2;
using Sck = GpioD3;

using AccInt1 = GpioC8;
using GyroInt3 = GpioC9;

using Transport = modm::Bmi088SpiTransport<Spi, CsAcc, CsGyro>;
using Imu = modm::Bmi088<Transport>;
Imu imu;

void initializeImu()
{
AccInt1::setInput(AccInt1::InputType::PullDown);
GyroInt3::setInput(GyroInt3::InputType::PullDown);

constexpr bool selfTest = true;
bool initialized = imu.initialize(selfTest);
while (!initialized) {
MODM_LOG_ERROR << "Initialization failed, retrying ...\n";
initialized = imu.initialize(selfTest);
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();
Dma1::enable();
Spi::connect<Sck::Sck, Mosi::Mosi, Miso::Miso>();
Spi::initialize<Board::SystemClock, 9_MHz, 10_pct>();

MODM_LOG_INFO << "BMI088 SPI 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;
}
15 changes: 15 additions & 0 deletions examples/nucleo_h723zg/bmi088/spi/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<library>
<extends>modm:nucleo-h723zg</extends>
<options>
<option name="modm:build:build.path">../../../../build/nucleo_h723zg/bmi088_spi</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:dma</module>
<module>modm:platform:exti</module>
<module>modm:platform:spi:2</module>
<module>modm:driver:bmi088</module>
</modules>
</library>

0 comments on commit f25619d

Please sign in to comment.