Skip to content

Commit b7f561b

Browse files
committed
refactored imu headers
1 parent c5eecfd commit b7f561b

File tree

8 files changed

+81
-57
lines changed

8 files changed

+81
-57
lines changed

tracking/imu/imu.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
#include "imu.h"
22

3-
bool bmi160_begin();
4-
int bmi160_read(double* vec);
5-
6-
bool lsm6ds3trc_begin();
7-
void lsm6ds3trc_end();
8-
int lsm6ds3trc_read(double* vec);
9-
103
bool imu_begin() {
114
furi_hal_i2c_acquire(&furi_hal_i2c_handle_external);
125
bool ret = lsm6dso_begin(); // lsm6ds3trc_begin();
@@ -26,3 +19,13 @@ int imu_read(double* vec) {
2619
furi_hal_i2c_release(&furi_hal_i2c_handle_external);
2720
return ret;
2821
}
22+
23+
void imu_scan_i2c() {
24+
unsigned int address;
25+
unsigned int *found;
26+
for(address = 1; address < 0xff; address++) {
27+
if(furi_hal_i2c_is_device_ready(&furi_hal_i2c_handle_external, address, 50)) {
28+
FURI_LOG_E(IMU_TAG, "<<<<<<<found Device>>>>>>> ID 0x%X", address);
29+
}
30+
}
31+
}

tracking/imu/imu.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
#pragma once
1+
#ifndef IMU_H
2+
#define IMU_H
23

34
#include <stdbool.h>
45

56
#include <furi_hal.h>
67

8+
#include "imu_bmi160.h"
9+
#include "imu_lsm6ds3trc.h"
710
#include "imu_lsm6dso.h"
811

912
#ifdef __cplusplus
@@ -13,10 +16,13 @@ extern "C" {
1316
#define ACC_DATA_READY (1 << 0)
1417
#define GYR_DATA_READY (1 << 1)
1518

19+
#define IMU_TAG "IMU_H"
20+
1621
bool imu_begin();
1722
void imu_end();
1823
int imu_read(double* vec);
1924

2025
#ifdef __cplusplus
2126
}
2227
#endif
28+
#endif

tracking/imu/imu_bmi160.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
#include "../../lib/bmi160-api/bmi160.h"
2-
3-
#include <furi_hal.h>
4-
5-
#include "imu.h"
6-
7-
#define TAG "BMI160"
8-
9-
#define BMI160_DEV_ADDR (0x69 << 1)
1+
#include "imu_bmi160.h"
102

113
static const double DEG_TO_RAD = 0.017453292519943295769236907684886;
124
static const double G = 9.81;
@@ -28,14 +20,14 @@ int8_t bmi160_read_i2c(uint8_t dev_addr, uint8_t reg_addr, uint8_t* read_data, u
2820
}
2921

3022
bool bmi160_begin() {
31-
FURI_LOG_I(TAG, "Init BMI160");
23+
FURI_LOG_I(BMI160_TAG, "Init BMI160");
3224

3325
if(!furi_hal_i2c_is_device_ready(&furi_hal_i2c_handle_external, BMI160_DEV_ADDR, 50)) {
34-
FURI_LOG_E(TAG, "Device not ready!");
26+
FURI_LOG_E(BMI160_TAG, "Device not ready!");
3527
return false;
3628
}
3729

38-
FURI_LOG_I(TAG, "Device ready!");
30+
FURI_LOG_I(BMI160_TAG, "Device ready!");
3931

4032
bmi160dev.id = BMI160_DEV_ADDR;
4133
bmi160dev.intf = BMI160_I2C_INTF;
@@ -44,8 +36,8 @@ bool bmi160_begin() {
4436
bmi160dev.delay_ms = furi_delay_ms;
4537

4638
if(bmi160_init(&bmi160dev) != BMI160_OK) {
47-
FURI_LOG_E(TAG, "Initialization failure!");
48-
FURI_LOG_E(TAG, "Chip ID 0x%X", bmi160dev.chip_id);
39+
FURI_LOG_E(BMI160_TAG, "Initialization failure!");
40+
FURI_LOG_E(BMI160_TAG, "Chip ID 0x%X", bmi160dev.chip_id);
4941
return false;
5042
}
5143

@@ -59,13 +51,13 @@ bool bmi160_begin() {
5951
bmi160dev.gyro_cfg.power = BMI160_GYRO_NORMAL_MODE;
6052

6153
if(bmi160_set_sens_conf(&bmi160dev) != BMI160_OK) {
62-
FURI_LOG_E(TAG, "Initialization failure!");
63-
FURI_LOG_E(TAG, "Chip ID 0x%X", bmi160dev.chip_id);
54+
FURI_LOG_E(BMI160_TAG, "Initialization failure!");
55+
FURI_LOG_E(BMI160_TAG, "Chip ID 0x%X", bmi160dev.chip_id);
6456
return false;
6557
}
6658

67-
FURI_LOG_I(TAG, "Initialization success!");
68-
FURI_LOG_I(TAG, "Chip ID 0x%X", bmi160dev.chip_id);
59+
FURI_LOG_I(BMI160_TAG, "Initialization success!");
60+
FURI_LOG_I(BMI160_TAG, "Chip ID 0x%X", bmi160dev.chip_id);
6961

7062
return true;
7163
}

tracking/imu/imu_bmi160.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef IMU_BMI160_H
2+
#define IMU_BMI160_H
3+
4+
#include "../../lib/bmi160-api/bmi160.h"
5+
#include <furi_hal.h>
6+
#include "imu.h"
7+
8+
// Define constants
9+
#define BMI160_TAG "BMI160"
10+
#define BMI160_DEV_ADDR (0x69 << 1)
11+
12+
// Function declarations
13+
int8_t bmi160_write_i2c(uint8_t dev_addr, uint8_t reg_addr, uint8_t* data, uint16_t len);
14+
int8_t bmi160_read_i2c(uint8_t dev_addr, uint8_t reg_addr, uint8_t* read_data, uint16_t len);
15+
bool bmi160_begin(void);
16+
int bmi160_read(double* vec);
17+
18+
#endif // IMU_BMI160_H

tracking/imu/imu_lsm6ds3trc.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
#include "../../lib/lsm6ds3tr-api/lsm6ds3tr-c_reg.h"
1+
#include "imu_lsm6ds3trc.h"
22

3-
#include <furi_hal.h>
4-
5-
#include "imu.h"
6-
7-
#define TAG "LSM6DS3TR-C"
8-
9-
#define LSM6DS3_ADDRESS (0x6A << 1)
10-
11-
static const double DEG_TO_RAD = 0.017453292519943295769236907684886;
3+
static const double LSM6DS3_DEG_TO_RAD = 0.017453292519943295769236907684886;
124

135
stmdev_ctx_t lsm6ds3trc_ctx;
146

@@ -24,10 +16,10 @@ int32_t lsm6ds3trc_read_i2c(void* handle, uint8_t reg_addr, uint8_t* read_data,
2416
}
2517

2618
bool lsm6ds3trc_begin() {
27-
FURI_LOG_I(TAG, "Init LSM6DS3TR-C");
19+
FURI_LOG_I(LSM6DS3_TAG, "Init LSM6DS3TR-C");
2820

2921
if(!furi_hal_i2c_is_device_ready(&furi_hal_i2c_handle_external, LSM6DS3_ADDRESS, 50)) {
30-
FURI_LOG_E(TAG, "Not ready");
22+
FURI_LOG_E(LSM6DS3_TAG, "Not ready");
3123
return false;
3224
}
3325

@@ -39,7 +31,7 @@ bool lsm6ds3trc_begin() {
3931
uint8_t whoami;
4032
lsm6ds3tr_c_device_id_get(&lsm6ds3trc_ctx, &whoami);
4133
if(whoami != LSM6DS3TR_C_ID) {
42-
FURI_LOG_I(TAG, "Unknown model: %x", (int)whoami);
34+
FURI_LOG_I(LSM6DS3_TAG, "Unknown model: %x", (int)whoami);
4335
return false;
4436
}
4537

@@ -59,7 +51,7 @@ bool lsm6ds3trc_begin() {
5951
lsm6ds3tr_c_gy_power_mode_set(&lsm6ds3trc_ctx, LSM6DS3TR_C_GY_HIGH_PERFORMANCE);
6052
lsm6ds3tr_c_gy_band_pass_set(&lsm6ds3trc_ctx, LSM6DS3TR_C_LP2_ONLY);
6153

62-
FURI_LOG_I(TAG, "Init OK");
54+
FURI_LOG_I(LSM6DS3_TAG, "Init OK");
6355
return true;
6456
}
6557

@@ -84,9 +76,9 @@ int lsm6ds3trc_read(double* vec) {
8476

8577
if(reg.status_reg.gda) {
8678
lsm6ds3tr_c_angular_rate_raw_get(&lsm6ds3trc_ctx, data);
87-
vec[5] = (double)lsm6ds3tr_c_from_fs2000dps_to_mdps(data[0]) * DEG_TO_RAD / 1000;
88-
vec[3] = (double)lsm6ds3tr_c_from_fs2000dps_to_mdps(data[1]) * DEG_TO_RAD / 1000;
89-
vec[4] = (double)lsm6ds3tr_c_from_fs2000dps_to_mdps(data[2]) * DEG_TO_RAD / 1000;
79+
vec[5] = (double)lsm6ds3tr_c_from_fs2000dps_to_mdps(data[0]) * LSM6DS3_DEG_TO_RAD / 1000;
80+
vec[3] = (double)lsm6ds3tr_c_from_fs2000dps_to_mdps(data[1]) * LSM6DS3_DEG_TO_RAD / 1000;
81+
vec[4] = (double)lsm6ds3tr_c_from_fs2000dps_to_mdps(data[2]) * LSM6DS3_DEG_TO_RAD / 1000;
9082
ret |= GYR_DATA_READY;
9183
}
9284

tracking/imu/imu_lsm6ds3trc.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef IMU_LSM6DS3TRC_H
2+
#define IMU_LSM6DS3TRC_H
3+
4+
#include "../../lib/lsm6ds3tr-api/lsm6ds3tr-c_reg.h"
5+
6+
#include <furi_hal.h>
7+
8+
#include "imu.h"
9+
10+
#define LSM6DS3_TAG "LSM6DS3TR-C"
11+
12+
#define LSM6DS3_ADDRESS (0x6A << 1)
13+
14+
// External variable declaration
15+
extern stmdev_ctx_t lsm6ds3trc_ctx;
16+
17+
// Function declarations
18+
int32_t lsm6ds3trc_write_i2c(void* handle, uint8_t reg_addr, const uint8_t* data, uint16_t len);
19+
int32_t lsm6ds3trc_read_i2c(void* handle, uint8_t reg_addr, uint8_t* read_data, uint16_t len);
20+
bool lsm6ds3trc_begin(void);
21+
void lsm6ds3trc_end(void);
22+
int lsm6ds3trc_read(double* vec);
23+
24+
25+
#endif // LSM6DS3TRC_H

tracking/imu/imu_lsm6dso.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@ int32_t lsm6dso_read_i2c(void* handle, uint8_t reg_addr, uint8_t* read_data, uin
1414
return -2;
1515
}
1616

17-
void lsm6dso_scan_i2c() {
18-
unsigned int address;
19-
for(address = 1; address < 0xff; address++) {
20-
if(!furi_hal_i2c_is_device_ready(&furi_hal_i2c_handle_external, address, 50)) {
21-
FURI_LOG_E(LSM6DSO_TAG, "No answer on ID 0x%X", address);
22-
} else {
23-
FURI_LOG_E(LSM6DSO_TAG, "<<<<<<<found Device>>>>>>> ID 0x%X", address);
24-
}
25-
}
26-
}
27-
2817
bool lsm6dso_begin() {
2918
FURI_LOG_I(LSM6DSO_TAG, "Init LSM6DSOTR-C");
3019

tracking/imu/imu_lsm6dso.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef LSM6DSO_H_ /* Include guard */
2-
#define LSM6DSO_H_
1+
#ifndef IMU_LSM6DSO_H /* Include guard */
2+
#define IMU_LSM6DSO_H
33

44
#include "../../lib/lsm6dso-api/lsm6dso_reg.h"
55

@@ -17,7 +17,6 @@ int32_t lsm6dso_write_i2c(void* handle, uint8_t reg_addr, uint8_t* data, uint16_
1717
int32_t lsm6dso_read_i2c(void* handle, uint8_t reg_addr, uint8_t* read_data, uint16_t len);
1818

1919
bool lsm6dso_begin();
20-
void lsm6dso_scan_i2c();
2120
void lsm6dso_end();
2221
int lsm6dso_read(double* vec);
2322

0 commit comments

Comments
 (0)