From 455c4403a674b548e9e7e386e63d2d35752dd2fc Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 27 Sep 2024 12:55:04 -0400 Subject: [PATCH 1/8] generalized funcs --- general/include/lsm6dso.h | 40 +++++++++++++++++++++++++++++++++++++++ general/src/lsm6dso.c | 37 ++++++++++++++++++++++++++---------- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/general/include/lsm6dso.h b/general/include/lsm6dso.h index 362a0c2..4725017 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -44,6 +44,12 @@ #define LSM6DSO_REG_ACCEL_Z_AXIS_L 0x2C /* Accelerometer Z axis lower bits */ #define LSM6DSO_REG_ACCEL_Z_AXIS_H 0x2D /* Accelerometer Z axis upper bits */ +/* Function Pointers*/ +typedef uint8_t (*I2C_Read_Reg)(uint8_t *data, uint8_t reg); +typedef uint8_t (*I2C_Read_Mult_Reg)(uint8_t *data, uint8_t reg, + uint8_t length); +typedef uint8_t (*I2C_Write_Reg)(uint8_t *data, uint8_t reg); + /** * @brief Enumeration of axes of data available from the LSM6DSO IMU * @@ -70,6 +76,12 @@ typedef struct { int16_t gyro_data[3]; + I2C_Read_Reg lsm6dso_read_reg; + + I2C_Read_Mult_Reg lsm6dso_read_mult_reg; + + I2C_Write_Reg lsm6dso_write_reg; + } lsm6dso_t; /** @@ -81,6 +93,34 @@ typedef struct { */ HAL_StatusTypeDef lsm6dso_init(lsm6dso_t *imu, I2C_HandleTypeDef *i2c_handle); +/** + * @brief reads LSM6DSO memory register + * + * @param data + * @param reg + * @return 0 if HAL_OK, 1 if HAL_ERROR + */ +uint8_t lsm6dso_read_reg(uint8_t *data, uint8_t reg); + +/** + * @brief reads multiple LSM6DSO memory registers + * + * @param data + * @param reg + * @param length + * @return 0 if HAL_OK, 1 if HAL_ERROR + */ +uint8_t lsm6dso_read_mult_reg(uint8_t *data, uint8_t reg, uint8_t length); + +/** + * @brief wrties to LSM6DSO memory register + * + * @param data + * @param reg + * @return 0 if HAL_OK, 1 if HAL_ERROR + */ +uint8_t lsm6dso_write_reg(uint8_t *data, uint8_t reg); + /* IMU Setting Configuration */ /** * @brief Configures the accelerometer of the LSM6DSO IMU diff --git a/general/src/lsm6dso.c b/general/src/lsm6dso.c index 4ffb5c6..dacf89b 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -13,28 +13,45 @@ 4 /* The range of values in g's returned from accelerometer */ #define GYRO_RANGE 1000 /* The range of values from the gyro in dps */ -static inline HAL_StatusTypeDef lsm6dso_read_reg(lsm6dso_t *imu, uint8_t *data, - uint8_t reg) +lsm6dso_t *imu; + +static inline HAL_StatusTypeDef hal_lsm6dso_read_reg(lsm6dso_t *imu, + uint8_t *data, uint8_t reg) { return HAL_I2C_Mem_Read(imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); } -static inline HAL_StatusTypeDef lsm6dso_read_mult_reg(lsm6dso_t *imu, - uint8_t *data, - uint8_t reg, - uint8_t length) +static inline HAL_StatusTypeDef hal_lsm6dso_read_mult_reg(lsm6dso_t *imu, + uint8_t *data, + uint8_t reg, + uint8_t length) { return HAL_I2C_Mem_Read(imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, data, length, HAL_MAX_DELAY); } -static inline HAL_StatusTypeDef lsm6dso_write_reg(lsm6dso_t *imu, uint8_t reg, - uint8_t *data) +static inline HAL_StatusTypeDef +hal_lsm6dso_write_reg(lsm6dso_t *imu, uint8_t reg, uint8_t *data) +{ + return (imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, + data, 1, HAL_MAX_DELAY); +} + +uint8_t lsm6dso_read_reg(uint8_t *data, uint8_t reg) +{ + return hal_lsm6dso_read_reg(imu, data, reg); +} + +uint8_t lsm6dso_read_mult_reg(uint8_t *data, uint8_t reg, uint8_t length) +{ + return hal_lsm6dso_read_mult_reg(imu, data, reg, length); +} + +uint8_t lsm6dso_write_reg(uint8_t *data, uint8_t reg) { - return HAL_I2C_Mem_Write(imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, - I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); + return hal_lsm6dso_write_reg(imu, data, reg); } static int16_t accel_data_convert(int16_t raw_accel) From db07c7924e518f999a3c525de62ac64d136f1e97 Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 27 Sep 2024 13:16:53 -0400 Subject: [PATCH 2/8] changed function names --- general/include/lsm6dso.h | 12 ++++++------ general/src/lsm6dso.c | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/general/include/lsm6dso.h b/general/include/lsm6dso.h index 4725017..38ff23a 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -76,11 +76,11 @@ typedef struct { int16_t gyro_data[3]; - I2C_Read_Reg lsm6dso_read_reg; + I2C_Read_Reg read_reg; - I2C_Read_Mult_Reg lsm6dso_read_mult_reg; + I2C_Read_Mult_Reg read_mult_reg; - I2C_Write_Reg lsm6dso_write_reg; + I2C_Write_Reg write_reg; } lsm6dso_t; @@ -100,7 +100,7 @@ HAL_StatusTypeDef lsm6dso_init(lsm6dso_t *imu, I2C_HandleTypeDef *i2c_handle); * @param reg * @return 0 if HAL_OK, 1 if HAL_ERROR */ -uint8_t lsm6dso_read_reg(uint8_t *data, uint8_t reg); +uint8_t read_reg(uint8_t *data, uint8_t reg); /** * @brief reads multiple LSM6DSO memory registers @@ -110,7 +110,7 @@ uint8_t lsm6dso_read_reg(uint8_t *data, uint8_t reg); * @param length * @return 0 if HAL_OK, 1 if HAL_ERROR */ -uint8_t lsm6dso_read_mult_reg(uint8_t *data, uint8_t reg, uint8_t length); +uint8_t read_mult_reg(uint8_t *data, uint8_t reg, uint8_t length); /** * @brief wrties to LSM6DSO memory register @@ -119,7 +119,7 @@ uint8_t lsm6dso_read_mult_reg(uint8_t *data, uint8_t reg, uint8_t length); * @param reg * @return 0 if HAL_OK, 1 if HAL_ERROR */ -uint8_t lsm6dso_write_reg(uint8_t *data, uint8_t reg); +uint8_t write_reg(uint8_t *data, uint8_t reg); /* IMU Setting Configuration */ /** diff --git a/general/src/lsm6dso.c b/general/src/lsm6dso.c index dacf89b..80ef5d0 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -15,41 +15,41 @@ lsm6dso_t *imu; -static inline HAL_StatusTypeDef hal_lsm6dso_read_reg(lsm6dso_t *imu, - uint8_t *data, uint8_t reg) +static inline HAL_StatusTypeDef lsm6dso_read_reg(lsm6dso_t *imu, uint8_t *data, + uint8_t reg) { return HAL_I2C_Mem_Read(imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); } -static inline HAL_StatusTypeDef hal_lsm6dso_read_mult_reg(lsm6dso_t *imu, - uint8_t *data, - uint8_t reg, - uint8_t length) +static inline HAL_StatusTypeDef lsm6dso_read_mult_reg(lsm6dso_t *imu, + uint8_t *data, + uint8_t reg, + uint8_t length) { return HAL_I2C_Mem_Read(imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, data, length, HAL_MAX_DELAY); } -static inline HAL_StatusTypeDef -hal_lsm6dso_write_reg(lsm6dso_t *imu, uint8_t reg, uint8_t *data) +static inline HAL_StatusTypeDef lsm6dso_write_reg(lsm6dso_t *imu, uint8_t reg, + uint8_t *data) { return (imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); } -uint8_t lsm6dso_read_reg(uint8_t *data, uint8_t reg) +uint8_t read_reg(uint8_t *data, uint8_t reg) { return hal_lsm6dso_read_reg(imu, data, reg); } -uint8_t lsm6dso_read_mult_reg(uint8_t *data, uint8_t reg, uint8_t length) +uint8_t read_mult_reg(uint8_t *data, uint8_t reg, uint8_t length) { return hal_lsm6dso_read_mult_reg(imu, data, reg, length); } -uint8_t lsm6dso_write_reg(uint8_t *data, uint8_t reg) +uint8_t write_reg(uint8_t *data, uint8_t reg) { return hal_lsm6dso_write_reg(imu, data, reg); } From eee3b4a92079fb3a78b8619588259a0afda4eb79 Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 27 Sep 2024 21:45:53 -0400 Subject: [PATCH 3/8] fixed naming --- general/include/lsm6dso.h | 6 +++--- general/src/lsm6dso.c | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/general/include/lsm6dso.h b/general/include/lsm6dso.h index 38ff23a..44c0bba 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -100,7 +100,7 @@ HAL_StatusTypeDef lsm6dso_init(lsm6dso_t *imu, I2C_HandleTypeDef *i2c_handle); * @param reg * @return 0 if HAL_OK, 1 if HAL_ERROR */ -uint8_t read_reg(uint8_t *data, uint8_t reg); +uint8_t lsm6dso_read(uint8_t *data, uint8_t reg); /** * @brief reads multiple LSM6DSO memory registers @@ -110,7 +110,7 @@ uint8_t read_reg(uint8_t *data, uint8_t reg); * @param length * @return 0 if HAL_OK, 1 if HAL_ERROR */ -uint8_t read_mult_reg(uint8_t *data, uint8_t reg, uint8_t length); +uint8_t lsm6dso_read_mult(uint8_t *data, uint8_t reg, uint8_t length); /** * @brief wrties to LSM6DSO memory register @@ -119,7 +119,7 @@ uint8_t read_mult_reg(uint8_t *data, uint8_t reg, uint8_t length); * @param reg * @return 0 if HAL_OK, 1 if HAL_ERROR */ -uint8_t write_reg(uint8_t *data, uint8_t reg); +uint8_t lsm6dso_write(uint8_t *data, uint8_t reg); /* IMU Setting Configuration */ /** diff --git a/general/src/lsm6dso.c b/general/src/lsm6dso.c index 80ef5d0..096f547 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -35,23 +35,23 @@ static inline HAL_StatusTypeDef lsm6dso_read_mult_reg(lsm6dso_t *imu, static inline HAL_StatusTypeDef lsm6dso_write_reg(lsm6dso_t *imu, uint8_t reg, uint8_t *data) { - return (imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, + return HAL_I2C_Mem_Write(imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); } -uint8_t read_reg(uint8_t *data, uint8_t reg) +uint8_t lsm6dso_read(uint8_t *data, uint8_t reg) { - return hal_lsm6dso_read_reg(imu, data, reg); + return lsm6dso_read_reg(imu, data, reg); } -uint8_t read_mult_reg(uint8_t *data, uint8_t reg, uint8_t length) +uint8_t lsm6dso_read_mult(uint8_t *data, uint8_t reg, uint8_t length) { - return hal_lsm6dso_read_mult_reg(imu, data, reg, length); + return lsm6dso_read_mult_reg(imu, data, reg, length); } -uint8_t write_reg(uint8_t *data, uint8_t reg) +uint8_t lsm6dso_write(uint8_t *data, uint8_t reg) { - return hal_lsm6dso_write_reg(imu, data, reg); + return lsm6dso_write_reg(imu, reg, data); } static int16_t accel_data_convert(int16_t raw_accel) From 172529a35fa171efbec72427a3301b5f36298663 Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 29 Sep 2024 17:26:46 -0400 Subject: [PATCH 4/8] fully generalized --- general/include/lsm6dso.h | 69 ++++------------- general/src/lsm6dso.c | 156 +++++++++++++++++--------------------- 2 files changed, 85 insertions(+), 140 deletions(-) diff --git a/general/include/lsm6dso.h b/general/include/lsm6dso.h index 44c0bba..8ba9e47 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -45,10 +45,8 @@ #define LSM6DSO_REG_ACCEL_Z_AXIS_H 0x2D /* Accelerometer Z axis upper bits */ /* Function Pointers*/ -typedef uint8_t (*I2C_Read_Reg)(uint8_t *data, uint8_t reg); -typedef uint8_t (*I2C_Read_Mult_Reg)(uint8_t *data, uint8_t reg, - uint8_t length); -typedef uint8_t (*I2C_Write_Reg)(uint8_t *data, uint8_t reg); +typedef int (*I2C_Read)(uint8_t *data, uint8_t reg, uint8_t length); +typedef int (*I2C_Write)(uint8_t *data, uint8_t reg, uint8_t length); /** * @brief Enumeration of axes of data available from the LSM6DSO IMU @@ -76,50 +74,20 @@ typedef struct { int16_t gyro_data[3]; - I2C_Read_Reg read_reg; + I2C_Read read_reg; - I2C_Read_Mult_Reg read_mult_reg; - - I2C_Write_Reg write_reg; + I2C_Write write_reg; } lsm6dso_t; /** * @brief Initializes the LSM6DSO IMU * - * @param imu * @param i2c_handle - * @return HAL_StatusTypeDef - */ -HAL_StatusTypeDef lsm6dso_init(lsm6dso_t *imu, I2C_HandleTypeDef *i2c_handle); - -/** - * @brief reads LSM6DSO memory register - * - * @param data - * @param reg - * @return 0 if HAL_OK, 1 if HAL_ERROR - */ -uint8_t lsm6dso_read(uint8_t *data, uint8_t reg); - -/** - * @brief reads multiple LSM6DSO memory registers - * - * @param data - * @param reg - * @param length - * @return 0 if HAL_OK, 1 if HAL_ERROR + * @return 0 if OK, other if ERROR */ -uint8_t lsm6dso_read_mult(uint8_t *data, uint8_t reg, uint8_t length); - -/** - * @brief wrties to LSM6DSO memory register - * - * @param data - * @param reg - * @return 0 if HAL_OK, 1 if HAL_ERROR - */ -uint8_t lsm6dso_write(uint8_t *data, uint8_t reg); +int lsm6dso_init(I2C_HandleTypeDef *i2c_handle, I2C_Read read_func, + I2C_Write write_func); /* IMU Setting Configuration */ /** @@ -129,38 +97,31 @@ uint8_t lsm6dso_write(uint8_t *data, uint8_t reg); * @param odr_sel * @param fs_sel * @param lp_f2_enable - * @return HAL_StatusTypeDef + * @return 0 if OK, other if ERROR */ -HAL_StatusTypeDef lsm6dso_set_accel_cfg(lsm6dso_t *imu, int8_t odr_sel, - int8_t fs_sel, int8_t lp_f2_enable); +int lsm6dso_set_accel_cfg(int8_t odr_sel, int8_t fs_sel, int8_t lp_f2_enable); /** * @brief Configures the accelerometer of the LSM6DSO IMU * - * @param imu * @param odr_sel * @param fs_sel * @param fs_125 - * @return HAL_StatusTypeDef + * @return 0 if OK, other if ERROR */ -HAL_StatusTypeDef lsm6dso_set_gyro_cfg(lsm6dso_t *imu, int8_t odr_sel, - int8_t fs_sel, int8_t fs_125); +int lsm6dso_set_gyro_cfg(int8_t odr_sel, int8_t fs_sel, int8_t fs_125); /* Data Aquisition */ /** * @brief Retrieves accelerometer data from the LSM6DSO IMU - * - * @param imu - * @return HAL_StatusTypeDef + * @return 0 if OK, other if ERROR */ -HAL_StatusTypeDef lsm6dso_read_accel(lsm6dso_t *imu); +int lsm6dso_read_accel(); /** * @brief Retreives the gyroscope data from the LSM6DSO IMU - * - * @param imu - * @return HAL_StatusTypeDef + * @return 0 if OK, other if ERROR */ -HAL_StatusTypeDef lsm6dso_read_gyro(lsm6dso_t *imu); +int lsm6dso_read_gyro(); #endif // LSM6DSO_H diff --git a/general/src/lsm6dso.c b/general/src/lsm6dso.c index 096f547..0d0d048 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -13,45 +13,28 @@ 4 /* The range of values in g's returned from accelerometer */ #define GYRO_RANGE 1000 /* The range of values from the gyro in dps */ -lsm6dso_t *imu; +lsm6dso_t imu; -static inline HAL_StatusTypeDef lsm6dso_read_reg(lsm6dso_t *imu, uint8_t *data, - uint8_t reg) +static inline int lsm6dso_read_reg(uint8_t *data, uint8_t reg) { - return HAL_I2C_Mem_Read(imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, - I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); + return imu.read_reg(data, reg, 1); } -static inline HAL_StatusTypeDef lsm6dso_read_mult_reg(lsm6dso_t *imu, - uint8_t *data, - uint8_t reg, - uint8_t length) +static inline int lsm6dso_read_mult_reg(uint8_t *data, uint8_t reg, + uint8_t length) { - return HAL_I2C_Mem_Read(imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, - I2C_MEMADD_SIZE_8BIT, data, length, - HAL_MAX_DELAY); + return imu.read_reg(data, reg, length); } -static inline HAL_StatusTypeDef lsm6dso_write_reg(lsm6dso_t *imu, uint8_t reg, - uint8_t *data) +static inline int lsm6dso_write_reg(uint8_t *data, uint8_t reg) { - return HAL_I2C_Mem_Write(imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, - data, 1, HAL_MAX_DELAY); + return imu.write_reg(data, reg, 1); } -uint8_t lsm6dso_read(uint8_t *data, uint8_t reg) +static inline int lsm6dso_write_mult_reg(uint8_t *data, uint8_t reg, + uint8_t length) { - return lsm6dso_read_reg(imu, data, reg); -} - -uint8_t lsm6dso_read_mult(uint8_t *data, uint8_t reg, uint8_t length) -{ - return lsm6dso_read_mult_reg(imu, data, reg, length); -} - -uint8_t lsm6dso_write(uint8_t *data, uint8_t reg) -{ - return lsm6dso_write_reg(imu, reg, data); + return imu.write_reg(data, reg, length); } static int16_t accel_data_convert(int16_t raw_accel) @@ -75,136 +58,137 @@ static int16_t gyro_data_convert(int16_t gyro_accel) return (int16_t)(((int32_t)val * GYRO_RANGE * 100) / REG_RESOLUTION); } -static HAL_StatusTypeDef lsm6dso_ping_imu(lsm6dso_t *imu) +static int lsm6dso_ping_imu() { uint8_t reg_data; - HAL_StatusTypeDef status; + int status; - status = lsm6dso_read_reg(imu, ®_data, LSM6DSO_REG_DEVICE_ID); - if (status != HAL_OK) + status = lsm6dso_read_reg(®_data, LSM6DSO_REG_DEVICE_ID); + if (status != 0) return status; if (reg_data != 0x6C) - return HAL_ERROR; + return -1; - return HAL_OK; + return 0; } -HAL_StatusTypeDef lsm6dso_set_accel_cfg(lsm6dso_t *imu, int8_t odr_sel, - int8_t fs_sel, int8_t lp_f2_enable) +int lsm6dso_set_accel_cfg(int8_t odr_sel, int8_t fs_sel, int8_t lp_f2_enable) { uint8_t config = (((odr_sel << 4) | (fs_sel << 2) | (lp_f2_enable << 1)) << 1); - imu->accel_config = config; + imu.accel_config = config; - return lsm6dso_write_reg(imu, LSM6DSO_REG_ACCEL_CTRL, - &imu->accel_config); + return lsm6dso_write_reg(&imu.accel_config, LSM6DSO_REG_ACCEL_CTRL); } -HAL_StatusTypeDef lsm6dso_set_gyro_cfg(lsm6dso_t *imu, int8_t odr_sel, - int8_t fs_sel, int8_t fs_125) +int lsm6dso_set_gyro_cfg(int8_t odr_sel, int8_t fs_sel, int8_t fs_125) { uint8_t config = (((odr_sel << 4) | (fs_sel << 2) | (fs_125 << 1)) << 1); - imu->gyro_config = config; + imu.gyro_config = config; - return lsm6dso_write_reg(imu, LSM6DSO_REG_GYRO_CTRL, &imu->gyro_config); + return lsm6dso_write_reg(&imu.gyro_config, LSM6DSO_REG_GYRO_CTRL); } -HAL_StatusTypeDef lsm6dso_init(lsm6dso_t *imu, I2C_HandleTypeDef *i2c_handle) +int lsm6dso_init(I2C_HandleTypeDef *i2c_handle, I2C_Read read_func, + I2C_Write write_func) { - HAL_StatusTypeDef status; + int status; + + imu.i2c_handle = i2c_handle; - imu->i2c_handle = i2c_handle; + imu.accel_data[LSM6DSO_X_AXIS] = 0; + imu.accel_data[LSM6DSO_Y_AXIS] = 0; + imu.accel_data[LSM6DSO_Z_AXIS] = 0; - imu->accel_data[LSM6DSO_X_AXIS] = 0; - imu->accel_data[LSM6DSO_Y_AXIS] = 0; - imu->accel_data[LSM6DSO_Z_AXIS] = 0; + imu.gyro_data[LSM6DSO_X_AXIS] = 0; + imu.gyro_data[LSM6DSO_Y_AXIS] = 0; + imu.gyro_data[LSM6DSO_Z_AXIS] = 0; - imu->gyro_data[LSM6DSO_X_AXIS] = 0; - imu->gyro_data[LSM6DSO_Y_AXIS] = 0; - imu->gyro_data[LSM6DSO_Z_AXIS] = 0; + imu.read_reg = read_func; + imu.write_reg = write_func; /* Quick check to make sure I2C is working */ - status = lsm6dso_ping_imu(imu); - if (status != HAL_OK) + status = lsm6dso_ping_imu(); + if (status != 0) return status; /* - Configure IMU to default params - Refer to datasheet pages 56-57 - */ - status = lsm6dso_set_accel_cfg(imu, 0x08, 0x02, 0x01); - if (status != HAL_OK) + Configure IMU to default params + Refer to datasheet pages 56-57 + */ + status = lsm6dso_set_accel_cfg(0x08, 0x02, 0x01); + if (status != 0) return status; - status = lsm6dso_set_gyro_cfg(imu, 0x08, 0x02, 0x00); - if (status != HAL_OK) + status = lsm6dso_set_gyro_cfg(0x08, 0x02, 0x00); + if (status != 0) return status; - return HAL_OK; + return 0; } -HAL_StatusTypeDef lsm6dso_read_accel(lsm6dso_t *imu) +int lsm6dso_read_accel() { union { uint8_t buf[2]; int16_t data; } accel_x_raw, accel_y_raw, accel_z_raw; - HAL_StatusTypeDef status; + int status; /* Getting raw data from registers */ - status = lsm6dso_read_mult_reg(imu, accel_x_raw.buf, + status = lsm6dso_read_mult_reg(accel_x_raw.buf, LSM6DSO_REG_ACCEL_X_AXIS_L, 2); - if (status != HAL_OK) + if (status != 0) return status; - status = lsm6dso_read_mult_reg(imu, accel_y_raw.buf, + status = lsm6dso_read_mult_reg(accel_y_raw.buf, LSM6DSO_REG_ACCEL_Y_AXIS_L, 2); - if (status != HAL_OK) + if (status != 0) return status; - status = lsm6dso_read_mult_reg(imu, accel_z_raw.buf, + status = lsm6dso_read_mult_reg(accel_z_raw.buf, LSM6DSO_REG_ACCEL_Z_AXIS_L, 2); - if (status != HAL_OK) + if (status != 0) return status; /* Setting imu struct values to converted measurements */ - imu->accel_data[LSM6DSO_X_AXIS] = accel_data_convert(accel_x_raw.data); - imu->accel_data[LSM6DSO_Y_AXIS] = accel_data_convert(accel_y_raw.data); - imu->accel_data[LSM6DSO_Z_AXIS] = accel_data_convert(accel_z_raw.data); + imu.accel_data[LSM6DSO_X_AXIS] = accel_data_convert(accel_x_raw.data); + imu.accel_data[LSM6DSO_Y_AXIS] = accel_data_convert(accel_y_raw.data); + imu.accel_data[LSM6DSO_Z_AXIS] = accel_data_convert(accel_z_raw.data); - return HAL_OK; + return 0; } -HAL_StatusTypeDef lsm6dso_read_gyro(lsm6dso_t *imu) +int lsm6dso_read_gyro() { union { uint8_t buf[2]; int16_t data; } gyro_x_raw, gyro_y_raw, gyro_z_raw; - HAL_StatusTypeDef status; + int status; /* Aquire raw data from registers */ - status = lsm6dso_read_mult_reg(imu, gyro_x_raw.buf, + status = lsm6dso_read_mult_reg(gyro_x_raw.buf, LSM6DSO_REG_GYRO_X_AXIS_L, 2); - if (status != HAL_OK) + if (status != 0) return status; - status = lsm6dso_read_mult_reg(imu, gyro_y_raw.buf, + status = lsm6dso_read_mult_reg(gyro_y_raw.buf, LSM6DSO_REG_GYRO_Y_AXIS_L, 2); - if (status != HAL_OK) + if (status != 0) return status; - status = lsm6dso_read_mult_reg(imu, gyro_z_raw.buf, + status = lsm6dso_read_mult_reg(gyro_z_raw.buf, LSM6DSO_REG_GYRO_Z_AXIS_L, 2); - if (status != HAL_OK) + if (status != 0) return status; /* Setting imu struct values to converted measurements */ - imu->gyro_data[LSM6DSO_X_AXIS] = gyro_data_convert(gyro_x_raw.data); - imu->gyro_data[LSM6DSO_Y_AXIS] = gyro_data_convert(gyro_y_raw.data); - imu->gyro_data[LSM6DSO_Z_AXIS] = gyro_data_convert(gyro_z_raw.data); + imu.gyro_data[LSM6DSO_X_AXIS] = gyro_data_convert(gyro_x_raw.data); + imu.gyro_data[LSM6DSO_Y_AXIS] = gyro_data_convert(gyro_y_raw.data); + imu.gyro_data[LSM6DSO_Z_AXIS] = gyro_data_convert(gyro_z_raw.data); - return HAL_OK; + return 0; } From 2d4813dc34c5a44c5517ddf77ce8d571a798b317 Mon Sep 17 00:00:00 2001 From: Caio Date: Mon, 30 Sep 2024 13:15:46 -0400 Subject: [PATCH 5/8] removed i2c handle --- general/include/lsm6dso.h | 6 +----- general/src/lsm6dso.c | 5 +---- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/general/include/lsm6dso.h b/general/include/lsm6dso.h index 8ba9e47..0fa009f 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -63,8 +63,6 @@ enum lsm6dso_axes { * */ typedef struct { - I2C_HandleTypeDef *i2c_handle; - uint8_t accel_config; // TODO: We should make these cfg packed unions with // bitfield structs @@ -83,11 +81,9 @@ typedef struct { /** * @brief Initializes the LSM6DSO IMU * - * @param i2c_handle * @return 0 if OK, other if ERROR */ -int lsm6dso_init(I2C_HandleTypeDef *i2c_handle, I2C_Read read_func, - I2C_Write write_func); +int lsm6dso_init(I2C_Read read_func, I2C_Write write_func); /* IMU Setting Configuration */ /** diff --git a/general/src/lsm6dso.c b/general/src/lsm6dso.c index 0d0d048..ae11648 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -91,13 +91,10 @@ int lsm6dso_set_gyro_cfg(int8_t odr_sel, int8_t fs_sel, int8_t fs_125) return lsm6dso_write_reg(&imu.gyro_config, LSM6DSO_REG_GYRO_CTRL); } -int lsm6dso_init(I2C_HandleTypeDef *i2c_handle, I2C_Read read_func, - I2C_Write write_func) +int lsm6dso_init(I2C_Read read_func, I2C_Write write_func) { int status; - imu.i2c_handle = i2c_handle; - imu.accel_data[LSM6DSO_X_AXIS] = 0; imu.accel_data[LSM6DSO_Y_AXIS] = 0; imu.accel_data[LSM6DSO_Z_AXIS] = 0; From f8aa44d93b577b7dd8a9565ba1cccc4f45141f79 Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 13 Oct 2024 16:32:30 -0400 Subject: [PATCH 6/8] imu pointer in param --- general/src/lsm6dso.c | 93 ++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/general/src/lsm6dso.c b/general/src/lsm6dso.c index ae11648..43df6c7 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -13,28 +13,26 @@ 4 /* The range of values in g's returned from accelerometer */ #define GYRO_RANGE 1000 /* The range of values from the gyro in dps */ -lsm6dso_t imu; - -static inline int lsm6dso_read_reg(uint8_t *data, uint8_t reg) +static inline int lsm6dso_read_reg(lsm6dso_t *imu, uint8_t *data, uint8_t reg) { - return imu.read_reg(data, reg, 1); + return imu->read_reg(data, reg, 1); } -static inline int lsm6dso_read_mult_reg(uint8_t *data, uint8_t reg, - uint8_t length) +static inline int lsm6dso_read_mult_reg(lsm6dso_t *imu, uint8_t *data, + uint8_t reg, uint8_t length) { - return imu.read_reg(data, reg, length); + return imu->read_reg(data, reg, length); } -static inline int lsm6dso_write_reg(uint8_t *data, uint8_t reg) +static inline int lsm6dso_write_reg(lsm6dso_t *imu, uint8_t *data, uint8_t reg) { - return imu.write_reg(data, reg, 1); + return imu->write_reg(data, reg, 1); } -static inline int lsm6dso_write_mult_reg(uint8_t *data, uint8_t reg, - uint8_t length) +static inline int lsm6dso_write_mult_reg(lsm6dso_t *imu, uint8_t *data, + uint8_t reg, uint8_t length) { - return imu.write_reg(data, reg, length); + return imu->write_reg(data, reg, length); } static int16_t accel_data_convert(int16_t raw_accel) @@ -58,12 +56,12 @@ static int16_t gyro_data_convert(int16_t gyro_accel) return (int16_t)(((int32_t)val * GYRO_RANGE * 100) / REG_RESOLUTION); } -static int lsm6dso_ping_imu() +static int lsm6dso_ping_imu(lsm6dso_t *imu) { uint8_t reg_data; int status; - status = lsm6dso_read_reg(®_data, LSM6DSO_REG_DEVICE_ID); + status = lsm6dso_read_reg(imu, ®_data, LSM6DSO_REG_DEVICE_ID); if (status != 0) return status; @@ -73,41 +71,44 @@ static int lsm6dso_ping_imu() return 0; } -int lsm6dso_set_accel_cfg(int8_t odr_sel, int8_t fs_sel, int8_t lp_f2_enable) +int lsm6dso_set_accel_cfg(lsm6dso_t *imu, int8_t odr_sel, int8_t fs_sel, + int8_t lp_f2_enable) { uint8_t config = (((odr_sel << 4) | (fs_sel << 2) | (lp_f2_enable << 1)) << 1); - imu.accel_config = config; + imu->accel_config = config; - return lsm6dso_write_reg(&imu.accel_config, LSM6DSO_REG_ACCEL_CTRL); + return lsm6dso_write_reg(imu, imu->accel_config, + LSM6DSO_REG_ACCEL_CTRL); } -int lsm6dso_set_gyro_cfg(int8_t odr_sel, int8_t fs_sel, int8_t fs_125) +int lsm6dso_set_gyro_cfg(lsm6dso_t *imu, int8_t odr_sel, int8_t fs_sel, + int8_t fs_125) { uint8_t config = (((odr_sel << 4) | (fs_sel << 2) | (fs_125 << 1)) << 1); - imu.gyro_config = config; + imu->gyro_config = config; - return lsm6dso_write_reg(&imu.gyro_config, LSM6DSO_REG_GYRO_CTRL); + return lsm6dso_write_reg(imu, imu->gyro_config, LSM6DSO_REG_GYRO_CTRL); } -int lsm6dso_init(I2C_Read read_func, I2C_Write write_func) +int lsm6dso_init(lsm6dso_t *imu, I2C_Read read_func, I2C_Write write_func) { int status; - imu.accel_data[LSM6DSO_X_AXIS] = 0; - imu.accel_data[LSM6DSO_Y_AXIS] = 0; - imu.accel_data[LSM6DSO_Z_AXIS] = 0; + imu->accel_data[LSM6DSO_X_AXIS] = 0; + imu->accel_data[LSM6DSO_Y_AXIS] = 0; + imu->accel_data[LSM6DSO_Z_AXIS] = 0; - imu.gyro_data[LSM6DSO_X_AXIS] = 0; - imu.gyro_data[LSM6DSO_Y_AXIS] = 0; - imu.gyro_data[LSM6DSO_Z_AXIS] = 0; + imu->gyro_data[LSM6DSO_X_AXIS] = 0; + imu->gyro_data[LSM6DSO_Y_AXIS] = 0; + imu->gyro_data[LSM6DSO_Z_AXIS] = 0; - imu.read_reg = read_func; - imu.write_reg = write_func; + imu->read_reg = read_func; + imu->write_reg = write_func; /* Quick check to make sure I2C is working */ - status = lsm6dso_ping_imu(); + status = lsm6dso_ping_imu(imu); if (status != 0) return status; @@ -115,18 +116,18 @@ int lsm6dso_init(I2C_Read read_func, I2C_Write write_func) Configure IMU to default params Refer to datasheet pages 56-57 */ - status = lsm6dso_set_accel_cfg(0x08, 0x02, 0x01); + status = lsm6dso_set_accel_cfg(imu, 0x08, 0x02, 0x01); if (status != 0) return status; - status = lsm6dso_set_gyro_cfg(0x08, 0x02, 0x00); + status = lsm6dso_set_gyro_cfg(imu, 0x08, 0x02, 0x00); if (status != 0) return status; return 0; } -int lsm6dso_read_accel() +int lsm6dso_read_accel(lsm6dso_t *imu) { union { uint8_t buf[2]; @@ -135,30 +136,30 @@ int lsm6dso_read_accel() int status; /* Getting raw data from registers */ - status = lsm6dso_read_mult_reg(accel_x_raw.buf, + status = lsm6dso_read_mult_reg(imu, accel_x_raw.buf, LSM6DSO_REG_ACCEL_X_AXIS_L, 2); if (status != 0) return status; - status = lsm6dso_read_mult_reg(accel_y_raw.buf, + status = lsm6dso_read_mult_reg(imu, accel_y_raw.buf, LSM6DSO_REG_ACCEL_Y_AXIS_L, 2); if (status != 0) return status; - status = lsm6dso_read_mult_reg(accel_z_raw.buf, + status = lsm6dso_read_mult_reg(imu, accel_z_raw.buf, LSM6DSO_REG_ACCEL_Z_AXIS_L, 2); if (status != 0) return status; /* Setting imu struct values to converted measurements */ - imu.accel_data[LSM6DSO_X_AXIS] = accel_data_convert(accel_x_raw.data); - imu.accel_data[LSM6DSO_Y_AXIS] = accel_data_convert(accel_y_raw.data); - imu.accel_data[LSM6DSO_Z_AXIS] = accel_data_convert(accel_z_raw.data); + imu->accel_data[LSM6DSO_X_AXIS] = accel_data_convert(accel_x_raw.data); + imu->accel_data[LSM6DSO_Y_AXIS] = accel_data_convert(accel_y_raw.data); + imu->accel_data[LSM6DSO_Z_AXIS] = accel_data_convert(accel_z_raw.data); return 0; } -int lsm6dso_read_gyro() +int lsm6dso_read_gyro(lsm6dso_t *imu) { union { uint8_t buf[2]; @@ -167,25 +168,25 @@ int lsm6dso_read_gyro() int status; /* Aquire raw data from registers */ - status = lsm6dso_read_mult_reg(gyro_x_raw.buf, + status = lsm6dso_read_mult_reg(imu, gyro_x_raw.buf, LSM6DSO_REG_GYRO_X_AXIS_L, 2); if (status != 0) return status; - status = lsm6dso_read_mult_reg(gyro_y_raw.buf, + status = lsm6dso_read_mult_reg(imu, gyro_y_raw.buf, LSM6DSO_REG_GYRO_Y_AXIS_L, 2); if (status != 0) return status; - status = lsm6dso_read_mult_reg(gyro_z_raw.buf, + status = lsm6dso_read_mult_reg(imu, gyro_z_raw.buf, LSM6DSO_REG_GYRO_Z_AXIS_L, 2); if (status != 0) return status; /* Setting imu struct values to converted measurements */ - imu.gyro_data[LSM6DSO_X_AXIS] = gyro_data_convert(gyro_x_raw.data); - imu.gyro_data[LSM6DSO_Y_AXIS] = gyro_data_convert(gyro_y_raw.data); - imu.gyro_data[LSM6DSO_Z_AXIS] = gyro_data_convert(gyro_z_raw.data); + imu->gyro_data[LSM6DSO_X_AXIS] = gyro_data_convert(gyro_x_raw.data); + imu->gyro_data[LSM6DSO_Y_AXIS] = gyro_data_convert(gyro_y_raw.data); + imu->gyro_data[LSM6DSO_Z_AXIS] = gyro_data_convert(gyro_z_raw.data); return 0; } From 6fbf4f368bb8c1fce47c9fbbb3da4b62a4d58893 Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 13 Oct 2024 16:40:13 -0400 Subject: [PATCH 7/8] fixed header --- general/include/lsm6dso.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/general/include/lsm6dso.h b/general/include/lsm6dso.h index 0fa009f..6ac3d55 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -53,6 +53,7 @@ typedef int (*I2C_Write)(uint8_t *data, uint8_t reg, uint8_t length); * */ enum lsm6dso_axes { + LSM6DSO_X_AXIS = 0, LSM6DSO_Y_AXIS = 1, LSM6DSO_Z_AXIS = 2 @@ -83,7 +84,7 @@ typedef struct { * * @return 0 if OK, other if ERROR */ -int lsm6dso_init(I2C_Read read_func, I2C_Write write_func); +int lsm6dso_init(lsm6dso_t *imu, I2C_Read read_func, I2C_Write write_func); /* IMU Setting Configuration */ /** @@ -95,7 +96,9 @@ int lsm6dso_init(I2C_Read read_func, I2C_Write write_func); * @param lp_f2_enable * @return 0 if OK, other if ERROR */ -int lsm6dso_set_accel_cfg(int8_t odr_sel, int8_t fs_sel, int8_t lp_f2_enable); + +int lsm6dso_set_accel_cfg(lsm6dso_t *imu, int8_t odr_sel, int8_t fs_sel, + int8_t lp_f2_enable); /** * @brief Configures the accelerometer of the LSM6DSO IMU @@ -105,19 +108,21 @@ int lsm6dso_set_accel_cfg(int8_t odr_sel, int8_t fs_sel, int8_t lp_f2_enable); * @param fs_125 * @return 0 if OK, other if ERROR */ -int lsm6dso_set_gyro_cfg(int8_t odr_sel, int8_t fs_sel, int8_t fs_125); + +int lsm6dso_set_gyro_cfg(lsm6dso_t *imu, int8_t odr_sel, int8_t fs_sel, + int8_t fs_125); /* Data Aquisition */ /** * @brief Retrieves accelerometer data from the LSM6DSO IMU * @return 0 if OK, other if ERROR */ -int lsm6dso_read_accel(); +int lsm6dso_read_accel(lsm6dso_t *imu); /** * @brief Retreives the gyroscope data from the LSM6DSO IMU * @return 0 if OK, other if ERROR */ -int lsm6dso_read_gyro(); +int lsm6dso_read_gyro(lsm6dso_t *imu); #endif // LSM6DSO_H From 1e3e701bb131e43685339b9d041f2aca715cff62 Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 13 Oct 2024 16:42:59 -0400 Subject: [PATCH 8/8] #162-fixed param --- general/src/lsm6dso.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/general/src/lsm6dso.c b/general/src/lsm6dso.c index 43df6c7..6db0ab6 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -78,7 +78,7 @@ int lsm6dso_set_accel_cfg(lsm6dso_t *imu, int8_t odr_sel, int8_t fs_sel, (((odr_sel << 4) | (fs_sel << 2) | (lp_f2_enable << 1)) << 1); imu->accel_config = config; - return lsm6dso_write_reg(imu, imu->accel_config, + return lsm6dso_write_reg(imu, &imu->accel_config, LSM6DSO_REG_ACCEL_CTRL); } @@ -89,7 +89,7 @@ int lsm6dso_set_gyro_cfg(lsm6dso_t *imu, int8_t odr_sel, int8_t fs_sel, (((odr_sel << 4) | (fs_sel << 2) | (fs_125 << 1)) << 1); imu->gyro_config = config; - return lsm6dso_write_reg(imu, imu->gyro_config, LSM6DSO_REG_GYRO_CTRL); + return lsm6dso_write_reg(imu, &imu->gyro_config, LSM6DSO_REG_GYRO_CTRL); } int lsm6dso_init(lsm6dso_t *imu, I2C_Read read_func, I2C_Write write_func)