From ba119007ffc7360a0da50357030ba84ec387b93b Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Sat, 12 Feb 2022 11:04:28 +1100 Subject: [PATCH 1/3] Step 1: make LIS2DH12 driver stricter --- source/Core/Drivers/LIS2DH12.cpp | 12 ++++++++- source/Core/Drivers/LIS2DH12_defines.hpp | 31 ++++++++++++------------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/source/Core/Drivers/LIS2DH12.cpp b/source/Core/Drivers/LIS2DH12.cpp index 6aea8ba2b7..a75f802813 100644 --- a/source/Core/Drivers/LIS2DH12.cpp +++ b/source/Core/Drivers/LIS2DH12.cpp @@ -36,4 +36,14 @@ void LIS2DH12::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) { z = sensorData[2]; } -bool LIS2DH12::detect() { return FRToSI2C::probe(LIS2DH_I2C_ADDRESS); } +bool LIS2DH12::detect() { + if (!FRToSI2C::probe(LIS2DH_I2C_ADDRESS)) { + return false; + } + // Read chip id to ensure its not an address collision + uint8_t id = 0; + if (FRToSI2C::Mem_Read(LIS2DH_I2C_ADDRESS, LIS2DH_WHOAMI_REG, &id, 1)) { + return id == LIS2DH_WHOAMI_ID; + } + return false; // cant read ID +} diff --git a/source/Core/Drivers/LIS2DH12_defines.hpp b/source/Core/Drivers/LIS2DH12_defines.hpp index 9a80e3df27..99053f84d8 100644 --- a/source/Core/Drivers/LIS2DH12_defines.hpp +++ b/source/Core/Drivers/LIS2DH12_defines.hpp @@ -9,19 +9,20 @@ #define LIS2DH12_DEFINES_HPP_ #define LIS2DH_I2C_ADDRESS (25 << 1) - -#define LIS_CTRL_REG1 0x20 | 0x80 -#define LIS_CTRL_REG2 0x21 | 0x80 -#define LIS_CTRL_REG3 0x22 | 0x80 -#define LIS_CTRL_REG4 0x23 | 0x80 -#define LIS_CTRL_REG5 0x24 | 0x80 -#define LIS_CTRL_REG6 0x25 | 0x80 -#define LIS_INT1_CFG 0xB0 | 0x80 -#define LIS_INT2_CFG 0xB4 | 0x80 -#define LIS_INT1_DURATION 0x33 | 0x80 -#define LIS_INT1_THS 0x32 | 0x80 -#define LIS_INT1_SRC 0x31 | 0x80 -#define LIS_INT2_DURATION 0x37 | 0x80 -#define LIS_INT2_THS 0x36 | 0x80 -#define LIS_INT2_SRC 0x35 | 0x80 +#define LIS2DH_WHOAMI_REG 0x0F +#define LIS2DH_WHOAMI_ID (0b00110011) +#define LIS_CTRL_REG1 0x20 | 0x80 +#define LIS_CTRL_REG2 0x21 | 0x80 +#define LIS_CTRL_REG3 0x22 | 0x80 +#define LIS_CTRL_REG4 0x23 | 0x80 +#define LIS_CTRL_REG5 0x24 | 0x80 +#define LIS_CTRL_REG6 0x25 | 0x80 +#define LIS_INT1_CFG 0xB0 | 0x80 +#define LIS_INT2_CFG 0xB4 | 0x80 +#define LIS_INT1_DURATION 0x33 | 0x80 +#define LIS_INT1_THS 0x32 | 0x80 +#define LIS_INT1_SRC 0x31 | 0x80 +#define LIS_INT2_DURATION 0x37 | 0x80 +#define LIS_INT2_THS 0x36 | 0x80 +#define LIS_INT2_SRC 0x35 | 0x80 #endif /* LIS2DH12_DEFINES_HPP_ */ From 730f7cf41567a2b3fbc6b9187d30c57bbdeac470 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Sat, 12 Feb 2022 11:13:41 +1100 Subject: [PATCH 2/3] SC7A20 handle address --- source/Core/Drivers/SC7A20.cpp | 20 ++++++-- source/Core/Drivers/SC7A20.hpp | 3 +- source/Core/Drivers/SC7A20_defines.h | 74 +++++++++++++++------------- 3 files changed, 58 insertions(+), 39 deletions(-) diff --git a/source/Core/Drivers/SC7A20.cpp b/source/Core/Drivers/SC7A20.cpp index b6b7ee4f63..cd37e584d9 100644 --- a/source/Core/Drivers/SC7A20.cpp +++ b/source/Core/Drivers/SC7A20.cpp @@ -9,15 +9,29 @@ #include #include +uint8_t SC7A20::activeAddress; + bool SC7A20::detect() { if (FRToSI2C::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)) { - return id == 0b00010001; + if (id == SC7A20_WHO_AM_I_VALUE) { + activeAddress = SC7A20_ADDRESS; + return true; + } + } + } + if (FRToSI2C::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 (id == SC7A20_WHO_AM_I_VALUE) { + activeAddress = SC7A20_ADDRESS2; + return true; + } } } - return false; } @@ -50,7 +64,7 @@ bool SC7A20::initalize() { // Hysteresis is set to ~ 16 counts // Theta blocking is set to 0b10 - return FRToSI2C::writeRegistersBulk(SC7A20_ADDRESS, i2c_registers, sizeof(i2c_registers) / sizeof(i2c_registers[0])); + return FRToSI2C::writeRegistersBulk(activeAddress, i2c_registers, sizeof(i2c_registers) / sizeof(i2c_registers[0])); } void SC7A20::getAxisReadings(int16_t &x, int16_t &y, int16_t &z) { diff --git a/source/Core/Drivers/SC7A20.hpp b/source/Core/Drivers/SC7A20.hpp index f7bba230af..f7cd9776ab 100644 --- a/source/Core/Drivers/SC7A20.hpp +++ b/source/Core/Drivers/SC7A20.hpp @@ -17,7 +17,7 @@ class SC7A20 { static bool initalize(); // 1 = rh, 2,=lh, 8=flat static Orientation getOrientation() { - uint8_t val = ((FRToSI2C::I2C_RegisterRead(SC7A20_ADDRESS, SC7A20_INT2_SOURCE) >> 2) - 1); + uint8_t val = ((FRToSI2C::I2C_RegisterRead(activeAddress, SC7A20_INT2_SOURCE) >> 2) - 1); if (val == 1) return Orientation::ORIENTATION_LEFT_HAND; else if (val == 4 || val == 0) @@ -28,6 +28,7 @@ class SC7A20 { static void getAxisReadings(int16_t &x, int16_t &y, int16_t &z); private: + static uint8_t activeAddress; }; #endif /* CORE_DRIVERS_BMA223_HPP_ */ diff --git a/source/Core/Drivers/SC7A20_defines.h b/source/Core/Drivers/SC7A20_defines.h index fc3d6d6d4a..85fc5720d4 100644 --- a/source/Core/Drivers/SC7A20_defines.h +++ b/source/Core/Drivers/SC7A20_defines.h @@ -1,5 +1,5 @@ /* - * BMA223_defines.h + * SC7A20_defines.h * * Created on: 18 Sep. 2020 * Author: Ralim @@ -8,39 +8,43 @@ #ifndef CORE_DRIVERS_SC7A20_DEFINES_H_ #define CORE_DRIVERS_SC7A20_DEFINES_H_ -#define SC7A20_ADDRESS 0x18 << 1 -#define SC7A20_WHO_AMI_I 0x0F -#define SC7A20_CTRL_REG1 0x20 -#define SC7A20_CTRL_REG2 0x21 -#define SC7A20_CTRL_REG3 0x22 -#define SC7A20_CTRL_REG4 0x23 -#define SC7A20_CTRL_REG5 0x24 -#define SC7A20_CTRL_REG6 0x25 -#define SC7A20_REFERENCE 0x26 -#define SC7A20_STATUS_REG 0x27 -#define SC7A20_OUT_X_L 0x28 -#define SC7A20_OUT_X_H 0x29 -#define SC7A20_OUT_Y_L 0x2A -#define SC7A20_OUT_Y_H 0x2B -#define SC7A20_OUT_Z_L 0x2C -#define SC7A20_OUT_Z_H 0x2D -#define SC7A20_FIFO_CTRL 0x2E -#define SC7A20_FIFO_SRC 0x2F -#define SC7A20_INT1_CFG 0x30 -#define SC7A20_INT1_SOURCE 0x31 -#define SC7A20_INT1_THS 0x32 -#define SC7A20_INT1_DURATION 0x33 -#define SC7A20_INT2_CFG 0x34 -#define SC7A20_INT2_SOURCE 0x35 -#define SC7A20_INT2_THS 0x36 -#define SC7A20_INT2_DURATION 0x37 -#define SC7A20_CLICK_CFG 0x38 -#define SC7A20_CLICK_SRC 0x39 -#define SC7A20_CLICK_THS 0x3A -#define SC7A20_TIME_LIMIT 0x3B -#define SC7A20_TIME_LATENCY 0x3C -#define SC7A20_TIME_WINDOW 0x3D -#define SC7A20_ACT_THS 0x3E -#define SC7A20_ACT_DURATION 0x3F +#define SC7A20_ADDRESS 0x18 << 1 +// Sometimes the SC7A20 turns up programmed to impersonate the LIS2DH12 +#define SC7A20_ADDRESS2 (25 << 1) + +#define SC7A20_WHO_AM_I_VALUE (0b00010001) +#define SC7A20_WHO_AMI_I 0x0F +#define SC7A20_CTRL_REG1 0x20 +#define SC7A20_CTRL_REG2 0x21 +#define SC7A20_CTRL_REG3 0x22 +#define SC7A20_CTRL_REG4 0x23 +#define SC7A20_CTRL_REG5 0x24 +#define SC7A20_CTRL_REG6 0x25 +#define SC7A20_REFERENCE 0x26 +#define SC7A20_STATUS_REG 0x27 +#define SC7A20_OUT_X_L 0x28 +#define SC7A20_OUT_X_H 0x29 +#define SC7A20_OUT_Y_L 0x2A +#define SC7A20_OUT_Y_H 0x2B +#define SC7A20_OUT_Z_L 0x2C +#define SC7A20_OUT_Z_H 0x2D +#define SC7A20_FIFO_CTRL 0x2E +#define SC7A20_FIFO_SRC 0x2F +#define SC7A20_INT1_CFG 0x30 +#define SC7A20_INT1_SOURCE 0x31 +#define SC7A20_INT1_THS 0x32 +#define SC7A20_INT1_DURATION 0x33 +#define SC7A20_INT2_CFG 0x34 +#define SC7A20_INT2_SOURCE 0x35 +#define SC7A20_INT2_THS 0x36 +#define SC7A20_INT2_DURATION 0x37 +#define SC7A20_CLICK_CFG 0x38 +#define SC7A20_CLICK_SRC 0x39 +#define SC7A20_CLICK_THS 0x3A +#define SC7A20_TIME_LIMIT 0x3B +#define SC7A20_TIME_LATENCY 0x3C +#define SC7A20_TIME_WINDOW 0x3D +#define SC7A20_ACT_THS 0x3E +#define SC7A20_ACT_DURATION 0x3F #endif /* CORE_DRIVERS_BMA223_DEFINES_H_ */ From 3747d22773996bdc6e10aab9d4ececabb30dbc34 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Sat, 12 Feb 2022 11:30:03 +1100 Subject: [PATCH 3/3] Flip SC7 --- source/Core/BSP/Miniware/configuration.h | 1 + source/Core/Drivers/SC7A20.hpp | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/source/Core/BSP/Miniware/configuration.h b/source/Core/BSP/Miniware/configuration.h index 7b5d88d65c..0218394938 100644 --- a/source/Core/BSP/Miniware/configuration.h +++ b/source/Core/BSP/Miniware/configuration.h @@ -210,6 +210,7 @@ #define TEMP_NTC #define I2C_SOFT #define LIS_ORI_FLIP +#define SC7_ORI_FLIP #define OLED_FLIP #endif diff --git a/source/Core/Drivers/SC7A20.hpp b/source/Core/Drivers/SC7A20.hpp index f7cd9776ab..88a942cb33 100644 --- a/source/Core/Drivers/SC7A20.hpp +++ b/source/Core/Drivers/SC7A20.hpp @@ -18,11 +18,19 @@ class SC7A20 { // 1 = rh, 2,=lh, 8=flat static Orientation getOrientation() { uint8_t val = ((FRToSI2C::I2C_RegisterRead(activeAddress, SC7A20_INT2_SOURCE) >> 2) - 1); - if (val == 1) + if (val == 1) { +#ifdef SC7_ORI_FLIP + return Orientation::ORIENTATION_RIGHT_HAND; +#else + return Orientation::ORIENTATION_LEFT_HAND; +#endif + } else if (val == 4 || val == 0) { +#ifdef SC7_ORI_FLIP return Orientation::ORIENTATION_LEFT_HAND; - else if (val == 4 || val == 0) +#else return Orientation::ORIENTATION_RIGHT_HAND; - else +#endif + } else return Orientation::ORIENTATION_FLAT; } static void getAxisReadings(int16_t &x, int16_t &y, int16_t &z);