1
0
forked from me/IronOS

Move Accel probe into class

This commit is contained in:
Ben V. Brown
2020-05-29 20:46:19 +10:00
parent 3b0fc9c6f8
commit ef5ba8b650
5 changed files with 232 additions and 203 deletions

View File

@@ -12,12 +12,16 @@
#include "LIS2DH12_defines.hpp" #include "LIS2DH12_defines.hpp"
#include "hardware.h" #include "hardware.h"
#ifdef MODEL_TS80
#define LIS_ORI_FLIP
#endif
class LIS2DH12 { class LIS2DH12 {
public: public:
static bool detect();
static void initalize(); static void initalize();
//1 = rh, 2,=lh, 8=flat //1 = rh, 2,=lh, 8=flat
static Orientation getOrientation() { static Orientation getOrientation() {
#ifdef MODEL_TS80 #ifdef LIS_ORI_FLIP
uint8_t val = (FRToSI2C::I2C_RegisterRead(LIS2DH_I2C_ADDRESS, uint8_t val = (FRToSI2C::I2C_RegisterRead(LIS2DH_I2C_ADDRESS,
LIS_INT2_SRC) >> 2); LIS_INT2_SRC) >> 2);
if (val == 8) if (val == 8)

View File

@@ -15,9 +15,10 @@
class MMA8652FC { class MMA8652FC {
public: public:
//Returns true if this accelerometer is detected
static bool detect();
static void initalize(); // Initalize the system //Init any internal state
static void initalize();
static Orientation getOrientation(); // Reads the I2C register and returns the orientation (true == left) static Orientation getOrientation(); // Reads the I2C register and returns the orientation (true == left)
static void getAxisReadings(int16_t &x, int16_t &y, int16_t &z); static void getAxisReadings(int16_t &x, int16_t &y, int16_t &z);

View File

@@ -15,25 +15,24 @@ typedef struct {
const uint8_t value; const uint8_t value;
} LIS_REG; } LIS_REG;
static const LIS_REG i2c_registers[] = { static const LIS_REG i2c_registers[] =
{LIS_CTRL_REG1, 0x17}, // 25Hz { { LIS_CTRL_REG1, 0x17 }, // 25Hz
{ LIS_CTRL_REG2, 0b00001000 }, // Highpass filter off { LIS_CTRL_REG2, 0b00001000 }, // Highpass filter off
{ LIS_CTRL_REG3, 0b01100000 }, // Setup interrupt pins { LIS_CTRL_REG3, 0b01100000 }, // Setup interrupt pins
{ LIS_CTRL_REG4, 0b00001000 }, // Block update mode off, HR on { LIS_CTRL_REG4, 0b00001000 }, // Block update mode off, HR on
{LIS_CTRL_REG5, 0b00000010}, { LIS_CTRL_REG5, 0b00000010 }, { LIS_CTRL_REG6, 0b01100010 },
{LIS_CTRL_REG6, 0b01100010},
//Basically setup the unit to run, and enable 4D orientation detection //Basically setup the unit to run, and enable 4D orientation detection
{ LIS_INT2_CFG, 0b01111110 }, //setup for movement detection { LIS_INT2_CFG, 0b01111110 }, //setup for movement detection
{LIS_INT2_THS, 0x28}, { LIS_INT2_THS, 0x28 }, { LIS_INT2_DURATION, 64 }, {
{LIS_INT2_DURATION, 64}, LIS_INT1_CFG, 0b01111110 }, { LIS_INT1_THS, 0x28 }, {
{LIS_INT1_CFG, 0b01111110}, LIS_INT1_DURATION, 64 } };
{LIS_INT1_THS, 0x28},
{LIS_INT1_DURATION, 64}
};
void LIS2DH12::initalize() { void LIS2DH12::initalize() {
for (size_t index = 0; index < (sizeof(i2c_registers) / sizeof(i2c_registers[0])); index++) { for (size_t index = 0;
FRToSI2C::I2C_RegisterWrite(LIS2DH_I2C_ADDRESS,i2c_registers[index].reg, i2c_registers[index].value); index < (sizeof(i2c_registers) / sizeof(i2c_registers[0]));
index++) {
FRToSI2C::I2C_RegisterWrite(LIS2DH_I2C_ADDRESS,
i2c_registers[index].reg, i2c_registers[index].value);
} }
} }
@@ -48,3 +47,14 @@ void LIS2DH12::getAxisReadings(int16_t& x, int16_t& y, int16_t& z) {
y = sensorData[1]; y = sensorData[1];
z = sensorData[2]; z = sensorData[2];
} }
bool LIS2DH12::detect() {
uint8_t buffer[1];
if (HAL_I2C_Mem_Read(&hi2c1, 25 << 1, 0x0F, I2C_MEMADD_SIZE_8BIT, buffer, 1,
1000) == HAL_OK) {
//ACK'd
return true;
}
//NAK'd
return false;
}

View File

@@ -30,28 +30,31 @@ static const MMA_REG i2c_registers[] = { { CTRL_REG2, 0 }, //Normal mode
{ CTRL_REG1, 0x19 } // ODR=12 Hz, Active mode { CTRL_REG1, 0x19 } // ODR=12 Hz, Active mode
}; };
void MMA8652FC::initalize() { void MMA8652FC::initalize() {
size_t index = 0; size_t index = 0;
//send all the init commands to the unit //send all the init commands to the unit
FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS,i2c_registers[index].reg, i2c_registers[index].val); FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS, i2c_registers[index].reg,
i2c_registers[index].val);
index++; index++;
FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS,i2c_registers[index].reg, i2c_registers[index].val); FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS, i2c_registers[index].reg,
i2c_registers[index].val);
index++; index++;
HAL_Delay(2); // ~1ms delay HAL_Delay(2); // ~1ms delay
while (index < (sizeof(i2c_registers) / sizeof(i2c_registers[0]))) { while (index < (sizeof(i2c_registers) / sizeof(i2c_registers[0]))) {
FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS,i2c_registers[index].reg, i2c_registers[index].val); FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS,
i2c_registers[index].reg, i2c_registers[index].val);
index++; index++;
} }
} }
Orientation MMA8652FC::getOrientation() { Orientation MMA8652FC::getOrientation() {
//First read the PL_STATUS register //First read the PL_STATUS register
uint8_t plStatus = FRToSI2C::I2C_RegisterRead(MMA8652FC_I2C_ADDRESS,PL_STATUS_REG); uint8_t plStatus = FRToSI2C::I2C_RegisterRead(MMA8652FC_I2C_ADDRESS,
PL_STATUS_REG);
if ((plStatus & 0b10000000) == 0b10000000) { if ((plStatus & 0b10000000) == 0b10000000) {
plStatus >>= 1; //We don't need the up/down bit plStatus >>= 1; //We don't need the up/down bit
plStatus &= 0x03; //mask to the two lower bits plStatus &= 0x03; //mask to the two lower bits
@@ -68,11 +71,25 @@ Orientation MMA8652FC::getOrientation() {
void MMA8652FC::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) { void MMA8652FC::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) {
std::array<int16_t, 3> sensorData; std::array<int16_t, 3> sensorData;
FRToSI2C::Mem_Read(MMA8652FC_I2C_ADDRESS, OUT_X_MSB_REG, I2C_MEMADD_SIZE_8BIT, FRToSI2C::Mem_Read(MMA8652FC_I2C_ADDRESS, OUT_X_MSB_REG,
reinterpret_cast<uint8_t*>(sensorData.begin()), I2C_MEMADD_SIZE_8BIT, reinterpret_cast<uint8_t*>(sensorData.begin()),
sensorData.size() * sizeof(int16_t)); sensorData.size() * sizeof(int16_t));
x = static_cast<int16_t>(__builtin_bswap16(*reinterpret_cast<uint16_t*>(&sensorData[0]))); x = static_cast<int16_t>(__builtin_bswap16(
y = static_cast<int16_t>(__builtin_bswap16(*reinterpret_cast<uint16_t*>(&sensorData[1]))); *reinterpret_cast<uint16_t*>(&sensorData[0])));
z = static_cast<int16_t>(__builtin_bswap16(*reinterpret_cast<uint16_t*>(&sensorData[2]))); y = static_cast<int16_t>(__builtin_bswap16(
*reinterpret_cast<uint16_t*>(&sensorData[1])));
z = static_cast<int16_t>(__builtin_bswap16(
*reinterpret_cast<uint16_t*>(&sensorData[2])));
}
bool MMA8652FC::detect() {
uint8_t buffer[1];
if (HAL_I2C_Mem_Read(&hi2c1, 29 << 1, 0x0F, I2C_MEMADD_SIZE_8BIT, buffer, 1,
1000) == HAL_OK) {
//The device ACK'd
return true;
}
//NAK'd
return false;
} }

View File

@@ -55,14 +55,11 @@ int main(void) {
OLED::initialize(); // start up the LCD OLED::initialize(); // start up the LCD
OLED::setFont(0); // default to bigger font OLED::setFont(0); // default to bigger font
// Testing for which accelerometer is mounted // Testing for which accelerometer is mounted
uint8_t buffer[1]; resetWatchdog();
HAL_IWDG_Refresh(&hiwdg); if (MMA8652FC::detect()) {
if (HAL_I2C_Mem_Read(&hi2c1, 29 << 1, 0x0F, I2C_MEMADD_SIZE_8BIT, buffer, 1,
1000) == HAL_OK) {
PCBVersion = 1; PCBVersion = 1;
MMA8652FC::initalize(); // this sets up the I2C registers MMA8652FC::initalize(); // this sets up the I2C registers
} else if (HAL_I2C_Mem_Read(&hi2c1, 25 << 1, 0x0F, I2C_MEMADD_SIZE_8BIT, } else if (LIS2DH12::detect()) {
buffer, 1, 1000) == HAL_OK) {
PCBVersion = 2; PCBVersion = 2;
// Setup the ST Accelerometer // Setup the ST Accelerometer
LIS2DH12::initalize(); // startup the accelerometer LIS2DH12::initalize(); // startup the accelerometer