From 7b8e07dce32a788048c508c37d44c321159ebf6a Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 27 Sep 2024 12:55:04 -0400 Subject: [PATCH 1/9] generalized read, read mult, and write functions --- general/include/lsm6dso.h | 106 +++++++++++++++++++++++++++----------- general/src/lsm6dso.c | 94 +++++++++++++++++++++++---------- 2 files changed, 142 insertions(+), 58 deletions(-) diff --git a/general/include/lsm6dso.h b/general/include/lsm6dso.h index b49c21d..3082e11 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -11,39 +11,50 @@ #include #include "stm32xx_hal.h" -#define LSM6DSO_I2C_ADDRESS 0x6A << 1 /* Shifted because datasheet said to */ +#define LSM6DSO_I2C_ADDRESS 0x6A << 1 /* Shifted because datasheet said to */ // Not sure if these are all needed, also not sure if more need to be added /* For register descriptions reference datasheet pages 47 - 98 */ -#define LSM6DSO_REG_FUNC_CFG_ACCESS 0x01 /* Enable embedded functions register */ -#define LSM6DSO_REG_INTERRUPT_CTRL_1 0x0D /* INT1 pin control, used for interrupts */ -#define LSM6DSO_REG_INTERRUPT_CTRL_2 0x0E /* INT2 pin control, used for interrupts */ -#define LSM6DSO_REG_DEVICE_ID 0x0F /* Register for checking communication */ -#define LSM6DSO_REG_ACCEL_CTRL 0x10 /* Accelerometer Control Register */ -#define LSM6DSO_REG_GYRO_CTRL 0x11 /* Gyroscope Control Register */ -#define LSM6DSO_REG_ALL_INTERRUPT_SRC 0x1A /* Source Register for all interupsts */ -#define LSM6DSO_REG_WAKEUP_INTERRUPT_SRC 0x1B /* Wake up interupt source register */ -#define LSM6DSO_REG_TAP_INTERRUPT_SRC 0x1C /* Tap Interrupt source register */ -#define LSM6DSO_REG_6D_INTERRUPT_SRC 0x1D /* 6-direction Interrupt source register */ -#define LSM6DSO_REG_STATUS 0x1E /* Status register */ -#define LSM6DSO_REG_GYRO_X_AXIS_L 0x22 /* Gyro pitch axis lower bits */ -#define LSM6DSO_REG_GYRO_X_AXIS_H 0x23 /* Gyro pitch axis upper bits */ -#define LSM6DSO_REG_GYRO_Y_AXIS_L 0x24 /* Gyro roll axis lower bits */ -#define LSM6DSO_REG_GYRO_Y_AXIS_H 0x25 /* Gyro roll axis upper bits */ -#define LSM6DSO_REG_GYRO_Z_AXIS_L 0x26 /* Gyro yaw axis lower bits */ -#define LSM6DSO_REG_GYRO_Z_AXIS_H 0x27 /* Gyro yaw axis higher bits */ -#define LSM6DSO_REG_ACCEL_X_AXIS_L 0x28 /* Accelerometer X axis lower bits */ -#define LSM6DSO_REG_ACCEL_X_AXIS_H 0x29 /* Accelerometer X axis upper bits */ -#define LSM6DSO_REG_ACCEL_Y_AXIS_L 0x2A /* Accelerometer Y axis lower bits */ -#define LSM6DSO_REG_ACCEL_Y_AXIS_H 0x2B /* Accelerometer Y axis upper bits */ -#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 */ +#define LSM6DSO_REG_FUNC_CFG_ACCESS \ + 0x01 /* Enable embedded functions register */ +#define LSM6DSO_REG_INTERRUPT_CTRL_1 \ + 0x0D /* INT1 pin control, used for interrupts */ +#define LSM6DSO_REG_INTERRUPT_CTRL_2 \ + 0x0E /* INT2 pin control, used for interrupts */ +#define LSM6DSO_REG_DEVICE_ID 0x0F /* Register for checking communication */ +#define LSM6DSO_REG_ACCEL_CTRL 0x10 /* Accelerometer Control Register */ +#define LSM6DSO_REG_GYRO_CTRL 0x11 /* Gyroscope Control Register */ +#define LSM6DSO_REG_ALL_INTERRUPT_SRC \ + 0x1A /* Source Register for all interupsts */ +#define LSM6DSO_REG_WAKEUP_INTERRUPT_SRC \ + 0x1B /* Wake up interupt source register */ +#define LSM6DSO_REG_TAP_INTERRUPT_SRC 0x1C /* Tap Interrupt source register */ +#define LSM6DSO_REG_6D_INTERRUPT_SRC \ + 0x1D /* 6-direction Interrupt source register */ +#define LSM6DSO_REG_STATUS 0x1E /* Status register */ +#define LSM6DSO_REG_GYRO_X_AXIS_L 0x22 /* Gyro pitch axis lower bits */ +#define LSM6DSO_REG_GYRO_X_AXIS_H 0x23 /* Gyro pitch axis upper bits */ +#define LSM6DSO_REG_GYRO_Y_AXIS_L 0x24 /* Gyro roll axis lower bits */ +#define LSM6DSO_REG_GYRO_Y_AXIS_H 0x25 /* Gyro roll axis upper bits */ +#define LSM6DSO_REG_GYRO_Z_AXIS_L 0x26 /* Gyro yaw axis lower bits */ +#define LSM6DSO_REG_GYRO_Z_AXIS_H 0x27 /* Gyro yaw axis higher bits */ +#define LSM6DSO_REG_ACCEL_X_AXIS_L 0x28 /* Accelerometer X axis lower bits */ +#define LSM6DSO_REG_ACCEL_X_AXIS_H 0x29 /* Accelerometer X axis upper bits */ +#define LSM6DSO_REG_ACCEL_Y_AXIS_L 0x2A /* Accelerometer Y axis lower bits */ +#define LSM6DSO_REG_ACCEL_Y_AXIS_H 0x2B /* Accelerometer Y axis upper bits */ +#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 * */ -enum lsm6dso_axes -{ +enum lsm6dso_axes { LSM6DSO_X_AXIS = 0, LSM6DSO_Y_AXIS = 1, LSM6DSO_Z_AXIS = 2 @@ -53,8 +64,7 @@ enum lsm6dso_axes * @brief Struct containing data relevant to the LSM6DSO IMU * */ -typedef struct -{ +typedef struct { I2C_HandleTypeDef *i2c_handle; uint8_t accel_config; //TODO: We should make these cfg packed unions with bitfield structs @@ -65,6 +75,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; /** @@ -76,6 +92,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 @@ -86,7 +130,8 @@ HAL_StatusTypeDef lsm6dso_init(lsm6dso_t *imu, I2C_HandleTypeDef *i2c_handle); * @param lp_f2_enable * @return HAL_StatusTypeDef */ -HAL_StatusTypeDef lsm6dso_set_accel_cfg(lsm6dso_t *imu, int8_t odr_sel, int8_t fs_sel, int8_t lp_f2_enable); +HAL_StatusTypeDef 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 @@ -97,7 +142,8 @@ HAL_StatusTypeDef lsm6dso_set_accel_cfg(lsm6dso_t *imu, int8_t odr_sel, int8_t f * @param fs_125 * @return HAL_StatusTypeDef */ -HAL_StatusTypeDef lsm6dso_set_gyro_cfg(lsm6dso_t *imu, int8_t odr_sel, int8_t fs_sel, int8_t fs_125); +HAL_StatusTypeDef lsm6dso_set_gyro_cfg(lsm6dso_t *imu, int8_t odr_sel, + int8_t fs_sel, int8_t fs_125); /* Data Aquisition */ /** diff --git a/general/src/lsm6dso.c b/general/src/lsm6dso.c index 8542d3d..dc67abe 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -8,23 +8,50 @@ #include "lsm6dso.h" -#define REG_RESOLUTION 32768 /* Half the range of a 16 bit signed integer */ -#define ACCEL_RANGE 4 /* The range of values in g's returned from accelerometer */ -#define GYRO_RANGE 1000 /* The range of values from the gyro in dps */ +#define REG_RESOLUTION 32768 /* Half the range of a 16 bit signed integer */ +#define ACCEL_RANGE \ + 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); + 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); + 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 HAL_I2C_Mem_Write(imu->i2c_handle, LSM6DSO_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY); + 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_lsm6dso_write_reg(imu, data, reg); } static int16_t accel_data_convert(int16_t raw_accel) @@ -57,23 +84,28 @@ static HAL_StatusTypeDef lsm6dso_ping_imu(lsm6dso_t *imu) if (status != HAL_OK) return status; - if(reg_data != 0x6C) + if (reg_data != 0x6C) return HAL_ERROR; return HAL_OK; } -HAL_StatusTypeDef lsm6dso_set_accel_cfg(lsm6dso_t *imu, int8_t odr_sel, int8_t fs_sel, int8_t lp_f2_enable) +HAL_StatusTypeDef 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); + uint8_t config = + (((odr_sel << 4) | (fs_sel << 2) | (lp_f2_enable << 1)) << 1); imu->accel_config = config; - return lsm6dso_write_reg(imu, LSM6DSO_REG_ACCEL_CTRL, &imu->accel_config); + return lsm6dso_write_reg(imu, LSM6DSO_REG_ACCEL_CTRL, + &imu->accel_config); } -HAL_StatusTypeDef lsm6dso_set_gyro_cfg(lsm6dso_t *imu, int8_t odr_sel, int8_t fs_sel, int8_t fs_125) +HAL_StatusTypeDef 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); + uint8_t config = + (((odr_sel << 4) | (fs_sel << 2) | (fs_125 << 1)) << 1); imu->gyro_config = config; return lsm6dso_write_reg(imu, LSM6DSO_REG_GYRO_CTRL, &imu->gyro_config); @@ -95,7 +127,7 @@ HAL_StatusTypeDef lsm6dso_init(lsm6dso_t *imu, I2C_HandleTypeDef *i2c_handle) /* Quick check to make sure I2C is working */ status = lsm6dso_ping_imu(imu); - if(status != HAL_OK) + if (status != HAL_OK) return status; /* @@ -122,16 +154,19 @@ HAL_StatusTypeDef lsm6dso_read_accel(lsm6dso_t *imu) HAL_StatusTypeDef status; /* Getting raw data from registers */ - status = lsm6dso_read_mult_reg(imu, accel_x_raw.buf, LSM6DSO_REG_ACCEL_X_AXIS_L, 2); - if(status != HAL_OK) + status = lsm6dso_read_mult_reg(imu, accel_x_raw.buf, + LSM6DSO_REG_ACCEL_X_AXIS_L, 2); + if (status != HAL_OK) return status; - status = lsm6dso_read_mult_reg(imu, accel_y_raw.buf, LSM6DSO_REG_ACCEL_Y_AXIS_L, 2); - if(status != HAL_OK) + status = lsm6dso_read_mult_reg(imu, accel_y_raw.buf, + LSM6DSO_REG_ACCEL_Y_AXIS_L, 2); + if (status != HAL_OK) return status; - status = lsm6dso_read_mult_reg(imu, accel_z_raw.buf, LSM6DSO_REG_ACCEL_Z_AXIS_L, 2); - if(status != HAL_OK) + status = lsm6dso_read_mult_reg(imu, accel_z_raw.buf, + LSM6DSO_REG_ACCEL_Z_AXIS_L, 2); + if (status != HAL_OK) return status; /* Setting imu struct values to converted measurements */ @@ -151,16 +186,19 @@ HAL_StatusTypeDef lsm6dso_read_gyro(lsm6dso_t *imu) HAL_StatusTypeDef status; /* Aquire raw data from registers */ - status = lsm6dso_read_mult_reg(imu, gyro_x_raw.buf, LSM6DSO_REG_GYRO_X_AXIS_L, 2); - if(status != HAL_OK) + status = lsm6dso_read_mult_reg(imu, gyro_x_raw.buf, + LSM6DSO_REG_GYRO_X_AXIS_L, 2); + if (status != HAL_OK) return status; - status = lsm6dso_read_mult_reg(imu, gyro_y_raw.buf, LSM6DSO_REG_GYRO_Y_AXIS_L, 2); - if(status != HAL_OK) + status = lsm6dso_read_mult_reg(imu, gyro_y_raw.buf, + LSM6DSO_REG_GYRO_Y_AXIS_L, 2); + if (status != HAL_OK) return status; - status = lsm6dso_read_mult_reg(imu, gyro_z_raw.buf, LSM6DSO_REG_GYRO_Z_AXIS_L, 2); - if(status != HAL_OK) + status = lsm6dso_read_mult_reg(imu, gyro_z_raw.buf, + LSM6DSO_REG_GYRO_Z_AXIS_L, 2); + if (status != HAL_OK) return status; /* Setting imu struct values to converted measurements */ From b8673d38151a9d28fb52295b7945aa70713c9d3e Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 27 Sep 2024 13:16:53 -0400 Subject: [PATCH 2/9] 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 3082e11..ac0bdc5 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -75,11 +75,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; @@ -99,7 +99,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 @@ -109,7 +109,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 @@ -118,7 +118,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 dc67abe..3f2ddb2 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 24a77c15b2cb344731ff994762037b165467ef3c Mon Sep 17 00:00:00 2001 From: Caio Date: Fri, 27 Sep 2024 21:45:53 -0400 Subject: [PATCH 3/9] 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 ac0bdc5..3104561 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -99,7 +99,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 @@ -109,7 +109,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 @@ -118,7 +118,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 3f2ddb2..c19a9f1 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 ede639a8ce3b15e3c7aa3c7b456f2dbba4af1b65 Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 29 Sep 2024 17:26:46 -0400 Subject: [PATCH 4/9] fully generalized lsm6dso driver --- general/include/lsm6dso.h | 65 ++++------------- general/src/lsm6dso.c | 143 ++++++++++++++++++-------------------- 2 files changed, 80 insertions(+), 128 deletions(-) diff --git a/general/include/lsm6dso.h b/general/include/lsm6dso.h index 3104561..de7e77e 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -45,10 +45,9 @@ #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, +typedef int (*I2C_Read)(uint8_t *data, uint8_t reg, uint8_t length); -typedef uint8_t (*I2C_Write_Reg)(uint8_t *data, uint8_t reg); +typedef int (*I2C_Write)(uint8_t *data, uint8_t reg, uint8_t length); /** * @brief Enumeration of axes of data available from the LSM6DSO IMU @@ -75,50 +74,19 @@ 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 */ /** @@ -128,38 +96,33 @@ 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, +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, +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 c19a9f1..bd5f199 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -13,45 +13,32 @@ 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, +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, +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 +62,138 @@ 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, +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, +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) + 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 430241c4a0ca15981422600836939d663d159755 Mon Sep 17 00:00:00 2001 From: Caio Date: Mon, 30 Sep 2024 13:15:46 -0400 Subject: [PATCH 5/9] removed i2c_handle from driver --- general/include/lsm6dso.h | 4 +--- general/src/lsm6dso.c | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/general/include/lsm6dso.h b/general/include/lsm6dso.h index de7e77e..6ee9f99 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -64,7 +64,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,10 +82,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 bd5f199..71963c7 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -97,12 +97,10 @@ int lsm6dso_set_gyro_cfg(int8_t odr_sel, 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 6aa5dd339a54d2412312a4154e4e8610a4ff4532 Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Tue, 8 Oct 2024 15:20:47 -0400 Subject: [PATCH 6/9] fixed conflict --- general/include/lsm6dso.h | 61 ++++++++++++++++++++------------------- general/src/lsm6dso.c | 38 ++++++++++++------------ 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/general/include/lsm6dso.h b/general/include/lsm6dso.h index 6ee9f99..7c1ff74 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -1,42 +1,42 @@ /* - LSM6DSOXTR IMU DRIVER Header File - Link to part Datasheet for reference: - https://www.st.com/resource/en/datasheet/lsm6dsox.pdf + LSM6DSOXTR IMU DRIVER Header File + Link to part Datasheet for reference: + https://www.st.com/resource/en/datasheet/lsm6dsox.pdf - Author: Hamza Iqbal + Author: Hamza Iqbal */ #ifndef LSM6DSO_H #define LSM6DSO_H -#include #include "stm32xx_hal.h" +#include #define LSM6DSO_I2C_ADDRESS 0x6A << 1 /* Shifted because datasheet said to */ // Not sure if these are all needed, also not sure if more need to be added /* For register descriptions reference datasheet pages 47 - 98 */ -#define LSM6DSO_REG_FUNC_CFG_ACCESS \ - 0x01 /* Enable embedded functions register */ -#define LSM6DSO_REG_INTERRUPT_CTRL_1 \ - 0x0D /* INT1 pin control, used for interrupts */ -#define LSM6DSO_REG_INTERRUPT_CTRL_2 \ - 0x0E /* INT2 pin control, used for interrupts */ -#define LSM6DSO_REG_DEVICE_ID 0x0F /* Register for checking communication */ +#define LSM6DSO_REG_FUNC_CFG_ACCESS \ + 0x01 /* Enable embedded functions register */ +#define LSM6DSO_REG_INTERRUPT_CTRL_1 \ + 0x0D /* INT1 pin control, used for interrupts */ +#define LSM6DSO_REG_INTERRUPT_CTRL_2 \ + 0x0E /* INT2 pin control, used for interrupts */ +#define LSM6DSO_REG_DEVICE_ID 0x0F /* Register for checking communication */ #define LSM6DSO_REG_ACCEL_CTRL 0x10 /* Accelerometer Control Register */ -#define LSM6DSO_REG_GYRO_CTRL 0x11 /* Gyroscope Control Register */ -#define LSM6DSO_REG_ALL_INTERRUPT_SRC \ - 0x1A /* Source Register for all interupsts */ -#define LSM6DSO_REG_WAKEUP_INTERRUPT_SRC \ - 0x1B /* Wake up interupt source register */ +#define LSM6DSO_REG_GYRO_CTRL 0x11 /* Gyroscope Control Register */ +#define LSM6DSO_REG_ALL_INTERRUPT_SRC \ + 0x1A /* Source Register for all interupsts */ +#define LSM6DSO_REG_WAKEUP_INTERRUPT_SRC \ + 0x1B /* Wake up interupt source register */ #define LSM6DSO_REG_TAP_INTERRUPT_SRC 0x1C /* Tap Interrupt source register */ -#define LSM6DSO_REG_6D_INTERRUPT_SRC \ - 0x1D /* 6-direction Interrupt source register */ -#define LSM6DSO_REG_STATUS 0x1E /* Status register */ -#define LSM6DSO_REG_GYRO_X_AXIS_L 0x22 /* Gyro pitch axis lower bits */ -#define LSM6DSO_REG_GYRO_X_AXIS_H 0x23 /* Gyro pitch axis upper bits */ -#define LSM6DSO_REG_GYRO_Y_AXIS_L 0x24 /* Gyro roll axis lower bits */ -#define LSM6DSO_REG_GYRO_Y_AXIS_H 0x25 /* Gyro roll axis upper bits */ -#define LSM6DSO_REG_GYRO_Z_AXIS_L 0x26 /* Gyro yaw axis lower bits */ -#define LSM6DSO_REG_GYRO_Z_AXIS_H 0x27 /* Gyro yaw axis higher bits */ +#define LSM6DSO_REG_6D_INTERRUPT_SRC \ + 0x1D /* 6-direction Interrupt source register */ +#define LSM6DSO_REG_STATUS 0x1E /* Status register */ +#define LSM6DSO_REG_GYRO_X_AXIS_L 0x22 /* Gyro pitch axis lower bits */ +#define LSM6DSO_REG_GYRO_X_AXIS_H 0x23 /* Gyro pitch axis upper bits */ +#define LSM6DSO_REG_GYRO_Y_AXIS_L 0x24 /* Gyro roll axis lower bits */ +#define LSM6DSO_REG_GYRO_Y_AXIS_H 0x25 /* Gyro roll axis upper bits */ +#define LSM6DSO_REG_GYRO_Z_AXIS_L 0x26 /* Gyro yaw axis lower bits */ +#define LSM6DSO_REG_GYRO_Z_AXIS_H 0x27 /* Gyro yaw axis higher bits */ #define LSM6DSO_REG_ACCEL_X_AXIS_L 0x28 /* Accelerometer X axis lower bits */ #define LSM6DSO_REG_ACCEL_X_AXIS_H 0x29 /* Accelerometer X axis upper bits */ #define LSM6DSO_REG_ACCEL_Y_AXIS_L 0x2A /* Accelerometer Y axis lower bits */ @@ -65,13 +65,14 @@ enum lsm6dso_axes { */ typedef struct { - uint8_t accel_config; //TODO: We should make these cfg packed unions with bitfield structs + uint8_t accel_config; // TODO: We should make these cfg packed unions with + // bitfield structs - uint8_t gyro_config; + uint8_t gyro_config; - int16_t accel_data[3]; + int16_t accel_data[3]; - int16_t gyro_data[3]; + int16_t gyro_data[3]; I2C_Read read_reg; diff --git a/general/src/lsm6dso.c b/general/src/lsm6dso.c index 71963c7..c0451a5 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -1,9 +1,9 @@ /* - LSM6DSOXTR IMU DRIVER Source File - Link to part Datasheet for reference: - https://www.st.com/resource/en/datasheet/lsm6dsox.pdf + LSM6DSOXTR IMU DRIVER Source File + Link to part Datasheet for reference: + https://www.st.com/resource/en/datasheet/lsm6dsox.pdf - Author: Hamza Iqbal + Author: Hamza Iqbal */ #include "lsm6dso.h" @@ -41,25 +41,23 @@ static inline int lsm6dso_write_mult_reg(uint8_t *data, return imu.write_reg(data, reg, length); } -static int16_t accel_data_convert(int16_t raw_accel) -{ - int8_t msb, lsb; - int16_t val; - msb = (raw_accel & 0x00FF) << 8; - lsb = (raw_accel & 0xFF00) >> 8; - val = msb | lsb; +static int16_t accel_data_convert(int16_t raw_accel) { + int8_t msb, lsb; + int16_t val; + msb = (raw_accel & 0x00FF) << 8; + lsb = (raw_accel & 0xFF00) >> 8; + val = msb | lsb; - return (int16_t)(((int32_t)val * ACCEL_RANGE * 1000) / REG_RESOLUTION); + return (int16_t)(((int32_t)val * ACCEL_RANGE * 1000) / REG_RESOLUTION); } -static int16_t gyro_data_convert(int16_t gyro_accel) -{ - int8_t msb, lsb; - int16_t val; - msb = (gyro_accel & 0x00FF) << 8; - lsb = (gyro_accel & 0xFF00) >> 8; - val = msb | lsb; - return (int16_t)(((int32_t)val * GYRO_RANGE * 100) / REG_RESOLUTION); +static int16_t gyro_data_convert(int16_t gyro_accel) { + int8_t msb, lsb; + int16_t val; + msb = (gyro_accel & 0x00FF) << 8; + lsb = (gyro_accel & 0xFF00) >> 8; + val = msb | lsb; + return (int16_t)(((int32_t)val * GYRO_RANGE * 100) / REG_RESOLUTION); } static int lsm6dso_ping_imu() From 225d53887a56df1bf5055de8a082c745ab178932 Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 13 Oct 2024 16:32:30 -0400 Subject: [PATCH 7/9] #162-added imu pointer to each function --- general/src/lsm6dso.c | 131 +++++++++++++++++++++--------------------- 1 file changed, 64 insertions(+), 67 deletions(-) diff --git a/general/src/lsm6dso.c b/general/src/lsm6dso.c index 4387cd5..dbd608e 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -14,59 +14,55 @@ 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) { - int8_t msb, lsb; - int16_t val; - msb = (raw_accel & 0x00FF) << 8; - lsb = (raw_accel & 0xFF00) >> 8; - val = msb | lsb; +static int16_t accel_data_convert(int16_t raw_accel) +{ + int8_t msb, lsb; + int16_t val; + msb = (raw_accel & 0x00FF) << 8; + lsb = (raw_accel & 0xFF00) >> 8; + val = msb | lsb; - return (int16_t)(((int32_t)val * ACCEL_RANGE * 1000) / REG_RESOLUTION); + return (int16_t)(((int32_t)val * ACCEL_RANGE * 1000) / REG_RESOLUTION); } -static int16_t gyro_data_convert(int16_t gyro_accel) { - int8_t msb, lsb; - int16_t val; - msb = (gyro_accel & 0x00FF) << 8; - lsb = (gyro_accel & 0xFF00) >> 8; - val = msb | lsb; - return (int16_t)(((int32_t)val * GYRO_RANGE * 100) / REG_RESOLUTION); +static int16_t gyro_data_convert(int16_t gyro_accel) +{ + int8_t msb, lsb; + int16_t val; + msb = (gyro_accel & 0x00FF) << 8; + lsb = (gyro_accel & 0xFF00) >> 8; + val = msb | lsb; + 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; @@ -76,43 +72,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; @@ -120,18 +117,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]; @@ -140,30 +137,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]; @@ -172,25 +169,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 626a36c8be64515e7efc890cd7ee1e11e36501fc Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 13 Oct 2024 16:40:13 -0400 Subject: [PATCH 8/9] #162-fixed header files --- general/include/lsm6dso.h | 73 +++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/general/include/lsm6dso.h b/general/include/lsm6dso.h index fb42810..67ae0e1 100644 --- a/general/include/lsm6dso.h +++ b/general/include/lsm6dso.h @@ -14,29 +14,29 @@ #define LSM6DSO_I2C_ADDRESS 0x6A << 1 /* Shifted because datasheet said to */ // Not sure if these are all needed, also not sure if more need to be added /* For register descriptions reference datasheet pages 47 - 98 */ -#define LSM6DSO_REG_FUNC_CFG_ACCESS \ - 0x01 /* Enable embedded functions register */ -#define LSM6DSO_REG_INTERRUPT_CTRL_1 \ - 0x0D /* INT1 pin control, used for interrupts */ -#define LSM6DSO_REG_INTERRUPT_CTRL_2 \ - 0x0E /* INT2 pin control, used for interrupts */ -#define LSM6DSO_REG_DEVICE_ID 0x0F /* Register for checking communication */ +#define LSM6DSO_REG_FUNC_CFG_ACCESS \ + 0x01 /* Enable embedded functions register */ +#define LSM6DSO_REG_INTERRUPT_CTRL_1 \ + 0x0D /* INT1 pin control, used for interrupts */ +#define LSM6DSO_REG_INTERRUPT_CTRL_2 \ + 0x0E /* INT2 pin control, used for interrupts */ +#define LSM6DSO_REG_DEVICE_ID 0x0F /* Register for checking communication */ #define LSM6DSO_REG_ACCEL_CTRL 0x10 /* Accelerometer Control Register */ -#define LSM6DSO_REG_GYRO_CTRL 0x11 /* Gyroscope Control Register */ -#define LSM6DSO_REG_ALL_INTERRUPT_SRC \ - 0x1A /* Source Register for all interupsts */ -#define LSM6DSO_REG_WAKEUP_INTERRUPT_SRC \ - 0x1B /* Wake up interupt source register */ +#define LSM6DSO_REG_GYRO_CTRL 0x11 /* Gyroscope Control Register */ +#define LSM6DSO_REG_ALL_INTERRUPT_SRC \ + 0x1A /* Source Register for all interupsts */ +#define LSM6DSO_REG_WAKEUP_INTERRUPT_SRC \ + 0x1B /* Wake up interupt source register */ #define LSM6DSO_REG_TAP_INTERRUPT_SRC 0x1C /* Tap Interrupt source register */ -#define LSM6DSO_REG_6D_INTERRUPT_SRC \ - 0x1D /* 6-direction Interrupt source register */ -#define LSM6DSO_REG_STATUS 0x1E /* Status register */ -#define LSM6DSO_REG_GYRO_X_AXIS_L 0x22 /* Gyro pitch axis lower bits */ -#define LSM6DSO_REG_GYRO_X_AXIS_H 0x23 /* Gyro pitch axis upper bits */ -#define LSM6DSO_REG_GYRO_Y_AXIS_L 0x24 /* Gyro roll axis lower bits */ -#define LSM6DSO_REG_GYRO_Y_AXIS_H 0x25 /* Gyro roll axis upper bits */ -#define LSM6DSO_REG_GYRO_Z_AXIS_L 0x26 /* Gyro yaw axis lower bits */ -#define LSM6DSO_REG_GYRO_Z_AXIS_H 0x27 /* Gyro yaw axis higher bits */ +#define LSM6DSO_REG_6D_INTERRUPT_SRC \ + 0x1D /* 6-direction Interrupt source register */ +#define LSM6DSO_REG_STATUS 0x1E /* Status register */ +#define LSM6DSO_REG_GYRO_X_AXIS_L 0x22 /* Gyro pitch axis lower bits */ +#define LSM6DSO_REG_GYRO_X_AXIS_H 0x23 /* Gyro pitch axis upper bits */ +#define LSM6DSO_REG_GYRO_Y_AXIS_L 0x24 /* Gyro roll axis lower bits */ +#define LSM6DSO_REG_GYRO_Y_AXIS_H 0x25 /* Gyro roll axis upper bits */ +#define LSM6DSO_REG_GYRO_Z_AXIS_L 0x26 /* Gyro yaw axis lower bits */ +#define LSM6DSO_REG_GYRO_Z_AXIS_H 0x27 /* Gyro yaw axis higher bits */ #define LSM6DSO_REG_ACCEL_X_AXIS_L 0x28 /* Accelerometer X axis lower bits */ #define LSM6DSO_REG_ACCEL_X_AXIS_H 0x29 /* Accelerometer X axis upper bits */ #define LSM6DSO_REG_ACCEL_Y_AXIS_L 0x2A /* Accelerometer Y axis lower bits */ @@ -45,17 +45,15 @@ #define LSM6DSO_REG_ACCEL_Z_AXIS_H 0x2D /* Accelerometer Z axis upper bits */ /* Function Pointers*/ -typedef int (*I2C_Read)(uint8_t *data, uint8_t reg, - uint8_t length); +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 * */ enum lsm6dso_axes { - + LSM6DSO_X_AXIS = 0, LSM6DSO_Y_AXIS = 1, LSM6DSO_Z_AXIS = 2 @@ -67,15 +65,14 @@ enum lsm6dso_axes { * */ typedef struct { + uint8_t accel_config; // TODO: We should make these cfg packed unions with + // bitfield structs - uint8_t accel_config; // TODO: We should make these cfg packed unions with - // bitfield structs - - uint8_t gyro_config; + uint8_t gyro_config; - int16_t accel_data[3]; + int16_t accel_data[3]; - int16_t gyro_data[3]; + int16_t gyro_data[3]; I2C_Read read_reg; @@ -88,7 +85,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 */ /** @@ -101,8 +98,8 @@ int lsm6dso_init(I2C_Read read_func, I2C_Write write_func); * @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 @@ -113,20 +110,20 @@ int lsm6dso_set_accel_cfg(int8_t odr_sel, * @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 10483ddd14f1b4f3e40590940e60f0999281be3c Mon Sep 17 00:00:00 2001 From: Caio Date: Sun, 13 Oct 2024 16:42:59 -0400 Subject: [PATCH 9/9] #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 dbd608e..7a75283 100644 --- a/general/src/lsm6dso.c +++ b/general/src/lsm6dso.c @@ -79,7 +79,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); } @@ -90,7 +90,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)