From 4a81beaa4ca0d34f417807ae57a55c5136c5e7f5 Mon Sep 17 00:00:00 2001 From: fize Date: Mon, 12 Dec 2022 21:23:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=BF=E6=8D=A2tdk=E9=A9=B1=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Src/main.c | 37 ++--- TDK/IMU_ IAM20680.c | 250 ++++++++++++++++++++++++++++++ TDK/IMU_IAM20680.h | 367 ++++++++++++++++++++++++++++++++++++++++++++ TDK/iam20680.c | 361 ------------------------------------------- TDK/iam20680.h | 329 --------------------------------------- TDK/user_iam20680.c | 35 ++++- TDK/user_iam20680.h | 25 ++- 7 files changed, 688 insertions(+), 716 deletions(-) create mode 100644 TDK/IMU_ IAM20680.c create mode 100644 TDK/IMU_IAM20680.h delete mode 100644 TDK/iam20680.c delete mode 100644 TDK/iam20680.h diff --git a/Core/Src/main.c b/Core/Src/main.c index 047912e..0e72967 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -24,8 +24,6 @@ #include "user_asm330lhh.h" #include "asm330lhh_reg.h" #include "user_iam20680.h" -#include "iam20680.h" -#include "iam20680.h" #include "rtklib.h" #include "stdio.h" /* USER CODE END Includes */ @@ -88,11 +86,6 @@ uint32_t time_100us=0; nmea_t nmea={0}; extern IMU_mng IMU_mng_st; extern IMU_mng IMU_mng_tdk; -static int16_t data_raw_acceleration[3]; -static int16_t data_raw_angular_rate[3]; -static int16_t data_raw_temperature; -static float acceleration_mg[3]; -static float angular_rate_mdps[3]; /* USER CODE END 0 */ /** @@ -144,19 +137,27 @@ int main(void) { TIM3_Get_100us(); if(1 == SPI2_INT_RD) - { - SPI2_INT_RD = 0; -// application_task(); - } - if(1 == SPI1_INT_RD) { - SPI1_INT_RD=0; - asm_sample(); - printf("{ GPS_week: %d week_sec: %.3lf \r\n",GPS_week,GPS_sec+time_ms*0.001); - printf("x_a=%.3fm/s2 y_a=%.3fm/s2 z_a=%.3fm/s2\r\n",asm330.x_acc,asm330.y_acc,asm330.z_acc); - printf("x_g=%.3fdeg/s y_g=%.3fdeg/s z_g=%.3fdeg/s }\r\n\r\n\r\n",asm330.x_gyro,asm330.y_gyro,asm330.z_gyro); -// printf("time: %.0lfh:%.0lfm:%.0lfs:%.3fms \r\n",time_hh,time_mm,time_ss,time_ms); + SPI2_INT_RD = 0; + tdk_sample(); + printf("{ GPS_week: %d week_sec: %.3lf \r\n", GPS_week, + GPS_sec + time_ms * 0.001); + printf("x_a=%.3fm/s2 y_a=%.3fm/s2 z_a=%.3fm/s2\r\n", iam20680.x_acc, + iam20680.y_acc, iam20680.z_acc); + printf("x_g=%.3fdeg/s y_g=%.3fdeg/s z_g=%.3fdeg/s }\r\n\r\n\r\n", + iam20680.x_gyro, iam20680.y_gyro, iam20680.z_gyro); + + // application_task(); } +// if(1 == SPI1_INT_RD) +// { +// SPI1_INT_RD=0; +// asm_sample(); +// printf("{ GPS_week: %d week_sec: %.3lf \rextern tdk_value iam20680;\n",GPS_week,GPS_sec+time_ms*0.001); +// printf("x_a=%.3fm/s2 y_a=%.3fm/s2 z_a=%.3fm/s2\r\n",asm330.x_acc,asm330.y_acc,asm330.z_acc); +// printf("x_g=%.3fdeg/s y_g=%.3fdeg/s z_g=%.3fdeg/s }\r\n\r\n\r\n",asm330.x_gyro,asm330.y_gyro,asm330.z_gyro); +//// printf("time: %.0lfh:%.0lfm:%.0lfs:%.3fms \r\n",time_hh,time_mm,time_ss,time_ms); +// } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ diff --git a/TDK/IMU_ IAM20680.c b/TDK/IMU_ IAM20680.c new file mode 100644 index 0000000..457b6f9 --- /dev/null +++ b/TDK/IMU_ IAM20680.c @@ -0,0 +1,250 @@ +/* + * IMU_ IAM20680.c + * + * Created on: Nov 5, 2022 + * Author: gcstk + */ + +#include +#include +#include "user_iam20680.h" + +static void IAM20680_spi_write(IAM20680_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len); + +static void IAM20680_spi_read(IAM20680_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len); + +extern SPI_HandleTypeDef hspi2; + + +// ------------------------------------------------ PUBLIC FUNCTION DEFINITIONS +uint8_t IAM20680_init(IAM20680_t *ctx, IAM20680_cfg_t *cfg) +{ + uint8_t retry = 3; + + while (retry != 0) + { + retry--; + HAL_Delay(150); + if (IAM20680_OK == IAM20680_lowlevel_init(ctx, cfg)) + return IAM20680_OK; + } + return IAM20680_INIT_ERROR; +} +uint8_t IAM20680_lowlevel_init(IAM20680_t *ctx, IAM20680_cfg_t *cfg) +{ + uint8_t device_id; + + ctx->read_f = IAM20680_spi_read; + ctx->write_f = IAM20680_spi_write; + /* cheak IMU in board */ + device_id = IAM20680_get_device_id(ctx); + if (device_id != IAM20680_DEVICE_ID) + { + return IAM20680_INIT_ERROR; + } + + /* initialize the sensor */ + IAM20680_reg_write(ctx, IAM20680_REG_PWR_MGMT_1, 0x80, 0); // reset IMU + HAL_Delay(100); + IAM20680_set_spi_mode(ctx); // set to spi mode + IAM20680_reg_write(ctx, IAM20680_REG_PWR_MGMT_1, 0x01, 0); // Auto selects the best available clock source + + /* Disable Accel and Gyro all axes */ + IAM20680_reg_write(ctx, IAM20680_REG_PWR_MGMT_2, 0x3F, 1); + + /* Set default full scale range */ + IAM20680_set_gyro_measurement_range(ctx, IAM20680_GYRO_CONFIG_FS_SEL_2000dps); + IAM20680_set_accel_measurement_range(ctx, IAM20680_ACCEL_CONFIG_FS_SEL_2g); + + /* Set default bandwidth */ + IAM20680_set_gyro_DLPF_CFG(ctx, IAM20680_CONFIG_DLPF_CFG_92); + IAM20680_set_accel_A_DLPF_CFG(ctx, IAM20680_ACCEL_CONFIG2_A_DLPF_CFG_99); + + /* Initial sampling rate to 100Hz*/ + IAM20680_reg_write(ctx, IAM20680_REG_SMPLRT_DIV, 9, 1); + + /* Set interrupt */ + IAM20680_reg_write(ctx, IAM20680_REG_INT_PIN_CFG, 0x10, 1); // set interrupt pin mode + IAM20680_reg_write(ctx, IAM20680_REG_INT_ENABLE, 0x01, 1); // enable interrupt + + /* Enable Accel and Gyro all axes */ + IAM20680_reg_write(ctx, IAM20680_REG_PWR_MGMT_2, 0x00, 1); + + return IAM20680_OK; +} + +void IAM20680_generic_write(IAM20680_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len) +{ + ctx->write_f(ctx, reg, data_buf, len); +} + +void IAM20680_generic_read(IAM20680_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len) +{ + ctx->read_f(ctx, reg, data_buf, len); +} + +void IAM20680_set_config(IAM20680_t *ctx, uint8_t config_data) +{ + config_data &= 0x7F; + + IAM20680_generic_write(ctx, IAM20680_REG_CONFIG, &config_data, 1); +} + +void IAM20680_set_gyro_measurement_range(IAM20680_t *ctx, IAM20680_GYRO_CONFIG_FS_SEL_t new_value) +{ + uint8_t value; + + IAM20680_generic_read(ctx, IAM20680_REG_GYRO_CONFIG, &value, 1); + value &= ~BIT_GYRO_FS_SEL_MASK; + value |= new_value; + IAM20680_generic_write(ctx, IAM20680_REG_GYRO_CONFIG, &value, 1); +} + +void IAM20680_set_accel_measurement_range(IAM20680_t *ctx, IAM20680_ACCEL_CONFIG_FS_SEL_t new_value) +{ + uint8_t value; + + IAM20680_generic_read(ctx, IAM20680_REG_ACCEL_CONFIG, &value, 1); + value &= ~BIT_ACCEL_FS_SEL_MASK; + value |= new_value; + + IAM20680_generic_write(ctx, IAM20680_REG_ACCEL_CONFIG, &value, 1); +} + +void IAM20680_set_gyro_DLPF_CFG(IAM20680_t *ctx, IAM20680_CONFIG_DLPF_CFG_t new_value) +{ + uint8_t value; + + IAM20680_generic_read(ctx, IAM20680_REG_CONFIG, &value, 1); + value &= ~BIT_DLPF_CFG_MASK; + value |= new_value; + IAM20680_generic_write(ctx, IAM20680_REG_CONFIG, &value, 1); +} + +void IAM20680_set_accel_A_DLPF_CFG(IAM20680_t *ctx, IAM20680_ACCEL_CONFIG2_A_DLPF_CFG_t new_value) +{ + uint8_t value; + + IAM20680_generic_read(ctx, IAM20680_REG_ACCEL_CONFIG_2, &value, 1); + value &= ~BIT_A_DLPF_CFG_MASK; + value |= new_value; + + IAM20680_generic_write(ctx, IAM20680_REG_ACCEL_CONFIG_2, &value, 1); +} + +uint8_t IAM20680_get_interrupt_status(IAM20680_t *ctx) +{ + uint8_t temp; + + IAM20680_generic_read(ctx, IAM20680_REG_INT_STATUS, &temp, 1); + + return temp; +} + +void IAM20680_set_spi_mode(IAM20680_t *ctx) +{ + uint8_t temp; + + IAM20680_generic_read(ctx, IAM20680_REG_USER_CTRL, &temp, 1); + + temp |= 0x10; + + IAM20680_generic_write(ctx, IAM20680_REG_USER_CTRL, &temp, 1); + + temp = 0; + + IAM20680_generic_read(ctx, IAM20680_REG_USER_CTRL, &temp, 1); +} + +uint8_t IAM20680_get_device_id(IAM20680_t *ctx) +{ + uint8_t temp; + + IAM20680_generic_read(ctx, IAM20680_REG_WHO_AM_I, &temp, 1); + + return temp; +} + +int16_t IAM20680_get_axis(IAM20680_t *ctx, uint8_t addr_reg_msb, uint8_t addr_reg_lsb) +{ + int16_t result; + uint16_t temp; + uint8_t read_buffer[2]; + + IAM20680_generic_read(ctx, addr_reg_msb, read_buffer, 2); + + temp = read_buffer[0]; + temp <<= 8; + temp |= read_buffer[1]; + + result = (int16_t) temp; + + return result; +} + +void IAM20680_get_accel_data(IAM20680_t *ctx, int16_t *p_accel_x, int16_t *p_accel_y, int16_t *p_accel_z) +{ + *p_accel_x = IAM20680_get_axis(ctx, IAM20680_REG_ACCEL_XOUT_H, IAM20680_REG_ACCEL_XOUT_L); + *p_accel_y = IAM20680_get_axis(ctx, IAM20680_REG_ACCEL_YOUT_H, IAM20680_REG_ACCEL_YOUT_L); + *p_accel_z = IAM20680_get_axis(ctx, IAM20680_REG_ACCEL_ZOUT_H, IAM20680_REG_ACCEL_ZOUT_L); +} + +void IAM20680_get_gyro_data(IAM20680_t *ctx, int16_t *p_gyro_x, int16_t *p_gyro_y, int16_t *p_gyro_z) +{ + *p_gyro_x = IAM20680_get_axis(ctx, IAM20680_REG_GYRO_XOUT_H, IAM20680_REG_GYRO_XOUT_L); + *p_gyro_y = IAM20680_get_axis(ctx, IAM20680_REG_GYRO_YOUT_H, IAM20680_REG_GYRO_YOUT_L); + *p_gyro_z = IAM20680_get_axis(ctx, IAM20680_REG_GYRO_ZOUT_H, IAM20680_REG_GYRO_ZOUT_L); +} + +static void IAM20680_spi_write(IAM20680_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len) +{ + uint8_t tx_buf[265]; + uint8_t cnt; + + tx_buf[0] = reg; + for (cnt = 1; cnt <= len; cnt++) + { + tx_buf[cnt] = data_buf[cnt - 1]; + } + HAL_GPIO_WritePin(SPI2_CSN_GPIO_Port, SPI2_CSN_Pin, GPIO_PIN_RESET); + HAL_SPI_Transmit(&hspi2, tx_buf, len + 1, 0xff); + HAL_GPIO_WritePin(SPI2_CSN_GPIO_Port, SPI2_CSN_Pin, GPIO_PIN_SET); +} + +static void IAM20680_spi_read(IAM20680_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len) +{ + uint8_t tx_buf[1]; + uint8_t rx_buf[265] = { 0 }; + uint8_t cnt; + + tx_buf[0] = reg | 0x80; + + HAL_GPIO_WritePin(SPI2_CSN_GPIO_Port, SPI2_CSN_Pin, GPIO_PIN_RESET); + HAL_SPI_Transmit(&hspi2, tx_buf, 1, 1000); + HAL_SPI_Receive(&hspi2, rx_buf, len, 1000); + HAL_GPIO_WritePin(SPI2_CSN_GPIO_Port, SPI2_CSN_Pin, GPIO_PIN_SET); + for (cnt = 0; cnt < len; cnt++) + { + data_buf[cnt] = rx_buf[cnt]; + } +} + +uint8_t IAM20680_reg_write(IAM20680_t *ctx, uint8_t reg, uint8_t reg_val, uint8_t cheak) +{ + uint8_t ans = IAM20680_OK; + uint8_t RD_reg = 0; + + IAM20680_generic_write(ctx, reg, ®_val, 1); + + if (1 == cheak) + { + IAM20680_generic_read(ctx, reg, &RD_reg, 1); + if (RD_reg != reg_val) + { + ans = IAM20680_INIT_ERROR; + } + } + + return ans; +} +// ------------------------------------------------------------------------- END diff --git a/TDK/IMU_IAM20680.h b/TDK/IMU_IAM20680.h new file mode 100644 index 0000000..b56a839 --- /dev/null +++ b/TDK/IMU_IAM20680.h @@ -0,0 +1,367 @@ +/* + * 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 + *
+ * -  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;
+ *
+ * + * @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 + *
+ * -  2 : Accel Full Scale Select  2g;
+ * -  4 : Accel Full Scale Select  4g;
+ * -  8 : Accel Full Scale Select  8g;
+ * - 16 : Accel Full Scale Select 16g;
+ *
+ * @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_ */ diff --git a/TDK/iam20680.c b/TDK/iam20680.c deleted file mode 100644 index 7c840dd..0000000 --- a/TDK/iam20680.c +++ /dev/null @@ -1,361 +0,0 @@ -/** - * @file iam20680.c - * @author Joseph Gillispie - * @date 22Aug2022 - * @brief This is the source file for the TDK IAM-20680 acceleromter/gyrometer. - */ - -/*! @file iam20680.c - * @brief Driver for IAM-20680 sensor - */ -#include "iam20680.h" - -/**\name Internal macros */ - -/**\name Internal APIs */ - -// Ex: power modes, calibration checks, etc - -/*! - * @brief This API must be called before other APIs. It verifies the chip ID of the sensor. - */ -uint8_t iam20680_init(struct iam20680_dev *dev) -{ - uint8_t status; - uint8_t buff; - - // Reset device to defaults. - buff = 0x80; - status = iam20680_write_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - - // Check chip ID to ensure IAM-20680 exists on board. - // I2C:0x68 or 0x69 depending upon the value driven on AD0 pin - status = iam20680_read_regs((uint8_t)IAM20680_WHO_AM_I, &buff, 1, dev); - dev->chip_id = buff; - - // Configure int pin as data ready. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_INT_ENABLE, &buff, 1, dev); - buff &= ~0x01; - buff |= 1 << 0; // DATA_RDY_INT_EN - status |= iam20680_write_regs((uint8_t)IAM20680_INT_ENABLE, &buff, 1, dev); - - - // Set gyro low noise mode. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_LP_MODE_CFG, &buff, 1, dev); - buff &= ~0x80; - buff |= 1 << 7; // GYRO_CYCLE - status |= iam20680_write_regs((uint8_t)IAM20680_LP_MODE_CFG, &buff, 1, dev); - - // Set accel low noise mode. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - buff &= ~0x20; - buff |= 1 << 5; // ACCEL_CYCLE - status |= iam20680_write_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - - // Wait 20 ms. - iam20680_delay_ms(20, dev); - - // Bypass gyro DLPF. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_GYRO_CONFIG, &buff, 1, dev); - buff &= ~0x03; - buff |= 0 << 0; // FCHOICE - status |= iam20680_write_regs((uint8_t)IAM20680_GYRO_CONFIG, &buff, 1, dev); - - // Bypass accel DLPF. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_ACCEL_CONFIG2, &buff, 1, dev); - buff &= ~0x08; - buff |= 0 << 3; // ACCEL_FCHOICE_B - status |= iam20680_write_regs((uint8_t)IAM20680_ACCEL_CONFIG2, &buff, 1, dev); - - // Set DLPF_CFG. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_CONFIG, &buff, 1, dev); - buff &= ~0x07; - buff |= 1 << 0; // DLPF_CFG - status |= iam20680_write_regs((uint8_t)IAM20680_CONFIG, &buff, 1, dev); - - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_ACCEL_CONFIG2, &buff, 1, dev); - buff &= ~0x07; - buff |= 1 << 0; // A_DLPF_CFG - status |= iam20680_write_regs((uint8_t)IAM20680_ACCEL_CONFIG2, &buff, 1, dev); - - // Set averaging filter. - // Gyro - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_LP_MODE_CFG, &buff, 1, dev); - buff &= ~0x70; - buff |= 0 << 4; // G_AVGCFG - status |= iam20680_write_regs((uint8_t)IAM20680_LP_MODE_CFG, &buff, 1, dev); - // Accel - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_ACCEL_CONFIG2, &buff, 1, dev); - buff &= ~0x30; - buff |= 0 << 4; // DEC2_CFG - status |= iam20680_write_regs((uint8_t)IAM20680_ACCEL_CONFIG2, &buff, 1, dev); - - // Set SMPLRT_DIV. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_SMPLRT_DIV, &buff, 1, dev); - buff = 0x09; // 10 ms / 100 Hz - status |= iam20680_write_regs((uint8_t)IAM20680_SMPLRT_DIV, &buff, 1, dev); - - // Disable accel and gyro all axes. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_PWR_MGMT_2, &buff, 1, dev); - buff |= 0x3F; - status |= iam20680_write_regs((uint8_t)IAM20680_PWR_MGMT_2, &buff, 1, dev); - - // Set full scale range. - // Gyro - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_GYRO_CONFIG, &buff, 1, dev); - buff &= ~0x18; - buff |= 1 << 3; // FS_SEL - status |= iam20680_write_regs((uint8_t)IAM20680_GYRO_CONFIG, &buff, 1, dev); - // Accel - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_ACCEL_CONFIG, &buff, 1, dev); - buff &= ~0x18; - buff |= 0 << 3; // ACCEL_FS_SEL - status |= iam20680_write_regs((uint8_t)IAM20680_ACCEL_CONFIG, &buff, 1, dev); - - // Enable accel. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_PWR_MGMT_2, &buff, 1, dev); - buff &= ~0x38; - buff |= 0 << 3; // Enable x, y, and z. - status |= iam20680_write_regs((uint8_t)IAM20680_PWR_MGMT_2, &buff, 1, dev); - iam20680_delay_ms(20, dev); - - // Enable gyro. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_PWR_MGMT_2, &buff, 1, dev); - buff &= ~0x07; - buff |= 0 << 0; // Enable x, y, and z. - status |= iam20680_write_regs((uint8_t)IAM20680_PWR_MGMT_2, &buff, 1, dev); - iam20680_delay_ms(50, dev); - - // Reset and enable FIFO. - // Disable FIFO - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_USER_CTRL, &buff, 1, dev); - buff &= ~0x40; - buff |= 0 << 6; // FIFO_EN - status |= iam20680_write_regs((uint8_t)IAM20680_USER_CTRL, &buff, 1, dev); - // Reset FIFO - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_USER_CTRL, &buff, 1, dev); - buff &= ~0x04; - buff |= 1 << 2; // FIFO_RST - status |= iam20680_write_regs((uint8_t)IAM20680_USER_CTRL, &buff, 1, dev); - // Enable gyro FIFO - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_FIFO_EN, &buff, 1, dev); - buff &= ~0x70; - buff |= 7 << 4; // Write x, y, and z to FIFO at data rate. - status |= iam20680_write_regs((uint8_t)IAM20680_FIFO_EN, &buff, 1, dev); - // Enable accel FIFO - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_FIFO_EN, &buff, 1, dev); - buff &= ~0x08; - buff |= 1 << 3; // Write x, y, and z to FIFO at data rate. - status |= iam20680_write_regs((uint8_t)IAM20680_FIFO_EN, &buff, 1, dev); - // Enable FIFO - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_USER_CTRL, &buff, 1, dev); - buff &= ~0x40; - buff |= 1 << 6; // FIFO_EN - status |= iam20680_write_regs((uint8_t)IAM20680_USER_CTRL, &buff, 1, dev); - - return status; -} - -/*! - * @brief This API must be called before other APIs. It verifies the chip ID of the sensor. - */ -uint8_t iam20680_init2(struct iam20680_dev *dev) -{ - uint8_t status = 0x00; - uint8_t buff; - - // Delay 100 ms from power-up before register read/write. - iam20680_delay_ms(100, dev); - - // Disable I2C. - status |= iam20680_read_regs((uint8_t)IAM20680_USER_CTRL, &buff, 1, dev); - buff |= 0x10; // I2C_IF_DIS - status |= iam20680_write_regs((uint8_t)IAM20680_USER_CTRL, &buff, 1, dev); - - // Check WHO_AM_I register. - status = iam20680_read_regs((uint8_t)IAM20680_WHO_AM_I, &buff, 1, dev); - dev->chip_id = buff; - - // Reset driver states. - status |= iam20680_read_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - buff |= 0x80; // DEVICE_RESET - status |= iam20680_write_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - iam20680_delay_ms(100, dev); - //buff = 0x00; - while ((buff & (0x80)) != 0x00) - { - iam20680_read_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - iam20680_delay_ms(1, dev); - } - - // Disable I2C. - status |= iam20680_read_regs((uint8_t)IAM20680_USER_CTRL, &buff, 1, dev); - buff |= 0x10; // I2C_IF_DIS - status |= iam20680_write_regs((uint8_t)IAM20680_USER_CTRL, &buff, 1, dev); - - // Wake up. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - buff &= ~0x40; // SLEEP - status |= iam20680_write_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - iam20680_delay_ms(5, dev); - - // Set up CLKSEL. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - buff &= ~0x07; // Clear CLKSEL - buff |= 0x01; // Set CLKSEL - status |= iam20680_write_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - - // Disable gyro and accel. - // Set full scale range. - // Set bandwidth. - // Set averaging filter. - // Set sampling rate. - // Disable FIFO. - // Configure FIFO. - // Enable Data Ready interrupt. - - - return status; -} - -/*! - * @brief This API must be called before other APIs. It verifies the chip ID of the sensor. - */ -uint8_t iam20680_init_simple(struct iam20680_dev *dev) -{ - uint8_t buff; - uint8_t status = 0x00; - - // Reset driver states. - status |= iam20680_read_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - buff |= 0x80; // DEVICE_RESET - status |= iam20680_write_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - iam20680_delay_ms(100, dev); - //buff = 0x00; - while ((buff & (0x80)) != 0x00) - { - iam20680_read_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - iam20680_delay_ms(1, dev); - } - - // Let device select best clock source. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - buff &= ~0x07; // Clear CLKSEL - buff |= 0x01; // Set CLKSEL - status |= iam20680_write_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - - // Select ODR. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_SMPLRT_DIV, &buff, 1, dev); - buff = 0x09; // Set ODR = 100 Hz - status |= iam20680_write_regs((uint8_t)IAM20680_PWR_MGMT_1, &buff, 1, dev); - - // Select FS range. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_ACCEL_CONFIG, &buff, 1, dev); - buff &= 0x18; - buff |= 0 << 3; - status |= iam20680_write_regs((uint8_t)IAM20680_ACCEL_CONFIG, &buff, 1, dev); - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_GYRO_CONFIG, &buff, 1, dev); - buff &= 0x18; - buff |= 0 << 3; - status |= iam20680_write_regs((uint8_t)IAM20680_GYRO_CONFIG, &buff, 1, dev); - - // Select filter. - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_ACCEL_CONFIG2, &buff, 1, dev); - buff &= 0x07; - buff |= 5 << 0; - status |= iam20680_write_regs((uint8_t)IAM20680_ACCEL_CONFIG2, &buff, 1, dev); - buff = 0x00; - status |= iam20680_read_regs((uint8_t)IAM20680_CONFIG, &buff, 1, dev); - buff &= 0x07; - buff |= 5 << 0; - status |= iam20680_write_regs((uint8_t)IAM20680_CONFIG, &buff, 1, dev); - - - return status; -} - -/*! - * @brief This API writes the data to the given register address of the sensor. - */ -uint8_t iam20680_write_regs(uint8_t reg_addr, uint8_t *reg_data, uint8_t len, struct iam20680_dev *dev) -{ - // Write the data. - dev->status = dev->write(reg_addr, reg_data, len); - - return dev->status; -} - -/*! - * @brief This api reads the data from the given register address of the sensor. - */ -uint8_t iam20680_read_regs(uint8_t reg_addr, uint8_t *reg_data, uint8_t len, struct iam20680_dev *dev) -{ - // Check if SPI is used. - if (dev->interface == IAM20680_SPI) - { - reg_addr |= 0x80; - } - - // Read the data. - dev->status = dev->read(reg_addr, reg_data, len); - - return dev->status; -} - -/*! - * @brief This api provides and blocking ms delay function. - */ -uint8_t iam20680_delay_ms(uint32_t delay, struct iam20680_dev *dev) -{ - dev->status = 1; - dev->delay(delay); - dev->status = 0; - - return dev->status; -} - -/*! - * @brief This api gets acceleromter, temperature, and gyrometer data. - */ -uint8_t iam20680_get_data(struct iam20680_data *data, struct iam20680_dev *dev) -{ - uint8_t buff[14] = {0}; // Accel, temp, and gyro two bytes each. - - dev->status = iam20680_read_regs((uint8_t)IAM20680_ACCEL_XOUT_H, &buff[0], sizeof(buff), dev); - data->accel_x = (buff[0] << 8) | buff[1]; - data->accel_y = (buff[2] << 8) | buff[3]; - data->accel_z = (buff[4] << 8) | buff[5]; - data->temp = (buff[6] << 8) | buff[7]; - data->gyro_x = (buff[8] << 8) | buff[9]; - data->gyro_y = (buff[10] << 8) | buff[11]; - data->gyro_z = (buff[12] << 8) | buff[13]; - - return dev->status; -} diff --git a/TDK/iam20680.h b/TDK/iam20680.h deleted file mode 100644 index 7d56997..0000000 --- a/TDK/iam20680.h +++ /dev/null @@ -1,329 +0,0 @@ -/** - * @file iam20680.h - * @author Joseph Gillispie - * @date 22Aug2022 - * @brief This is the header file for the TDK IAM-20680 acceleromter/gyrometer. - */ - -#ifndef __IAM20680_H -#define __IAM20680_H - -#ifdef __cplusplus -extern "C" { -#endif - -/***************************************************************************** - * INCLUDES - ****************************************************************************/ -/** - * @brief Required includes - */ -#include -#include - -/***************************************************************************** - * MACROS AND DEFINES - ****************************************************************************/ -/** - * @brief Register addresses - */ -#define IAM20680_SELF_TEST_X_GYRO 0x00 -#define IAM20680_SELF_TEST_Y_GYRO 0x01 -#define IAM20680_SELF_TEST_Z_GYRO 0x02 -#define IAM20680_SELF_TEST_X_ACCEL 0x0D -#define IAM20680_SELF_TEST_Y_ACCEL 0x0E -#define IAM20680_SELF_TEST_Z_ACCEL 0x0F -#define IAM20680_XG_OFFS_USRH 0x13 -#define IAM20680_XG_OFFS_USRL 0x14 -#define IAM20680_YG_OFFS_USRH 0x15 -#define IAM20680_YG_OFFS_USRL 0x16 -#define IAM20680_ZG_OFFS_USRH 0x17 -#define IAM20680_ZG_OFFS_USRL 0x18 -#define IAM20680_SMPLRT_DIV 0x19 -#define IAM20680_CONFIG 0x1A -#define IAM20680_GYRO_CONFIG 0x1B -#define IAM20680_ACCEL_CONFIG 0x1C -#define IAM20680_ACCEL_CONFIG2 0x1D -#define IAM20680_LP_MODE_CFG 0x1E -#define IAM20680_ACCEL_WOM_THR 0x1F -#define IAM20680_FIFO_EN 0x23 -#define IAM20680_FSYNC_INT 0x36 -#define IAM20680_INT_PIN_CFG 0x37 -#define IAM20680_INT_ENABLE 0x38 -#define IAM20680_INT_STATUS 0x3A -#define IAM20680_ACCEL_XOUT_H 0x3B -#define IAM20680_ACCEL_XOUT_L 0x3C -#define IAM20680_ACCEL_YOUT_H 0x3D -#define IAM20680_ACCEL_YOUT_L 0x3E -#define IAM20680_ACCEL_ZOUT_H 0x3F -#define IAM20680_ACCEL_ZOUT_L 0x40 -#define IAM20680_TEMP_OUT_H 0x41 -#define IAM20680_TEMP_OUT_L 0x42 -#define IAM20680_GYRO_XOUT_H 0x43 -#define IAM20680_GYRO_XOUT_L 0x44 -#define IAM20680_GYRO_YOUT_H 0x45 -#define IAM20680_GYRO_YOUT_L 0x46 -#define IAM20680_GYRO_ZOUT_H 0x47 -#define IAM20680_GYRO_ZOUT_L 0x48 -#define IAM20680_SIGNAL_PATH_RESET 0x68 -#define IAM20680_ACCEL_INTEL_CTRL 0x69 -#define IAM20680_USER_CTRL 0x6A -#define IAM20680_PWR_MGMT_1 0x6B -#define IAM20680_PWR_MGMT_2 0x6C -#define IAM20680_FIFO_COUNTH 0x72 -#define IAM20680_FIFO_COUNTL 0x73 -#define IAM20680_FIFO_R_W 0x74 -#define IAM20680_WHO_AM_I 0x75 -#define IAM20680_XA_OFFSET_H 0x77 -#define IAM20680_XA_OFFSET_L 0x78 -#define IAM20680_YA_OFFSET_H 0x7A -#define IAM20680_YA_OFFSET_L 0x7B -#define IAM20680_ZA_OFFSET_H 0x7D -#define IAM20680_ZA_OFFSET_L 0x7E - -/**\name Status */ -#define IAM20680_OK 0x00 /*< OK */ -#define IAM20680_ERR 0x01 /*< ERROR */ - -/**\name Who Am I */ -#define IAM20680_CHIP_ID 0xA9 - -/**\name Interface */ -#define IAM20680_SPI 0x00 -#define IAM20680_I2C 0x01 - -/***************************************************************************** - * TYPEDEFS - ****************************************************************************/ -/** - * @brief Type definitions - */ - -/** - * @brief Bus communication read function pointer. This should be mapped to - * the platform-specific read function of the user application. - * - * @param[in] reg_addr : Register address from which data is read. - * @param[in] reg_data : Pointer to data buffer where read data is stored. - * @param[in] len : Number of bytes of data to be read. - * - * @retval 0 -> Success. - * @retval Non-zero -> Fail. - */ -typedef uint8_t (*iam20680_read_fptr_typedef)(uint8_t reg_addr, uint8_t *reg_data, uint16_t len); - - -/** - * @brief Bus communication write function pointer. Should be mapped to - * the platform-specific write function of the user application. - * - * @param[in] reg_addr : Register address to which data is written. - * @param[in] reg_data : Pointer to data buffer in which data to be written is stored. - * @param[in] len : Number of bytes of data to write. - * - * @retval 0 -> Success. - * @retval Non-zero -> Fail. - */ -typedef uint8_t (*iam20680_write_fptr_typedef)(uint8_t reg_addr, uint8_t *reg_data, uint16_t len); - -/** - * @brief Timer delay function pointer. This function should be mapped to a platform-specific - * hardware timer. - * - * @param[in] delay : Desired delay in ms. - * - * @retval 0 -> Success. - * @retval Non-zero -> Fail. - */ -typedef void (*iam20680_delay_fptr_typedef)(uint32_t delay); - -/** - * @brief IAM-20680 accelerometer and gyrometer data. - */ -struct iam20680_data { - - int16_t accel_x; /*< Acclerometer x data */ - int16_t accel_y; /*< Acclerometer y data */ - int16_t accel_z; /*< Acclerometer z data */ - int16_t temp; /*< Temperature data */ - int16_t gyro_x; /*< Gyrometer x data */ - int16_t gyro_y; /*< Gyrometer y data */ - int16_t gyro_z; /*< Gyrometer z data */ -}; - -/** - * @brief IAM-20680 register settings. - */ -struct iam20680_settings { - -}; - -/** - * @brief IAM-20680 device parameters. - */ -struct iam20680_dev { - iam20680_read_fptr_typedef read; /*< Read function pointer */ - iam20680_write_fptr_typedef write; /*< Write function pointer */ - iam20680_delay_fptr_typedef delay; /*< Delay function pointer */ - uint8_t interface; /*< Interface type (I2C, SPI) */ - struct iam20680_settings settings; /*< Sensor settings */ - uint8_t status; /*< Returned status of read/write functions */ - uint8_t chip_id; /*< Chip ID */ -}; - - -/***************************************************************************** - * GLOBAL FUNCTION PROTOTYPES - ****************************************************************************/ -/** - * \ingroup iam20680 - * \defgroup iam20680ApiInit Initialization - * @brief Initialize the sensor and device structure - */ - -/*! - * \ingroup iam20680ApiInit - * \page iam20680_api_iam20680_init iam20680_init - * \code - * uint8_t iam20680_init(struct iam20680_dev *dev); - * \endcode - * @details This API must be called before other APIs. It verifies the chip ID of the sensor. - * - * @param[in, out] dev : Structure Instance of iam20680_dev - * @return Result of API execution status. - * - * @retval 0 -> Success - * @retval Non-zero -> Fail - */ -uint8_t iam20680_init(struct iam20680_dev *dev); - -/** - * \ingroup iam20680 - * \defgroup iam20680ApiInit2 Initialization - * @brief Initialize the sensor and device structure - */ - -/*! - * \ingroup iam20680ApiInit2 - * \page iam20680_api_iam20680_init2 iam20680_init2 - * \code - * uint8_t iam20680_init2(struct iam20680_dev *dev); - * \endcode - * @details This API must be called before other APIs. It verifies the chip ID of the sensor. - * - * @param[in, out] dev : Structure Instance of iam20680_dev - * @return Result of API execution status. - * - * @retval 0 -> Success - * @retval Non-zero -> Fail - */ -uint8_t iam20680_init2(struct iam20680_dev *dev); - -/*! - * \ingroup iam20680ApiInit2 - * \page iam20680_api_iam20680_init_simple iam20680_init_simple - * \code - * uint8_t iam20680_init_simple(struct iam20680_dev *dev); - * \endcode - * @details This API must be called before other APIs. It verifies the chip ID of the sensor. - * - * @param[in, out] dev : Structure Instance of iam20680_dev - * @return Result of API execution status. - * - * @retval 0 -> Success - * @retval Non-zero -> Fail - */ -uint8_t iam20680_init_simple(struct iam20680_dev *dev); - -/** - * \ingroup iam20680 - * \defgroup iam20680ApiRegister Registers - * @brief Generic API for accessing sensor registers - */ - -/*! - * \ingroup iam20680ApiRegister - * \page iam20680_api_iam20680_write_regs iam20680_write_regs - * \code - * uint8_t iam20680_write_regs(uint8_t reg_addr, uint8_t *reg_data, uint8_t len, struct iam20680_dev *dev); - * \endcode - * @details This API writes the given data to the register address of the sensor - * - * @param[in] reg_addr : Register address to where the data is to be written. - * @param[in] reg_data : Pointer to data buffer which is to be written in the reg_addr of sensor. - * @param[in] len : Number of bytes of data to write. - * @param[in, out] : Structure instance of iam20680_dev. - * - * @regurn Result of API execution status. - * - * @retval 0 -> Success. - * @retvan Non-zero -> Fail. - * - */ -uint8_t iam20680_write_regs(uint8_t reg_addr, uint8_t *reg_data, uint8_t len, struct iam20680_dev *dev); - -/*! - * \ingroup iam20680ApiRegister - * \page iam20680_api_iam20680_read_regs iam20680_read_regs - * \code - * uint8_t iam20680_read_regs(uint8_t reg_addr, uint8_t *reg_data, uint8_t len, struct iam20680_dev *dev); - * \endcode - * @details This API writes the given data to the register address of the sensor - * - * @param[in] reg_addr : Register address from where the data is to be read. - * @param[in] reg_data : Pointer to data buffer to store the read data. - * @param[in] len : Number of bytes of data to be read. - * @param[in, out] : Structure instance of iam20680_dev. - * - * @regurn Result of API execution status. - * - * @retval 0 -> Success. - * @retvan Non-zero -> Fail. - * - */ -uint8_t iam20680_read_regs(uint8_t reg_addr, uint8_t *reg_data, uint8_t len, struct iam20680_dev *dev); - -/*! - * \ingroup iam20680ApiRegister - * \page iam20680_api_iam20680_delay iam20680_delay - * \code - * uint8_t iam20680_delay(uint32_t delay, iam20680_dev *dev); - * \endcode - * @details This API provides a blocking delay with one ms resolution - * - * @param[in] delay : Delay in ms. - * @param[in, out] : Structure instance of iam20680_dev. - * - * @regurn Result of API execution status. - * - * @retval 0 -> Success. - * @retvan Non-zero -> Fail. - * - */ -uint8_t iam20680_delay_ms(uint32_t delay, struct iam20680_dev *dev); - -/*! - * \ingroup iam20680ApiRegister - * \page iam20680_api_iam20680_get_data iam20680_delay - * \code - * uint8_t iam20680_get_data(struct iam20680_data *data, iam20680_dev *dev); - * \endcode - * @details This API gets accelerometer, temperature, and gyrometer data - * - * @param[in] data : Data structure to store sensor readings. - * @param[in, out] : Structure instance of iam20680_dev. - * - * @regurn Result of API execution status. - * - * @retval 0 -> Success. - * @retvan Non-zero -> Fail. - * - */ -uint8_t iam20680_get_data(struct iam20680_data *data, struct iam20680_dev *dev); - - -#ifdef __cplusplus -} -#endif - -#endif /* __IAM20680_H */ diff --git a/TDK/user_iam20680.c b/TDK/user_iam20680.c index cbffe0f..8da0a84 100644 --- a/TDK/user_iam20680.c +++ b/TDK/user_iam20680.c @@ -1,7 +1,9 @@ #include "user_iam20680.h" -struct iam20680_dev spi2_dev; +//struct iam20680_dev spi2_dev; +IAM20680_t spi2_dev; +tdk_value iam20680; extern SPI_HandleTypeDef hspi2; @@ -27,10 +29,31 @@ uint8_t iam_write(uint8_t reg_addr, uint8_t *reg_data, uint16_t len) void user_dev_init1() { - spi2_dev.delay=HAL_Delay; - spi2_dev.interface = IAM20680_SPI; - spi2_dev.read=iam_read; - spi2_dev.write=iam_write; +// spi2_dev.delay=HAL_Delay; +// spi2_dev.interface = IAM20680_SPI; +// spi2_dev.read=iam_read; +// spi2_dev.write=iam_write; +// iam20680_init(&spi2_dev); + IAM20680_cfg_t cfg; + IAM20680_init(&spi2_dev, &cfg); - iam20680_init(&spi2_dev); } + +void tdk_sample() +{ + if((IAM20680_get_interrupt_status(&spi2_dev)|1)==1) + { + IAM20680_get_accel_data(&spi2_dev, &iam20680.x_a, &iam20680.y_a, &iam20680.z_a); + IAM20680_get_gyro_data(&spi2_dev, &iam20680.x_g, &iam20680.y_g, &iam20680.z_g); + iam20680. x_gyro= iam20680.x_g/131.0; + iam20680. y_gyro= iam20680.y_g/131.0; + iam20680. z_gyro= iam20680.z_g/131.0; + iam20680.x_acc = iam20680.x_a/16384.0*9.8; + iam20680.y_acc = iam20680.y_a/16384.0*9.8; + iam20680.z_acc = iam20680.z_a/16384.0*9.8; + } + + +} + + diff --git a/TDK/user_iam20680.h b/TDK/user_iam20680.h index fa01186..1d800c5 100644 --- a/TDK/user_iam20680.h +++ b/TDK/user_iam20680.h @@ -10,11 +10,32 @@ #include #include #include "rtklib.h" -#include "iam20680.h" +//#include "iam20680.h" +#include "IMU_IAM20680.h" #include "stm32f1xx_hal.h" void user_dev_init1(); +void tdk_sample(); +uint8_t iam_write(uint8_t reg_addr, uint8_t *reg_data, uint16_t len); +uint8_t iam_read(uint8_t reg_addr, uint8_t *reg_data, uint16_t len); -extern struct iam20680_dev spi2_dev; + +typedef struct{ + int16_t x_a; //加速度值原始数据 + int16_t y_a; + int16_t z_a; + int16_t x_g; //角速度值原始数据 + int16_t y_g; + int16_t z_g; + float x_acc; //转换为真实加速度 + float y_acc; + float z_acc; + float x_gyro; //转换为真实角速度 + float y_gyro; + float z_gyro; +}tdk_value; +extern tdk_value iam20680; + +extern IAM20680_t spi2_dev; #endif /* USER_IAM20680_H_ */