Skip to content

Commit f2743b9

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 6c83df7 commit f2743b9

File tree

9 files changed

+1062
-24
lines changed

9 files changed

+1062
-24
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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,21 @@ menuconfig IIS3DWB
1616

1717
if IIS3DWB
1818

19+
config IIS3DWB_STREAM
20+
bool "IIS3DWB data streaming"
21+
select IIS3DWB_TRIGGER
22+
default y
23+
depends on SPI_RTIO
24+
depends on SENSOR_ASYNC_API
25+
help
26+
Use this config option to enable streaming sensor data via RTIO subsystem.
27+
28+
config IIS3DWB_TRIGGER
29+
bool
30+
1931
config IIS3DWB_ENABLE_TEMP
2032
bool "Temperature"
2133
help
2234
Enable/disable temperature sensor
2335

24-
2536
endif # IIS3DWB

drivers/sensor/st/iis3dwb/iis3dwb.c

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -296,32 +296,19 @@ static void iis3dwb_submit_one_shot(const struct device *dev, struct rtio_iodev_
296296
}
297297
}
298298

299-
void iis3dwb_submit_sync(struct rtio_iodev_sqe *iodev_sqe)
299+
void iis3dwb_submit(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe)
300300
{
301301
const struct sensor_read_config *cfg = iodev_sqe->sqe.iodev->data;
302-
const struct device *dev = cfg->sensor;
303302

304303
if (!cfg->is_streaming) {
305304
iis3dwb_submit_one_shot(dev, iodev_sqe);
305+
} else if (IS_ENABLED(CONFIG_IIS3DWB_STREAM)) {
306+
iis3dwb_submit_stream(dev, iodev_sqe);
306307
} else {
307308
rtio_iodev_sqe_err(iodev_sqe, -ENOTSUP);
308309
}
309310
}
310311

311-
void iis3dwb_submit(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe)
312-
{
313-
struct rtio_work_req *req = rtio_work_req_alloc();
314-
315-
if (req == NULL) {
316-
LOG_ERR("RTIO work item allocation failed. Consider to increase "
317-
"CONFIG_RTIO_WORKQ_POOL_ITEMS.");
318-
rtio_iodev_sqe_err(iodev_sqe, -ENOMEM);
319-
return;
320-
}
321-
322-
rtio_work_req_submit(req, iodev_sqe, iis3dwb_submit_sync);
323-
}
324-
325312
static DEVICE_API(sensor, iis3dwb_driver_api) = {
326313
.attr_set = iis3dwb_attr_set,
327314
#ifdef CONFIG_SENSOR_ASYNC_API
@@ -371,6 +358,15 @@ static int iis3dwb_init(const struct device *dev)
371358
return -EIO;
372359
}
373360

361+
#ifdef CONFIG_IIS3DWB_TRIGGER
362+
if (cfg->trig_enabled) {
363+
if (iis3dwb_init_interrupt(dev) < 0) {
364+
LOG_ERR("Failed to initialize interrupt.");
365+
return -EIO;
366+
}
367+
}
368+
#endif
369+
374370
/* set sensor default scale (used to convert sample values) */
375371
LOG_DBG("%s: range is %d", dev->name, cfg->range);
376372
ret = iis3dwb_set_range_raw(dev, cfg->range);
@@ -406,6 +402,17 @@ static int iis3dwb_init(const struct device *dev)
406402
DT_DRV_INST(inst), IIS3DWB_SPI_OPERATION, 0U); \
407403
RTIO_DEFINE(iis3dwb_rtio_ctx_##inst, 8, 8);
408404

405+
#ifdef CONFIG_IIS3DWB_TRIGGER
406+
#define IIS3DWB_CFG_IRQ(inst) \
407+
.trig_enabled = true, \
408+
.int1_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int1_gpios, {0}), \
409+
.int2_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int2_gpios, {0}), \
410+
.drdy_pulsed = DT_INST_PROP(inst, drdy_pulsed), \
411+
.drdy_pin = DT_INST_PROP(inst, drdy_pin),
412+
#else
413+
#define IIS3DWB_CFG_IRQ(inst)
414+
#endif /* CONFIG_IIS3DWB_TRIGGER */
415+
409416
#define IIS3DWB_CONFIG(inst) \
410417
{ \
411418
STMEMSC_CTX_SPI(&iis3dwb_config_##inst.stmemsc_cfg), \
@@ -417,6 +424,16 @@ static int iis3dwb_init(const struct device *dev)
417424
.range = DT_INST_PROP(inst, range), \
418425
.filter = DT_INST_PROP(inst, filter), \
419426
.odr = DT_INST_PROP(inst, odr), \
427+
\
428+
IF_ENABLED(CONFIG_IIS3DWB_STREAM, \
429+
(.fifo_wtm = DT_INST_PROP(inst, fifo_watermark), \
430+
.accel_batch = DT_INST_PROP(inst, accel_fifo_batch_rate), \
431+
.temp_batch = DT_INST_PROP(inst, temp_fifo_batch_rate), \
432+
.ts_batch = DT_INST_PROP(inst, timestamp_fifo_batch_rate),)) \
433+
\
434+
IF_ENABLED(UTIL_OR(DT_INST_NODE_HAS_PROP(inst, int1_gpios), \
435+
DT_INST_NODE_HAS_PROP(inst, int2_gpios)), \
436+
(IIS3DWB_CFG_IRQ(inst))) \
420437
}
421438

422439
#define IIS3DWB_DEFINE(inst) \

drivers/sensor/st/iis3dwb/iis3dwb.h

Lines changed: 58 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 {
@@ -36,6 +45,20 @@ struct iis3dwb_config {
3645
uint8_t range;
3746
uint8_t filter;
3847
uint8_t odr;
48+
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
3962
};
4063

4164
struct iis3dwb_data {
@@ -47,10 +70,35 @@ struct iis3dwb_data {
4770
struct rtio *rtio_ctx;
4871
struct rtio_iodev *iodev;
4972
struct rtio_iodev_sqe *streaming_sqe;
73+
74+
#ifdef CONFIG_IIS3DWB_STREAM
75+
uint64_t timestamp;
76+
uint8_t status;
77+
uint8_t fifo_status[2];
78+
uint16_t fifo_count;
79+
struct trigger_config trig_cfg;
80+
uint8_t accel_batch_odr : 4;
81+
uint8_t temp_batch_odr : 2;
82+
uint8_t ts_batch_odr : 2;
83+
#endif
84+
85+
#ifdef CONFIG_IIS3DWB_TRIGGER
86+
struct gpio_dt_spec *drdy_gpio;
87+
struct gpio_callback gpio_cb;
88+
89+
const struct device *dev;
90+
#endif /* CONFIG_IIS3DWB_TRIGGER */
5091
};
5192

5293
int iis3dwb_spi_init(const struct device *dev);
5394

95+
#define IIS3DWB_FIFO_ITEM_LEN 7
96+
#define IIS3DWB_FIFO_SIZE(x) (x * IIS3DWB_FIFO_ITEM_LEN)
97+
98+
#ifdef CONFIG_IIS3DWB_TRIGGER
99+
int iis3dwb_init_interrupt(const struct device *dev);
100+
#endif
101+
54102
/* decoder */
55103
struct iis3dwb_decoder_header {
56104
uint64_t timestamp;
@@ -64,11 +112,12 @@ struct iis3dwb_fifo_data {
64112
struct iis3dwb_decoder_header header;
65113
uint32_t accel_odr: 4;
66114
uint32_t fifo_mode_sel: 2;
67-
uint32_t fifo_count: 7;
115+
uint32_t fifo_count: 10;
68116
uint32_t reserved_1: 5;
69-
uint32_t accel_batch_odr: 3;
117+
uint32_t accel_batch_odr: 4;
118+
uint32_t temp_batch_odr: 2;
70119
uint32_t ts_batch_odr: 2;
71-
uint32_t reserved: 9;
120+
uint32_t reserved: 3;
72121
} __attribute__((__packed__));
73122

74123
struct iis3dwb_rtio_data {
@@ -84,9 +133,14 @@ struct iis3dwb_rtio_data {
84133

85134
int iis3dwb_encode(const struct device *dev, const struct sensor_chan_spec *const channels,
86135
const size_t num_channels, uint8_t *buf);
87-
88136
int iis3dwb_get_decoder(const struct device *dev, const struct sensor_decoder_api **decoder);
89137

138+
#ifdef CONFIG_IIS3DWB_TRIGGER
139+
int iis3dwb_route_int1(const struct device *dev, iis3dwb_pin_int1_route_t pin_int);
140+
int iis3dwb_route_int2(const struct device *dev, iis3dwb_pin_int2_route_t pin_int);
141+
void iis3dwb_stream_irq_handler(const struct device *dev);
142+
#endif
143+
90144
void iis3dwb_rtio_rd_transaction(const struct device *dev,
91145
uint8_t *regs, uint8_t regs_num,
92146
struct spi_buf *buf,

0 commit comments

Comments
 (0)