Skip to content

Commit 6540c50

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 df4e1cf commit 6540c50

File tree

9 files changed

+1061
-8
lines changed

9 files changed

+1061
-8
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
@@ -303,6 +303,8 @@ void iis3dwb_submit_sync(struct rtio_iodev_sqe *iodev_sqe)
303303

304304
if (!cfg->is_streaming) {
305305
iis3dwb_submit_one_shot(dev, iodev_sqe);
306+
} else if (IS_ENABLED(CONFIG_IIS3DWB_STREAM)) {
307+
iis3dwb_submit_stream(dev, iodev_sqe);
306308
} else {
307309
rtio_iodev_sqe_err(iodev_sqe, -ENOTSUP);
308310
}
@@ -371,6 +373,15 @@ static int iis3dwb_init(const struct device *dev)
371373
return -EIO;
372374
}
373375

376+
#ifdef CONFIG_IIS3DWB_TRIGGER
377+
if (cfg->trig_enabled) {
378+
if (iis3dwb_init_interrupt(dev) < 0) {
379+
LOG_ERR("Failed to initialize interrupt.");
380+
return -EIO;
381+
}
382+
}
383+
#endif
384+
374385
/* set sensor default scale (used to convert sample values) */
375386
LOG_DBG("%s: range is %d", dev->name, cfg->range);
376387
ret = iis3dwb_set_range_raw(dev, cfg->range);
@@ -406,6 +417,17 @@ static int iis3dwb_init(const struct device *dev)
406417
DT_DRV_INST(inst), IIS3DWB_SPI_OPERATION, 0U); \
407418
RTIO_DEFINE(iis3dwb_rtio_ctx_##inst, 8, 8);
408419

420+
#ifdef CONFIG_IIS3DWB_TRIGGER
421+
#define IIS3DWB_CFG_IRQ(inst) \
422+
.trig_enabled = true, \
423+
.int1_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int1_gpios, {0}), \
424+
.int2_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int2_gpios, {0}), \
425+
.drdy_pulsed = DT_INST_PROP(inst, drdy_pulsed), \
426+
.drdy_pin = DT_INST_PROP(inst, drdy_pin),
427+
#else
428+
#define IIS3DWB_CFG_IRQ(inst)
429+
#endif /* CONFIG_IIS3DWB_TRIGGER */
430+
409431
#define IIS3DWB_CONFIG(inst) \
410432
{ \
411433
STMEMSC_CTX_SPI(&iis3dwb_config_##inst.stmemsc_cfg), \
@@ -417,6 +439,16 @@ static int iis3dwb_init(const struct device *dev)
417439
.range = DT_INST_PROP(inst, range), \
418440
.filter = DT_INST_PROP(inst, filter), \
419441
.odr = DT_INST_PROP(inst, odr), \
442+
\
443+
IF_ENABLED(CONFIG_IIS3DWB_STREAM, \
444+
(.fifo_wtm = DT_INST_PROP(inst, fifo_watermark), \
445+
.accel_batch = DT_INST_PROP(inst, accel_fifo_batch_rate), \
446+
.temp_batch = DT_INST_PROP(inst, temp_fifo_batch_rate), \
447+
.ts_batch = DT_INST_PROP(inst, timestamp_fifo_batch_rate),)) \
448+
\
449+
IF_ENABLED(UTIL_OR(DT_INST_NODE_HAS_PROP(inst, int1_gpios), \
450+
DT_INST_NODE_HAS_PROP(inst, int2_gpios)), \
451+
(IIS3DWB_CFG_IRQ(inst))) \
420452
}
421453

422454
#define IIS3DWB_DEFINE(inst) \

drivers/sensor/st/iis3dwb/iis3dwb.h

Lines changed: 57 additions & 4 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,9 +136,14 @@ 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);
93140

141+
#ifdef CONFIG_IIS3DWB_TRIGGER
142+
int iis3dwb_route_int1(const struct device *dev, iis3dwb_pin_int1_route_t pin_int);
143+
int iis3dwb_route_int2(const struct device *dev, iis3dwb_pin_int2_route_t pin_int);
144+
void iis3dwb_stream_irq_handler(const struct device *dev);
145+
#endif
146+
94147
void iis3dwb_rtio_rd_transaction(const struct device *dev,
95148
uint8_t *regs, uint8_t regs_num,
96149
struct spi_buf *buf,

0 commit comments

Comments
 (0)