Ivan Podogov 1 год назад
Родитель
Сommit
4170177508

+ 9 - 0
README.md

@@ -36,6 +36,15 @@ See early prototype [in action](https://www.youtube.com/watch?v=DdxAmmsYfMA).
 
 The custom module is using Bosch BMI160 accelerometer/gyroscope chip connected via I2C.
 
+Note: in fact, some other IMU chips are also supported.
+It's detected via the first found I2C Address.
+
+|   Chip   | Expected I2C Address |
+|:--------:|:--------------------:|
+|  BMI160  |         0x69         |
+| LSM6DS3  |         0x6A         |
+| LSM6DSO  |         0x6B         |
+
 Take a look into the [schematic](https://github.com/ginkage/FlippAirMouse/tree/main/schematic) folder for Gerber, BOM and CPL files, so you can order directly from JLCPCB.
 
 Original idea:

+ 3 - 3
tracking/imu/bmi160.c

@@ -1,4 +1,6 @@
-#include "bmi160.h"
+#include <furi_hal.h>
+#include "imu.h"
+#include "../../lib/bmi160-api/bmi160.h"
 
 #define BMI160_TAG "BMI160"
 #define BMI160_DEV_ADDR (0x69 << 1)
@@ -89,5 +91,3 @@ struct imu_t imu_bmi160 = {
    bmi160_read,
    BMI160_TAG
 };
-
-

+ 0 - 17
tracking/imu/bmi160.h

@@ -1,17 +0,0 @@
-#ifndef IMU_BMI160_H
-#define IMU_BMI160_H
-
-#include "../../lib/bmi160-api/bmi160.h"
-#include <furi_hal.h>
-#include "imu.h"
-
-// Function declarations
-int8_t bmi160_write_i2c(uint8_t dev_addr, uint8_t reg_addr, uint8_t* data, uint16_t len);
-int8_t bmi160_read_i2c(uint8_t dev_addr, uint8_t reg_addr, uint8_t* read_data, uint16_t len);
-bool bmi160_begin(void);
-void bmi160_end(void);
-int bmi160_read(double* vec);
-
-extern struct imu_t imu_bmi160;
-
-#endif // IMU_BMI160_H

+ 12 - 7
tracking/imu/imu.c

@@ -1,25 +1,30 @@
 #include "imu.h"
 
+#define IMU_TAG "IMU_H"
+
+extern struct imu_t imu_bmi160;
+extern struct imu_t imu_lsm6ds3trc;
+extern struct imu_t imu_lsm6dso;
+
 struct imu_t* imu_types[] = {
     &imu_bmi160,
     &imu_lsm6ds3trc,
     &imu_lsm6dso
 };
 
+static const int imu_count = sizeof(imu_types) / sizeof(struct imu_t*);
+
 struct imu_t* imu_found;
 
 bool imu_begin() {
     furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
     if (imu_found == NULL) {
         imu_found = find_imu();
-        if (imu_found == NULL) {
-            furi_hal_i2c_release(&furi_hal_i2c_handle_external);
-            return false;
-        }
-        FURI_LOG_E(IMU_TAG, "Found Device %s", imu_found->name);
     }
-    bool ret = 0;
+
+    bool ret = false;
     if (imu_found != NULL) {
+        FURI_LOG_E(IMU_TAG, "Found Device %s", imu_found->name);
         ret = imu_found->begin();
     }
     furi_hal_i2c_release(&furi_hal_i2c_handle_external);
@@ -43,7 +48,7 @@ int imu_read(double* vec) {
 
 struct imu_t* find_imu() {
     unsigned int i;
-    for(i = 0; i < sizeof(imu_types) / sizeof(struct imu_t*); i++) {
+    for(i = 0; i < imu_count; i++) {
         if(furi_hal_i2c_is_device_ready(&furi_hal_i2c_handle_external, imu_types[i]->address, 50)) {
             FURI_LOG_E(IMU_TAG, "found i2c device address 0x%X", imu_types[i]->address);
             return imu_types[i];

+ 9 - 7
tracking/imu/imu.h

@@ -4,20 +4,22 @@
 #include <stdbool.h>
 #include <furi_hal.h>
 
-#include "imu_t.h"
-#include "bmi160.h"
-#include "lsm6ds3trc.h"
-#include "lsm6dso.h"
-
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+struct imu_t
+{
+    unsigned int address;
+    bool (*begin)(void);
+    void (*end)(void);
+    int (*read)(double* vec);
+    char* name;
+};
+
 #define ACC_DATA_READY (1 << 0)
 #define GYR_DATA_READY (1 << 1)
 
-#define IMU_TAG "IMU_H"
-
 static const double DEG_TO_RAD = 0.017453292519943295769236907684886;
 static const double GRAVITY = 9.81;
 

+ 0 - 13
tracking/imu/imu_t.h

@@ -1,13 +0,0 @@
-#ifndef IMU_T_H   /* Include guard */
-#define IMU_T_H
-
-struct imu_t
-{
-    unsigned int address;
-    bool (*begin)(void);
-    void (*end)(void);
-    int (*read)(double* vec);
-    char* name;
-};
-
-#endif

+ 4 - 2
tracking/imu/lsm6ds3trc.c

@@ -1,4 +1,6 @@
-#include "lsm6ds3trc.h"
+#include <furi_hal.h>
+#include "imu.h"
+#include "../../lib/lsm6ds3tr-api/lsm6ds3tr-c_reg.h"
 
 #define LSM6DS3_TAG "LSM6DS3"
 #define LSM6DS3_DEV_ADDRESS (0x6A << 1)
@@ -92,4 +94,4 @@ struct imu_t imu_lsm6ds3trc = {
     lsm6ds3trc_end,
     lsm6ds3trc_read,
     LSM6DS3_TAG
-};
+};

+ 0 - 22
tracking/imu/lsm6ds3trc.h

@@ -1,22 +0,0 @@
-#ifndef IMU_LSM6DS3TRC_H
-#define IMU_LSM6DS3TRC_H
-
-#include "../../lib/lsm6ds3tr-api/lsm6ds3tr-c_reg.h"
-
-#include <furi_hal.h>
-
-#include "imu.h"
-
-// External variable declaration
-extern stmdev_ctx_t lsm6ds3trc_ctx;
-
-// Function declarations
-int32_t lsm6ds3trc_write_i2c(void* handle, uint8_t reg_addr, const uint8_t* data, uint16_t len);
-int32_t lsm6ds3trc_read_i2c(void* handle, uint8_t reg_addr, uint8_t* read_data, uint16_t len);
-bool lsm6ds3trc_begin(void);
-void lsm6ds3trc_end(void);
-int lsm6ds3trc_read(double* vec);
-
-extern struct imu_t imu_lsm6ds3trc;
-
-#endif // LSM6DS3TRC_H

+ 3 - 1
tracking/imu/lsm6dso.c

@@ -1,4 +1,6 @@
-#include "lsm6dso.h"
+#include <furi_hal.h>
+#include "imu.h"
+#include "../../lib/lsm6dso-api/lsm6dso_reg.h"
 
 #define LSM6DSO_TAG "LSM6DO"
 #define LSM6DSO_DEV_ADDRESS (0x6B << 1)

+ 0 - 19
tracking/imu/lsm6dso.h

@@ -1,19 +0,0 @@
-#ifndef IMU_LSM6DSO_H
-#define IMU_LSM6DSO_H
-
-#include "../../lib/lsm6dso-api/lsm6dso_reg.h"
-
-#include <furi_hal.h>
-
-#include "imu.h"
-
-int32_t lsm6dso_write_i2c(void* handle, uint8_t reg_addr, uint8_t* data, uint16_t len);
-int32_t lsm6dso_read_i2c(void* handle, uint8_t reg_addr, uint8_t* read_data, uint16_t len);
-
-bool lsm6dso_begin();
-void lsm6dso_end();
-int lsm6dso_read(double* vec);
-
-extern struct imu_t imu_lsm6dso;
-
-#endif // IMU_LSM6DSO_H