11/*
2- * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+ * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7+ #include <string.h>
78#include <stdio.h>
89#include <math.h>
910#include <time.h>
1011#include <sys/time.h>
1112#include "esp_system.h"
1213#include "esp_check.h"
13- #include "driver/i2c.h"
1414#include "icm42670.h"
1515
16+ #define I2C_CLK_SPEED 400000
17+
1618#define ALPHA 0.99f /*!< Weight of gyroscope */
1719#define RAD_TO_DEG 57.27272727f /*!< Radians to degrees */
1820
4648*******************************************************************************/
4749
4850typedef struct {
49- i2c_port_t bus ;
50- uint8_t dev_addr ;
51+ i2c_master_dev_handle_t i2c_handle ;
5152 uint32_t counter ;
5253 float dt ; /*!< delay time between two measurements, dt should be small (ms level) */
5354 struct timeval * timer ;
@@ -70,31 +71,46 @@ static const char *TAG = "ICM42670";
7071* Public API functions
7172*******************************************************************************/
7273
73- icm42670_handle_t icm42670_create (i2c_port_t port , const uint8_t dev_addr )
74+ esp_err_t icm42670_create (i2c_master_bus_handle_t i2c_bus , const uint8_t dev_addr , icm42670_handle_t * handle_ret )
7475{
75- icm42670_dev_t * sensor = (icm42670_dev_t * ) heap_caps_calloc (1 , sizeof (icm42670_dev_t ), MALLOC_CAP_DEFAULT );
76- sensor -> bus = port ;
77- sensor -> dev_addr = dev_addr ;
78- sensor -> counter = 0 ;
79- sensor -> dt = 0 ;
80- sensor -> timer = (struct timeval * ) calloc (1 , sizeof (struct timeval ));
81-
76+ esp_err_t ret = ESP_OK ;
77+
78+ // Allocate memory and init the driver object
79+ icm42670_dev_t * sensor = (icm42670_dev_t * ) calloc (1 , sizeof (icm42670_dev_t ));
80+ struct timeval * timer = (struct timeval * ) calloc (1 , sizeof (struct timeval ));
81+ ESP_RETURN_ON_FALSE (sensor != NULL && timer != NULL , ESP_ERR_NO_MEM , TAG , "Not enough memory" );
82+ sensor -> timer = timer ;
83+
84+ // Add new I2C device
85+ const i2c_device_config_t i2c_dev_cfg = {
86+ .device_address = dev_addr ,
87+ .scl_speed_hz = I2C_CLK_SPEED ,
88+ };
89+ ESP_GOTO_ON_ERROR (i2c_master_bus_add_device (i2c_bus , & i2c_dev_cfg , & sensor -> i2c_handle ), err , TAG , "Failed to add new I2C device" );
90+ assert (sensor -> i2c_handle );
91+
92+ // Check device presence
8293 uint8_t dev_id = 0 ;
8394 icm42670_get_deviceid (sensor , & dev_id );
84- if (dev_id != ICM42607_ID && dev_id != ICM42670_ID ) {
85- ESP_LOGE (TAG , "Incorrect Device ID (0x%02x)." , dev_id );
86- return NULL ;
87- }
95+ ESP_GOTO_ON_FALSE (dev_id == ICM42607_ID || dev_id == ICM42670_ID , ESP_ERR_NOT_FOUND , err , TAG , "Incorrect Device ID (0x%02x)." , dev_id );
8896
89- ESP_LOGI (TAG , "Found device %s, ID: 0x%02x" , (dev_id == ICM42607_ID ? "ICM42607" : "ICM42670" ), dev_id );
97+ ESP_LOGD (TAG , "Found device %s, ID: 0x%02x" , (dev_id == ICM42607_ID ? "ICM42607" : "ICM42670" ), dev_id );
98+ * handle_ret = sensor ;
99+ return ret ;
90100
91- return (icm42670_handle_t ) sensor ;
101+ err :
102+ icm42670_delete (sensor );
103+ return ret ;
92104}
93105
94106void icm42670_delete (icm42670_handle_t sensor )
95107{
96108 icm42670_dev_t * sens = (icm42670_dev_t * ) sensor ;
97109
110+ if (sens -> i2c_handle ) {
111+ i2c_master_bus_rm_device (sens -> i2c_handle );
112+ }
113+
98114 if (sens -> timer ) {
99115 free (sens -> timer );
100116 }
@@ -343,36 +359,22 @@ static esp_err_t icm42670_get_raw_value(icm42670_handle_t sensor, uint8_t reg, i
343359static esp_err_t icm42670_write (icm42670_handle_t sensor , const uint8_t reg_start_addr , const uint8_t * data_buf , const uint8_t data_len )
344360{
345361 icm42670_dev_t * sens = (icm42670_dev_t * ) sensor ;
346- esp_err_t ret ;
347-
348362 assert (sens );
349363
350- i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
351- ret = i2c_master_start (cmd );
352- assert (ESP_OK == ret );
353- ret = i2c_master_write_byte (cmd , (sens -> dev_addr << 1 ) | I2C_MASTER_WRITE , true);
354- assert (ESP_OK == ret );
355- ret = i2c_master_write_byte (cmd , reg_start_addr , true);
356- assert (ESP_OK == ret );
357- ret = i2c_master_write (cmd , data_buf , data_len , true);
358- assert (ESP_OK == ret );
359- ret = i2c_master_stop (cmd );
360- assert (ESP_OK == ret );
361- ret = i2c_master_cmd_begin (sens -> bus , cmd , 1000 / portTICK_PERIOD_MS );
362- i2c_cmd_link_delete (cmd );
363-
364- return ret ;
364+ assert (data_len < 5 );
365+ uint8_t write_buff [5 ] = {reg_start_addr };
366+ memcpy (& write_buff [1 ], data_buf , data_len );
367+ return i2c_master_transmit (sens -> i2c_handle , write_buff , data_len + 1 , -1 );
365368}
366369
367370static esp_err_t icm42670_read (icm42670_handle_t sensor , const uint8_t reg_start_addr , uint8_t * data_buf , const uint8_t data_len )
368371{
369- icm42670_dev_t * sens = (icm42670_dev_t * ) sensor ;
370372 uint8_t reg_buff [] = {reg_start_addr };
371-
373+ icm42670_dev_t * sens = ( icm42670_dev_t * ) sensor ;
372374 assert (sens );
373375
374376 /* Write register number and read data */
375- return i2c_master_write_read_device (sens -> bus , sens -> dev_addr , reg_buff , sizeof (reg_buff ), data_buf , data_len , 1000 / portTICK_PERIOD_MS );
377+ return i2c_master_transmit_receive (sens -> i2c_handle , reg_buff , sizeof (reg_buff ), data_buf , data_len , -1 );
376378}
377379
378380esp_err_t icm42670_complimentory_filter (icm42670_handle_t sensor , const icm42670_value_t * const acce_value ,
0 commit comments