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

ESP-IDF I2C and SPI transport adapter for the ICM-42670-P driver. More...

#include "ICM42670_esp_idf.h"
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <string.h>
Include dependency graph for ICM42670_esp_idf.c:

Macros

#define ICM42670_ESP_SPI_READ_BIT   0x80U
 
#define ICM42670_ESP_MAX_TRANSFER_LEN   32U
 

Functions

esp_err_t ICM42670_ESP_I2C_Init (ICM42670_Config *config, ICM42670_ESP_I2CBus *bus, int i2c_port, gpio_num_t sda_pin, gpio_num_t scl_pin, uint8_t device_addr, uint32_t scl_speed_hz)
 Create an ESP-IDF I2C bus/device and populate ICM42670_Config.
 
esp_err_t ICM42670_ESP_I2C_AttachDevice (ICM42670_Config *config, ICM42670_ESP_I2CBus *bus, i2c_master_bus_handle_t bus_handle, i2c_master_dev_handle_t dev_handle)
 Attach an existing ESP-IDF I2C device to ICM42670_Config.
 
esp_err_t ICM42670_ESP_I2C_Deinit (ICM42670_ESP_I2CBus *bus)
 Release ESP-IDF I2C resources owned by this adapter.
 
esp_err_t ICM42670_ESP_I2C_Probe (ICM42670_ESP_I2CBus *bus, uint8_t device_addr)
 Probe an I2C address on the adapter's bus.
 
int8_t ICM42670_ESP_I2C_ReadReg (void *handle, uint8_t reg_addr, uint8_t *data, uint16_t len)
 ESP-IDF I2C register-read callback for ICM42670_Config.
 
int8_t ICM42670_ESP_I2C_WriteReg (void *handle, uint8_t reg_addr, const uint8_t *data, uint16_t len)
 ESP-IDF I2C register-write callback for ICM42670_Config.
 
esp_err_t ICM42670_ESP_SPI_Init (ICM42670_Config *config, ICM42670_ESP_SPIBus *bus, spi_host_device_t host_id, gpio_num_t miso_pin, gpio_num_t mosi_pin, gpio_num_t sclk_pin, gpio_num_t cs_pin, int clock_speed_hz)
 Create an ESP-IDF SPI bus/device and populate ICM42670_Config.
 
esp_err_t ICM42670_ESP_SPI_AttachDevice (ICM42670_Config *config, ICM42670_ESP_SPIBus *bus, spi_host_device_t host_id, spi_device_handle_t dev_handle)
 Attach an existing ESP-IDF SPI device to ICM42670_Config.
 
esp_err_t ICM42670_ESP_SPI_Deinit (ICM42670_ESP_SPIBus *bus)
 Release ESP-IDF SPI resources owned by this adapter.
 
int8_t ICM42670_ESP_SPI_ReadReg (void *handle, uint8_t reg_addr, uint8_t *data, uint16_t len)
 ESP-IDF SPI register-read callback for ICM42670_Config.
 
int8_t ICM42670_ESP_SPI_WriteReg (void *handle, uint8_t reg_addr, const uint8_t *data, uint16_t len)
 ESP-IDF SPI register-write callback for ICM42670_Config.
 

Detailed Description

ESP-IDF I2C and SPI transport adapter for the ICM-42670-P driver.

Function Documentation

◆ ICM42670_ESP_I2C_AttachDevice()

esp_err_t ICM42670_ESP_I2C_AttachDevice ( ICM42670_Config config,
ICM42670_ESP_I2CBus bus,
i2c_master_bus_handle_t  bus_handle,
i2c_master_dev_handle_t  dev_handle 
)

Attach an existing ESP-IDF I2C device to ICM42670_Config.

The adapter does not own attached handles and will not remove them during deinit.

Parameters
configDriver configuration to populate.
busI2C bus state owned by the caller.
bus_handleExisting ESP-IDF I2C master bus handle.
dev_handleExisting ESP-IDF I2C device handle.
Returns
ESP_OK on success, otherwise ESP_ERR_INVALID_ARG.

References ICM42670_ESP_I2CBus::bus_handle, ICM42670_ESP_I2CBus::dev_handle, ICM42670_ESP_I2C_ReadReg(), ICM42670_ESP_I2C_WriteReg(), and ICM42670_ESP_I2CBus::timeout_ms.

◆ ICM42670_ESP_I2C_Deinit()

esp_err_t ICM42670_ESP_I2C_Deinit ( ICM42670_ESP_I2CBus bus)

Release ESP-IDF I2C resources owned by this adapter.

Parameters
busI2C bus state.
Returns
ESP_OK on success, otherwise an ESP-IDF error code.

References ICM42670_ESP_I2CBus::bus_handle, ICM42670_ESP_I2CBus::dev_handle, ICM42670_ESP_I2CBus::owns_bus, and ICM42670_ESP_I2CBus::owns_device.

Referenced by ICM42670_ESP_I2C_Init().

◆ ICM42670_ESP_I2C_Init()

esp_err_t ICM42670_ESP_I2C_Init ( ICM42670_Config config,
ICM42670_ESP_I2CBus bus,
int  i2c_port,
gpio_num_t  sda_pin,
gpio_num_t  scl_pin,
uint8_t  device_addr,
uint32_t  scl_speed_hz 
)

Create an ESP-IDF I2C bus/device and populate ICM42670_Config.

The adapter owns the created bus and device until ICM42670_ESP_I2C_Deinit() is called.

Parameters
configDriver configuration to populate.
busI2C bus state owned by the caller.
i2c_portESP-IDF I2C port number.
sda_pinSDA GPIO.
scl_pinSCL GPIO.
device_addr7-bit I2C device address.
scl_speed_hzI2C clock speed, or 0 for the default.
Returns
ESP_OK on success, otherwise an ESP-IDF error code.

References ICM42670_ESP_I2CBus::bus_handle, ICM42670_ESP_I2CBus::dev_handle, ICM42670_ESP_DEFAULT_I2C_SPEED_HZ, ICM42670_ESP_I2C_Deinit(), ICM42670_ESP_I2C_ReadReg(), ICM42670_ESP_I2C_WriteReg(), ICM42670_ESP_I2CBus::owns_bus, ICM42670_ESP_I2CBus::owns_device, and ICM42670_ESP_I2CBus::timeout_ms.

◆ ICM42670_ESP_I2C_Probe()

esp_err_t ICM42670_ESP_I2C_Probe ( ICM42670_ESP_I2CBus bus,
uint8_t  device_addr 
)

Probe an I2C address on the adapter's bus.

Parameters
busI2C bus state with a valid bus handle.
device_addr7-bit I2C address to probe.
Returns
ESP_OK on success, otherwise an ESP-IDF error code.

References ICM42670_ESP_I2CBus::bus_handle, and ICM42670_ESP_I2CBus::timeout_ms.

◆ ICM42670_ESP_I2C_ReadReg()

int8_t ICM42670_ESP_I2C_ReadReg ( void *  handle,
uint8_t  reg_addr,
uint8_t *  data,
uint16_t  len 
)

ESP-IDF I2C register-read callback for ICM42670_Config.

Parameters
handlePointer to ICM42670_ESP_I2CBus.
reg_addrRegister address to read from.
dataDestination buffer.
lenNumber of bytes to read.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_ESP_I2CBus::bus_handle, ICM42670_ESP_I2CBus::dev_handle, ICM42670_ERROR, ICM42670_OK, and ICM42670_ESP_I2CBus::timeout_ms.

Referenced by ICM42670_ESP_I2C_AttachDevice(), and ICM42670_ESP_I2C_Init().

◆ ICM42670_ESP_I2C_WriteReg()

int8_t ICM42670_ESP_I2C_WriteReg ( void *  handle,
uint8_t  reg_addr,
const uint8_t *  data,
uint16_t  len 
)

ESP-IDF I2C register-write callback for ICM42670_Config.

Parameters
handlePointer to ICM42670_ESP_I2CBus.
reg_addrRegister address to write to.
dataSource buffer.
lenNumber of bytes to write.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_ESP_I2CBus::bus_handle, ICM42670_ESP_I2CBus::dev_handle, ICM42670_ERROR, ICM42670_OK, and ICM42670_ESP_I2CBus::timeout_ms.

Referenced by ICM42670_ESP_I2C_AttachDevice(), and ICM42670_ESP_I2C_Init().

◆ ICM42670_ESP_SPI_AttachDevice()

esp_err_t ICM42670_ESP_SPI_AttachDevice ( ICM42670_Config config,
ICM42670_ESP_SPIBus bus,
spi_host_device_t  host_id,
spi_device_handle_t  dev_handle 
)

Attach an existing ESP-IDF SPI device to ICM42670_Config.

The adapter does not own attached handles and will not remove them during deinit.

Parameters
configDriver configuration to populate.
busSPI bus state owned by the caller.
host_idESP-IDF SPI host used by dev_handle.
dev_handleExisting ESP-IDF SPI device handle.
Returns
ESP_OK on success, otherwise ESP_ERR_INVALID_ARG.

References ICM42670_ESP_SPIBus::dev_handle, ICM42670_ESP_SPIBus::host_id, ICM42670_ESP_SPI_ReadReg(), ICM42670_ESP_SPI_WriteReg(), and ICM42670_ESP_SPIBus::timeout_ms.

◆ ICM42670_ESP_SPI_Deinit()

esp_err_t ICM42670_ESP_SPI_Deinit ( ICM42670_ESP_SPIBus bus)

Release ESP-IDF SPI resources owned by this adapter.

Parameters
busSPI bus state.
Returns
ESP_OK on success, otherwise an ESP-IDF error code.

References ICM42670_ESP_SPIBus::dev_handle, ICM42670_ESP_SPIBus::host_id, ICM42670_ESP_SPIBus::owns_bus, and ICM42670_ESP_SPIBus::owns_device.

Referenced by ICM42670_ESP_SPI_Init().

◆ ICM42670_ESP_SPI_Init()

esp_err_t ICM42670_ESP_SPI_Init ( ICM42670_Config config,
ICM42670_ESP_SPIBus bus,
spi_host_device_t  host_id,
gpio_num_t  miso_pin,
gpio_num_t  mosi_pin,
gpio_num_t  sclk_pin,
gpio_num_t  cs_pin,
int  clock_speed_hz 
)

Create an ESP-IDF SPI bus/device and populate ICM42670_Config.

The adapter owns the created bus and device until ICM42670_ESP_SPI_Deinit() is called.

Parameters
configDriver configuration to populate.
busSPI bus state owned by the caller.
host_idESP-IDF SPI host.
miso_pinMISO GPIO.
mosi_pinMOSI GPIO.
sclk_pinSCLK GPIO.
cs_pinChip-select GPIO.
clock_speed_hzSPI clock speed, or 0 for the default.
Returns
ESP_OK on success, otherwise an ESP-IDF error code.

References ICM42670_ESP_SPIBus::dev_handle, ICM42670_ESP_SPIBus::host_id, ICM42670_ESP_DEFAULT_SPI_SPEED_HZ, ICM42670_ESP_SPI_Deinit(), ICM42670_ESP_SPI_ReadReg(), ICM42670_ESP_SPI_WriteReg(), ICM42670_ESP_SPIBus::owns_bus, ICM42670_ESP_SPIBus::owns_device, and ICM42670_ESP_SPIBus::timeout_ms.

◆ ICM42670_ESP_SPI_ReadReg()

int8_t ICM42670_ESP_SPI_ReadReg ( void *  handle,
uint8_t  reg_addr,
uint8_t *  data,
uint16_t  len 
)

ESP-IDF SPI register-read callback for ICM42670_Config.

Parameters
handlePointer to ICM42670_ESP_SPIBus.
reg_addrRegister address to read from.
dataDestination buffer.
lenNumber of bytes to read.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_ESP_SPIBus::dev_handle, ICM42670_ERROR, and ICM42670_OK.

Referenced by ICM42670_ESP_SPI_AttachDevice(), and ICM42670_ESP_SPI_Init().

◆ ICM42670_ESP_SPI_WriteReg()

int8_t ICM42670_ESP_SPI_WriteReg ( void *  handle,
uint8_t  reg_addr,
const uint8_t *  data,
uint16_t  len 
)

ESP-IDF SPI register-write callback for ICM42670_Config.

Parameters
handlePointer to ICM42670_ESP_SPIBus.
reg_addrRegister address to write to.
dataSource buffer.
lenNumber of bytes to write.
Returns
ICM42670_OK on success, otherwise ICM42670_ERROR.

References ICM42670_ESP_SPIBus::dev_handle, ICM42670_ERROR, and ICM42670_OK.

Referenced by ICM42670_ESP_SPI_AttachDevice(), and ICM42670_ESP_SPI_Init().