From ee0385e380984bf1141a31061ba3c9802b9817e2 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Fri, 15 Jan 2021 21:26:39 +1100 Subject: [PATCH 1/7] Check chip ID on the BMA --- workspace/TS100/Core/Drivers/BMA223.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/workspace/TS100/Core/Drivers/BMA223.cpp b/workspace/TS100/Core/Drivers/BMA223.cpp index ac049b0774..4da4ae76f4 100755 --- a/workspace/TS100/Core/Drivers/BMA223.cpp +++ b/workspace/TS100/Core/Drivers/BMA223.cpp @@ -9,7 +9,15 @@ #include bool BMA223::detect() { - return FRToSI2C::probe(BMA223_ADDRESS); + if (FRToSI2C::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)) { + return id == 0b11111000; + } + } + + return false; } static const FRToSI2C::I2C_REG i2c_registers[] = { // From 7ba2bda27590c8e74fe56fa2db403887fcd54c9a Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Fri, 15 Jan 2021 22:02:07 +1100 Subject: [PATCH 2/7] Ballpark some SC7A20 code Using some guesses from the LIS datasheet --- workspace/TS100/Core/Drivers/SC7A20.cpp | 69 +++++++++++++++++++ workspace/TS100/Core/Drivers/SC7A20.hpp | 27 ++++++++ workspace/TS100/Core/Drivers/SC7A20_defines.h | 46 +++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 workspace/TS100/Core/Drivers/SC7A20.cpp create mode 100644 workspace/TS100/Core/Drivers/SC7A20.hpp create mode 100644 workspace/TS100/Core/Drivers/SC7A20_defines.h diff --git a/workspace/TS100/Core/Drivers/SC7A20.cpp b/workspace/TS100/Core/Drivers/SC7A20.cpp new file mode 100644 index 0000000000..7bebb29d21 --- /dev/null +++ b/workspace/TS100/Core/Drivers/SC7A20.cpp @@ -0,0 +1,69 @@ +/* + * SC7A20.cpp + * + * Created on: 18 Sep. 2020 + * Author: Ralim + */ + +#include +#include +#include + +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; + } + } + + return false; +} + +static const FRToSI2C::I2C_REG i2c_registers[] = { // + // + { SC7A20_CTRL_REG1, 0b01100111, 0 }, //200Hz, XYZ enabled + { SC7A20_CTRL_REG2, 0b00000000, 0 }, //Setup filter to 0x00 ?? + { SC7A20_CTRL_REG3, 0b00000000, 0 }, //int1 off + { SC7A20_CTRL_REG4, 0b01001000, 0 }, //Block mode off,little-endian,2G,High-pres,self test off + { SC7A20_CTRL_REG5, 0b00000100, 0 }, //fifo off, D4D on int1 + { SC7A20_CTRL_REG6, 0x00, 0 }, //INT2 off + //Basically setup the unit to run, and enable 4D orientation detection + { SC7A20_INT2_CFG, 0b01111110, 0 }, //setup for movement detection + { SC7A20_INT2_THS, 0x28, 0 }, // + { SC7A20_INT2_DURATION, 64, 0 }, // + { SC7A20_INT1_CFG, 0b01111110, 0 }, // + { SC7A20_INT1_THS, 0x28, 0 }, // + { SC7A20_INT1_DURATION, 64, 0 } + + // + }; +bool SC7A20::initalize() { + //Setup acceleration readings + //2G range + //bandwidth = 250Hz + //High pass filter on (Slow compensation) + //Turn off IRQ output pins + //Orientation recognition in symmetrical mode + // 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])); + +} + +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(SC7A20_ADDRESS, SC7A20_OUT_X_L, (uint8_t*) sensorData, 6) == false) { + x = y = z = 0; + return; + } + //Shift 6 to make its range ~= the other accelerometers + x = sensorData[0]; + y = sensorData[1]; + z = sensorData[2]; + +} diff --git a/workspace/TS100/Core/Drivers/SC7A20.hpp b/workspace/TS100/Core/Drivers/SC7A20.hpp new file mode 100644 index 0000000000..213c1898dc --- /dev/null +++ b/workspace/TS100/Core/Drivers/SC7A20.hpp @@ -0,0 +1,27 @@ +/* + * BMA223.hpp + * + * Created on: 18 Sep. 2020 + * Author: Ralim + */ + +#ifndef CORE_DRIVERS_BMA223_HPP_ +#define CORE_DRIVERS_BMA223_HPP_ +#include "I2C_Wrapper.hpp" +#include "BSP.h" +#include "SC7A20_defines.h" + +class SC7A20 { +public: + static bool detect(); + static bool initalize(); + //1 = rh, 2,=lh, 8=flat + static Orientation getOrientation() { + return static_cast((FRToSI2C::I2C_RegisterRead(SC7A20_ADDRESS,SC7A20_INT2_SOURCE) >> 2) - 1); + } + static void getAxisReadings(int16_t& x, int16_t& y, int16_t& z); + +private: +}; + +#endif /* CORE_DRIVERS_BMA223_HPP_ */ diff --git a/workspace/TS100/Core/Drivers/SC7A20_defines.h b/workspace/TS100/Core/Drivers/SC7A20_defines.h new file mode 100644 index 0000000000..1b74063ea4 --- /dev/null +++ b/workspace/TS100/Core/Drivers/SC7A20_defines.h @@ -0,0 +1,46 @@ +/* + * BMA223_defines.h + * + * Created on: 18 Sep. 2020 + * Author: Ralim + */ + +#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 + +#endif /* CORE_DRIVERS_BMA223_DEFINES_H_ */ From df2f97a1deb4fa17f4ce9bdad67c4d1da7e2b1b3 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Fri, 15 Jan 2021 22:07:26 +1100 Subject: [PATCH 3/7] Link into MOV thread --- workspace/TS100/Core/BSP/Pine64/Model_Config.h | 1 + workspace/TS100/Core/Threads/MOVThread.cpp | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/workspace/TS100/Core/BSP/Pine64/Model_Config.h b/workspace/TS100/Core/BSP/Pine64/Model_Config.h index 13846fc513..0c8577cf77 100755 --- a/workspace/TS100/Core/BSP/Pine64/Model_Config.h +++ b/workspace/TS100/Core/BSP/Pine64/Model_Config.h @@ -23,6 +23,7 @@ #define ENABLE_QC2 #define TEMP_TMP36 #define ACCEL_BMA +#define ACCEL_SC7 #define HALL_SENSOR #define HALL_SI7210 #define BATTFILTERDEPTH 32 diff --git a/workspace/TS100/Core/Threads/MOVThread.cpp b/workspace/TS100/Core/Threads/MOVThread.cpp index d35ebd89d9..90205a7816 100644 --- a/workspace/TS100/Core/Threads/MOVThread.cpp +++ b/workspace/TS100/Core/Threads/MOVThread.cpp @@ -21,6 +21,7 @@ #include "power.hpp" #include "stdlib.h" #include "task.h" +#include "SC7A20.hpp" #define MOVFilter 8 uint8_t accelInit = 0; TickType_t lastMovementTime = 0; @@ -56,6 +57,14 @@ void detectAccelerometerVersion() { DetectedAccelerometerVersion = 4; } } else +#endif +#ifdef ACCEL_SC7 + if (SC7A20::detect()) { + // Setup the SC7A20 Accelerometer + if (SC7A20::initalize()) { + DetectedAccelerometerVersion = 5; + } + } else #endif { // disable imu sensitivity @@ -86,6 +95,12 @@ inline void readAccelerometer(int16_t &tx, int16_t &ty, int16_t &tz, Orientation MSA301::getAxisReadings(tx, ty, tz); rotation = MSA301::getOrientation(); } else +#endif +#ifdef ACCEL_SC7 + if (DetectedAccelerometerVersion == 5) { + SC7A20::getAxisReadings(tx, ty, tz); + rotation = SC7A20::getOrientation(); + } else #endif { // do nothing :( From beef76666acea0e156c967686cea99cb3c57ddd7 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Fri, 15 Jan 2021 22:11:21 +1100 Subject: [PATCH 4/7] Typo --- workspace/TS100/Core/Drivers/SC7A20.hpp | 4 ++-- workspace/TS100/Core/Threads/MOVThread.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/workspace/TS100/Core/Drivers/SC7A20.hpp b/workspace/TS100/Core/Drivers/SC7A20.hpp index 213c1898dc..501b7260d8 100644 --- a/workspace/TS100/Core/Drivers/SC7A20.hpp +++ b/workspace/TS100/Core/Drivers/SC7A20.hpp @@ -5,8 +5,8 @@ * Author: Ralim */ -#ifndef CORE_DRIVERS_BMA223_HPP_ -#define CORE_DRIVERS_BMA223_HPP_ +#ifndef CORE_DRIVERS_SC7A20_HPP_ +#define CORE_DRIVERS_SC7A20_HPP_ #include "I2C_Wrapper.hpp" #include "BSP.h" #include "SC7A20_defines.h" diff --git a/workspace/TS100/Core/Threads/MOVThread.cpp b/workspace/TS100/Core/Threads/MOVThread.cpp index 90205a7816..c26e4294c1 100644 --- a/workspace/TS100/Core/Threads/MOVThread.cpp +++ b/workspace/TS100/Core/Threads/MOVThread.cpp @@ -6,6 +6,7 @@ */ #include "BMA223.hpp" +#include "SC7A20.hpp" #include "BSP.h" #include "FreeRTOS.h" #include "I2C_Wrapper.hpp" @@ -21,7 +22,6 @@ #include "power.hpp" #include "stdlib.h" #include "task.h" -#include "SC7A20.hpp" #define MOVFilter 8 uint8_t accelInit = 0; TickType_t lastMovementTime = 0; From b46d19b5f462787342f466774983f45c24b99814 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Fri, 15 Jan 2021 22:20:24 +1100 Subject: [PATCH 5/7] Flip orientation --- workspace/TS100/Core/Drivers/SC7A20.hpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/workspace/TS100/Core/Drivers/SC7A20.hpp b/workspace/TS100/Core/Drivers/SC7A20.hpp index 501b7260d8..a9d5ab80cc 100644 --- a/workspace/TS100/Core/Drivers/SC7A20.hpp +++ b/workspace/TS100/Core/Drivers/SC7A20.hpp @@ -17,9 +17,18 @@ class SC7A20 { static bool initalize(); //1 = rh, 2,=lh, 8=flat static Orientation getOrientation() { - return static_cast((FRToSI2C::I2C_RegisterRead(SC7A20_ADDRESS,SC7A20_INT2_SOURCE) >> 2) - 1); + uint8_t val = ((FRToSI2C::I2C_RegisterRead(SC7A20_ADDRESS, SC7A20_INT2_SOURCE) >> 2) - 1); + if (val == 8) + val = 3; + else if (val == 1) + val = 1; + else if (val == 2) + val = 0; + else + val = 3; + return static_cast(val); } - static void getAxisReadings(int16_t& x, int16_t& y, int16_t& z); + static void getAxisReadings(int16_t &x, int16_t &y, int16_t &z); private: }; From 7e9ab6ae8694e506dffe944df1338d14eb5ae7e1 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Fri, 15 Jan 2021 22:27:11 +1100 Subject: [PATCH 6/7] Flip the other way --- workspace/TS100/Core/Drivers/SC7A20.hpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/workspace/TS100/Core/Drivers/SC7A20.hpp b/workspace/TS100/Core/Drivers/SC7A20.hpp index a9d5ab80cc..757c4f5fc5 100644 --- a/workspace/TS100/Core/Drivers/SC7A20.hpp +++ b/workspace/TS100/Core/Drivers/SC7A20.hpp @@ -18,12 +18,10 @@ class SC7A20 { //1 = rh, 2,=lh, 8=flat static Orientation getOrientation() { uint8_t val = ((FRToSI2C::I2C_RegisterRead(SC7A20_ADDRESS, SC7A20_INT2_SOURCE) >> 2) - 1); - if (val == 8) - val = 3; - else if (val == 1) - val = 1; - else if (val == 2) + if (val == 1) val = 0; + else if (val == 0) + val = 1; else val = 3; return static_cast(val); From 75a2d7008094acb20c9c76a2cad893acedf751ae Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Fri, 15 Jan 2021 22:45:53 +1100 Subject: [PATCH 7/7] Fix orientation --- workspace/TS100/Core/Drivers/SC7A20.hpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/workspace/TS100/Core/Drivers/SC7A20.hpp b/workspace/TS100/Core/Drivers/SC7A20.hpp index 757c4f5fc5..f58214dac0 100644 --- a/workspace/TS100/Core/Drivers/SC7A20.hpp +++ b/workspace/TS100/Core/Drivers/SC7A20.hpp @@ -19,12 +19,11 @@ class SC7A20 { static Orientation getOrientation() { uint8_t val = ((FRToSI2C::I2C_RegisterRead(SC7A20_ADDRESS, SC7A20_INT2_SOURCE) >> 2) - 1); if (val == 1) - val = 0; - else if (val == 0) - val = 1; + return Orientation::ORIENTATION_LEFT_HAND; + else if (val == 4 || val == 0) + return Orientation::ORIENTATION_RIGHT_HAND; else - val = 3; - return static_cast(val); + return Orientation::ORIENTATION_FLAT; } static void getAxisReadings(int16_t &x, int16_t &y, int16_t &z);