Skip to content

Commit 76587b9

Browse files
committed
drivers/sensor: iis3dwb: add streaming capabality
Add read_and_decode streaming APIs support. Triggers supported: - SENSOR_TRIG_FIFO_WATERMARK - SENSOR_TRIG_FIFO_FULL - SENSOR_TRIG_DATA_READY Signed-off-by: Armando Visconti <armando.visconti@st.com>
1 parent a92b3df commit 76587b9

File tree

9 files changed

+1049
-9
lines changed

9 files changed

+1049
-9
lines changed

drivers/sensor/st/iis3dwb/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
zephyr_library()
88

99
zephyr_library_sources(iis3dwb.c)
10+
zephyr_library_sources_ifdef(CONFIG_IIS3DWB_TRIGGER iis3dwb_trigger.c)
1011
zephyr_library_sources_ifdef(CONFIG_SENSOR_ASYNC_API iis3dwb_decoder.c)
12+
zephyr_library_sources_ifdef(CONFIG_IIS3DWB_STREAM iis3dwb_stream.c)
1113

1214
zephyr_library_include_directories(../stmemsc)

drivers/sensor/st/iis3dwb/Kconfig

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,27 @@ menuconfig IIS3DWB
1111
select SPI
1212
select HAS_STMEMSC
1313
select USE_STDC_IIS3DWB
14+
select RTIO_WORKQ if SENSOR_ASYNC_API
1415
help
1516
Enable driver for IIS3DWB SPI-based accelerometer sensor.
1617

1718
if IIS3DWB
1819

20+
config IIS3DWB_STREAM
21+
bool "IIS3DWB data streaming"
22+
select IIS3DWB_TRIGGER
23+
default y
24+
depends on SPI_RTIO
25+
depends on SENSOR_ASYNC_API
26+
help
27+
Use this config option to enable streaming sensor data via RTIO subsystem.
28+
29+
config IIS3DWB_TRIGGER
30+
bool
31+
1932
config IIS3DWB_ENABLE_TEMP
2033
bool "Temperature"
2134
help
2235
Enable/disable temperature sensor
2336

24-
2537
endif # IIS3DWB

drivers/sensor/st/iis3dwb/iis3dwb.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ void iis3dwb_submit_sync(struct rtio_iodev_sqe *iodev_sqe)
283283

284284
if (!cfg->is_streaming) {
285285
iis3dwb_submit_one_shot(dev, iodev_sqe);
286+
} else if (IS_ENABLED(CONFIG_IIS3DWB_STREAM)) {
287+
iis3dwb_submit_stream(dev, iodev_sqe);
286288
} else {
287289
rtio_iodev_sqe_err(iodev_sqe, -ENOTSUP);
288290
}
@@ -351,6 +353,15 @@ static int iis3dwb_init(const struct device *dev)
351353
return -EIO;
352354
}
353355

356+
#ifdef CONFIG_IIS3DWB_TRIGGER
357+
if (cfg->trig_enabled) {
358+
if (iis3dwb_init_interrupt(dev) < 0) {
359+
LOG_ERR("Failed to initialize interrupt.");
360+
return -EIO;
361+
}
362+
}
363+
#endif
364+
354365
/* set sensor default scale (used to convert sample values) */
355366
LOG_DBG("%s: range is %d", dev->name, cfg->range);
356367
ret = iis3dwb_set_range_raw(dev, cfg->range);
@@ -386,6 +397,17 @@ static int iis3dwb_init(const struct device *dev)
386397
DT_DRV_INST(inst), IIS3DWB_SPI_OPERATION, 0U); \
387398
RTIO_DEFINE(iis3dwb_rtio_ctx_##inst, 4, 4);
388399

400+
#ifdef CONFIG_IIS3DWB_TRIGGER
401+
#define IIS3DWB_CFG_IRQ(inst) \
402+
.trig_enabled = true, \
403+
.int1_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int1_gpios, {0}), \
404+
.int2_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int2_gpios, {0}), \
405+
.drdy_pulsed = DT_INST_PROP(inst, drdy_pulsed), \
406+
.drdy_pin = DT_INST_PROP(inst, drdy_pin),
407+
#else
408+
#define IIS3DWB_CFG_IRQ(inst)
409+
#endif /* CONFIG_IIS3DWB_TRIGGER */
410+
389411
#define IIS3DWB_CONFIG(inst) \
390412
{ \
391413
STMEMSC_CTX_SPI(&iis3dwb_config_##inst.stmemsc_cfg), \
@@ -397,6 +419,16 @@ static int iis3dwb_init(const struct device *dev)
397419
.range = DT_INST_PROP(inst, range), \
398420
.filter = DT_INST_PROP(inst, filter), \
399421
.odr = DT_INST_PROP(inst, odr), \
422+
\
423+
IF_ENABLED(CONFIG_IIS3DWB_STREAM, \
424+
(.fifo_wtm = DT_INST_PROP(inst, fifo_watermark), \
425+
.accel_batch = DT_INST_PROP(inst, accel_fifo_batch_rate), \
426+
.temp_batch = DT_INST_PROP(inst, temp_fifo_batch_rate), \
427+
.ts_batch = DT_INST_PROP(inst, timestamp_fifo_batch_rate),)) \
428+
\
429+
IF_ENABLED(UTIL_OR(DT_INST_NODE_HAS_PROP(inst, int1_gpios), \
430+
DT_INST_NODE_HAS_PROP(inst, int2_gpios)), \
431+
(IIS3DWB_CFG_IRQ(inst))) \
400432
}
401433

402434
#define IIS3DWB_DEFINE(inst) \

drivers/sensor/st/iis3dwb/iis3dwb.h

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@
1919
#include <zephyr/types.h>
2020
#include <zephyr/sys/util.h>
2121
#include <stmemsc.h>
22+
2223
#include "iis3dwb_reg.h"
2324
#include <zephyr/dt-bindings/sensor/iis3dwb.h>
2425
#include <zephyr/drivers/sensor_clock.h>
2526

2627
#define GAIN_UNIT (61LL)
2728

29+
#ifdef CONFIG_IIS3DWB_STREAM
30+
struct trigger_config {
31+
uint8_t int_fifo_th : 1;
32+
uint8_t int_fifo_full : 1;
33+
uint8_t int_drdy : 1;
34+
};
35+
#endif
36+
2837
struct iis3dwb_config {
2938
stmdev_ctx_t ctx;
3039
union {
@@ -37,6 +46,19 @@ struct iis3dwb_config {
3746
uint8_t filter;
3847
uint8_t odr;
3948

49+
#ifdef CONFIG_IIS3DWB_STREAM
50+
uint16_t fifo_wtm;
51+
uint8_t accel_batch : 4;
52+
uint8_t temp_batch : 2;
53+
uint8_t ts_batch : 2;
54+
#endif
55+
#ifdef CONFIG_IIS3DWB_TRIGGER
56+
const struct gpio_dt_spec int1_gpio;
57+
const struct gpio_dt_spec int2_gpio;
58+
uint8_t drdy_pulsed;
59+
uint8_t drdy_pin;
60+
bool trig_enabled;
61+
#endif
4062
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
4163
struct spi_dt_spec spi;
4264
#endif
@@ -51,10 +73,35 @@ struct iis3dwb_data {
5173
struct rtio *rtio_ctx;
5274
struct rtio_iodev *iodev;
5375
struct rtio_iodev_sqe *streaming_sqe;
76+
77+
#ifdef CONFIG_IIS3DWB_STREAM
78+
uint64_t timestamp;
79+
uint8_t status;
80+
uint8_t fifo_status[2];
81+
uint16_t fifo_count;
82+
struct trigger_config trig_cfg;
83+
uint8_t accel_batch_odr : 4;
84+
uint8_t temp_batch_odr : 2;
85+
uint8_t ts_batch_odr : 2;
86+
#endif
87+
88+
#ifdef CONFIG_IIS3DWB_TRIGGER
89+
struct gpio_dt_spec *drdy_gpio;
90+
struct gpio_callback gpio_cb;
91+
92+
const struct device *dev;
93+
#endif /* CONFIG_IIS3DWB_TRIGGER */
5494
};
5595

5696
int iis3dwb_spi_init(const struct device *dev);
5797

98+
#define IIS3DWB_FIFO_ITEM_LEN 7
99+
#define IIS3DWB_FIFO_SIZE(x) (x * IIS3DWB_FIFO_ITEM_LEN)
100+
101+
#ifdef CONFIG_IIS3DWB_TRIGGER
102+
int iis3dwb_init_interrupt(const struct device *dev);
103+
#endif
104+
58105
/* decoder */
59106
struct iis3dwb_decoder_header {
60107
uint64_t timestamp;
@@ -68,11 +115,12 @@ struct iis3dwb_fifo_data {
68115
struct iis3dwb_decoder_header header;
69116
uint32_t accel_odr: 4;
70117
uint32_t fifo_mode_sel: 2;
71-
uint32_t fifo_count: 7;
118+
uint32_t fifo_count: 10;
72119
uint32_t reserved_1: 5;
73-
uint32_t accel_batch_odr: 3;
120+
uint32_t accel_batch_odr: 4;
121+
uint32_t temp_batch_odr: 2;
74122
uint32_t ts_batch_odr: 2;
75-
uint32_t reserved: 9;
123+
uint32_t reserved: 3;
76124
} __attribute__((__packed__));
77125

78126
struct iis3dwb_rtio_data {
@@ -88,7 +136,12 @@ struct iis3dwb_rtio_data {
88136

89137
int iis3dwb_encode(const struct device *dev, const struct sensor_chan_spec *const channels,
90138
const size_t num_channels, uint8_t *buf);
91-
92139
int iis3dwb_get_decoder(const struct device *dev, const struct sensor_decoder_api **decoder);
93-
140+
int iis3dwb_route_int1(const struct device *dev, iis3dwb_pin_int1_route_t pin_int);
141+
int iis3dwb_route_int2(const struct device *dev, iis3dwb_pin_int2_route_t pin_int);
142+
void iis3dwb_stream_irq_handler(const struct device *dev);
143+
void iis3dwb_rtio_rw_transaction(const struct device *dev, uint8_t reg,
144+
uint8_t *buf, uint32_t len,
145+
struct rtio_iodev_sqe *iodev_sqe,
146+
rtio_callback_t complete_op_cb);
94147
#endif /* ZEPHYR_DRIVERS_SENSOR_IIS3DWB_IIS3DWB_H_ */

0 commit comments

Comments
 (0)