Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lsm6dsv16x: add RTIO async and fifo stream #81447

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

avisconti
Copy link
Collaborator

@avisconti avisconti commented Nov 15, 2024

Add RTIO async and RTIO stream functionalities that enables, among all the other things, the sensor data streaming from FIFO. RTIO stream is using both SENSOR_TRIG_FIFO_WATERMARK and SENSOR_TRIG_FIFO_FULL triggers. The decoder currently only supports following FIFO tags:

  - LSM6DSV16X_XL_NC_TAG
  - LSM6DSV16X_GY_NC_TAG
  - LSM6DSV16X_TEMP_NC_TAG

In DT it is possible to configure following FIFO params:

  - fifo-watermark (defaults to 32)
  - accel-fifo-batch-rate (defaults to LSM6DSV16X_DT_XL_NOT_BATCHED)
  - gyro-fifo-batch-rate (defaults to LSM6DSV16X_DT_GY_NOT_BATCHED)
  - temp-fifo-batch-rate (defaults to LSM6DSV16X_DT_TEMP_NOT_BATCHED)

Since I do not have any boards with a chipset with I2C/SPI rtio supported zephyr driver, I tested this solution using I2C rtio default driver. Waiting for #71961 to be reviewed, as well as a proper solution for STM32 SPI.

Moreover, this PR does not contain any specific sample (in the future I may add one), but I'm using a modified version of samples/sensor/accel_polling to test the FIFO streaming.

Save gyroscope range set from DT or SENSOR_ATTR in data
structure.

Signed-off-by: Armando Visconti <armando.visconti@st.com>
Add attr_get() driver API

Signed-off-by: Armando Visconti <armando.visconti@st.com>
Copy link
Collaborator

@teburd teburd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking pretty good to me, I do think the workq could be avoided here with a little effort to do the reads asynchronously, or configure the sensor asynchronously (though I get this is a little bit of a hassle).

The workq does add some code size in trade for convenience.

#define LSM6DSV16X_SPI_RTIO_DEFINE(inst) \
SPI_DT_IODEV_DEFINE(lsm6dsv16x_iodev_##inst, \
DT_DRV_INST(inst), LSM6DSV16X_SPI_OP, 0U); \
RTIO_DEFINE(lsm6dsv16x_rtio_ctx_##inst, 16, 16);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16 is quite a large number of submissions and completions to keep in the pool Each one is going to be ~32bytes, 16*32 512B of potential space here.

Likely only needs 2 to 4 depending on what kind of transactions you are doing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

/*
* Instantiation macros used when a device is on an I2C bus.
*/

#define LSM6DSV16X_I2C_RTIO_DEFINE(inst) \
I2C_DT_IODEV_DEFINE(lsm6dsv16x_iodev_##inst, DT_DRV_INST(inst));\
RTIO_DEFINE(lsm6dsv16x_rtio_ctx_##inst, 16, 16);
Copy link
Collaborator

@teburd teburd Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above, I'd recommend reducing the number of submissions (and completions) in the pool to something like 2 or 4 here. Likely 4 for doing the typical write a register, read a value, do a callback.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But other sensor drivers are using 8/16. Maybe I could fix it to 4?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they are using 16, this is likely wasteful... I'd try fixing it to 4

read_fifo_status_reg->iodev_flags |= RTIO_IODEV_I2C_STOP | RTIO_IODEV_I2C_RESTART;
}
rtio_sqe_prep_callback(check_fifo_status_reg,
lsm6dsv16x_read_fifo_cb, (void *)dev, NULL);
Copy link
Collaborator

@teburd teburd Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required, but one possibility here is to look at the current request options and sensor config to assign an appropriate callback here avoiding some branching.

Potentially you could directly read the FIFO perhaps because the watermark is set, you can see if the data is requested with it.

Other possibilities to reduce branching are likely here but I get the current example drivers don't do this.

};
#endif

static int lsm6dsv16x_get_shift(enum sensor_channel channel,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could a constant lookup table be used instead?

return ret;
}

static int lsm6dsv16x_convert_raw_to_q31(const struct lsm6dsv16x_decoder_header *hdr,
Copy link
Collaborator

@teburd teburd Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting from the sensors sample representation, almost always a fixed point value with its own scale, to the larger q31 format should be a simple integer multiplier provided by a lookup table.

E.g.

static inline void lsm6dsv16x_accel_to_q31(int16_t accel_raw, int accel_fs, q31_t *out)
{
    *out = accel_raw * ACCEL_CONVERSION[accel_fs];
}

See issue #76968

Add RTIO async and RTIO stream functionalities that enables,
among all the other things, the sensor data streaming from FIFO.

RTIO stream is using both SENSOR_TRIG_FIFO_WATERMARK and
SENSOR_TRIG_FIFO_FULL triggers. The decoder currently only supports
following FIFO tags:

  - LSM6DSV16X_XL_NC_TAG
  - LSM6DSV16X_GY_NC_TAG
  - LSM6DSV16X_TEMP_NC_TAG

Following FIFO parameters has to be defined in device tree to
correctly stream sensor data:

  - fifo-watermark (defaults to 32)
  - accel-fifo-batch-rate (defaults to LSM6DSV16X_DT_XL_NOT_BATCHED)
  - gyro-fifo-batch-rate (defaults to LSM6DSV16X_DT_GY_NOT_BATCHED)
  - temp-fifo-batch-rate (defaults to LSM6DSV16X_DT_TEMP_NOT_BATCHED)

Signed-off-by: Armando Visconti <armando.visconti@st.com>
@avisconti
Copy link
Collaborator Author

re-pushed with some fix in order to (hopefully) pass CI. Still need to go thru some of the review comments...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Devicetree Binding PR modifies or adds a Device Tree binding area: Drivers area: RTIO area: Sensors Sensors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants