ICM42670 Portable Driver
Public API reference for the ICM-42670-P IMU driver
Loading...
Searching...
No Matches
ICM42670_driver.c File Reference

Core ICM-42670-P initialization, configuration, and read functions. More...

#include "ICM42670_internal.h"
#include <stdint.h>
Include dependency graph for ICM42670_driver.c:

Functions

ICM42670_Status_t ICM42670_SetPowerState (ICM42670_Config *config, ICM42670_PowerState_t state)
 Switch the ICM-42670-P to one of the standard datasheet power modes.
 
ICM42670_Status_t ICM42670_SetAccelRange (ICM42670_Config *config, ICM42670_AccelFS_t accel_fs)
 Set the accelerometer full-scale range.
 
ICM42670_Status_t ICM42670_SetGyroRange (ICM42670_Config *config, ICM42670_GyroFS_t gyro_fs)
 Set the gyroscope full-scale range.
 
ICM42670_Status_t ICM42670_SetAccelOdr (ICM42670_Config *config, ICM42670_Odr_t odr)
 Set the accelerometer output data rate.
 
ICM42670_Status_t ICM42670_SetGyroOdr (ICM42670_Config *config, ICM42670_Odr_t odr)
 Set the gyroscope output data rate.
 
ICM42670_Status_t ICM42670_SetAccelLpf (ICM42670_Config *config, ICM42670_Lpf_t lpf)
 Set the accelerometer UI low-pass filter bandwidth.
 
ICM42670_Status_t ICM42670_SetGyroLpf (ICM42670_Config *config, ICM42670_Lpf_t lpf)
 Set the gyroscope UI low-pass filter bandwidth.
 
ICM42670_Status_t ICM42670_Init (ICM42670_Config *config)
 Initialize the device and enter 6-axis low-noise mode.
 
ICM42670_Status_t ICM42670_ReadAccelRaw (const ICM42670_Config *config, int16_t accel_raw[3])
 Read raw accelerometer counts.
 
ICM42670_Status_t ICM42670_ReadGyroRaw (const ICM42670_Config *config, int16_t gyro_raw[3])
 Read raw gyroscope counts.
 
ICM42670_Status_t ICM42670_ReadTempRaw (const ICM42670_Config *config, int16_t *temp_raw)
 Read the raw temperature register value.
 
ICM42670_Status_t ICM42670_ReadAccelG (const ICM42670_Config *config, ICM42670_Accel_t *accel)
 Read accelerometer data converted to g.
 
ICM42670_Status_t ICM42670_ReadGyroDps (const ICM42670_Config *config, ICM42670_Gyro_t *gyro)
 Read gyroscope data converted to degrees per second.
 
ICM42670_Status_t ICM42670_ReadTempC (const ICM42670_Config *config, float *temp_c)
 Read temperature converted to degrees Celsius.
 
ICM42670_Status_t ICM42670_Gyro_Calibration (ICM42670_Config *config)
 Estimate gyroscope bias while the board is stationary.
 

Detailed Description

Core ICM-42670-P initialization, configuration, and read functions.

Author
Anthony / SleepPandas

Function Documentation

◆ ICM42670_Gyro_Calibration()

ICM42670_Status_t ICM42670_Gyro_Calibration ( ICM42670_Config config)

Estimate gyroscope bias while the board is stationary.

This averages raw gyro samples and stores the offsets in config.

Parameters
configDriver configuration with read and delay callbacks.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_Config::delay_ms, ICM42670_Config::gyro_offsets, ICM42670_ERROR, ICM42670_OK, ICM42670_ReadGyroRaw(), ICM42670_Gyro_Offsets_t::x_raw_offset, ICM42670_Gyro_Offsets_t::y_raw_offset, and ICM42670_Gyro_Offsets_t::z_raw_offset.

◆ ICM42670_Init()

ICM42670_Status_t ICM42670_Init ( ICM42670_Config config)

Initialize the device and enter 6-axis low-noise mode.

The function validates callbacks, checks WHO_AM_I, normalizes invalid range/ODR values to defaults, writes accel/gyro configuration, and enables both sensors.

Parameters
configDriver configuration with valid callbacks.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_Config::accel_fs, ICM42670_Config::accel_odr, ICM42670_Config::delay_ms, ICM42670_Config::gyro_fs, ICM42670_Config::gyro_odr, ICM42670_Config::handle, ICM42670_ERROR, ICM42670_OK, ICM42670_POWER_6AXIS_LN, ICM42670_SetPowerState(), ICM42670_Config::read_reg, and ICM42670_Config::write_reg.

◆ ICM42670_ReadAccelG()

ICM42670_Status_t ICM42670_ReadAccelG ( const ICM42670_Config config,
ICM42670_Accel_t accel 
)

Read accelerometer data converted to g.

Parameters
configDriver configuration with current accel_fs value.
accelDestination for scaled acceleration.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_Config::accel_fs, ICM42670_ERROR, ICM42670_OK, ICM42670_ReadAccelRaw(), ICM42670_Accel_t::x_g, ICM42670_Accel_t::y_g, and ICM42670_Accel_t::z_g.

◆ ICM42670_ReadAccelRaw()

ICM42670_Status_t ICM42670_ReadAccelRaw ( const ICM42670_Config config,
int16_t  accel_raw[3] 
)

Read raw accelerometer counts.

Parameters
configDriver configuration with a read callback.
accel_rawDestination array: X, Y, Z raw counts.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_Config::handle, ICM42670_ERROR, ICM42670_OK, and ICM42670_Config::read_reg.

Referenced by ICM42670_ReadAccelG().

◆ ICM42670_ReadGyroDps()

ICM42670_Status_t ICM42670_ReadGyroDps ( const ICM42670_Config config,
ICM42670_Gyro_t gyro 
)

Read gyroscope data converted to degrees per second.

Configured gyro_offsets are subtracted before scaling.

Parameters
configDriver configuration with current gyro_fs value.
gyroDestination for scaled angular rate.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_Config::gyro_fs, ICM42670_Config::gyro_offsets, ICM42670_ERROR, ICM42670_OK, ICM42670_ReadGyroRaw(), ICM42670_Gyro_t::x_dps, ICM42670_Gyro_Offsets_t::x_raw_offset, ICM42670_Gyro_t::y_dps, ICM42670_Gyro_Offsets_t::y_raw_offset, ICM42670_Gyro_t::z_dps, and ICM42670_Gyro_Offsets_t::z_raw_offset.

◆ ICM42670_ReadGyroRaw()

ICM42670_Status_t ICM42670_ReadGyroRaw ( const ICM42670_Config config,
int16_t  gyro_raw[3] 
)

Read raw gyroscope counts.

Parameters
configDriver configuration with a read callback.
gyro_rawDestination array: X, Y, Z raw counts.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_Config::handle, ICM42670_ERROR, ICM42670_OK, and ICM42670_Config::read_reg.

Referenced by ICM42670_Gyro_Calibration(), and ICM42670_ReadGyroDps().

◆ ICM42670_ReadTempC()

ICM42670_Status_t ICM42670_ReadTempC ( const ICM42670_Config config,
float *  temp_c 
)

Read temperature converted to degrees Celsius.

Parameters
configDriver configuration with a read callback.
temp_cDestination for temperature in degrees Celsius.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_ERROR, ICM42670_OK, and ICM42670_ReadTempRaw().

◆ ICM42670_ReadTempRaw()

ICM42670_Status_t ICM42670_ReadTempRaw ( const ICM42670_Config config,
int16_t *  temp_raw 
)

Read the raw temperature register value.

Parameters
configDriver configuration with a read callback.
temp_rawDestination for the signed raw temperature count.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_Config::handle, ICM42670_ERROR, ICM42670_OK, ICM42670_REG_TEMP_DATA1, and ICM42670_Config::read_reg.

Referenced by ICM42670_ReadTempC().

◆ ICM42670_SetAccelLpf()

ICM42670_Status_t ICM42670_SetAccelLpf ( ICM42670_Config config,
ICM42670_Lpf_t  lpf 
)

Set the accelerometer UI low-pass filter bandwidth.

Parameters
configDriver configuration with read and write callbacks.
lpfLow-pass filter selection.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

◆ ICM42670_SetAccelOdr()

ICM42670_Status_t ICM42670_SetAccelOdr ( ICM42670_Config config,
ICM42670_Odr_t  odr 
)

Set the accelerometer output data rate.

Parameters
configDriver configuration with read and write callbacks.
odrAccelerometer output data rate.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_Config::accel_odr, ICM42670_ERROR, and ICM42670_OK.

◆ ICM42670_SetAccelRange()

ICM42670_Status_t ICM42670_SetAccelRange ( ICM42670_Config config,
ICM42670_AccelFS_t  accel_fs 
)

Set the accelerometer full-scale range.

Parameters
configDriver configuration with read and write callbacks.
accel_fsAccelerometer range selection.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_Config::accel_fs, ICM42670_ERROR, and ICM42670_OK.

◆ ICM42670_SetGyroLpf()

ICM42670_Status_t ICM42670_SetGyroLpf ( ICM42670_Config config,
ICM42670_Lpf_t  lpf 
)

Set the gyroscope UI low-pass filter bandwidth.

Parameters
configDriver configuration with read and write callbacks.
lpfLow-pass filter selection.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

◆ ICM42670_SetGyroOdr()

ICM42670_Status_t ICM42670_SetGyroOdr ( ICM42670_Config config,
ICM42670_Odr_t  odr 
)

Set the gyroscope output data rate.

Parameters
configDriver configuration with read and write callbacks.
odrGyroscope output data rate.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_Config::gyro_odr, ICM42670_ERROR, and ICM42670_OK.

◆ ICM42670_SetGyroRange()

ICM42670_Status_t ICM42670_SetGyroRange ( ICM42670_Config config,
ICM42670_GyroFS_t  gyro_fs 
)

Set the gyroscope full-scale range.

Parameters
configDriver configuration with read and write callbacks.
gyro_fsGyroscope range selection.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_Config::gyro_fs, ICM42670_ERROR, and ICM42670_OK.

◆ ICM42670_SetPowerState()

ICM42670_Status_t ICM42670_SetPowerState ( ICM42670_Config config,
ICM42670_PowerState_t  state 
)

Switch the ICM-42670-P to one of the standard datasheet power modes.

This writes PWR_MGMT0 and then waits 1 ms so the datasheet's 200 us no-register-write window is respected when either sensor transitions from off to an active mode. Gyro off/on timing and accel LP ODR/filter restrictions remain caller responsibilities.

Parameters
configDriver configuration with write and delay callbacks.
stateTarget power state.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_Config::delay_ms, ICM42670_Config::handle, ICM42670_ERROR, ICM42670_OK, and ICM42670_Config::write_reg.

Referenced by ICM42670_Init().