Skip to content

Commit baec782

Browse files
committed
drivers: icm42688: pin9 function
PIN9 of an ICM42688 can be configured as an interrupt output, external clock input or frame sync output. Pin function can now be set via a sensor attribute. Signed-off-by: Yau-ming Leung <[email protected]>
1 parent 379eb7a commit baec782

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

drivers/sensor/tdk/icm42688/icm42688.c

+24-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define DT_DRV_COMPAT invensense_icm42688
1010

1111
#include <zephyr/drivers/sensor.h>
12+
#include <zephyr/drivers/sensor/icm42688.h>
1213
#include <zephyr/drivers/spi.h>
1314
#include <zephyr/sys/byteorder.h>
1415

@@ -162,6 +163,23 @@ static int icm42688_attr_set(const struct device *dev, enum sensor_channel chan,
162163
return -EINVAL;
163164
}
164165
new_config.batch_ticks = val->val1;
166+
} else if ((enum sensor_attribute_icm42688)attr ==
167+
SENSOR_ATTR_ICM42688_PIN9_FUNCTION) {
168+
if (val->val1 != ICM42688_PIN9_FUNCTION_INT2 &&
169+
val->val1 != ICM42688_PIN9_FUNCTION_FSYNC &&
170+
val->val1 != ICM42688_PIN9_FUNCTION_CLKIN) {
171+
LOG_ERR("Unknown pin function");
172+
return -EINVAL;
173+
}
174+
175+
/* TODO: Allow this if FSYNC is configurable later. */
176+
if (val->val1 == ICM42688_PIN9_FUNCTION_FSYNC) {
177+
LOG_ERR("FSYNC is disabled, PIN9_FUNCTION should not be set to "
178+
"FSYNC");
179+
return -ENOTSUP;
180+
}
181+
182+
new_config.pin9_function = val->val1;
165183
} else {
166184
LOG_ERR("Unsupported attribute");
167185
res = -EINVAL;
@@ -219,6 +237,10 @@ static int icm42688_attr_get(const struct device *dev, enum sensor_channel chan,
219237
if (attr == SENSOR_ATTR_BATCH_DURATION) {
220238
val->val1 = cfg->batch_ticks;
221239
val->val2 = 0;
240+
} else if ((enum sensor_attribute_icm42688)attr ==
241+
SENSOR_ATTR_ICM42688_PIN9_FUNCTION) {
242+
val->val1 = cfg->pin9_function;
243+
val->val2 = 0;
222244
} else {
223245
LOG_ERR("Unsupported attribute");
224246
res = -EINVAL;
@@ -313,7 +335,8 @@ void icm42688_unlock(const struct device *dev)
313335
.fifo_hires = false, \
314336
.interrupt1_drdy = false, \
315337
.interrupt1_fifo_ths = false, \
316-
.interrupt1_fifo_full = false \
338+
.interrupt1_fifo_full = false, \
339+
.pin9_function = ICM42688_PIN9_FUNCTION_INT2 \
317340
}
318341

319342
#define ICM42688_DEFINE_DATA(inst) \

drivers/sensor/tdk/icm42688/icm42688.h

+2
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ struct icm42688_cfg {
308308
bool interrupt1_drdy;
309309
bool interrupt1_fifo_ths;
310310
bool interrupt1_fifo_full;
311+
312+
uint8_t pin9_function;
311313
};
312314

313315
struct icm42688_trigger_entry {

drivers/sensor/tdk/icm42688/icm42688_common.c

+42
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include <zephyr/drivers/sensor.h>
10+
#include <zephyr/drivers/sensor/icm42688.h>
1011
#include <zephyr/drivers/spi.h>
1112
#include <zephyr/sys/byteorder.h>
1213
#include "icm42688.h"
@@ -148,6 +149,47 @@ int icm42688_configure(const struct device *dev, struct icm42688_cfg *cfg)
148149

149150
/* TODO maybe do the next few steps intelligently by checking current config */
150151

152+
/* Select register bank 1 */
153+
res = icm42688_spi_single_write(&dev_cfg->spi, REG_BANK_SEL, BIT_BANK1);
154+
if (res != 0) {
155+
LOG_ERR("Error selecting register bank 1");
156+
return -EINVAL;
157+
}
158+
159+
k_busy_wait(50);
160+
161+
/* Set pin 9 function */
162+
uint8_t intf_config5 = FIELD_PREP(MASK_PIN9_FUNCTION, cfg->pin9_function);
163+
164+
LOG_DBG("INTF_CONFIG5 (0x%lx) 0x%x", FIELD_GET(REG_ADDRESS_MASK, REG_INTF_CONFIG5),
165+
intf_config5);
166+
res = icm42688_spi_single_write(&dev_cfg->spi, REG_INTF_CONFIG5, intf_config5);
167+
if (res != 0) {
168+
LOG_ERR("Error writing INTF_CONFIG5");
169+
return -EINVAL;
170+
}
171+
172+
/* Select register bank 0 */
173+
res = icm42688_spi_single_write(&dev_cfg->spi, REG_BANK_SEL, BIT_BANK0);
174+
if (res != 0) {
175+
LOG_ERR("Error selecting register bank 0");
176+
return -EINVAL;
177+
}
178+
179+
k_busy_wait(50);
180+
181+
bool is_pin9_clkin = cfg->pin9_function == ICM42688_PIN9_FUNCTION_CLKIN;
182+
uint8_t intf_config1 = 0x91 | FIELD_PREP(BIT_RTC_MODE, is_pin9_clkin);
183+
184+
LOG_DBG("INTF_CONFIG1 (0x%x) 0x%x", REG_INTF_CONFIG1, intf_config1);
185+
res = icm42688_spi_single_write(&dev_cfg->spi, REG_INTF_CONFIG1, intf_config1);
186+
if (res != 0) {
187+
LOG_ERR("Error writing INTF_CONFIG1");
188+
return -EINVAL;
189+
}
190+
191+
k_busy_wait(250);
192+
151193
/* Power management to set gyro/accel modes */
152194
uint8_t pwr_mgmt0 = FIELD_PREP(MASK_GYRO_MODE, cfg->gyro_pwr_mode) |
153195
FIELD_PREP(MASK_ACCEL_MODE, cfg->accel_pwr_mode) |

drivers/sensor/tdk/icm42688/icm42688_reg.h

+3
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@
276276
#define BIT_INT_TDEASSERT_DISABLE BIT(5)
277277
#define BIT_INT_ASYNC_RESET BIT(4)
278278

279+
/* Bank1 REG_INTF_CONFIG5 */
280+
#define MASK_PIN9_FUNCTION GENMASK(2, 1)
281+
279282
/* misc. defines */
280283
#define WHO_AM_I_ICM42688 0x47
281284
#define MIN_ACCEL_SENS_SHIFT 11
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright The Zephyr Project Contributors
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_ICM42688_H_
8+
#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_ICM42688_H_
9+
10+
#include <zephyr/drivers/sensor.h>
11+
12+
/**
13+
* @file
14+
* @brief Extended public API for ICM42688
15+
*
16+
* Pin function configuration via attributes under the current sensor driver abstraction.
17+
*/
18+
19+
#define ICM42688_PIN9_FUNCTION_INT2 0
20+
#define ICM42688_PIN9_FUNCTION_FSYNC 1
21+
#define ICM42688_PIN9_FUNCTION_CLKIN 2
22+
23+
/**
24+
* @brief Extended sensor attributes for ICM42688
25+
*
26+
* Attributes for setting pin function.
27+
*/
28+
enum sensor_attribute_icm42688 {
29+
SENSOR_ATTR_ICM42688_PIN9_FUNCTION = SENSOR_ATTR_PRIV_START
30+
};
31+
32+
#endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_ICM42688_H_ */

0 commit comments

Comments
 (0)