12
12
#include <zephyr/sys/byteorder.h>
13
13
#include "icm42670.h"
14
14
#include "icm42670_reg.h"
15
- #include "icm42670_spi.h"
16
15
#include "icm42670_trigger.h"
17
16
18
17
#include <zephyr/logging/log.h>
@@ -52,7 +51,7 @@ static int icm42670_set_accel_fs(const struct device *dev, uint16_t fs)
52
51
53
52
data -> accel_sensitivity_shift = MIN_ACCEL_SENS_SHIFT + temp ;
54
53
55
- return icm42670_spi_update_register (& cfg -> spi , REG_ACCEL_CONFIG0 ,
54
+ return cfg -> bus_io -> update (& cfg -> bus , REG_ACCEL_CONFIG0 ,
56
55
(uint8_t )MASK_ACCEL_UI_FS_SEL , temp );
57
56
}
58
57
@@ -79,7 +78,7 @@ static int icm42670_set_gyro_fs(const struct device *dev, uint16_t fs)
79
78
80
79
data -> gyro_sensitivity_x10 = icm42670_gyro_sensitivity_x10 [temp ];
81
80
82
- return icm42670_spi_update_register (& cfg -> spi , REG_GYRO_CONFIG0 ,
81
+ return cfg -> bus_io -> update (& cfg -> bus , REG_GYRO_CONFIG0 ,
83
82
(uint8_t )MASK_GYRO_UI_FS_SEL , temp );
84
83
}
85
84
@@ -117,7 +116,7 @@ static int icm42670_set_accel_odr(const struct device *dev, uint16_t rate)
117
116
temp = BIT_ACCEL_ODR_1 ;
118
117
}
119
118
120
- return icm42670_spi_update_register (& cfg -> spi , REG_ACCEL_CONFIG0 , (uint8_t )MASK_ACCEL_ODR ,
119
+ return cfg -> bus_io -> update (& cfg -> bus , REG_ACCEL_CONFIG0 , (uint8_t )MASK_ACCEL_ODR ,
121
120
temp );
122
121
}
123
122
@@ -149,7 +148,7 @@ static int icm42670_set_gyro_odr(const struct device *dev, uint16_t rate)
149
148
temp = BIT_GYRO_ODR_12 ;
150
149
}
151
150
152
- return icm42670_spi_update_register (& cfg -> spi , REG_GYRO_CONFIG0 , (uint8_t )MASK_GYRO_ODR ,
151
+ return cfg -> bus_io -> update (& cfg -> bus , REG_GYRO_CONFIG0 , (uint8_t )MASK_GYRO_ODR ,
153
152
temp );
154
153
}
155
154
@@ -158,7 +157,7 @@ static int icm42670_enable_mclk(const struct device *dev)
158
157
const struct icm42670_config * cfg = dev -> config ;
159
158
160
159
/* switch on MCLK by setting the IDLE bit */
161
- int res = icm42670_spi_single_write (& cfg -> spi , REG_PWR_MGMT0 , BIT_IDLE );
160
+ int res = cfg -> bus_io -> write (& cfg -> bus , REG_PWR_MGMT0 , BIT_IDLE );
162
161
163
162
if (res ) {
164
163
return res ;
@@ -169,7 +168,7 @@ static int icm42670_enable_mclk(const struct device *dev)
169
168
uint8_t value = 0 ;
170
169
171
170
k_usleep (MCLK_POLL_INTERVAL_US );
172
- res = icm42670_spi_read (& cfg -> spi , REG_MCLK_RDY , & value , 1 );
171
+ res = cfg -> bus_io -> read (& cfg -> bus , REG_MCLK_RDY , & value , 1 );
173
172
174
173
if (res ) {
175
174
return res ;
@@ -193,7 +192,7 @@ static int icm42670_sensor_init(const struct device *dev)
193
192
k_msleep (3 );
194
193
195
194
/* perform a soft reset to ensure a clean slate, reset bit will auto-clear */
196
- res = icm42670_spi_single_write (& cfg -> spi , REG_SIGNAL_PATH_RESET , BIT_SOFT_RESET );
195
+ res = cfg -> bus_io -> write (& cfg -> bus , REG_SIGNAL_PATH_RESET , BIT_SOFT_RESET );
197
196
198
197
if (res ) {
199
198
LOG_ERR ("write REG_SIGNAL_PATH_RESET failed" );
@@ -204,22 +203,22 @@ static int icm42670_sensor_init(const struct device *dev)
204
203
k_msleep (SOFT_RESET_TIME_MS );
205
204
206
205
/* force SPI-4w hardware configuration (so that next read is correct) */
207
- res = icm42670_spi_single_write (& cfg -> spi , REG_DEVICE_CONFIG , BIT_SPI_AP_4WIRE );
206
+ res = cfg -> bus_io -> write (& cfg -> bus , REG_DEVICE_CONFIG , BIT_SPI_AP_4WIRE );
208
207
209
208
if (res ) {
210
209
return res ;
211
210
}
212
211
213
212
/* always use internal RC oscillator */
214
- res = icm42670_spi_single_write (& cfg -> spi , REG_INTF_CONFIG1 ,
213
+ res = cfg -> bus_io -> write (& cfg -> bus , REG_INTF_CONFIG1 ,
215
214
(uint8_t )FIELD_PREP (MASK_CLKSEL , BIT_CLKSEL_INT_RC ));
216
215
217
216
if (res ) {
218
217
return res ;
219
218
}
220
219
221
220
/* clear reset done int flag */
222
- res = icm42670_spi_read (& cfg -> spi , REG_INT_STATUS , & value , 1 );
221
+ res = cfg -> bus_io -> read (& cfg -> bus , REG_INT_STATUS , & value , 1 );
223
222
224
223
if (res ) {
225
224
return res ;
@@ -237,7 +236,7 @@ static int icm42670_sensor_init(const struct device *dev)
237
236
return res ;
238
237
}
239
238
240
- res = icm42670_spi_read (& cfg -> spi , REG_WHO_AM_I , & value , 1 );
239
+ res = cfg -> bus_io -> read (& cfg -> bus , REG_WHO_AM_I , & value , 1 );
241
240
242
241
if (res ) {
243
242
return res ;
@@ -263,7 +262,7 @@ static int icm42670_turn_on_sensor(const struct device *dev)
263
262
value = FIELD_PREP (MASK_ACCEL_MODE , BIT_ACCEL_MODE_LNM ) |
264
263
FIELD_PREP (MASK_GYRO_MODE , BIT_GYRO_MODE_LNM );
265
264
266
- res = icm42670_spi_update_register (& cfg -> spi , REG_PWR_MGMT0 ,
265
+ res = cfg -> bus_io -> update (& cfg -> bus , REG_PWR_MGMT0 ,
267
266
(uint8_t )(MASK_ACCEL_MODE | MASK_GYRO_MODE ), value );
268
267
269
268
if (res ) {
@@ -394,7 +393,7 @@ static int icm42670_sample_fetch_accel(const struct device *dev)
394
393
struct icm42670_data * data = dev -> data ;
395
394
uint8_t buffer [ACCEL_DATA_SIZE ];
396
395
397
- int res = icm42670_spi_read (& cfg -> spi , REG_ACCEL_DATA_X1 , buffer , ACCEL_DATA_SIZE );
396
+ int res = cfg -> bus_io -> read (& cfg -> bus , REG_ACCEL_DATA_X1 , buffer , ACCEL_DATA_SIZE );
398
397
399
398
if (res ) {
400
399
return res ;
@@ -413,7 +412,7 @@ static int icm42670_sample_fetch_gyro(const struct device *dev)
413
412
struct icm42670_data * data = dev -> data ;
414
413
uint8_t buffer [GYRO_DATA_SIZE ];
415
414
416
- int res = icm42670_spi_read (& cfg -> spi , REG_GYRO_DATA_X1 , buffer , GYRO_DATA_SIZE );
415
+ int res = cfg -> bus_io -> read (& cfg -> bus , REG_GYRO_DATA_X1 , buffer , GYRO_DATA_SIZE );
417
416
418
417
if (res ) {
419
418
return res ;
@@ -432,7 +431,7 @@ static int icm42670_sample_fetch_temp(const struct device *dev)
432
431
struct icm42670_data * data = dev -> data ;
433
432
uint8_t buffer [TEMP_DATA_SIZE ];
434
433
435
- int res = icm42670_spi_read (& cfg -> spi , REG_TEMP_DATA1 , buffer , TEMP_DATA_SIZE );
434
+ int res = cfg -> bus_io -> read (& cfg -> bus , REG_TEMP_DATA1 , buffer , TEMP_DATA_SIZE );
436
435
437
436
if (res ) {
438
437
return res ;
@@ -450,7 +449,7 @@ static int icm42670_sample_fetch(const struct device *dev, enum sensor_channel c
450
449
451
450
icm42670_lock (dev );
452
451
453
- int res = icm42670_spi_read (& cfg -> spi , REG_INT_STATUS_DRDY , & status , 1 );
452
+ int res = cfg -> bus_io -> read (& cfg -> bus , REG_INT_STATUS_DRDY , & status , 1 );
454
453
455
454
if (res ) {
456
455
goto cleanup ;
@@ -616,12 +615,18 @@ static int icm42670_attr_get(const struct device *dev, enum sensor_channel chan,
616
615
return res ;
617
616
}
618
617
618
+ static inline int icm42670_bus_check (const struct device * dev )
619
+ {
620
+ const struct icm42670_config * cfg = dev -> config ;
621
+
622
+ return cfg -> bus_io -> check (& cfg -> bus );
623
+ }
624
+
619
625
static int icm42670_init (const struct device * dev )
620
626
{
621
627
struct icm42670_data * data = dev -> data ;
622
- const struct icm42670_config * cfg = dev -> config ;
623
628
624
- if (! spi_is_ready_dt ( & cfg -> spi ) ) {
629
+ if (icm42670_bus_check ( dev ) < 0 ) {
625
630
LOG_ERR ("SPI bus is not ready" );
626
631
return - ENODEV ;
627
632
}
@@ -686,19 +691,31 @@ static const struct sensor_driver_api icm42670_driver_api = {
686
691
#define ICM42670_SPI_CFG \
687
692
SPI_OP_MODE_MASTER | SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_WORD_SET(8) | SPI_TRANSFER_MSB
688
693
694
+ /* Initializes the bus members for an instance on a SPI bus. */
695
+ #define ICM42670_CONFIG_SPI (inst ) \
696
+ .bus.spi = SPI_DT_SPEC_INST_GET(inst, ICM42670_SPI_CFG, 0), \
697
+ .bus_io = &icm42670_bus_io_spi,
698
+
699
+ /* Initializes the bus members for an instance on an I2C bus. */
700
+ #define ICM42670_CONFIG_I2C (inst ) \
701
+ .bus.i2c = I2C_DT_SPEC_INST_GET(inst), \
702
+ .bus_io = &icm42670_bus_io_i2c,
703
+
689
704
#define ICM42670_INIT (inst ) \
690
705
static struct icm42670_data icm42670_driver_##inst = { \
691
706
.accel_hz = DT_INST_PROP(inst, accel_hz), \
692
707
.accel_fs = DT_INST_PROP(inst, accel_fs), \
693
708
.gyro_hz = DT_INST_PROP(inst, gyro_hz), \
694
709
.gyro_fs = DT_INST_PROP(inst, gyro_fs), \
695
710
}; \
696
- \
711
+ \
697
712
static const struct icm42670_config icm42670_cfg_##inst = { \
698
- .spi = SPI_DT_SPEC_INST_GET(inst, ICM42670_SPI_CFG, 0U), \
699
- .gpio_int = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, { 0 }), \
713
+ COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
714
+ (ICM42670_CONFIG_SPI(inst)), \
715
+ (ICM42670_CONFIG_I2C(inst))) \
716
+ .gpio_int = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, {0}), \
700
717
}; \
701
- \
718
+ \
702
719
SENSOR_DEVICE_DT_INST_DEFINE(inst, icm42670_init, NULL, &icm42670_driver_##inst, \
703
720
&icm42670_cfg_##inst, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
704
721
&icm42670_driver_api);
0 commit comments