Skip to content

Commit

Permalink
generalized funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodasilva2005 committed Oct 27, 2024
1 parent d87a3e0 commit 455c440
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
40 changes: 40 additions & 0 deletions general/include/lsm6dso.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;

/**
Expand All @@ -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
Expand Down
37 changes: 27 additions & 10 deletions general/src/lsm6dso.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 455c440

Please sign in to comment.