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

drivers: sensor: adxl372: Updated driver with RTIO stream functionality #78941

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

Conversation

vladislav-pejic
Copy link
Contributor

Updated ADXL372 driver with RTIO stream functionality.
RTIO stream is using both FIFO threshold and FIFO full triggers.
Together with RTIO stream, RTIO async read is also implemented.

Signed-off-by: Vladislav Pejic [email protected]

Copy link

Hello @vladislav-pejic, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

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.

Looks like a really nice driver, very happy to see another device taking advantage of this functionality. Now I need to get an analog adxl372 on my bench it seems 💯

uint8_t has_z: 1;
uint16_t accel_odr: 4;
uint16_t fifo_byte_count: 12;
} __attribute__((__packed__));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since its packed, its likely worth asserting the size is aligned to 4 bytes with a build assert here.

BUILD_ASSERT(sizeof(struct adxl372_fifo_data) % 4 == 0, "struct should be word aligned");

Or something along these lines, otherwise unaligned access is likely to bite someone as soon as the struct is changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree and changes are in the latest code. I needed to do some rearranging to align it and should be ok now.


#ifdef CONFIG_ADXL372_STREAM

static uint32_t accel_period_ns[] = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

possible to make const?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Changes are in the latest code.

[ADXL372_ODR_6400HZ] = UINT32_C(100000000) / 6400,
};

static void adxl372_accel_convert_q31(q31_t *out, uint16_t data_in)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be doable with a constant multiplier that is pre-calculated, and the function can be inlined in that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you check the latest code to see if this is what you had in mind?

{
struct sensor_value *out = (struct sensor_value *) data_out;

if (*fit > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

looks like a spacing thing, missing a tab?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed


__ASSERT_NO_MSG(req);

rtio_work_req_submit(req, iodev_sqe, adxl372_submit_helper);
Copy link
Collaborator

@teburd teburd Sep 24, 2024

Choose a reason for hiding this comment

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

Is the work queue usage really needed here? Ideally you'd use RTIO with the spi bus as well to allow for the gpio ISR to directly start a spi transfer and avoid the context swaps.

It seems like the fetch vs stream path are using different methods, which is fine but in the stream case the work queue can be bypassed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is not necessary. I updated the implementation in the latest code.

@vladislav-pejic vladislav-pejic force-pushed the adi_adxl372_rtio_stream branch 4 times, most recently from de34935 to 80a5167 Compare September 25, 2024 13:58
Updated ADXL372 driver with RTIO stream functionality.
RTIO stream is using both FIFO threshold and FIFO full triggers.
Together with RTIO stream, RTIO async read is also implemented.

Signed-off-by: Vladislav Pejic <[email protected]>
@zephyrbot
Copy link
Collaborator

zephyrbot commented Sep 30, 2024

The following west manifest projects have been modified in this Pull Request:

Name Old Revision New Revision Diff

Note: This message is automatically posted and updated by the Manifest GitHub Action.

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.

Looks pretty good, some minor things I noticed and commented on

if (fifo_samples > sample_set_size/2) {
fifo_samples -= sample_set_size/2;
} else {
LOG_ERR("fifo sample caunt error %d\n", fifo_samples);
Copy link
Collaborator

Choose a reason for hiding this comment

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

s/caunt/count I think? or caught?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

drivers/sensor/adi/adxl372/adxl372_stream.c Show resolved Hide resolved

uint32_t buf_avail = buf_len;

memcpy(buf, &hdr, sizeof(hdr));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could avoid the memcpy here perhaps by treating the buffer as a *hdr and initializing from there, though that requires that everything is word aligned.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

/* Flush completions */
struct rtio_cqe *cqe;

do {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's likely worth checking error codes here and bailing with rtio_iodev_sqe_err() on a bus error if one occurs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you give me more information? rtio_cqe_consume returns NULL when there is no CQE and that is cowered, and rtio_cqe_release is void func.
Did you mean for this part:
struct rtio_sqe *write_fifo_addr = rtio_sqe_acquire(data->rtio_ctx); ?

Copy link
Collaborator

@teburd teburd Oct 4, 2024

Choose a reason for hiding this comment

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

When there is a completion queue event from the bus request, its result may be -errno, e.g.

struct rtio_cqe *cqe;
int res = 0;

do {
    cqe = rtio_cqe_consume(...);
    if (cqe != NULL) {
        if(cqe->result < 0 && res == 0) {
            /* handle spi/i2c bus error here */
            LOG_ERR("first bus error: %d", cqe->result);
            res = cqe->result;
        }
        rtio_cqe_release(cqe);
    }
} while (cqe != NULL);

/* Bail/cancel attempt to read sensor on any error */
if (res != 0) {
   rtio_iodev_sqe_err(...);
   return;
}

Fixed some formating issues.

Signed-off-by: Vladislav Pejic <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Sensors Sensors platform: ADI Analog Devices, Inc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants