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

@@ -1,42 +1,46 @@
/* /*
* LIS2DH12.hpp * LIS2DH12.hpp
* *
* Created on: 27Feb.,2018 * Created on: 27Feb.,2018
* Author: Ralim * Author: Ralim
*/ */
#ifndef LIS2DH12_HPP_ #ifndef LIS2DH12_HPP_
#define LIS2DH12_HPP_ #define LIS2DH12_HPP_
#include "stm32f1xx_hal.h" #include "stm32f1xx_hal.h"
#include "FRToSI2C.hpp" #include "FRToSI2C.hpp"
#include "LIS2DH12_defines.hpp" #include "LIS2DH12_defines.hpp"
#include "hardware.h" #include "hardware.h"
class LIS2DH12 { #ifdef MODEL_TS80
public: #define LIS_ORI_FLIP
static void initalize(); #endif
//1 = rh, 2,=lh, 8=flat class LIS2DH12 {
static Orientation getOrientation() { public:
#ifdef MODEL_TS80 static bool detect();
uint8_t val = (FRToSI2C::I2C_RegisterRead(LIS2DH_I2C_ADDRESS, static void initalize();
LIS_INT2_SRC) >> 2); //1 = rh, 2,=lh, 8=flat
if (val == 8) static Orientation getOrientation() {
val = 3; #ifdef LIS_ORI_FLIP
else if (val == 1) uint8_t val = (FRToSI2C::I2C_RegisterRead(LIS2DH_I2C_ADDRESS,
val = 1; LIS_INT2_SRC) >> 2);
else if (val == 2) if (val == 8)
val = 0; val = 3;
else else if (val == 1)
val = 3; val = 1;
return static_cast<Orientation>(val); else if (val == 2)
#endif val = 0;
#ifdef MODEL_TS100 else
return static_cast<Orientation>((FRToSI2C::I2C_RegisterRead(LIS2DH_I2C_ADDRESS,LIS_INT2_SRC) >> 2) - 1); val = 3;
#endif return static_cast<Orientation>(val);
} #endif
static void getAxisReadings(int16_t& x, int16_t& y, int16_t& z); #ifdef MODEL_TS100
return static_cast<Orientation>((FRToSI2C::I2C_RegisterRead(LIS2DH_I2C_ADDRESS,LIS_INT2_SRC) >> 2) - 1);
private: #endif
}; }
static void getAxisReadings(int16_t& x, int16_t& y, int16_t& z);
#endif /* LIS2DH12_HPP_ */
private:
};
#endif /* LIS2DH12_HPP_ */

View File

@@ -1,27 +1,28 @@
/* /*
* MMA8652FC.h * MMA8652FC.h
* *
* Created on: 31Aug.,2017 * Created on: 31Aug.,2017
* Author: Ben V. Brown * Author: Ben V. Brown
*/ */
#ifndef MMA8652FC_HPP_ #ifndef MMA8652FC_HPP_
#define MMA8652FC_HPP_ #define MMA8652FC_HPP_
#include "stm32f1xx_hal.h" #include "stm32f1xx_hal.h"
#include "MMA8652FC_defines.h" #include "MMA8652FC_defines.h"
#include "FRToSI2C.hpp" #include "FRToSI2C.hpp"
#include "hardware.h" #include "hardware.h"
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 Orientation getOrientation();// Reads the I2C register and returns the orientation (true == left) static void initalize();
static void getAxisReadings(int16_t& x, int16_t& y, int16_t& z); 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);
private:
}; private:
};
#endif /* MMA8652FC_HPP_ */
#endif /* MMA8652FC_HPP_ */

View File

@@ -1,50 +1,60 @@
/* /*
* LIS2DH12.cpp * LIS2DH12.cpp
* *
* Created on: 27Feb.,2018 * Created on: 27Feb.,2018
* Author: Ralim * Author: Ralim
*/ */
#include <array> #include <array>
#include "LIS2DH12.hpp" #include "LIS2DH12.hpp"
#include "cmsis_os.h" #include "cmsis_os.h"
typedef struct { typedef struct {
const uint8_t reg; const uint8_t reg;
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_DURATION, 64 }, {
{LIS_INT2_THS, 0x28}, LIS_INT1_CFG, 0b01111110 }, { LIS_INT1_THS, 0x28 }, {
{LIS_INT2_DURATION, 64}, LIS_INT1_DURATION, 64 } };
{LIS_INT1_CFG, 0b01111110},
{LIS_INT1_THS, 0x28}, void LIS2DH12::initalize() {
{LIS_INT1_DURATION, 64} for (size_t index = 0;
}; index < (sizeof(i2c_registers) / sizeof(i2c_registers[0]));
index++) {
void LIS2DH12::initalize() { FRToSI2C::I2C_RegisterWrite(LIS2DH_I2C_ADDRESS,
for (size_t index = 0; index < (sizeof(i2c_registers) / sizeof(i2c_registers[0])); index++) { i2c_registers[index].reg, i2c_registers[index].value);
FRToSI2C::I2C_RegisterWrite(LIS2DH_I2C_ADDRESS,i2c_registers[index].reg, i2c_registers[index].value); }
} }
}
void LIS2DH12::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) {
void LIS2DH12::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(LIS2DH_I2C_ADDRESS, 0xA8, I2C_MEMADD_SIZE_8BIT,
FRToSI2C::Mem_Read(LIS2DH_I2C_ADDRESS, 0xA8, I2C_MEMADD_SIZE_8BIT, reinterpret_cast<uint8_t*>(sensorData.begin()),
reinterpret_cast<uint8_t*>(sensorData.begin()), sensorData.size() * sizeof(int16_t));
sensorData.size() * sizeof(int16_t));
x = sensorData[0];
x = sensorData[0]; 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

@@ -1,78 +1,95 @@
/* /*
* MMA8652FC.cpp * MMA8652FC.cpp
* *
* Created on: 31Aug.,2017 * Created on: 31Aug.,2017
* Author: Ben V. Brown * Author: Ben V. Brown
*/ */
#include <array> #include <array>
#include "MMA8652FC.hpp" #include "MMA8652FC.hpp"
#include "cmsis_os.h" #include "cmsis_os.h"
typedef struct { typedef struct {
const uint8_t reg; const uint8_t reg;
const uint8_t val; const uint8_t val;
} MMA_REG; } MMA_REG;
static const MMA_REG i2c_registers[] = { { CTRL_REG2, 0 }, //Normal mode static const MMA_REG i2c_registers[] = { { CTRL_REG2, 0 }, //Normal mode
{ CTRL_REG2, 0x40 }, // Reset all registers to POR values { CTRL_REG2, 0x40 }, // Reset all registers to POR values
{ FF_MT_CFG_REG, 0x78 }, // Enable motion detection for X, Y, Z axis, latch disabled { FF_MT_CFG_REG, 0x78 }, // Enable motion detection for X, Y, Z axis, latch disabled
{ PL_CFG_REG, 0x40 }, //Enable the orientation detection { PL_CFG_REG, 0x40 }, //Enable the orientation detection
{ PL_COUNT_REG, 200 }, //200 count debounce { PL_COUNT_REG, 200 }, //200 count debounce
{ PL_BF_ZCOMP_REG, 0b01000111 }, //Set the threshold to 42 degrees { PL_BF_ZCOMP_REG, 0b01000111 }, //Set the threshold to 42 degrees
{ P_L_THS_REG, 0b10011100 }, //Up the trip angles { P_L_THS_REG, 0b10011100 }, //Up the trip angles
{ CTRL_REG4, 0x01 | (1 << 4) }, // Enable dataready interrupt & orientation interrupt { CTRL_REG4, 0x01 | (1 << 4) }, // Enable dataready interrupt & orientation interrupt
{ CTRL_REG5, 0x01 }, // Route data ready interrupts to INT1 ->PB5 ->EXTI5, leaving orientation routed to INT2 { CTRL_REG5, 0x01 }, // Route data ready interrupts to INT1 ->PB5 ->EXTI5, leaving orientation routed to INT2
{ CTRL_REG2, 0x12 }, //Set maximum resolution oversampling { CTRL_REG2, 0x12 }, //Set maximum resolution oversampling
{ XYZ_DATA_CFG_REG, (1 << 4) }, //select high pass filtered data { XYZ_DATA_CFG_REG, (1 << 4) }, //select high pass filtered data
{ HP_FILTER_CUTOFF_REG, 0x03 }, //select high pass filtered data { HP_FILTER_CUTOFF_REG, 0x03 }, //select high pass filtered data
{ 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,
FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS,i2c_registers[index].reg, i2c_registers[index].val); 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,
index++; i2c_registers[index].val);
index++;
HAL_Delay(2); // ~1ms delay
HAL_Delay(2); // ~1ms delay
while (index < (sizeof(i2c_registers) / sizeof(i2c_registers[0]))) {
FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS,i2c_registers[index].reg, i2c_registers[index].val); while (index < (sizeof(i2c_registers) / sizeof(i2c_registers[0]))) {
index++; FRToSI2C::I2C_RegisterWrite(MMA8652FC_I2C_ADDRESS,
} i2c_registers[index].reg, i2c_registers[index].val);
} index++;
}
Orientation MMA8652FC::getOrientation() { }
//First read the PL_STATUS register
uint8_t plStatus = FRToSI2C::I2C_RegisterRead(MMA8652FC_I2C_ADDRESS,PL_STATUS_REG); Orientation MMA8652FC::getOrientation() {
if ((plStatus & 0b10000000) == 0b10000000) { //First read the PL_STATUS register
plStatus >>= 1; //We don't need the up/down bit uint8_t plStatus = FRToSI2C::I2C_RegisterRead(MMA8652FC_I2C_ADDRESS,
plStatus &= 0x03; //mask to the two lower bits PL_STATUS_REG);
if ((plStatus & 0b10000000) == 0b10000000) {
//0 == left handed plStatus >>= 1; //We don't need the up/down bit
//1 == right handed plStatus &= 0x03; //mask to the two lower bits
return static_cast<Orientation>(plStatus); //0 == left handed
} //1 == right handed
return ORIENTATION_FLAT; return static_cast<Orientation>(plStatus);
} }
void MMA8652FC::getAxisReadings(int16_t& x, int16_t& y, int16_t& z) { return ORIENTATION_FLAT;
std::array<int16_t, 3> sensorData; }
FRToSI2C::Mem_Read(MMA8652FC_I2C_ADDRESS, OUT_X_MSB_REG, I2C_MEMADD_SIZE_8BIT, void MMA8652FC::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) {
reinterpret_cast<uint8_t*>(sensorData.begin()), std::array<int16_t, 3> sensorData;
sensorData.size() * sizeof(int16_t));
FRToSI2C::Mem_Read(MMA8652FC_I2C_ADDRESS, OUT_X_MSB_REG,
x = static_cast<int16_t>(__builtin_bswap16(*reinterpret_cast<uint16_t*>(&sensorData[0]))); I2C_MEMADD_SIZE_8BIT, reinterpret_cast<uint8_t*>(sensorData.begin()),
y = static_cast<int16_t>(__builtin_bswap16(*reinterpret_cast<uint16_t*>(&sensorData[1]))); sensorData.size() * sizeof(int16_t));
z = static_cast<int16_t>(__builtin_bswap16(*reinterpret_cast<uint16_t*>(&sensorData[2])));
} x = static_cast<int16_t>(__builtin_bswap16(
*reinterpret_cast<uint16_t*>(&sensorData[0])));
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