Skip to content
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
1 change: 1 addition & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

CompileFlags:
CompilationDatabase: .
# clang do not understand this flags
Remove:
- -mlongcalls
Expand Down
9 changes: 6 additions & 3 deletions lib/Espfc/src/Device/Baro/BaroBMP085.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ int BaroBMP085::getDelay(BaroDeviceMode mode) const
{
switch (mode)
{
case BARO_MODE_TEMP: return 4550; // temp
case BARO_MODE_TEMP:
return 4550; // temp
default:
// return 4550; // press_0
// return 7550; // press_1
Expand All @@ -119,8 +120,10 @@ int BaroBMP085::getDelay(BaroDeviceMode mode) const
bool BaroBMP085::testConnection()
{
uint8_t whoami = 0;
_bus->readByte(_addr, BMP085_WHOAMI_REG, &whoami);
return whoami == BMP085_WHOAMI_ID;
if (_bus->readByte(_addr, BMP085_WHOAMI_REG, &whoami) != 1) return false;
setChipId(whoami);
if (whoami != BMP085_WHOAMI_ID) return false;
return true;
}

} // namespace Espfc::Device::Baro
7 changes: 5 additions & 2 deletions lib/Espfc/src/Device/Baro/BaroBMP280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ int BaroBMP280::getDelay(BaroDeviceMode mode) const
{
switch (mode)
{
case BARO_MODE_TEMP: return 0;
case BARO_MODE_TEMP:
return 0;
default:
// return 5500; // if sapling X1
// return 7500; // if sampling X2
Expand All @@ -132,7 +133,9 @@ bool BaroBMP280::testConnection()
{
uint8_t whoami = 0;
if (_bus->read(_addr, BMP280_WHOAMI_REG, 1, &whoami) != 1) return false;
return whoami == BMP280_WHOAMI_ID;
setChipId(whoami);
if (whoami != BMP280_WHOAMI_ID) return false;
return true;
}

void BaroBMP280::readMesurment()
Expand Down
7 changes: 5 additions & 2 deletions lib/Espfc/src/Device/Baro/BaroSPL06.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ int BaroSPL06::getDelay(BaroDeviceMode mode) const
{
switch (mode)
{
case BARO_MODE_TEMP: return 0;
case BARO_MODE_TEMP:
return 0;
default:
// return 5500; // if sapling X1
// return 7500; // if sampling X2
Expand All @@ -176,7 +177,9 @@ bool BaroSPL06::testConnection()
{
uint8_t whoami = 0;
if (_bus->read(_addr, SPL06_WHOAMI_REG, 1, &whoami) != 1) return false;
return whoami == SPL06_WHOAMI_ID;
setChipId(whoami);
if (whoami != SPL06_WHOAMI_ID) return false;
return true;
}

void BaroSPL06::readMesurment()
Expand Down
16 changes: 14 additions & 2 deletions lib/Espfc/src/Device/BusAwareDevice.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Device/BusDevice.hpp"
#include <optional>

namespace Espfc::Device {

Expand All @@ -23,9 +24,20 @@ class BusAwareDevice
return _addr;
}

std::optional<uint8_t> getChipId() const
{
return _chipId;
}

protected:
BusDevice* _bus;
uint8_t _addr;
void setChipId(uint8_t chipId)
{
_chipId = chipId;
}

BusDevice* _bus = nullptr;
uint8_t _addr = 0;
std::optional<uint8_t> _chipId = {};
};

} // namespace Espfc::Device
1 change: 1 addition & 0 deletions lib/Espfc/src/Device/Gyro/GyroBMI160.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ bool GyroBMI160::testConnection()
{
uint8_t whoami = 0;
if (_bus->readByte(_addr, BMI160_RA_CHIP_ID, &whoami) != 1) return false;
setChipId(whoami);
return whoami == BMI160_CHIP_ID_DEFAULT_VALUE;
}

Expand Down
7 changes: 4 additions & 3 deletions lib/Espfc/src/Device/Gyro/GyroICM20602.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "GyroICM20602.hpp"

#define MPU6050_RA_WHO_AM_I 0x75
#define ICM20602_RA_ACCEL2_CONFIG 0x1D
#define MPU6050_RA_WHO_AM_I 0x75
#define ICM20602_RA_ACCEL2_CONFIG 0x1D
#define ICM20602_WHOAMI_DEFAULT_VALUE 0x12

namespace Espfc::Device::Gyro {
Expand All @@ -20,7 +20,8 @@ void GyroICM20602::setDLPFMode(uint8_t mode)
bool GyroICM20602::testConnection()
{
uint8_t whoami = 0;
_bus->readByte(_addr, MPU6050_RA_WHO_AM_I, &whoami);
if (_bus->readByte(_addr, MPU6050_RA_WHO_AM_I, &whoami) != 1) return false;
setChipId(whoami);
return whoami == ICM20602_WHOAMI_DEFAULT_VALUE;
}

Expand Down
10 changes: 5 additions & 5 deletions lib/Espfc/src/Device/Gyro/GyroICM20602.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Espfc::Device::Gyro {

class GyroICM20602: public GyroMPU6050
class GyroICM20602 : public GyroMPU6050
{
public:
GyroDeviceType getType() const override;
public:
GyroDeviceType getType() const override;

void setDLPFMode(uint8_t mode) override;
void setDLPFMode(uint8_t mode) override;

bool testConnection() override;
bool testConnection() override;
};

} // namespace Espfc::Device::Gyro
27 changes: 14 additions & 13 deletions lib/Espfc/src/Device/Gyro/GyroICM42688.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
#include "Hal/Time.hpp"
#include "Utils/MemoryHelper.h"

#define ICM42688_REG_WHO_AM_I 0x75
#define ICM42688_REG_DEVICE_CONFIG 0x11
#define ICM42688_REG_PWR_MGMT0 0x4E
#define ICM42688_REG_GYRO_CONFIG0 0x4F
#define ICM42688_REG_ACCEL_CONFIG0 0x50
#define ICM42688_REG_ACCEL_DATA_X1 0x1F
#define ICM42688_REG_GYRO_DATA_X1 0x25

#define ICM42688_WHO_AM_I_VALUE 0x47
#define ICM42688_SOFT_RESET 0x01
#define ICM42688_PWR_GYRO_ACCEL_LN 0x0F
#define ICM42688_GYRO_2000DPS_8KHZ 0x03
#define ICM42688_ACCEL_16G_8KHZ 0x03
#define ICM42688_REG_WHO_AM_I 0x75
#define ICM42688_REG_DEVICE_CONFIG 0x11
#define ICM42688_REG_PWR_MGMT0 0x4E
#define ICM42688_REG_GYRO_CONFIG0 0x4F
#define ICM42688_REG_ACCEL_CONFIG0 0x50
#define ICM42688_REG_ACCEL_DATA_X1 0x1F
#define ICM42688_REG_GYRO_DATA_X1 0x25

#define ICM42688_WHO_AM_I_VALUE 0x47
#define ICM42688_SOFT_RESET 0x01
#define ICM42688_PWR_GYRO_ACCEL_LN 0x0F
#define ICM42688_GYRO_2000DPS_8KHZ 0x03
#define ICM42688_ACCEL_16G_8KHZ 0x03

namespace Espfc::Device::Gyro {

Expand Down Expand Up @@ -85,6 +85,7 @@ bool GyroICM42688::testConnection()
{
uint8_t whoami = 0;
if (_bus->readByte(_addr, ICM42688_REG_WHO_AM_I, &whoami) != 1) return false;
setChipId(whoami);
return whoami == ICM42688_WHO_AM_I_VALUE;
}

Expand Down
123 changes: 64 additions & 59 deletions lib/Espfc/src/Device/Gyro/GyroLSM6DSO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,93 +4,101 @@
#include "Utils/MemoryHelper.h"

// https://github.com/arduino-libraries/Arduino_LSM6DSOX/blob/master/src/LSM6DSOX.cpp
#define LSM6DSOX_ADDRESS_FIRST 0x6A
#define LSM6DSOX_ADDRESS_SECOND 0x6b
#define LSM6DSOX_ADDRESS_FIRST 0x6A
#define LSM6DSOX_ADDRESS_SECOND 0x6b

// registers
#define LSM6DSO_REG_WHO_AM_I 0x0F
#define LSM6DSO_REG_CTRL1_XL 0x10
#define LSM6DSO_REG_CTRL2_G 0x11
#define LSM6DSO_REG_CTRL3_C 0x12
#define LSM6DSO_REG_CTRL4_C 0x13
#define LSM6DSO_REG_CTRL5_C 0x14
#define LSM6DSO_REG_CTRL6_C 0x15
#define LSM6DSO_REG_CTRL7_G 0x16
#define LSM6DSO_REG_CTRL8_XL 0x17
#define LSM6DSO_REG_CTRL9_XL 0x18
#define LSM6DSO_REG_CTRL10_C 0x19
#define LSM6DSO_REG_STATUS 0x1E
#define LSM6DSO_REG_OUTX_L_G 0x22
#define LSM6DSO_REG_OUTX_L_XL 0x28
#define LSM6DSO_REG_WHO_AM_I 0x0F
#define LSM6DSO_REG_CTRL1_XL 0x10
#define LSM6DSO_REG_CTRL2_G 0x11
#define LSM6DSO_REG_CTRL3_C 0x12
#define LSM6DSO_REG_CTRL4_C 0x13
#define LSM6DSO_REG_CTRL5_C 0x14
#define LSM6DSO_REG_CTRL6_C 0x15
#define LSM6DSO_REG_CTRL7_G 0x16
#define LSM6DSO_REG_CTRL8_XL 0x17
#define LSM6DSO_REG_CTRL9_XL 0x18
#define LSM6DSO_REG_CTRL10_C 0x19
#define LSM6DSO_REG_STATUS 0x1E
#define LSM6DSO_REG_OUTX_L_G 0x22
#define LSM6DSO_REG_OUTX_L_XL 0x28

// values
#define LSM6DSO_VAL_INT1_CTRL 0x02 // enable gyro data ready interrupt pin 1
#define LSM6DSO_VAL_INT2_CTRL 0x02 // enable gyro data ready interrupt pin 2
#define LSM6DSO_VAL_CTRL1_XL_ODR833 0x07 // accelerometer 833hz output data rate (gyro/8)
#define LSM6DSO_VAL_CTRL1_XL_ODR1667 0x08 // accelerometer 1666hz output data rate (gyro/4)
#define LSM6DSO_VAL_CTRL1_XL_ODR3332 0x09 // accelerometer 3332hz output data rate (gyro/2)
#define LSM6DSO_VAL_CTRL1_XL_ODR3333 0x0A // accelerometer 6664hz output data rate (gyro/1)
#define LSM6DSO_VAL_CTRL1_XL_8G 0x03 // accelerometer 8G scale
#define LSM6DSO_VAL_CTRL1_XL_16G 0x01 // accelerometer 16G scale
#define LSM6DSO_VAL_CTRL1_XL_LPF1 0x00 // accelerometer output from LPF1
#define LSM6DSO_VAL_CTRL1_XL_LPF2 0x01 // accelerometer output from LPF2
#define LSM6DSO_VAL_CTRL2_G_ODR6664 0x0A // gyro 6664hz output data rate
#define LSM6DSO_VAL_CTRL2_G_ODR3332 0x09 // gyro 3332hz output data rate
#define LSM6DSO_VAL_CTRL2_G_2000DPS 0x03 // gyro 2000dps scale
#define LSM6DSO_VAL_CTRL3_C_BDU 0x40 // (bit 6) output registers are not updated until MSB and LSB have been read (prevents MSB from being updated while burst reading LSB/MSB)
#define LSM6DSO_VAL_CTRL3_C_H_LACTIVE 0x00 // (bit 5) interrupt pins active high
#define LSM6DSO_VAL_CTRL3_C_PP_OD 0x00 // (bit 4) interrupt pins push/pull
#define LSM6DSO_VAL_CTRL3_C_SIM 0x00 // (bit 3) SPI 4-wire interface mode
#define LSM6DSO_VAL_CTRL3_C_IF_INC 0x04 // (bit 2) auto-increment address for burst reads
#define LSM6DSO_VAL_CTRL4_C_I2C_DISABLE 0x04 // (bit 2) disable I2C interface
#define LSM6DSO_VAL_CTRL4_C_LPF1_SEL_G 0x02 // (bit 1) enable gyro LPF1
#define LSM6DSO_VAL_CTRL6_C_XL_HM_MODE 0x00 // (bit 4) enable accelerometer high performance mode
#define LSM6DSO_VAL_CTRL6_C_FTYPE_335HZ 0x00 // (bits 2:0) gyro LPF1 cutoff 335.5hz
#define LSM6DSO_VAL_CTRL6_C_FTYPE_232HZ 0x01 // (bits 2:0) gyro LPF1 cutoff 232.0hz
#define LSM6DSO_VAL_CTRL6_C_FTYPE_171HZ 0x02 // (bits 2:0) gyro LPF1 cutoff 171.1hz
#define LSM6DSO_VAL_CTRL6_C_FTYPE_609HZ 0x03 // (bits 2:0) gyro LPF1 cutoff 609.0hz
#define LSM6DSO_VAL_CTRL9_XL_I3C_DISABLE 0x02 // (bit 1) disable I3C interface
#define LSM6DSO_VAL_INT1_CTRL 0x02 // enable gyro data ready interrupt pin 1
#define LSM6DSO_VAL_INT2_CTRL 0x02 // enable gyro data ready interrupt pin 2
#define LSM6DSO_VAL_CTRL1_XL_ODR833 0x07 // accelerometer 833hz output data rate (gyro/8)
#define LSM6DSO_VAL_CTRL1_XL_ODR1667 0x08 // accelerometer 1666hz output data rate (gyro/4)
#define LSM6DSO_VAL_CTRL1_XL_ODR3332 0x09 // accelerometer 3332hz output data rate (gyro/2)
#define LSM6DSO_VAL_CTRL1_XL_ODR3333 0x0A // accelerometer 6664hz output data rate (gyro/1)
#define LSM6DSO_VAL_CTRL1_XL_8G 0x03 // accelerometer 8G scale
#define LSM6DSO_VAL_CTRL1_XL_16G 0x01 // accelerometer 16G scale
#define LSM6DSO_VAL_CTRL1_XL_LPF1 0x00 // accelerometer output from LPF1
#define LSM6DSO_VAL_CTRL1_XL_LPF2 0x01 // accelerometer output from LPF2
#define LSM6DSO_VAL_CTRL2_G_ODR6664 0x0A // gyro 6664hz output data rate
#define LSM6DSO_VAL_CTRL2_G_ODR3332 0x09 // gyro 3332hz output data rate
#define LSM6DSO_VAL_CTRL2_G_2000DPS 0x03 // gyro 2000dps scale
#define LSM6DSO_VAL_CTRL3_C_BDU \
0x40 // (bit 6) output registers are not updated until MSB and LSB have been read (prevents MSB from being updated
// while burst reading LSB/MSB)
#define LSM6DSO_VAL_CTRL3_C_H_LACTIVE 0x00 // (bit 5) interrupt pins active high
#define LSM6DSO_VAL_CTRL3_C_PP_OD 0x00 // (bit 4) interrupt pins push/pull
#define LSM6DSO_VAL_CTRL3_C_SIM 0x00 // (bit 3) SPI 4-wire interface mode
#define LSM6DSO_VAL_CTRL3_C_IF_INC 0x04 // (bit 2) auto-increment address for burst reads
#define LSM6DSO_VAL_CTRL4_C_I2C_DISABLE 0x04 // (bit 2) disable I2C interface
#define LSM6DSO_VAL_CTRL4_C_LPF1_SEL_G 0x02 // (bit 1) enable gyro LPF1
#define LSM6DSO_VAL_CTRL6_C_XL_HM_MODE 0x00 // (bit 4) enable accelerometer high performance mode
#define LSM6DSO_VAL_CTRL6_C_FTYPE_335HZ 0x00 // (bits 2:0) gyro LPF1 cutoff 335.5hz
#define LSM6DSO_VAL_CTRL6_C_FTYPE_232HZ 0x01 // (bits 2:0) gyro LPF1 cutoff 232.0hz
#define LSM6DSO_VAL_CTRL6_C_FTYPE_171HZ 0x02 // (bits 2:0) gyro LPF1 cutoff 171.1hz
#define LSM6DSO_VAL_CTRL6_C_FTYPE_609HZ 0x03 // (bits 2:0) gyro LPF1 cutoff 609.0hz
#define LSM6DSO_VAL_CTRL9_XL_I3C_DISABLE 0x02 // (bit 1) disable I3C interface

// masks
#define LSM6DSO_MASK_CTRL3_C 0x7C // 0b01111100
#define LSM6DSO_MASK_CTRL3_C 0x7C // 0b01111100
#define LSM6DSO_MASK_CTRL3_C_RESET 0x01 // 0b00000001
#define LSM6DSO_MASK_CTRL4_C 0x06 // 0b00000110
#define LSM6DSO_MASK_CTRL6_C 0x17 // 0b00010111
#define LSM6DSO_MASK_CTRL9_XL 0x02 // 0b00000010
#define LSM6DSO_MASK_CTRL4_C 0x06 // 0b00000110
#define LSM6DSO_MASK_CTRL6_C 0x17 // 0b00010111
#define LSM6DSO_MASK_CTRL9_XL 0x02 // 0b00000010

namespace Espfc::Device::Gyro {

int GyroLSM6DSO::begin(BusDevice * bus)
int GyroLSM6DSO::begin(BusDevice* bus)
{
return begin(bus, LSM6DSOX_ADDRESS_FIRST) ? 1 : begin(bus, LSM6DSOX_ADDRESS_SECOND) ? 1 : 0;
}

int GyroLSM6DSO::begin(BusDevice * bus, uint8_t addr)
int GyroLSM6DSO::begin(BusDevice* bus, uint8_t addr)
{
setBus(bus, addr);

if(!testConnection()) return 0;
if (!testConnection()) return 0;

// reset device
_bus->writeMask(_addr, LSM6DSO_REG_CTRL3_C, LSM6DSO_MASK_CTRL3_C_RESET, 1);
delay(100);

// Accel, 833hz ODR, 16G scale, use LPF1 output
_bus->writeByte(_addr, LSM6DSO_REG_CTRL1_XL, (LSM6DSO_VAL_CTRL1_XL_ODR833 << 4) | (LSM6DSO_VAL_CTRL1_XL_16G << 2) | (LSM6DSO_VAL_CTRL1_XL_LPF1 << 1));
_bus->writeByte(_addr, LSM6DSO_REG_CTRL1_XL,
(LSM6DSO_VAL_CTRL1_XL_ODR833 << 4) | (LSM6DSO_VAL_CTRL1_XL_16G << 2) |
(LSM6DSO_VAL_CTRL1_XL_LPF1 << 1));
delay(1);

// Gyro, 6664hz ODR, 2000dps scale
_bus->writeByte(_addr, LSM6DSO_REG_CTRL2_G, (LSM6DSO_VAL_CTRL2_G_ODR6664 << 4) | (LSM6DSO_VAL_CTRL2_G_2000DPS << 2));
delay(1);

// latch LSB/MSB during reads; set interrupt pins active high; set interrupt pins push/pull; set 4-wire SPI; enable auto-increment burst reads
_bus->writeMask(_addr, LSM6DSO_REG_CTRL3_C, LSM6DSO_MASK_CTRL3_C, (LSM6DSO_VAL_CTRL3_C_BDU | LSM6DSO_VAL_CTRL3_C_H_LACTIVE | LSM6DSO_VAL_CTRL3_C_PP_OD | LSM6DSO_VAL_CTRL3_C_SIM | LSM6DSO_VAL_CTRL3_C_IF_INC));
// latch LSB/MSB during reads; set interrupt pins active high; set interrupt pins push/pull; set 4-wire SPI; enable
// auto-increment burst reads
_bus->writeMask(_addr, LSM6DSO_REG_CTRL3_C, LSM6DSO_MASK_CTRL3_C,
(LSM6DSO_VAL_CTRL3_C_BDU | LSM6DSO_VAL_CTRL3_C_H_LACTIVE | LSM6DSO_VAL_CTRL3_C_PP_OD |
LSM6DSO_VAL_CTRL3_C_SIM | LSM6DSO_VAL_CTRL3_C_IF_INC));

// enable accelerometer high performane mode; set gyro LPF1 cutoff to 335.5hz
_bus->writeMask(_addr, LSM6DSO_REG_CTRL4_C, LSM6DSO_MASK_CTRL4_C, (LSM6DSO_VAL_CTRL4_C_LPF1_SEL_G));

// enable gyro LPF1
_bus->writeMask(_addr, LSM6DSO_REG_CTRL6_C, LSM6DSO_MASK_CTRL6_C, (LSM6DSO_VAL_CTRL6_C_XL_HM_MODE | LSM6DSO_VAL_CTRL6_C_FTYPE_335HZ));
_bus->writeMask(_addr, LSM6DSO_REG_CTRL6_C, LSM6DSO_MASK_CTRL6_C,
(LSM6DSO_VAL_CTRL6_C_XL_HM_MODE | LSM6DSO_VAL_CTRL6_C_FTYPE_335HZ));

// disable I3C interface
_bus->writeMask(_addr, LSM6DSO_REG_CTRL9_XL, LSM6DSO_MASK_CTRL9_XL, LSM6DSO_VAL_CTRL9_XL_I3C_DISABLE);
Expand Down Expand Up @@ -129,23 +137,20 @@ int GyroLSM6DSO::readAccel(VectorInt16& v)
return 1;
}

void GyroLSM6DSO::setDLPFMode(uint8_t mode)
{
}
void GyroLSM6DSO::setDLPFMode(uint8_t mode) {}

int GyroLSM6DSO::getRate() const
{
return 6664;
}

void GyroLSM6DSO::setRate(int rate)
{
}
void GyroLSM6DSO::setRate(int rate) {}

bool GyroLSM6DSO::testConnection()
{
uint8_t whoami = 0;
if (_bus->readByte(_addr, LSM6DSO_REG_WHO_AM_I, &whoami) != 1) return false;
setChipId(whoami);
return whoami == 0x6C || whoami == 0x69;
}

Expand Down
Loading