Skip to content

Commit 088afe9

Browse files
dimitrije-lilickartben
authored andcommitted
drivers: sensor: adxl345: Support for RTIO I2C stream
Updated ADXL345 driver with RTIO I2C stream functionality. Signed-off-by: Dimitrije Lilic <[email protected]>
1 parent fa8ebd3 commit 088afe9

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

drivers/sensor/adi/adxl345/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ config ADXL345_STREAM
3838
bool "Use FIFO to stream data"
3939
select ADXL345_TRIGGER
4040
default y
41-
depends on SPI_RTIO
41+
depends on (SPI_RTIO || I2C_RTIO)
4242
depends on SENSOR_ASYNC_API
4343
help
4444
Use this configuration option to enable streaming sensor data via RTIO.

drivers/sensor/adi/adxl345/adxl345.c

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,43 @@ static int adxl345_init(const struct device *dev)
519519
#define ADXL345_CFG_IRQ(inst)
520520
#endif /* CONFIG_ADXL345_TRIGGER */
521521

522-
#define ADXL345_RTIO_DEFINE(inst) \
523-
SPI_DT_IODEV_DEFINE(adxl345_iodev_##inst, DT_DRV_INST(inst), \
524-
SPI_WORD_SET(8) | SPI_TRANSFER_MSB | \
525-
SPI_MODE_CPOL | SPI_MODE_CPHA, 0U); \
526-
RTIO_DEFINE(adxl345_rtio_ctx_##inst, 64, 64);
522+
#define ADXL345_RTIO_SPI_DEFINE(inst) \
523+
COND_CODE_1(CONFIG_SPI_RTIO, \
524+
(SPI_DT_IODEV_DEFINE(adxl345_iodev_##inst, DT_DRV_INST(inst), \
525+
SPI_WORD_SET(8) | SPI_TRANSFER_MSB | \
526+
SPI_MODE_CPOL | SPI_MODE_CPHA, 0U);), \
527+
())
528+
529+
#define ADXL345_RTIO_I2C_DEFINE(inst) \
530+
COND_CODE_1(CONFIG_I2C_RTIO, \
531+
(I2C_DT_IODEV_DEFINE(adxl345_iodev_##inst, DT_DRV_INST(inst));), \
532+
())
533+
534+
/* Conditionally set the RTIO size based on the presence of SPI/I2C
535+
* lines 541 - 542.
536+
* The sizes of sqe and cqe pools are increased due to the amount of
537+
* multibyte reads needed for watermark using 31 samples
538+
* (adx345_stram - line 203), using smaller amounts of samples
539+
* to trigger an interrupt can decrease the pool sizes.
540+
*/
541+
#define ADXL345_RTIO_DEFINE(inst) \
542+
/* Conditionally include SPI and/or I2C parts based on their presence */ \
543+
COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
544+
(ADXL345_RTIO_SPI_DEFINE(inst)), \
545+
()) \
546+
COND_CODE_1(DT_INST_ON_BUS(inst, i2c), \
547+
(ADXL345_RTIO_I2C_DEFINE(inst)), \
548+
()) \
549+
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, spi_dt_spec) && \
550+
DT_INST_NODE_HAS_PROP(inst, i2c_dt_spec), \
551+
(RTIO_DEFINE(adxl345_rtio_ctx_##inst, 128, 128);), \
552+
(RTIO_DEFINE(adxl345_rtio_ctx_##inst, 64, 64);)) \
527553

528554
#define ADXL345_CONFIG(inst) \
529555
.odr = DT_INST_PROP(inst, odr), \
530556
.fifo_config.fifo_mode = ADXL345_FIFO_STREAMED, \
531557
.fifo_config.fifo_trigger = ADXL345_INT2, \
532558
.fifo_config.fifo_samples = SAMPLE_NUM, \
533-
.op_mode = TRUE, \
534559
.odr = ADXL345_RATE_25HZ, \
535560

536561
#define ADXL345_CONFIG_SPI(inst) \
@@ -543,6 +568,7 @@ static int adxl345_init(const struct device *dev)
543568
0)}, \
544569
.bus_is_ready = adxl345_bus_is_ready_spi, \
545570
.reg_access = adxl345_reg_access_spi, \
571+
.bus_type = ADXL345_BUS_SPI, \
546572
ADXL345_CONFIG(inst) \
547573
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, int2_gpios), \
548574
(ADXL345_CFG_IRQ(inst)), ()) \
@@ -553,13 +579,17 @@ static int adxl345_init(const struct device *dev)
553579
.bus = {.i2c = I2C_DT_SPEC_INST_GET(inst)}, \
554580
.bus_is_ready = adxl345_bus_is_ready_i2c, \
555581
.reg_access = adxl345_reg_access_i2c, \
582+
.bus_type = ADXL345_BUS_I2C, \
583+
ADXL345_CONFIG(inst) \
584+
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, int2_gpios), \
585+
(ADXL345_CFG_IRQ(inst)), ()) \
556586
}
557587

558588
#define ADXL345_DEFINE(inst) \
559589
IF_ENABLED(CONFIG_ADXL345_STREAM, (ADXL345_RTIO_DEFINE(inst))); \
560-
static struct adxl345_dev_data adxl345_data_##inst = { \
561-
IF_ENABLED(CONFIG_ADXL345_STREAM, (.rtio_ctx = &adxl345_rtio_ctx_##inst, \
562-
.iodev = &adxl345_iodev_##inst,)) \
590+
static struct adxl345_dev_data adxl345_data_##inst = { \
591+
COND_CODE_1(adxl345_iodev_##inst, (.rtio_ctx = &adxl345_rtio_ctx_##inst, \
592+
.iodev = &adxl345_iodev_##inst,), ()) \
563593
}; \
564594
static const struct adxl345_dev_config adxl345_config_##inst = \
565595
COND_CODE_1(DT_INST_ON_BUS(inst, spi), (ADXL345_CONFIG_SPI(inst)), \

drivers/sensor/adi/adxl345/adxl345.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@
113113
#define ADXL345_ODR_MSK GENMASK(3, 0)
114114
#define ADXL345_ODR_MODE(x) ((x) & 0xF)
115115

116+
#define ADXL345_BUS_I2C 0
117+
#define ADXL345_BUS_SPI 1
118+
116119
enum adxl345_odr {
117120
ADXL345_ODR_12HZ = 0x7,
118121
ADXL345_ODR_25HZ,
@@ -226,6 +229,7 @@ struct adxl345_dev_config {
226229
enum adxl345_odr odr;
227230
bool op_mode;
228231
struct adxl345_fifo_config fifo_config;
232+
uint8_t bus_type;
229233
#ifdef CONFIG_ADXL345_TRIGGER
230234
struct gpio_dt_spec interrupt;
231235
#endif

drivers/sensor/adi/adxl345/adxl345_stream.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ static void adxl345_process_fifo_samples_cb(struct rtio *r, const struct rtio_sq
216216
read_buf + data->fifo_total_bytes,
217217
SAMPLE_SIZE, current_sqe);
218218
data->fifo_total_bytes += SAMPLE_SIZE;
219+
if (cfg->bus_type == ADXL345_BUS_I2C) {
220+
read_fifo_data->iodev_flags |= RTIO_IODEV_I2C_STOP | RTIO_IODEV_I2C_RESTART;
221+
}
219222
if (i == fifo_samples-1) {
220223
struct rtio_sqe *complete_op = rtio_sqe_acquire(data->rtio_ctx);
221224

@@ -342,6 +345,9 @@ static void adxl345_process_status1_cb(struct rtio *r, const struct rtio_sqe *sq
342345
rtio_sqe_prep_read(read_fifo_data, data->iodev, RTIO_PRIO_NORM, data->fifo_ent, 1,
343346
current_sqe);
344347
read_fifo_data->flags = RTIO_SQE_CHAINED;
348+
if (cfg->bus_type == ADXL345_BUS_I2C) {
349+
read_fifo_data->iodev_flags |= RTIO_IODEV_I2C_STOP | RTIO_IODEV_I2C_RESTART;
350+
}
345351
rtio_sqe_prep_callback(complete_op, adxl345_process_fifo_samples_cb, (void *)dev,
346352
current_sqe);
347353

@@ -351,6 +357,7 @@ static void adxl345_process_status1_cb(struct rtio *r, const struct rtio_sqe *sq
351357
void adxl345_stream_irq_handler(const struct device *dev)
352358
{
353359
struct adxl345_dev_data *data = (struct adxl345_dev_data *) dev->data;
360+
const struct adxl345_dev_config *cfg = (const struct adxl345_dev_config *) dev->config;
354361

355362
if (data->sqe == NULL) {
356363
return;
@@ -366,6 +373,9 @@ void adxl345_stream_irq_handler(const struct device *dev)
366373
rtio_sqe_prep_read(read_status_reg, data->iodev, RTIO_PRIO_NORM, &data->status1, 1, NULL);
367374
read_status_reg->flags = RTIO_SQE_CHAINED;
368375

376+
if (cfg->bus_type == ADXL345_BUS_I2C) {
377+
read_status_reg->iodev_flags |= RTIO_IODEV_I2C_STOP | RTIO_IODEV_I2C_RESTART;
378+
}
369379
rtio_sqe_prep_callback(check_status_reg, adxl345_process_status1_cb, (void *)dev, NULL);
370380
rtio_submit(data->rtio_ctx, 0);
371381
}

dts/bindings/sensor/adi,adxl345-i2c.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ description: ADXL345 3-axis I2C accelerometer
55

66
compatible: "adi,adxl345"
77

8-
include: [sensor-device.yaml, i2c-device.yaml]
8+
include: ["i2c-device.yaml", "adi,adxl345-common.yaml"]

0 commit comments

Comments
 (0)