40 static void ICM20648_chipSelectSet(
bool select );
159 temp = ( (int16_t) rawData[0] << 8 ) | rawData[1];
160 accel[0] = (float) temp * accelRes;
161 temp = ( (int16_t) rawData[2] << 8 ) | rawData[3];
162 accel[1] = (float) temp * accelRes;
163 temp = ( (int16_t) rawData[4] << 8 ) | rawData[5];
164 accel[2] = (float) temp * accelRes;
196 temp = ( (int16_t) rawData[0] << 8 ) | rawData[1];
197 gyro[0] = (float) temp * gyroRes;
198 temp = ( (int16_t) rawData[2] << 8 ) | rawData[3];
199 gyro[1] = (float) temp * gyroRes;
200 temp = ( (int16_t) rawData[4] << 8 ) | rawData[5];
201 gyro[2] = (float) temp * gyroRes;
230 *accelRes = 2.0 / 32768.0;
234 *accelRes = 4.0 / 32768.0;
238 *accelRes = 8.0 / 32768.0;
242 *accelRes = 16.0 / 32768.0;
273 *gyroRes = 250.0 / 32768.0;
277 *gyroRes = 500.0 / 32768.0;
281 *gyroRes = 1000.0 / 32768.0;
285 *gyroRes = 2000.0 / 32768.0;
384 float gyroSampleRate;
387 gyroSampleRate = ( 1125.0 / sampleRate ) - 1.0;
390 if( gyroSampleRate > 255.0 ) {
391 gyroSampleRate = 255.0;
394 if( gyroSampleRate < 0 ) {
395 gyroSampleRate = 0.0;
399 gyroDiv = (uint8_t) gyroSampleRate;
403 gyroSampleRate = 1125.0 / ( gyroDiv + 1 );
405 return gyroSampleRate;
424 float accelSampleRate;
427 accelSampleRate = ( 1125.0 / sampleRate ) - 1.0;
430 if( accelSampleRate > 4095.0 ) {
431 accelSampleRate = 4095.0;
434 if( accelSampleRate < 0 ) {
435 accelSampleRate = 0.0;
439 accelDiv = (uint16_t) accelSampleRate;
444 accelSampleRate = 1125.0 / ( accelDiv + 1 );
446 return accelSampleRate;
587 uint8_t pwrManagement1;
588 uint8_t pwrManagement2;
648 if( enAccel || enGyro || enTemp ) {
715 if( dataReadyEnable ) {
743 *intStatus = (uint32_t) reg[0];
744 *intStatus |= ( ( (uint32_t) reg[1] ) << 8 );
745 *intStatus |= ( ( (uint32_t) reg[2] ) << 16 );
746 *intStatus |= ( ( (uint32_t) reg[3] ) << 24 );
867 uint16_t i, packetCount, fifoCount;
868 int32_t gyroBias[3] = { 0, 0, 0 };
869 int32_t accelBias[3] = { 0, 0, 0 };
870 int32_t accelTemp[3];
872 int32_t accelBiasFactory[3];
873 int32_t gyroBiasStored[3];
874 float gyroRes, accelRes;
916 while( fifoCount < 4080 ) {
921 fifoCount = ( (uint16_t) ( data[0] << 8 ) | data[1] );
931 fifoCount = ( (uint16_t) ( data[0] << 8 ) | data[1] );
934 packetCount = fifoCount / 12;
937 for( i = 0; i < packetCount; i++ ) {
941 accelTemp[0] = ( (int16_t) ( data[0] << 8 ) | data[1] );
942 accelTemp[1] = ( (int16_t) ( data[2] << 8 ) | data[3] );
943 accelTemp[2] = ( (int16_t) ( data[4] << 8 ) | data[5] );
944 gyroTemp[0] = ( (int16_t) ( data[6] << 8 ) | data[7] );
945 gyroTemp[1] = ( (int16_t) ( data[8] << 8 ) | data[9] );
946 gyroTemp[2] = ( (int16_t) ( data[10] << 8 ) | data[11] );
949 accelBias[0] += accelTemp[0];
950 accelBias[1] += accelTemp[1];
951 accelBias[2] += accelTemp[2];
952 gyroBias[0] += gyroTemp[0];
953 gyroBias[1] += gyroTemp[1];
954 gyroBias[2] += gyroTemp[2];
959 accelBias[0] /= packetCount;
960 accelBias[1] /= packetCount;
961 accelBias[2] /= packetCount;
962 gyroBias[0] /= packetCount;
963 gyroBias[1] /= packetCount;
964 gyroBias[2] /= packetCount;
967 if( accelBias[2] > 0L ) {
968 accelBias[2] -= (int32_t) ( 1.0 / accelRes );
971 accelBias[2] += (int32_t) ( 1.0 / accelRes );
975 gyroBiasScaled[0] = (float) gyroBias[0] * gyroRes;
976 gyroBiasScaled[1] = (float) gyroBias[1] * gyroRes;
977 gyroBiasScaled[2] = (float) gyroBias[2] * gyroRes;
981 gyroBiasStored[0] = ( (int16_t) ( data[0] << 8 ) | data[1] );
983 gyroBiasStored[1] = ( (int16_t) ( data[0] << 8 ) | data[1] );
985 gyroBiasStored[2] = ( (int16_t) ( data[0] << 8 ) | data[1] );
990 gyroBiasStored[0] -= gyroBias[0] / 4;
991 gyroBiasStored[1] -= gyroBias[1] / 4;
992 gyroBiasStored[2] -= gyroBias[2] / 4;
995 data[0] = ( gyroBiasStored[0] >> 8 ) & 0xFF;
996 data[1] = ( gyroBiasStored[0] ) & 0xFF;
997 data[2] = ( gyroBiasStored[1] >> 8 ) & 0xFF;
998 data[3] = ( gyroBiasStored[1] ) & 0xFF;
999 data[4] = ( gyroBiasStored[2] >> 8 ) & 0xFF;
1000 data[5] = ( gyroBiasStored[2] ) & 0xFF;
1018 accelBiasFactory[0] = ( (int16_t) ( data[0] << 8 ) | data[1] );
1020 accelBiasFactory[1] = ( (int16_t) ( data[0] << 8 ) | data[1] );
1022 accelBiasFactory[2] = ( (int16_t) ( data[0] << 8 ) | data[1] );
1029 accelBiasFactory[0] -= ( ( accelBias[0] / 8 ) & ~1 );
1030 accelBiasFactory[1] -= ( ( accelBias[1] / 8 ) & ~1 );
1031 accelBiasFactory[2] -= ( ( accelBias[2] / 8 ) & ~1 );
1034 data[0] = ( accelBiasFactory[0] >> 8 ) & 0xFF;
1035 data[1] = ( accelBiasFactory[0] ) & 0xFF;
1036 data[2] = ( accelBiasFactory[1] >> 8 ) & 0xFF;
1037 data[3] = ( accelBiasFactory[1] ) & 0xFF;
1038 data[4] = ( accelBiasFactory[2] >> 8 ) & 0xFF;
1039 data[5] = ( accelBiasFactory[2] ) & 0xFF;
1050 accelBiasScaled[0] = (float) accelBias[0] * accelRes;
1051 accelBiasScaled[1] = (float) accelBias[1] * accelRes;
1052 accelBiasScaled[2] = (float) accelBias[2] * accelRes;
1081 uint16_t i, packetCount, fifoCount;
1082 int32_t gyroBias[3] = { 0, 0, 0 };
1083 int32_t gyroTemp[3];
1084 int32_t gyroBiasStored[3];
1124 while( fifoCount < 4080 ) {
1132 fifoCount = ( (uint16_t) ( data[0] << 8 ) | data[1] );
1143 fifoCount = ( (uint16_t) ( data[0] << 8 ) | data[1] );
1146 packetCount = fifoCount / 12;
1149 for( i = 0; i < packetCount; i++ ) {
1153 gyroTemp[0] = ( (int16_t) ( data[6] << 8 ) | data[7] );
1154 gyroTemp[1] = ( (int16_t) ( data[8] << 8 ) | data[9] );
1155 gyroTemp[2] = ( (int16_t) ( data[10] << 8 ) | data[11] );
1158 gyroBias[0] += gyroTemp[0];
1159 gyroBias[1] += gyroTemp[1];
1160 gyroBias[2] += gyroTemp[2];
1165 gyroBias[0] /= packetCount;
1166 gyroBias[1] /= packetCount;
1167 gyroBias[2] /= packetCount;
1170 gyroBiasScaled[0] = (float) gyroBias[0] * gyroRes;
1171 gyroBiasScaled[1] = (float) gyroBias[1] * gyroRes;
1172 gyroBiasScaled[2] = (float) gyroBias[2] * gyroRes;
1176 gyroBiasStored[0] = ( (int16_t) ( data[0] << 8 ) | data[1] );
1179 gyroBiasStored[1] = ( (int16_t) ( data[0] << 8 ) | data[1] );
1182 gyroBiasStored[2] = ( (int16_t) ( data[0] << 8 ) | data[1] );
1187 gyroBiasStored[0] -= gyroBias[0] / 4;
1188 gyroBiasStored[1] -= gyroBias[1] / 4;
1189 gyroBiasStored[2] -= gyroBias[2] / 4;
1192 data[0] = ( gyroBiasStored[0] >> 8 ) & 0xFF;
1193 data[1] = ( gyroBiasStored[0] ) & 0xFF;
1194 data[2] = ( gyroBiasStored[1] >> 8 ) & 0xFF;
1195 data[3] = ( gyroBiasStored[1] ) & 0xFF;
1196 data[4] = ( gyroBiasStored[2] >> 8 ) & 0xFF;
1197 data[5] = ( gyroBiasStored[2] ) & 0xFF;
1237 raw_temp = (int16_t) ( ( data[0] << 8 ) + data[1] );
1240 *temperature = ( (float) raw_temp / 333.87 ) + 21.0;
1329 regAddr = (uint8_t) ( addr & 0x7F );
1330 bank = (uint8_t) ( addr >> 7 );
1335 ICM20648_chipSelectSet(
true );
1338 USART_Tx( ICM20648_SPI_USART, ( regAddr | 0x80 ) );
1341 while( numBytes-- ) {
1342 USART_Tx( ICM20648_SPI_USART, 0x00 );
1343 *data++ =
USART_Rx( ICM20648_SPI_USART );
1347 ICM20648_chipSelectSet(
false );
1374 regAddr = (uint8_t) ( addr & 0x7F );
1375 bank = (uint8_t) ( addr >> 7 );
1380 ICM20648_chipSelectSet(
true );
1383 USART_Tx( ICM20648_SPI_USART, ( regAddr & 0x7F ) );
1387 USART_Tx( ICM20648_SPI_USART, data );
1391 ICM20648_chipSelectSet(
false );
1411 ICM20648_chipSelectSet(
true );
1418 USART_Tx( ICM20648_SPI_USART, ( bank << 4 ) );
1422 ICM20648_chipSelectSet(
false );
1440 static void ICM20648_chipSelectSet(
bool select )
Clock management unit (CMU) API.
uint32_t ICM20648_wakeOnMotionITEnable(bool enable, uint8_t womThreshold, float sampleRate)
Sets up and enables the Wake-up On Motion feature.
#define ICM20648_REG_USER_CTRL
#define USART_ROUTEPEN_RXPEN
void USART_Tx(USART_TypeDef *usart, uint8_t data)
Transmit one 4-9 bit frame.
#define ICM20648_MASK_GYRO_FULLSCALE
#define _USART_ROUTELOC0_TXLOC_SHIFT
#define ICM20648_BIT_LP_EN
void USART_InitSync(USART_TypeDef *usart, const USART_InitSync_TypeDef *init)
Init USART for synchronous mode.
#define ICM20648_REG_PWR_MGMT_2
#define ICM20648_ACCEL_FULLSCALE_8G
uint32_t ICM20648_sampleRateSet(float sampleRate)
Sets the sample rate both of the accelerometer and the gyroscope.
uint32_t ICM20648_deInit(void)
De-initializes the ICM20648 sensor by disconnecting the supply and SPI lines.
#define ICM20648_GYRO_FULLSCALE_250DPS
uint32_t ICM20648_interruptEnable(bool dataReadyEnable, bool womEnable)
Enables or disables the interrupts in the ICM20648 chip.
#define ICM20648_BIT_I2C_IF_DIS
#define ICM20648_REG_ACCEL_XOUT_H_SH
#define ICM20648_REG_TEMPERATURE_H
#define ICM20648_MASK_ACCEL_BW
#define ICM20648_BIT_ACCEL_INTEL_MODE
uint32_t ICM20648_temperatureRead(float *temperature)
Reads the temperature sensor raw value and converts to Celsius.
#define ICM20648_REG_FIFO_EN_2
uint8_t USART_Rx(USART_TypeDef *usart)
Receive one 4-8 bit frame, (or part of 10-16 bit frame).
#define ICM20648_REG_FIFO_R_W
void UTIL_delay(uint32_t ms)
Delays number of msTick Systicks (1 ms)
#define ICM20648_REG_ACCEL_WOM_THR
#define ICM20648_REG_INT_STATUS
#define ICM20648_BIT_INT_OPEN
#define ICM20648_REG_ACCEL_SMPLRT_DIV_1
Driver for the Invensense ICM20648 6-axis motion sensor.
#define ICM20648_BIT_ACCEL_INTEL_EN
void ICM20648_registerWrite(uint16_t addr, uint8_t data)
Writes a register in the ICM20648 device.
uint32_t ICM20648_gyroResolutionGet(float *gyroRes)
Gets the actual resolution of the gyroscope.
#define ICM20648_BIT_H_RESET
uint32_t ICM20648_gyroDataRead(float *gyro)
Reads the raw gyroscope value and converts to deg/sec value based on the actual resolution.
Universal synchronous/asynchronous receiver/transmitter (USART/UART) peripheral API.
#define ICM20648_DEVICE_ID
#define ICM20648_BIT_SLEEP
#define ICM20648_REG_GYRO_CONFIG_1
#define ICM20648_BIT_PWR_ACCEL_STBY
#define ICM20648_REG_GYRO_XOUT_H_SH
uint32_t ICM20648_gyroBandwidthSet(uint8_t gyroBw)
Sets the bandwidth of the gyroscope.
#define ICM20648_BIT_ACCEL_FIFO_EN
uint32_t ICM20648_lowPowerModeEnter(bool enAccel, bool enGyro, bool enTemp)
Enables or disables the sensors in low power mode in the ICM20648 chip.
void ICM20648_registerRead(uint16_t addr, int numBytes, uint8_t *data)
Reads register from the ICM20648 device.
#define ICM20648_REG_YA_OFFSET_L
#define _USART_ROUTELOC0_TXLOC_MASK
#define ICM20648_BIT_ACCEL_CYCLE
#define ICM20648_BIT_TEMP_DIS
float ICM20648_gyroSampleRateSet(float sampleRate)
Sets the sample rate of the accelerometer.
#define ICM20648_REG_ACCEL_CONFIG
void ICM20648_bankSelect(uint8_t bank)
Select the desired register bank.
#define ICM20648_BIT_WOM_INT_EN
#define USART_ROUTEPEN_TXPEN
#define ICM20648_GYRO_FULLSCALE_500DPS
#define ICM20648_REG_ZG_OFFS_USRL
#define _USART_ROUTELOC0_CLKLOC_SHIFT
#define ICM20648_REG_BANK_SEL
#define ICM20648_REG_ZA_OFFSET_H
#define ICM20648_REG_YG_OFFS_USRL
void GPIO_PinModeSet(GPIO_Port_TypeDef port, unsigned int pin, GPIO_Mode_TypeDef mode, unsigned int out)
Set the mode for a GPIO pin.
#define ICM20648_REG_INT_ENABLE_1
#define ICM20648_REG_XG_OFFS_USRH
General Purpose IO (GPIO) peripheral API.
#define ICM20648_REG_INT_ENABLE
uint32_t ICM20648_spiInit(void)
Initializes the SPI bus in order to communicate with the ICM20648.
Utility Functions for the Thunderboard Sense.
__STATIC_INLINE void GPIO_PinOutSet(GPIO_Port_TypeDef port, unsigned int pin)
Set a single pin in GPIO data out register to 1.
#define ICM20648_REG_ACCEL_SMPLRT_DIV_2
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
#define _USART_ROUTELOC0_RXLOC_MASK
#define ICM20648_BITS_GYRO_FIFO_EN
#define ICM20648_REG_FIFO_RST
uint32_t ICM20648_accelGyroCalibrate(float *accelBiasScaled, float *gyroBiasScaled)
Accelerometer and gyroscope calibration function. Reads the gyroscope and accelerometer values while ...
uint32_t ICM20648_init(void)
Initializes the ICM20648 sensor. Enables the power supply and SPI lines, sets up the host SPI control...
#define ICM20648_GYRO_FULLSCALE_2000DPS
#define ICM20648_ACCEL_FULLSCALE_4G
#define ICM20648_BIT_RAW_DATA_0_RDY_EN
uint32_t ICM20648_accelDataRead(float *accel)
Reads the raw acceleration value and converts to g value based on the actual resolution.
#define USART_ROUTEPEN_CLKPEN
#define ICM20648_REG_XA_OFFSET_H
#define ICM20648_BIT_INT_ACTL
#define ICM20648_REG_XA_OFFSET_L
#define ICM20648_ACCEL_BW_246HZ
void USART_Reset(USART_TypeDef *usart)
Reset USART/UART to same state as after a HW reset.
#define ICM20648_REG_FIFO_MODE
#define ICM20648_REG_ACCEL_INTEL_CTRL
uint32_t ICM20648_interruptStatusRead(uint32_t *intStatus)
Reads the interrupt status registers of the ICM20648 chip.
#define ICM20648_ERROR_INVALID_DEVICE_ID
#define ICM20648_REG_INT_STATUS_1
#define ICM20648_REG_GYRO_SMPLRT_DIV
#define ICM20648_REG_FIFO_COUNT_H
#define ICM20648_GYRO_BW_12HZ
uint32_t BOARD_imuEnable(bool enable)
Enables or disables the accelerometer and gyroscope sensor.
#define _USART_ROUTELOC0_CLKLOC_MASK
#define ICM20648_ACCEL_FULLSCALE_2G
#define ICM20648_REG_YG_OFFS_USRH
#define ICM20648_REG_PWR_MGMT_1
uint32_t ICM20648_gyroFullscaleSet(uint8_t gyroFs)
Sets the full scale value of the gyroscope.
float ICM20648_accelSampleRateSet(float sampleRate)
Sets the sample rate of the gyroscope.
bool ICM20648_isDataReady(void)
Checks if new data is available for read.
#define ICM20648_BIT_PWR_GYRO_STBY
#define ICM20648_ACCEL_FULLSCALE_16G
uint32_t ICM20648_sensorEnable(bool accel, bool gyro, bool temp)
Enables or disables the sensors in the ICM20648 chip.
#define ICM20648_REG_ZA_OFFSET_L
#define ICM20648_GYRO_FULLSCALE_1000DPS
uint32_t ICM20648_reset(void)
Performs soft reset on the ICM20648 chip.
uint32_t ICM20648_getDeviceID(uint8_t *devID)
Reads the device ID of the ICM20648.
uint32_t ICM20648_accelFullscaleSet(uint8_t accelFs)
Sets the full scale value of the accelerometer.
#define ICM20648_REG_WHO_AM_I
uint32_t ICM20648_cycleModeEnable(bool enable)
Enables or disables the cycle mode operation of the accel and gyro.
#define ICM20648_MASK_GYRO_BW
#define ICM20648_MASK_ACCEL_FULLSCALE
__STATIC_INLINE void GPIO_PinOutClear(GPIO_Port_TypeDef port, unsigned int pin)
Set a single pin in GPIO data out port register to 0.
#define ICM20648_ACCEL_BW_1210HZ
#define ICM20648_REG_ZG_OFFS_USRH
#define ICM20648_BIT_RAW_DATA_0_RDY_INT
#define _USART_ROUTELOC0_RXLOC_SHIFT
#define ICM20648_REG_INT_PIN_CFG
uint32_t ICM20648_sleepModeEnable(bool enable)
Enables or disables the sleep mode of the device.
#define ICM20648_REG_LP_CONFIG
#define ICM20648_BIT_CLK_PLL
#define ICM20648_BIT_FIFO_EN
BOARD module header file.
uint32_t ICM20648_gyroCalibrate(float *gyroBiasScaled)
Gyroscope calibration function. Reads the gyroscope values while the device is at rest and in level...
#define ICM20648_REG_XG_OFFS_USRL
#define ICM20648_BIT_GYRO_CYCLE
uint32_t ICM20648_accelResolutionGet(float *accelRes)
Gets the actual resolution of the accelerometer.
#define ICM20648_REG_YA_OFFSET_H
uint32_t ICM20648_accelBandwidthSet(uint8_t accelBw)
Sets the bandwidth of the accelerometer.