Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[driver] IMU LSM6DSO #950

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -712,52 +712,53 @@ you specific needs.
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lp503x">LP503x</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm303a">LSM303A</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm6ds33">LSM6DS33</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm6dso">LSM6DSO</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ltc2984">LTC2984</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max31855">MAX31855</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max6966">MAX6966</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max6966">MAX6966</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max7219">MAX7219</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp23x17">MCP23x17</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp2515">MCP2515</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp7941x">MCP7941x</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp990x">MCP990X</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mmc5603">MMC5603</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mmc5603">MMC5603</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5611">MS5611</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nokia5110">NOKIA5110</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nrf24">NRF24</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-parallel_tft_display">TFT-DISPLAY</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pat9125el">PAT9125EL</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca8574">PCA8574</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca8574">PCA8574</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9535">PCA9535</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9548a">PCA9548A</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9685">PCA9685</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sh1106">SH1106</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s65">SIEMENS-S65</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s75">SIEMENS-S75</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s75">SIEMENS-S75</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk6812">SK6812</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk9822">SK9822</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306">SSD1306</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7586s">ST7586S</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7789">ST7789</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stts22h">STTS22H</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stts22h">STTS22H</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stusb4500">STUSB4500</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx1276">SX1276</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3414">TCS3414</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3472">TCS3472</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tlc594x">TLC594x</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp102">TMP102</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp102">TMP102</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp12x">TMP12x</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-touch2046">TOUCH2046</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl53l0">VL53L0</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl6180">VL6180</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ws2812">WS2812</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ws2812">WS2812</a></td>
</tr>
</table>
<!--/drivertable-->
Expand Down
118 changes: 118 additions & 0 deletions examples/nucleo_g474re/imu_lsm6dso/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (c) 2023, Raphael Lehmann
*
* 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/board.hpp>
#include <modm/platform/spi/spi_master_1.hpp>
#include <modm/driver/inertial/lsm6dso.hpp>
#include <optional>

using namespace Board;

using SpiMaster = SpiMaster1;
using Mosi = GpioA7;
using Miso = GpioA6;
using Sck = GpioA5;
using Cs = GpioC5;
using Int1 = GpioC4;
using ImuVdd = GpioA10;

using Lsm6dso = modm::Lsm6dso<modm::Lsm6TransportSpi<SpiMaster, Cs>>;

Lsm6dso imu{};

int
main()
{
Board::initialize();
Leds::setOutput();

MODM_LOG_INFO << "LSM6DSO IMU demo on Nucleo-G474RE\n" << modm::endl;

SpiMaster::initialize<Board::SystemClock, 5300_kHz>();
SpiMaster::connect<Sck::Sck, Mosi::Mosi, Miso::Miso>();
Int1::setInput(Gpio::InputType::PullUp);

// The imu behaves weird when the pins are temporarily unconnected (e.g.
// while changing jumper wires) or after reprogramming the nucleo board,
// and a software reset does not work.
// Therefore the IMU can be powered through a GPIO pin (Vdd) which we
// switch on and off to reboot it completely.
ImuVdd::reset();
ImuVdd::setOutput();
modm::delay(10ms);
// Power up imu
ImuVdd::set();

// Give the sensor some time to boot...
modm::delay(10ms);

MODM_LOG_INFO << "Resetting and rebooting imu (BOOT | SW_RESET)..." << modm::endl;
// Write bits BOOT | SW_RESET (and IF_INC, because it default to 1) to reboot imu
if(!RF_CALL_BLOCKING(imu.writeRegister(modm::lsm6dso::Register::CTRL3_C, 0b1000'0101))) {
MODM_LOG_INFO.printf(" ... failed.");
Board::Leds::set();
}
chris-durand marked this conversation as resolved.
Show resolved Hide resolved

MODM_LOG_INFO << "Initializing IMU..." << modm::endl;
RF_CALL_BLOCKING(imu.initialize());

MODM_LOG_INFO << "Enable IMU by configuring data rate and range" << modm::endl;
if(!RF_CALL_BLOCKING(imu.setOutputDataRateAndRange<12_Hz, 5_pct>(
modm::lsm6dso::LinearRange::G2,
modm::lsm6dso::AngularRange::dps125))) {
MODM_LOG_INFO.printf(" ... failed.");
Board::Leds::set();
}

// Reading CTRL1_XL register
std::optional<uint8_t> reg = RF_CALL_BLOCKING(imu.readRegister(modm::lsm6dso::Register::CTRL1_XL));
if (reg.has_value()) {
MODM_LOG_INFO << "Reading CTRL1_XL register = " << *reg << modm::endl;
}
else {
MODM_LOG_INFO << "Unable to CTRL1_XL register." << modm::endl;
}

MODM_LOG_INFO << "Enable accelerometer and gyroscope data-ready interrupts on INT1 pin" << modm::endl;
if(!RF_CALL_BLOCKING(imu.writeRegister(modm::lsm6dso::Register::INT1_CTRL, 0b0000'0011))) {
MODM_LOG_INFO.printf(" ... failed.");
Board::Leds::set();
}

std::array<uint8_t, 14> data;

while (true)
{
MODM_LOG_INFO.printf("\n\nIMU data: ");

// Wait for rising edge
while (Int1::read()) {}
while (!Int1::read()) {}

if(!RF_CALL_BLOCKING(imu.readRegisters(modm::lsm6dso::Register::OUT_TEMP_L, data))) {
MODM_LOG_INFO.printf("failed. ");
Board::Leds::set();
}

// print data anyways
MODM_LOG_INFO.printf("TEMP=%+2.1f degC, ", (static_cast<int16_t>(data[0] | data[1] << 8) / 256.f) + 25.f);
MODM_LOG_INFO.printf("G_X=%+05i, ", static_cast<int16_t>(data[ 2] | data[ 3] << 8));
MODM_LOG_INFO.printf("G_Y=%+05i, ", static_cast<int16_t>(data[ 4] | data[ 5] << 8));
MODM_LOG_INFO.printf("G_Z=%+05i, ", static_cast<int16_t>(data[ 6] | data[ 7] << 8));
MODM_LOG_INFO.printf("A_X=%+05i, ", static_cast<int16_t>(data[ 8] | data[ 9] << 8));
MODM_LOG_INFO.printf("A_Y=%+05i, ", static_cast<int16_t>(data[10] | data[11] << 8));
MODM_LOG_INFO.printf("A_Z=%+05i, ", static_cast<int16_t>(data[12] | data[13] << 8));

Board::Leds::toggle();
}

return 0;
}
11 changes: 11 additions & 0 deletions examples/nucleo_g474re/imu_lsm6dso/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library>
<extends>modm:nucleo-g474re</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_g474re/imu_lsm6dso</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:spi:1</module>
<module>modm:driver:lsm6dso</module>
</modules>
</library>
Loading