Migrate all Miniware devices to use Bit-Bang I2C (#1838)

* MHP30 move to I2C Bit Banging

* Fixup Accelerometer drivers so all can use I2CBB

* No STM32 I2C driver anymore

* TS100 on I2CBB

* Miniware on BB

* Fixup S60 build

* format

format
This commit is contained in:
Ben V. Brown
2023-11-16 21:32:56 +11:00
committed by GitHub
parent c308fe8cc2
commit e3bad2adae
46 changed files with 144 additions and 8044 deletions

View File

@@ -5,14 +5,15 @@
* Author: Ralim
*/
#include "accelerometers_common.h"
#include <BMA223.hpp>
#include <array>
bool BMA223::detect() {
if (FRToSI2C::probe(BMA223_ADDRESS)) {
if (ACCEL_I2C_CLASS::probe(BMA223_ADDRESS)) {
// Read chip id to ensure its not an address collision
uint8_t id = 0;
if (FRToSI2C::Mem_Read(BMA223_ADDRESS, BMA223_BGW_CHIPID, &id, 1)) {
if (ACCEL_I2C_CLASS::Mem_Read(BMA223_ADDRESS, BMA223_BGW_CHIPID, &id, 1)) {
return id == 0b11111000;
}
}
@@ -20,7 +21,7 @@ bool BMA223::detect() {
return false;
}
static const FRToSI2C::I2C_REG i2c_registers[] = {
static const ACCEL_I2C_CLASS::I2C_REG i2c_registers[] = {
//
//
{BMA223_PMU_RANGE, 0b00000011, 0}, // 2G range
@@ -44,7 +45,7 @@ bool BMA223::initalize() {
// Hysteresis is set to ~ 16 counts
// Theta blocking is set to 0b10
return FRToSI2C::writeRegistersBulk(BMA223_ADDRESS, i2c_registers, sizeof(i2c_registers) / sizeof(i2c_registers[0]));
return ACCEL_I2C_CLASS::writeRegistersBulk(BMA223_ADDRESS, i2c_registers, sizeof(i2c_registers) / sizeof(i2c_registers[0]));
}
void BMA223::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) {
@@ -52,7 +53,7 @@ void BMA223::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) {
// And yet there are MSB and LSB registers _sigh_.
uint8_t sensorData[6] = {0, 0, 0, 0, 0, 0};
if (FRToSI2C::Mem_Read(BMA223_ADDRESS, BMA223_ACCD_X_LSB, sensorData, 6) == false) {
if (ACCEL_I2C_CLASS::Mem_Read(BMA223_ADDRESS, BMA223_ACCD_X_LSB, sensorData, 6) == false) {
x = y = z = 0;
return;
}

View File

@@ -10,6 +10,8 @@
#include "BMA223_defines.h"
#include "BSP.h"
#include "I2C_Wrapper.hpp"
#include "accelerometers_common.h"
class BMA223 {
public:
@@ -17,7 +19,7 @@ public:
static bool initalize();
// 1 = rh, 2,=lh, 8=flat
static Orientation getOrientation() {
uint8_t val = FRToSI2C::I2C_RegisterRead(BMA223_ADDRESS, BMA223_INT_STATUS_3);
uint8_t val = ACCEL_I2C_CLASS::I2C_RegisterRead(BMA223_ADDRESS, BMA223_INT_STATUS_3);
val >>= 4; // we dont need high values
val &= 0b11;
if (val & 0b10) {

View File

@@ -314,4 +314,15 @@ bool I2CBB1::writeRegistersBulk(const uint8_t address, const I2C_REG *registers,
}
return true;
}
bool I2CBB1::wakePart(uint16_t DevAddress) {
// wakepart is a special case where only the device address is sent
if (!lock())
return false;
start();
bool ack = send(DevAddress);
stop();
unlock();
return ack;
}
#endif

View File

@@ -36,6 +36,7 @@ public:
const uint8_t pause_ms; // How many ms to pause _after_ writing this reg
} I2C_REG;
static bool writeRegistersBulk(const uint8_t address, const I2C_REG *registers, const uint8_t registersLength);
static bool wakePart(uint16_t DevAddress);
private:
static SemaphoreHandle_t I2CSemaphore;

View File

@@ -313,4 +313,15 @@ bool I2CBB2::writeRegistersBulk(const uint8_t address, const I2C_REG *registers,
}
return true;
}
bool I2CBB2::wakePart(uint16_t DevAddress) {
// wakepart is a special case where only the device address is sent
if (!lock())
return false;
start();
bool ack = send(DevAddress);
stop();
unlock();
return ack;
}
#endif

View File

@@ -36,6 +36,7 @@ public:
const uint8_t pause_ms; // How many ms to pause _after_ writing this reg
} I2C_REG;
static bool writeRegistersBulk(const uint8_t address, const I2C_REG *registers, const uint8_t registersLength);
static bool wakePart(uint16_t DevAddress);
private:
static SemaphoreHandle_t I2CSemaphore;

View File

@@ -5,12 +5,12 @@
* Author: Ben V. Brown
*/
#include "MMA8652FC.hpp"
#include "accelerometers_common.h"
#include "cmsis_os.h"
#include <array>
#include "MMA8652FC.hpp"
#include "cmsis_os.h"
static const FRToSI2C::I2C_REG i2c_registers[] = {
static const ACCEL_I2C_CLASS::I2C_REG i2c_registers[] = {
{CTRL_REG2, 0, 0}, // Normal mode
{CTRL_REG2, 0x40, 2}, // Reset all registers to POR values
{FF_MT_CFG_REG, 0x78, 0}, // Enable motion detection for X, Y, Z axis, latch disabled
@@ -26,11 +26,11 @@ static const FRToSI2C::I2C_REG i2c_registers[] = {
{CTRL_REG1, 0x19, 0} // ODR=12 Hz, Active mode
};
bool MMA8652FC::initalize() { return FRToSI2C::writeRegistersBulk(MMA8652FC_I2C_ADDRESS, i2c_registers, sizeof(i2c_registers) / sizeof(i2c_registers[0])); }
bool MMA8652FC::initalize() { return ACCEL_I2C_CLASS::writeRegistersBulk(MMA8652FC_I2C_ADDRESS, i2c_registers, sizeof(i2c_registers) / sizeof(i2c_registers[0])); }
Orientation MMA8652FC::getOrientation() {
// First read the PL_STATUS register
uint8_t plStatus = FRToSI2C::I2C_RegisterRead(MMA8652FC_I2C_ADDRESS, PL_STATUS_REG);
uint8_t plStatus = ACCEL_I2C_CLASS::I2C_RegisterRead(MMA8652FC_I2C_ADDRESS, PL_STATUS_REG);
if ((plStatus & 0b10000000) == 0b10000000) {
plStatus >>= 1; // We don't need the up/down bit
plStatus &= 0x03; // mask to the two lower bits
@@ -47,11 +47,11 @@ Orientation MMA8652FC::getOrientation() {
void MMA8652FC::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) {
std::array<int16_t, 3> sensorData;
FRToSI2C::Mem_Read(MMA8652FC_I2C_ADDRESS, OUT_X_MSB_REG, reinterpret_cast<uint8_t *>(sensorData.begin()), sensorData.size() * sizeof(int16_t));
ACCEL_I2C_CLASS::Mem_Read(MMA8652FC_I2C_ADDRESS, OUT_X_MSB_REG, reinterpret_cast<uint8_t *>(sensorData.begin()), sensorData.size() * sizeof(int16_t));
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() { return FRToSI2C::probe(MMA8652FC_I2C_ADDRESS); }
bool MMA8652FC::detect() { return ACCEL_I2C_CLASS::probe(MMA8652FC_I2C_ADDRESS); }

View File

@@ -6,11 +6,13 @@
*/
#include "MSA301_defines.h"
#include "accelerometers_common.h"
#include <MSA301.h>
#define MSA301_I2C_ADDRESS 0x26 << 1
bool MSA301::detect() { return FRToSI2C::probe(MSA301_I2C_ADDRESS); }
static const FRToSI2C::I2C_REG i2c_registers[] = {
#define MSA301_I2C_ADDRESS 0x26 << 1
bool MSA301::detect() { return ACCEL_I2C_CLASS::probe(MSA301_I2C_ADDRESS); }
static const ACCEL_I2C_CLASS::I2C_REG i2c_registers[] = {
//
//
{MSA301_REG_ODR, 0b00001000, 1}, // X/Y/Z enabled @ 250Hz
@@ -21,11 +23,11 @@ static const FRToSI2C::I2C_REG i2c_registers[] = {
};
bool MSA301::initalize() { return FRToSI2C::writeRegistersBulk(MSA301_I2C_ADDRESS, i2c_registers, sizeof(i2c_registers) / sizeof(i2c_registers[0])); }
bool MSA301::initalize() { return ACCEL_I2C_CLASS::writeRegistersBulk(MSA301_I2C_ADDRESS, i2c_registers, sizeof(i2c_registers) / sizeof(i2c_registers[0])); }
Orientation MSA301::getOrientation() {
uint8_t temp = 0;
FRToSI2C::Mem_Read(MSA301_I2C_ADDRESS, MSA301_REG_ORIENT_STATUS, &temp, 1);
ACCEL_I2C_CLASS::Mem_Read(MSA301_I2C_ADDRESS, MSA301_REG_ORIENT_STATUS, &temp, 1);
switch (temp) {
case 112:
return Orientation::ORIENTATION_LEFT_HAND;
@@ -39,7 +41,7 @@ Orientation MSA301::getOrientation() {
void MSA301::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) {
uint8_t temp[6];
// Bulk read all 6 regs
FRToSI2C::Mem_Read(MSA301_I2C_ADDRESS, MSA301_REG_OUT_X_L, temp, 6);
ACCEL_I2C_CLASS::Mem_Read(MSA301_I2C_ADDRESS, MSA301_REG_OUT_X_L, temp, 6);
x = int16_t(((int16_t)temp[1]) << 8 | temp[0]) >> 2;
y = int16_t(((int16_t)temp[3]) << 8 | temp[2]) >> 2;
z = int16_t(((int16_t)temp[5]) << 8 | temp[4]) >> 2;

View File

@@ -46,9 +46,9 @@ extern "C" {
#define OLED_GRAM_START_FLIP 0
#define OLED_GRAM_END_FLIP 0x7F
#define OLED_VCOM_LAYOUT 0x12
#define OLED_VCOM_LAYOUT 0x12
#define OLED_SEGMENT_MAP_REVERSED
#define OLED_DIVIDER 0xD3
#define OLED_DIVIDER 0xD3
#else
@@ -59,14 +59,14 @@ extern "C" {
#define OLED_GRAM_START_FLIP 0
#define OLED_GRAM_END_FLIP 95
#define OLED_VCOM_LAYOUT 0x02
#define OLED_SEGMENT_MAP 0xA0
#define OLED_DIVIDER 0xD5
#define OLED_VCOM_LAYOUT 0x02
#define OLED_SEGMENT_MAP 0xA0
#define OLED_DIVIDER 0xD5
#endif /* OLED_128x32 */
#define OLED_ON 0xAF
#define OLED_OFF 0xAE
#define OLED_ON 0xAF
#define OLED_OFF 0xAE
#define FRAMEBUFFER_START 17
@@ -78,7 +78,10 @@ enum class FontStyle {
class OLED {
public:
enum DisplayState : bool { OFF = false, ON = true };
enum DisplayState : bool {
OFF = false,
ON = true
};
static void initialize(); // Startup the I2C coms (brings screen out of reset etc)
static bool isInitDone();
@@ -117,10 +120,10 @@ public:
static void setInverseDisplay(bool inverted);
static int16_t getCursorX() { return cursor_x; }
// Draw a string to the current location, with selected font; optionally - with MAX length only
static void print(const char *string, FontStyle fontStyle, uint8_t length = 255);
static void printWholeScreen(const char *string);
static void print(const char *string, FontStyle fontStyle, uint8_t length = 255);
static void printWholeScreen(const char *string);
// Print *F or *C - in font style of Small, Large (by default) or Extra based on input arg
static void printSymbolDeg(FontStyle fontStyle = FontStyle::LARGE);
static void printSymbolDeg(FontStyle fontStyle = FontStyle::LARGE);
// Set the cursor location by pixels
static void setCursor(int16_t x, int16_t y) {
cursor_x = x;

View File

@@ -6,6 +6,7 @@
*/
#include "LIS2DH12_defines.hpp"
#include "accelerometers_common.h"
#include <SC7A20.hpp>
#include <SC7A20_defines.h>
#include <array>
@@ -17,20 +18,20 @@ bool SC7A20::isInImitationMode;
*/
bool SC7A20::detect() {
if (FRToSI2C::probe(SC7A20_ADDRESS)) {
if (ACCEL_I2C_CLASS::probe(SC7A20_ADDRESS)) {
// Read chip id to ensure its not an address collision
uint8_t id = 0;
if (FRToSI2C::Mem_Read(SC7A20_ADDRESS, SC7A20_WHO_AMI_I, &id, 1)) {
if (ACCEL_I2C_CLASS::Mem_Read(SC7A20_ADDRESS, SC7A20_WHO_AMI_I, &id, 1)) {
if (id == SC7A20_WHO_AM_I_VALUE) {
isInImitationMode = false;
return true;
}
}
}
if (FRToSI2C::probe(SC7A20_ADDRESS2)) {
if (ACCEL_I2C_CLASS::probe(SC7A20_ADDRESS2)) {
// Read chip id to ensure its not an address collision
uint8_t id = 0;
if (FRToSI2C::Mem_Read(SC7A20_ADDRESS2, SC7A20_WHO_AMI_I, &id, 1)) {
if (ACCEL_I2C_CLASS::Mem_Read(SC7A20_ADDRESS2, SC7A20_WHO_AMI_I, &id, 1)) {
if (id == SC7A20_WHO_AM_I_VALUE) {
isInImitationMode = true;
return true;
@@ -40,7 +41,7 @@ bool SC7A20::detect() {
return false;
}
static const FRToSI2C::I2C_REG i2c_registers[] = {
static const ACCEL_I2C_CLASS::I2C_REG i2c_registers[] = {
//
//
{SC7A20_CTRL_REG1, 0b01100111, 0}, // 200Hz, XYZ enabled
@@ -59,19 +60,19 @@ static const FRToSI2C::I2C_REG i2c_registers[] = {
//
};
static const FRToSI2C::I2C_REG i2c_registers_alt[] = {{LIS_CTRL_REG1, 0b00110111, 0}, // 200Hz XYZ
{LIS_CTRL_REG2, 0b00000000, 0}, //
{LIS_CTRL_REG3, 0b01100000, 0}, // Setup interrupt pins
{LIS_CTRL_REG4, 0b00001000, 0}, // Block update mode off, HR on
{LIS_CTRL_REG5, 0b00000010, 0}, //
{LIS_CTRL_REG6, 0b01100010, 0},
// Basically setup the unit to run, and enable 4D orientation detection
{LIS_INT2_CFG, 0b01111110, 0}, // setup for movement detection
{LIS_INT2_THS, 0x28, 0}, //
{LIS_INT2_DURATION, 64, 0}, //
{LIS_INT1_CFG, 0b01111110, 0}, //
{LIS_INT1_THS, 0x28, 0}, //
{LIS_INT1_DURATION, 64, 0}};
static const ACCEL_I2C_CLASS::I2C_REG i2c_registers_alt[] = {{LIS_CTRL_REG1, 0b00110111, 0}, // 200Hz XYZ
{LIS_CTRL_REG2, 0b00000000, 0}, //
{LIS_CTRL_REG3, 0b01100000, 0}, // Setup interrupt pins
{LIS_CTRL_REG4, 0b00001000, 0}, // Block update mode off, HR on
{LIS_CTRL_REG5, 0b00000010, 0}, //
{LIS_CTRL_REG6, 0b01100010, 0},
// Basically setup the unit to run, and enable 4D orientation detection
{LIS_INT2_CFG, 0b01111110, 0}, // setup for movement detection
{LIS_INT2_THS, 0x28, 0}, //
{LIS_INT2_DURATION, 64, 0}, //
{LIS_INT1_CFG, 0b01111110, 0}, //
{LIS_INT1_THS, 0x28, 0}, //
{LIS_INT1_DURATION, 64, 0}};
bool SC7A20::initalize() {
// Setup acceleration readings
@@ -83,9 +84,9 @@ bool SC7A20::initalize() {
// Hysteresis is set to ~ 16 counts
// Theta blocking is set to 0b10
if (isInImitationMode) {
return FRToSI2C::writeRegistersBulk(SC7A20_ADDRESS2, i2c_registers_alt, sizeof(i2c_registers_alt) / sizeof(i2c_registers_alt[0]));
return ACCEL_I2C_CLASS::writeRegistersBulk(SC7A20_ADDRESS2, i2c_registers_alt, sizeof(i2c_registers_alt) / sizeof(i2c_registers_alt[0]));
} else {
return FRToSI2C::writeRegistersBulk(SC7A20_ADDRESS, i2c_registers, sizeof(i2c_registers) / sizeof(i2c_registers[0]));
return ACCEL_I2C_CLASS::writeRegistersBulk(SC7A20_ADDRESS, i2c_registers, sizeof(i2c_registers) / sizeof(i2c_registers[0]));
}
}
@@ -93,7 +94,7 @@ void SC7A20::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) {
// We can tell the accelerometer to output in LE mode which makes this simple
uint16_t sensorData[3] = {0, 0, 0};
if (FRToSI2C::Mem_Read(isInImitationMode ? SC7A20_ADDRESS2 : SC7A20_ADDRESS, isInImitationMode ? SC7A20_OUT_X_L_ALT : SC7A20_OUT_X_L, (uint8_t *)sensorData, 6) == false) {
if (ACCEL_I2C_CLASS::Mem_Read(isInImitationMode ? SC7A20_ADDRESS2 : SC7A20_ADDRESS, isInImitationMode ? SC7A20_OUT_X_L_ALT : SC7A20_OUT_X_L, (uint8_t *)sensorData, 6) == false) {
x = y = z = 0;
return;
}

View File

@@ -10,6 +10,7 @@
#include "BSP.h"
#include "I2C_Wrapper.hpp"
#include "SC7A20_defines.h"
#include "accelerometers_common.h"
class SC7A20 {
public:
@@ -17,7 +18,7 @@ public:
static bool initalize();
// 1 = rh, 2,=lh, 8=flat
static Orientation getOrientation() {
uint8_t val = ((FRToSI2C::I2C_RegisterRead(isInImitationMode ? SC7A20_ADDRESS2 : SC7A20_ADDRESS, SC7A20_INT2_SOURCE) >> 2) - 1);
uint8_t val = ((ACCEL_I2C_CLASS::I2C_RegisterRead(isInImitationMode ? SC7A20_ADDRESS2 : SC7A20_ADDRESS, SC7A20_INT2_SOURCE) >> 2) - 1);
if (val == 1) {
#ifdef SC7_ORI_FLIP
return Orientation::ORIENTATION_RIGHT_HAND;

View File

@@ -10,17 +10,17 @@
* This class is licensed as MIT to match this code base
*/
#include "I2C_Wrapper.hpp"
#include "Si7210_defines.h"
#include "accelerometers_common.h"
#include <Si7210.h>
bool Si7210::detect() { return FRToSI2C::wakePart(SI7210_ADDRESS); }
bool Si7210::detect() { return ACCEL_I2C_CLASS::wakePart(SI7210_ADDRESS); }
bool Si7210::init() {
// Turn on auto increment and sanity check ID
// Load OTP cal
uint8_t temp;
if (FRToSI2C::Mem_Read(SI7210_ADDRESS, SI7210_REG_ID, &temp, 1)) {
if (ACCEL_I2C_CLASS::Mem_Read(SI7210_ADDRESS, SI7210_REG_ID, &temp, 1)) {
// We don't really care what model it is etc, just probing to check its probably this iC
if (temp != 0x00 && temp != 0xFF) {
temp = 0x00;
@@ -77,10 +77,10 @@ bool Si7210::write_reg(const uint8_t reg, const uint8_t mask, const uint8_t val)
temp &= mask;
}
temp |= val;
return FRToSI2C::Mem_Write(SI7210_ADDRESS, reg, &temp, 1);
return ACCEL_I2C_CLASS::Mem_Write(SI7210_ADDRESS, reg, &temp, 1);
}
bool Si7210::read_reg(const uint8_t reg, uint8_t *val) { return FRToSI2C::Mem_Read(SI7210_ADDRESS, reg, val, 1); }
bool Si7210::read_reg(const uint8_t reg, uint8_t *val) { return ACCEL_I2C_CLASS::Mem_Read(SI7210_ADDRESS, reg, val, 1); }
bool Si7210::start_periodic_measurement() {
/* Enable periodic wakeup */
@@ -95,7 +95,7 @@ bool Si7210::start_periodic_measurement() {
bool Si7210::get_field_strength(int16_t *field) {
*field = 0;
uint8_t val = 0;
FRToSI2C::wakePart(SI7210_ADDRESS);
ACCEL_I2C_CLASS::wakePart(SI7210_ADDRESS);
if (!write_reg(SI7210_POWER_CTRL, MEAS_MASK | USESTORE_MASK, STOP_MASK))
return false;

View File

@@ -1,6 +1,6 @@
#ifndef CORE_DRIVERS_ACCELEROMTERS_COMMON_H_
#define CORE_DRIVERS_ACCELEROMTERS_COMMON_H_
#include "configuration.h"
#if defined(ACCEL_I2CBB2)
#include "I2CBB2.hpp"
#define ACCEL_I2C_CLASS I2CBB2