Skip to content

Commit fbf5501

Browse files
committed
[driver] bmi270 fixes for PR review
1 parent 9f40a69 commit fbf5501

File tree

10 files changed

+135
-175
lines changed

10 files changed

+135
-175
lines changed

README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -957,99 +957,100 @@ your specific needs.
957957
<td align="center"><a href="https://modm.io/reference/module/modm-driver-block-device-spi-flash">SPI Flash</a></td>
958958
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bme280">BME280</a></td>
959959
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bmi088">BMI088</a></td>
960-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bmp085">BMP085</a></td>
960+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bmi270">BMI270</a></td>
961961
</tr><tr>
962+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bmp085">BMP085</a></td>
962963
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bmp581">BMP581</a></td>
963964
<td align="center"><a href="https://modm.io/reference/module/modm-driver-bno055">BNO055</a></td>
964965
<td align="center"><a href="https://modm.io/reference/module/modm-driver-cat24aa">CAT24AA</a></td>
965966
<td align="center"><a href="https://modm.io/reference/module/modm-driver-cycle_counter">CYCLE-COUNTER</a></td>
966967
<td align="center"><a href="https://modm.io/reference/module/modm-driver-drv832x_spi">DRV832X</a></td>
967-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds1302">DS1302</a></td>
968968
</tr><tr>
969+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds1302">DS1302</a></td>
969970
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds1631">DS1631</a></td>
970971
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds18b20">DS18B20</a></td>
971972
<td align="center"><a href="https://modm.io/reference/module/modm-driver-dw3110">DW3110</a></td>
972973
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ea_dog">EA-DOG</a></td>
973974
<td align="center"><a href="https://modm.io/reference/module/modm-driver-encoder_input">Encoder Input</a></td>
974-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-encoder_input-bitbang">Encoder Input BitBang</a></td>
975975
</tr><tr>
976+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-encoder_input-bitbang">Encoder Input BitBang</a></td>
976977
<td align="center"><a href="https://modm.io/reference/module/modm-driver-encoder_output-bitbang">Encoder Output BitBang</a></td>
977978
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ft245">FT245</a></td>
978979
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ft6x06">FT6x06</a></td>
979980
<td align="center"><a href="https://modm.io/reference/module/modm-driver-gpio_sampler">Gpio Sampler</a></td>
980981
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hclax">HCLAx</a></td>
981-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hd44780">HD44780</a></td>
982982
</tr><tr>
983+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hd44780">HD44780</a></td>
983984
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hmc58x">HMC58x</a></td>
984985
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hmc6343">HMC6343</a></td>
985986
<td align="center"><a href="https://modm.io/reference/module/modm-driver-hx711">HX711</a></td>
986987
<td align="center"><a href="https://modm.io/reference/module/modm-driver-i2c-eeprom">I2C-EEPROM</a></td>
987988
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ili9341">ILI9341</a></td>
988-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-is31fl3733">IS31FL3733</a></td>
989989
</tr><tr>
990+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-is31fl3733">IS31FL3733</a></td>
990991
<td align="center"><a href="https://modm.io/reference/module/modm-driver-itg3200">ITG3200</a></td>
991992
<td align="center"><a href="https://modm.io/reference/module/modm-driver-iwr6843aop">IWR6843AOP</a></td>
992993
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ixm42xxx">IXM42XXX</a></td>
993994
<td align="center"><a href="https://modm.io/reference/module/modm-driver-l3gd20">L3GD20</a></td>
994995
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lan8720a">LAN8720A</a></td>
995-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lawicel">LAWICEL</a></td>
996996
</tr><tr>
997+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lawicel">LAWICEL</a></td>
997998
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lis302dl">LIS302DL</a></td>
998999
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lis3dsh">LIS3DSH</a></td>
9991000
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lis3mdl">LIS3MDL</a></td>
10001001
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lm75">LM75</a></td>
10011002
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lp503x">LP503x</a></td>
1002-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm303a">LSM303A</a></td>
10031003
</tr><tr>
1004+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm303a">LSM303A</a></td>
10041005
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm6ds33">LSM6DS33</a></td>
10051006
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm6dso">LSM6DSO</a></td>
10061007
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ltc2984">LTC2984</a></td>
10071008
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max31855">MAX31855</a></td>
10081009
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max31865">MAX31865</a></td>
1009-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max6966">MAX6966</a></td>
10101010
</tr><tr>
1011+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max6966">MAX6966</a></td>
10111012
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max7219">MAX7219</a></td>
10121013
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp23x17">MCP23x17</a></td>
10131014
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp2515">MCP2515</a></td>
10141015
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp3008">MCP3008</a></td>
10151016
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp7941x">MCP7941x</a></td>
1016-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp990x">MCP990X</a></td>
10171017
</tr><tr>
1018+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp990x">MCP990X</a></td>
10181019
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mmc5603">MMC5603</a></td>
10191020
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5611">MS5611</a></td>
10201021
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5837">MS5837</a></td>
10211022
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nokia5110">NOKIA5110</a></td>
10221023
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nrf24">NRF24</a></td>
1023-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-parallel_tft_display">TFT-DISPLAY</a></td>
10241024
</tr><tr>
1025+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-parallel_tft_display">TFT-DISPLAY</a></td>
10251026
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pat9125el">PAT9125EL</a></td>
10261027
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca8574">PCA8574</a></td>
10271028
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9535">PCA9535</a></td>
10281029
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9548a">PCA9548A</a></td>
10291030
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9685">PCA9685</a></td>
1030-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-qmc5883l">QMC5883L</a></td>
10311031
</tr><tr>
1032+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-qmc5883l">QMC5883L</a></td>
10321033
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sh1106">SH1106</a></td>
10331034
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s65">SIEMENS-S65</a></td>
10341035
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s75">SIEMENS-S75</a></td>
10351036
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk6812">SK6812</a></td>
10361037
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk9822">SK9822</a></td>
1037-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306">SSD1306</a></td>
10381038
</tr><tr>
1039+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306">SSD1306</a></td>
10391040
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7586s">ST7586S</a></td>
10401041
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7789">ST7789</a></td>
10411042
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stts22h">STTS22H</a></td>
10421043
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stusb4500">STUSB4500</a></td>
10431044
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx1276">SX1276</a></td>
1044-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx128x">SX128X</a></td>
10451045
</tr><tr>
1046+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx128x">SX128X</a></td>
10461047
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3414">TCS3414</a></td>
10471048
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3472">TCS3472</a></td>
10481049
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tlc594x">TLC594x</a></td>
10491050
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp102">TMP102</a></td>
10501051
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp12x">TMP12x</a></td>
1051-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
10521052
</tr><tr>
1053+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
10531054
<td align="center"><a href="https://modm.io/reference/module/modm-driver-touch2046">TOUCH2046</a></td>
10541055
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl53l0">VL53L0</a></td>
10551056
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl6180">VL6180</a></td>

examples/nucleo_h723zg/bmi270/calibration/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using Sda = GpioB9; // D14
2222
using Transport = modm::Bmi270I2cTransport<I2c>;
2323
using Imu = modm::Bmi270<Transport>;
2424

25-
Imu imu{static_cast<uint8_t>(0x68)};
25+
Imu imu{Transport::I2cAddress::SdoLow};
2626

2727
namespace
2828
{
@@ -71,7 +71,7 @@ main()
7171
{
7272
Board::initialize();
7373
Leds::setOutput();
74-
I2c::connect<Scl::Scl, Sda::Sda>(I2c::PullUps::Internal);
74+
I2c::connect<Scl::Scl, Sda::Sda>(I2c::PullUps::External);
7575
I2c::initialize<Board::SystemClock, 1_MHz, 10_pct>();
7676

7777
MODM_LOG_INFO << "BMI270 calibration example (CRT + gyro user gain restore)" << modm::endl;

examples/nucleo_h723zg/bmi270/i2c/main.cpp

Lines changed: 81 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
*/
1010
// ----------------------------------------------------------------------------
1111

12-
#include <atomic>
1312
#include <cstdint>
1413
#include <modm/board.hpp>
1514
#include <modm/driver/inertial/bmi270.hpp>
15+
#include <modm/processing/timer.hpp>
1616

1717
using namespace Board;
1818

@@ -26,71 +26,7 @@ using Int2 = GpioF3; // D9
2626
using Transport = modm::Bmi270I2cTransport<I2c>;
2727
using Imu = modm::Bmi270<Transport>;
2828

29-
Imu imu{static_cast<uint8_t>(0x68)};
30-
31-
std::atomic<uint32_t> accInterruptCount{0};
32-
std::atomic<uint32_t> gyroInterruptCount{0};
33-
34-
std::atomic<int32_t> accX{0};
35-
std::atomic<int32_t> accY{0};
36-
std::atomic<int32_t> accZ{0};
37-
38-
std::atomic<int32_t> gyroX{0};
39-
std::atomic<int32_t> gyroY{0};
40-
std::atomic<int32_t> gyroZ{0};
41-
42-
std::atomic_flag imuBusLock = ATOMIC_FLAG_INIT;
43-
44-
bool
45-
tryLockImuBus()
46-
{
47-
return !imuBusLock.test_and_set(std::memory_order_acquire);
48-
}
49-
50-
void
51-
unlockImuBus()
52-
{
53-
imuBusLock.clear(std::memory_order_release);
54-
}
55-
56-
void
57-
onDataReadyInterrupt()
58-
{
59-
if (!tryLockImuBus()) { return; }
60-
61-
const auto status = imu.getStatus();
62-
if (!status)
63-
{
64-
unlockImuBus();
65-
return;
66-
}
67-
68-
if (status->accDataReady)
69-
{
70-
const auto data = imu.readAccData();
71-
if (data)
72-
{
73-
accInterruptCount.fetch_add(1, std::memory_order_relaxed);
74-
accX.store(data->raw[0], std::memory_order_relaxed);
75-
accY.store(data->raw[1], std::memory_order_relaxed);
76-
accZ.store(data->raw[2], std::memory_order_relaxed);
77-
}
78-
}
79-
80-
if (status->gyroDataReady)
81-
{
82-
const auto data = imu.readGyroData();
83-
if (data)
84-
{
85-
gyroInterruptCount.fetch_add(1, std::memory_order_relaxed);
86-
gyroX.store(data->raw[0], std::memory_order_relaxed);
87-
gyroY.store(data->raw[1], std::memory_order_relaxed);
88-
gyroZ.store(data->raw[2], std::memory_order_relaxed);
89-
}
90-
}
91-
92-
unlockImuBus();
93-
}
29+
Imu imu{Transport::I2cAddress::SdoLow};
9430

9531
bool
9632
configureDriver()
@@ -117,15 +53,15 @@ configureDriver()
11753
Imu::InterruptIoControl int2{};
11854
int2.level = Imu::InterruptOutputLevel::ActiveHigh;
11955
int2.outputType = Imu::InterruptOutputType::PushPull;
120-
int2.outputEnable = false;
56+
int2.outputEnable = true;
12157
int2.inputEnable = false;
12258
ok &= imu.setInt2IoControl(int2);
12359

12460
ok &= imu.setInterruptLatch(Imu::InterruptLatch::None);
12561

12662
Imu::InterruptMapData intMap{};
12763
intMap.int1DataReady = true;
128-
intMap.int2DataReady = false;
64+
intMap.int2DataReady = true;
12965
ok &= imu.setInterruptMapData(intMap);
13066

13167
ok &= imu.setPowerControl(Imu::PowerControl::Accelerometer | Imu::PowerControl::Gyroscope |
@@ -138,75 +74,99 @@ main()
13874
{
13975
Board::initialize();
14076
Leds::setOutput();
141-
I2c::connect<Scl::Scl, Sda::Sda>(I2c::PullUps::Internal);
77+
I2c::connect<Scl::Scl, Sda::Sda>(I2c::PullUps::External);
14278
I2c::initialize<Board::SystemClock, 1_MHz, 10_pct>();
14379
Int1::setInput(Int1::InputType::PullDown);
14480
Int2::setInput(Int2::InputType::PullDown);
14581

146-
MODM_LOG_INFO << "BMI270 I2C interrupt example" << modm::endl;
82+
MODM_LOG_INFO << "BMI270 I2C polling example" << modm::endl;
14783

14884
if (!configureDriver()) { MODM_LOG_ERROR << "Configuration failed!" << modm::endl; }
14985

150-
Exti::connect<Int1>(Exti::Trigger::RisingEdge, [](auto) { onDataReadyInterrupt(); });
86+
uint32_t accSampleCount{0};
87+
uint32_t gyroSampleCount{0};
88+
modm::PeriodicTimer printTimer{1s};
89+
90+
Imu::AccData latestAccData{};
91+
latestAccData.raw = modm::Vector3i(0, 0, 0);
92+
latestAccData.range = Imu::AccRange::Range2g;
93+
94+
Imu::GyroData latestGyroData{};
95+
latestGyroData.raw = modm::Vector3i(0, 0, 0);
96+
latestGyroData.range = Imu::GyroRange::Range2000dps;
97+
98+
std::optional<Imu::SensorStatus> latestStatus;
15199

152100
while (true)
153101
{
154-
modm::this_fiber::sleep_for(1s);
155-
156-
const uint32_t accCount = accInterruptCount.exchange(0, std::memory_order_relaxed);
157-
const uint32_t gyroCount = gyroInterruptCount.exchange(0, std::memory_order_relaxed);
158-
159-
Imu::AccData accData{};
160-
accData.raw = modm::Vector3i(accX.load(std::memory_order_relaxed),
161-
accY.load(std::memory_order_relaxed),
162-
accZ.load(std::memory_order_relaxed));
163-
accData.range = Imu::AccRange::Range2g;
164-
165-
Imu::GyroData gyroData{};
166-
gyroData.raw = modm::Vector3i(gyroX.load(std::memory_order_relaxed),
167-
gyroY.load(std::memory_order_relaxed),
168-
gyroZ.load(std::memory_order_relaxed));
169-
gyroData.range = Imu::GyroRange::Range2000dps;
170-
171-
const modm::Vector3f acc = accData.getFloat();
172-
const modm::Vector3f gyro = gyroData.getFloat();
173-
174-
std::optional<Imu::Temperature> temperature;
175-
std::optional<Imu::SensorStatus> status;
176-
while (!tryLockImuBus()) { modm::this_fiber::sleep_for(50us); }
177-
temperature = imu.getTemperature();
178-
status = imu.getStatus();
179-
unlockImuBus();
180-
181-
MODM_LOG_INFO << "Interrupts in last 1s: acc=" << accCount << " gyro=" << gyroCount
182-
<< modm::endl;
183-
184-
MODM_LOG_INFO << "Latest Values Acc [mg] x: " << acc[0] << " y: " << acc[1]
185-
<< " z: " << acc[2] << modm::endl;
186-
MODM_LOG_INFO << "Latest Values Gyro [deg/s] x: " << gyro[0] << " y: " << gyro[1]
187-
<< " z: " << gyro[2] << modm::endl;
188-
189-
if (temperature and temperature->valid)
102+
if (Int1::read())
190103
{
191-
MODM_LOG_INFO << "Current Temperature [C]: " << temperature->celsius << modm::endl;
192-
} else
193-
{
194-
MODM_LOG_INFO << "Temperature: invalid" << modm::endl;
104+
if (const auto status = imu.getStatus())
105+
{
106+
latestStatus = status;
107+
108+
if (status->accDataReady)
109+
{
110+
if (const auto accData = imu.readAccData())
111+
{
112+
latestAccData = *accData;
113+
++accSampleCount;
114+
}
115+
}
116+
117+
if (status->gyroDataReady)
118+
{
119+
if (const auto gyroData = imu.readGyroData())
120+
{
121+
latestGyroData = *gyroData;
122+
++gyroSampleCount;
123+
}
124+
}
125+
}
195126
}
196127

197-
if (status)
198-
{
199-
MODM_LOG_INFO << "Current Status: acc=" << status->accDataReady
200-
<< " gyro=" << status->gyroDataReady << " aux=" << status->auxDataReady
201-
<< " cmd=" << status->commandReady << " auxBusy=" << status->auxBusy
202-
<< modm::endl;
203-
} else
128+
if (printTimer.execute())
204129
{
205-
MODM_LOG_INFO << "Status: unavailable" << modm::endl;
130+
const modm::Vector3f acc = latestAccData.getFloat();
131+
const modm::Vector3f gyro = latestGyroData.getFloat();
132+
const std::optional<Imu::Temperature> temperature = imu.getTemperature();
133+
134+
MODM_LOG_INFO << "Data-ready events in last 1s: acc=" << accSampleCount
135+
<< " gyro=" << gyroSampleCount << modm::endl;
136+
137+
accSampleCount = 0;
138+
gyroSampleCount = 0;
139+
140+
MODM_LOG_INFO << "Latest Values Acc [mg] x: " << acc[0] << " y: " << acc[1]
141+
<< " z: " << acc[2] << modm::endl;
142+
MODM_LOG_INFO << "Latest Values Gyro [deg/s] x: " << gyro[0] << " y: " << gyro[1]
143+
<< " z: " << gyro[2] << modm::endl;
144+
145+
if (temperature and temperature->valid)
146+
{
147+
MODM_LOG_INFO << "Current Temperature [C]: " << temperature->celsius << modm::endl;
148+
} else
149+
{
150+
MODM_LOG_INFO << "Temperature: invalid" << modm::endl;
151+
}
152+
153+
if (latestStatus)
154+
{
155+
MODM_LOG_INFO << "Current Status: acc=" << latestStatus->accDataReady
156+
<< " gyro=" << latestStatus->gyroDataReady
157+
<< " aux=" << latestStatus->auxDataReady
158+
<< " cmd=" << latestStatus->commandReady
159+
<< " auxBusy=" << latestStatus->auxBusy << modm::endl;
160+
} else
161+
{
162+
MODM_LOG_INFO << "Status: unavailable" << modm::endl;
163+
}
164+
MODM_LOG_INFO << modm::endl;
165+
166+
Board::LedGreen::toggle();
206167
}
207-
MODM_LOG_INFO << modm::endl;
208168

209-
Board::LedGreen::toggle();
169+
modm::this_fiber::yield();
210170
}
211171

212172
return 0;

examples/nucleo_h723zg/bmi270/i2c/project.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
</options>
66
<modules>
77
<module>modm:build:scons</module>
8-
<module>modm:platform:exti</module>
98
<module>modm:platform:i2c:1</module>
109
<module>modm:driver:bmi270</module>
10+
<module>modm:processing:fiber</module>
1111
</modules>
1212
</library>

0 commit comments

Comments
 (0)