IMU_DUAL/TDK/IMU_IAM20680.h

368 lines
15 KiB
C
Raw Normal View History

2022-12-12 21:23:33 +08:00
/*
* IMU_IAM20680.h
*
* Created on: Nov 5, 2022
* Author: gcstk
*/
#include "stm32f1xx_hal.h"
#ifndef INC_IMU_IAM20680_H_
#define INC_IMU_IAM20680_H_
#define IAM20680_MASTER_I2C 0
#define IAM20680_MASTER_SPI 1
#define IAM20680_OK 0x00
#define IAM20680_INIT_ERROR 0xFF
/* defgroup slave_address Slave Address */
#define IAM20680_I2C_SLAVE_ADDRESS_0 0x68
#define IAM20680_I2C_SLAVE_ADDRESS_1 0x69
/*defgroup spi_command Spi command */
#define IAM20680_SPI_WRITE_CMD 0x00
#define IAM20680_SPI_READ_CMD 0x80
/**
* \defgroup Register Map
*/
#define IAM20680_REG_SELF_TEST_X_GYRO 0x00
#define IAM20680_REG_SELF_TEST_Y_GYRO 0x01
#define IAM20680_REG_SELF_TEST_Z_GYRO 0x02
#define IAM20680_REG_SELF_TEST_X_ACCEL 0x0D
#define IAM20680_REG_SELF_TEST_Y_ACCEL 0x0E
#define IAM20680_REG_SELF_TEST_Z_ACCEL 0x0F
#define IAM20680_REG_XG_OFFS_USRH 0x13
#define IAM20680_REG_XG_OFFS_USRL 0x14
#define IAM20680_REG_YG_OFFS_USRH 0x15
#define IAM20680_REG_YG_OFFS_USRL 0x16
#define IAM20680_REG_ZG_OFFS_USRH 0x17
#define IAM20680_REG_ZG_OFFS_USRL 0x18
#define IAM20680_REG_SMPLRT_DIV 0x19
#define IAM20680_REG_CONFIG 0x1A
#define IAM20680_REG_GYRO_CONFIG 0x1B
#define IAM20680_REG_ACCEL_CONFIG 0x1C
#define IAM20680_REG_ACCEL_CONFIG_2 0x1D
#define IAM20680_REG_LP_MODE_CFG 0x1E
#define IAM20680_REG_ACCEL_WOM_THR 0x1F
#define IAM20680_REG_FIFO_EN 0x23
#define IAM20680_REG_FSYNC_INT 0x36
#define IAM20680_REG_INT_PIN_CFG 0x37
#define IAM20680_REG_INT_ENABLE 0x38
#define IAM20680_REG_INT_STATUS 0x3A
#define IAM20680_REG_ACCEL_XOUT_H 0x3B
#define IAM20680_REG_ACCEL_XOUT_L 0x3C
#define IAM20680_REG_ACCEL_YOUT_H 0x3D
#define IAM20680_REG_ACCEL_YOUT_L 0x3E
#define IAM20680_REG_ACCEL_ZOUT_H 0x3F
#define IAM20680_REG_ACCEL_ZOUT_L 0x40
#define IAM20680_REG_TEMP_OUT_H 0x41
#define IAM20680_REG_TEMP_OUT_L 0x42
#define IAM20680_REG_GYRO_XOUT_H 0x43
#define IAM20680_REG_GYRO_XOUT_L 0x44
#define IAM20680_REG_GYRO_YOUT_H 0x45
#define IAM20680_REG_GYRO_YOUT_L 0x46
#define IAM20680_REG_GYRO_ZOUT_H 0x47
#define IAM20680_REG_GYRO_ZOUT_L 0x48
#define IAM20680_REG_SIGNAL_PATH_RESET 0x68
#define IAM20680_REG_ACCEL_INTEL_CTRL 0x69
#define IAM20680_REG_USER_CTRL 0x6A
#define IAM20680_REG_PWR_MGMT_1 0x6B
#define IAM20680_REG_PWR_MGMT_2 0x6C
#define IAM20680_REG_FIFO_COUNTH 0x72
#define IAM20680_REG_FIFO_COUNTL 0x73
#define IAM20680_REG_FIFO_R_W 0x74
#define IAM20680_REG_WHO_AM_I 0x75
#define IAM20680_REG_XA_OFFSET_H 0x77
#define IAM20680_REG_XA_OFFSET_L 0x78
#define IAM20680_REG_YA_OFFSET_H 0x7A
#define IAM20680_REG_YA_OFFSET_L 0x7B
#define IAM20680_REG_ZA_OFFSET_H 0x7D
#define IAM20680_REG_ZA_OFFSET_L 0x7E
/**
* \defgroup configuration Configuration
*/
#define IAM20680_BM_FIFO_MODE_FULL_REPLACE_OLD_DATA 0x00
#define IAM20680_BM_FIFO_MODE_FULL_NO_WRITE_DATA 0x40
#define IAM20680_BM_DISABLE_FSYNC_PIN 0x00
#define IAM20680_BM_ENABLE_FSYNC_PIN_TEMP_OUT_L 0x08
#define IAM20680_BM_ENABLE_FSYNC_PIN_GYRO_XOUT_L 0x10
#define IAM20680_BM_ENABLE_FSYNC_PIN_GYRO_YOUT_L 0x18
#define IAM20680_BM_ENABLE_FSYNC_PIN_GYRO_ZOUT_L 0x20
#define IAM20680_BM_ENABLE_FSYNC_PIN_ACCEL_XOUT_L 0x28
#define IAM20680_BM_ENABLE_FSYNC_PIN_ACCEL_YOUT_L 0x30
#define IAM20680_BM_ENABLE_FSYNC_PIN_ACCEL_ZOUT_L 0x38
/**
* @brief GYRO_CONFIG Reg FS_SEL Bits enumeration
*/
typedef enum
{
IAM20680_GYRO_CONFIG_FS_SEL_250dps = 0x00, /*!< gyro full scale range = +/-250dps */
IAM20680_GYRO_CONFIG_FS_SEL_500dps = 0x08, /*!< gyro full scale range = +/-500dps */
IAM20680_GYRO_CONFIG_FS_SEL_1000dps = 0x10, /*!< gyro full scale range = +/-1000dps */
IAM20680_GYRO_CONFIG_FS_SEL_2000dps = 0x18, /*!< gyro full scale range = +/-2000dps */
} IAM20680_GYRO_CONFIG_FS_SEL_t;
#define BIT_GYRO_FS_SEL_MASK 0x18
#define BIT_GYRO_FS_SEL_POS 3
/**
* @brief CONFIG Reg DLPF_CFG Bits enumeration
*/
typedef enum
{
IAM20680_CONFIG_DLPF_CFG_250 = 0, /*!< gyro 3-dB BW(Hz) = 250, Rate(kHz) = 8 */
IAM20680_CONFIG_DLPF_CFG_176 = 1, /*!< gyro 3-dB BW(Hz) = 176, Rate(kHz) = 1 */
IAM20680_CONFIG_DLPF_CFG_92 = 2, /*!< gyro 3-dB BW(Hz) = 92, Rate(kHz) = 1 */
IAM20680_CONFIG_DLPF_CFG_41 = 3, /*!< gyro 3-dB BW(Hz) = 41, Rate(kHz) = 1 */
IAM20680_CONFIG_DLPF_CFG_20 = 4, /*!< gyro 3-dB BW(Hz) = 20, Rate(kHz) = 1 */
IAM20680_CONFIG_DLPF_CFG_10 = 5, /*!< gyro 3-dB BW(Hz) = 10, Rate(kHz) = 1 */
IAM20680_CONFIG_DLPF_CFG_5 = 6, /*!< gyro 3-dB BW(Hz) = 5, Rate(kHz) = 1 */
IAM20680_CONFIG_DLPF_CFG_3281 = 7, /*!< gyro 3-dB BW(Hz) = 3281, Rate(kHz) = 8 */
} IAM20680_CONFIG_DLPF_CFG_t;
#define BIT_DLPF_CFG_MASK 0x07
/**
* @brief ACCEL_CONFIG Reg ACCEL_FS_SEL Bits enumeration
*/
typedef enum
{
IAM20680_ACCEL_CONFIG_FS_SEL_2g = 0x00, /*!< accel full scale range = +/-2g */
IAM20680_ACCEL_CONFIG_FS_SEL_4g = 0x08, /*!< accel full scale range = +/-4g */
IAM20680_ACCEL_CONFIG_FS_SEL_8g = 0x10, /*!< accel full scale range = +/-8g */
IAM20680_ACCEL_CONFIG_FS_SEL_16g = 0x18, /*!< accel full scale range = +/-16g */
} IAM20680_ACCEL_CONFIG_FS_SEL_t;
#define BIT_ACCEL_FS_SEL_MASK 0x18
#define BIT_ACCEL_FS_SEL_POS 3
/**
* @brief ACCEL_CONFIG2 Reg A_DLPF_CFG Bits enumeration
*/
typedef enum
{
IAM20680_ACCEL_CONFIG2_A_DLPF_CFG_218 = 1, /*!< accel 3-dB BW(Hz) = 218.1, Rate(kHz) = 1 */
IAM20680_ACCEL_CONFIG2_A_DLPF_CFG_99 = 2, /*!< accel 3-dB BW(Hz) = 99.0, Rate(kHz) = 1 */
IAM20680_ACCEL_CONFIG2_A_DLPF_CFG_45 = 3, /*!< accel 3-dB BW(Hz) = 44.8, Rate(kHz) = 1 */
IAM20680_ACCEL_CONFIG2_A_DLPF_CFG_21 = 4, /*!< accel 3-dB BW(Hz) = 21.2, Rate(kHz) = 1 */
IAM20680_ACCEL_CONFIG2_A_DLPF_CFG_10 = 5, /*!< accel 3-dB BW(Hz) = 10.2, Rate(kHz) = 1 */
IAM20680_ACCEL_CONFIG2_A_DLPF_CFG_5 = 6, /*!< accel 3-dB BW(Hz) = 5.1, Rate(kHz) = 1 */
IAM20680_ACCEL_CONFIG2_A_DLPF_CFG_420 = 7, /*!< accel 3-dB BW(Hz) = 420.0, Rate(kHz) = 1 */
} IAM20680_ACCEL_CONFIG2_A_DLPF_CFG_t;
#define BIT_A_DLPF_CFG_MASK 0x07
/**
* \defgroup bit_mask_fifo_enable Bit mask FIFO enable
*/
#define IAM20680_BM_DISABLE_TEMP_FIFO 0x00
#define IAM20680_BM_ENABLE_TEMP_FIFO 0x80
#define IAM20680_BM_DISABLE_XGYRO_FIFO 0x00
#define IAM20680_BM_ENABLE_XGYRO_FIFO 0x40
#define IAM20680_BM_DISABLE_YGYRO_FIFO 0x00
#define IAM20680_BM_ENABLE_YGYRO_FIFO 0x20
#define IAM20680_BM_DISABLE_ZGYRO_FIFO 0x00
#define IAM20680_BM_ENABLE_ZGYRO_FIFO 0x10
#define IAM20680_BM_DISABLE_ACCEL_FIFO 0x00
#define IAM20680_BM_ENABLE_ACCEL_FIFO 0x08
/* defgroup device_id Device ID */
#define IAM20680_DEVICE_ID 0xA9
#define IAM20680HT_DEVICE_ID 0xFA
/**
* @brief Master Input/Output type.
*/
struct IAM20680_s;
typedef void (*IAM20680_master_io_t)(struct IAM20680_s*, uint8_t, uint8_t*, uint8_t);
/**
* @brief Click ctx object definition.
*/
typedef struct IAM20680_s
{
uint8_t slave_address;
IAM20680_master_io_t write_f;
IAM20680_master_io_t read_f;
uint8_t master_sel;
} IAM20680_t;
/**
* @brief Click configuration structure definition.
*/
typedef struct
{
uint32_t i2c_speed;
uint8_t i2c_address;
uint32_t spi_speed;
uint8_t spi_mode;
uint8_t sel;
} IAM20680_cfg_t;
// End types group
// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
/**
* \defgroup public_function Public function
*/
#ifdef __cplusplus
extern "C"{
#endif
/**
* @brief Config Object Initialization function.
*
* @param cfg Click configuration structure.
*
* @description This function initializes click configuration structure to init state.
* @note All used pins will be set to unconnected state.
*/
void IAM20680_cfg_setup(IAM20680_cfg_t *cfg);
/**
* @brief Initialization function.
* @param IAM20680 Click object.
* @param cfg Click configuration structure.
*
* @description This function initializes all necessary pins and peripherals used for this click.
*/
uint8_t IAM20680_lowlevel_init(IAM20680_t *ctx, IAM20680_cfg_t *cfg);
/**
* @brief Click Default Configuration function.
*
* @param ctx Click object.
*
* @description This function executes default configuration for 6DOF IMU 9 click.
*/
void IAM20680_default_cfg(IAM20680_t *ctx);
/**
* @brief Generic write function.
*
* @param ctx Click object.
* @param reg Register address.
* @param data_buf Output data buf
* @param len Number of the bytes to be read
*
* @description This function writes data to the desired register.
*/
void IAM20680_generic_write(IAM20680_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len);
/**
* @brief Generic read function.
*
* @param ctx Click object.
* @param reg Register address.
* @param data_buf Data buf to be written.
* @param len Number of the bytes in data buf.
*
* @description This function reads data from the desired register.
*/
void IAM20680_generic_read(IAM20680_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len);
/**
* @brief Set Gyro measurement range configuration function
*
* @param ctx Click object.
* @param gyro_full_scale_range
* @note
*<pre>
* - 250 : Gyro Full Scale Select 250 dps;
* - 500 : Gyro Full Scale Select 500 dps;
* - 1000 : Gyro Full Scale Select 1000 dps;
* - 2000 : Gyro Full Scale Select 2000 dps;
*</pre>
*
* @description Function set Gyro configuration data to the targeted IAM20680_REG_GYRO_CONFIG register address of
* IAM-20680 High Performance Automotive 6-Axis MotionTracking Device on 6DOF IMU 9 Click board.
*/
void IAM20680_set_gyro_measurement_range(IAM20680_t *ctx, IAM20680_GYRO_CONFIG_FS_SEL_t new_value);
/**
* @brief Set Accel measurement range configuration function
*
* @param ctx Click object.
* @param accel_full_scale_range
*
* @note
*<pre>
* - 2 : Accel Full Scale Select 2g;
* - 4 : Accel Full Scale Select 4g;
* - 8 : Accel Full Scale Select 8g;
* - 16 : Accel Full Scale Select 16g;
*</pre>
* @description Function set Accel measurement range configuration data to the targeted IAM20680_REG_ACCEL_CONFIG
* register address of IAM-20680 High Performance Automotive 6-Axis MotionTracking Device on 6DOF IMU 9 Click board.
*/
void IAM20680_set_accel_measurement_range(IAM20680_t *ctx, uint8_t accel_full_scale_range);
/**
* @brief Get device ID function
*
* @param ctx Click object.
*
* @return
* 8-bit device ID value ( default 0xA9 );
* @description This function reads data from the desired register.
*/
uint8_t IAM20680_get_device_id(IAM20680_t *ctx);
/**
* @brief Get axis data function
*
* @param ctx Click object.
* @param addr_reg_msb least significant bit register address
* @param addr_reg_lsb most significant bit register address
*
* @description Function get axis data by read data from the two targeted register address
* of IAM-20680 High Performance Automotive 6-Axis MotionTracking Device on 6DOF IMU 9 Click board.
*/
int16_t IAM20680_get_axis(IAM20680_t *ctx, uint8_t addr_reg_msb, uint8_t addr_reg_lsb);
/**
* @brief Read Accel X-axis, Y-axis and Z-axis axis function
*
* @param ctx Click object.
* @param p_accel_x pointer to memory location where Accel X-axis data be stored
* @param p_accel_y pointer to memory location where Accel Y-axis data be stored
* @param p_accel_z pointer to memory location where Accel Z-axis data be stored
*
* @description Function reads 16-bit ( signed ) Accel X-axis, Y-axis data and Z-axis data from the
* targeted starts from IAM20680_REG_ACCEL_XOUT_H to the IAM20680_REG_ACCEL_ZOUT_L register address
* of IAM-20680 High Performance Automotive 6-Axis MotionTracking Device on 6DOF IMU 9 Click board.
*/
void IAM20680_get_accel_data(IAM20680_t *ctx, int16_t *p_accel_x, int16_t *p_accel_y, int16_t *p_accel_z);
/**
* @brief Read Gyro X-axis, Y-axis and Z-axis axis function
*
* @param ctx Click object.
* @param p_gyro_x pointer to memory location where Accel X-axis data be stored
* @param p_gyro_y pointer to memory location where Accel Y-axis data be stored
* @param p_gyro_z pointer to memory location where Accel Z-axis data be stored
*
* @description Function reads 16-bit ( signed ) Gyro X-axis, Y-axis data and Z-axis data from the
* targeted starts from IAM20680_REG_GYRO_XOUT_H to the IAM20680_REG_GYRO_ZOUT_L register address
* of IAM-20680 High Performance Automotive 6-Axis MotionTracking Device on 6DOF IMU 9 Click board.
*/
void IAM20680_get_gyro_data(IAM20680_t *ctx, int16_t *p_gyro_x, int16_t *p_gyro_y, int16_t *p_gyro_z);
void IAM20680_set_gyro_DLPF_CFG(IAM20680_t *ctx, IAM20680_CONFIG_DLPF_CFG_t new_value);
void IAM20680_set_accel_A_DLPF_CFG(IAM20680_t *ctx, IAM20680_ACCEL_CONFIG2_A_DLPF_CFG_t new_value);
uint8_t IAM20680_reg_write(IAM20680_t *ctx, uint8_t reg, uint8_t reg_val, uint8_t cheak);
void IAM20680_set_spi_mode(IAM20680_t *ctx);
uint8_t IAM20680_init(IAM20680_t *ctx, IAM20680_cfg_t *cfg);
#endif /* INC_IMU_IAM20680_H_ */