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: sensors: add ST lis2dux12 accelerometer support #65259

Conversation

kdunn926
Copy link
Contributor

@kdunn926 kdunn926 commented Nov 15, 2023

The driver was based off the existing lis2dw12 implementation.

This was physically tested via SPI at 1MHz using a nrf52840 development kit. Both the Zephyr sensor and direct spi_write APIs were successfully used. Interrupts and runtime configuration changes (e.g. ODR, range) were not tested.

sensor API sample:

    struct sensor_value accel[3];
    int rc = sensor_sample_fetch_chan(accelerometer_dev, SENSOR_CHAN_ALL);
    [...]
    rc = sensor_channel_get(accelerometer_dev, SENSOR_CHAN_ACCEL_XYZ,  accel);

spi_write API sample:

int lis2dux12_print_device_id()
{

    static uint8_t tx_buffer[2] = { LIS2DUX12_WHO_AM_I, 0x0 };
    static uint8_t rx_buffer[2] = { 0x0, 0x0 };

    const struct spi_buf tx_buf = {
            .buf = tx_buffer,
            .len = sizeof(tx_buffer)
    };
    const struct spi_buf_set tx =  {
            .buffers = &tx_buf,
            .count = 1
    };

    const struct spi_buf rx_buf = {
            .buf = rx_buffer,
            .len = sizeof(rx_buffer)
    };
    const struct spi_buf_set rx =  {
            .buffers = &rx_buf,
            .count = 1
    };

    int ret = spi_transceive(spi3_dev, &spi3_cfg, &tx, &rx);
    if (ret != 0) {
        LOG_ERR("SPI write error: %s [%d]\n", strerror(ret), ret);
    } else {
        // ignore first byte
        LOG_WRN("Device ID: %x", rx_buffer[1]);
    }

    return ret;
}

Copy link

Hello @kdunn926, 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. 😊

@kdunn926 kdunn926 force-pushed the drivers-sensors-add-lis2dux12-support branch 6 times, most recently from d07595b to fd64e81 Compare November 15, 2023 23:55
@kdunn926 kdunn926 force-pushed the drivers-sensors-add-lis2dux12-support branch from fd64e81 to 4bb4096 Compare November 16, 2023 02:17
drivers/sensor/lis2dux12/lis2dux12.c Outdated Show resolved Hide resolved
drivers/sensor/lis2dux12/lis2dux12.c Outdated Show resolved Hide resolved
drivers/sensor/lis2dux12/lis2dux12.c Outdated Show resolved Hide resolved
drivers/sensor/lis2dux12/lis2dux12.c Outdated Show resolved Hide resolved
ycsin
ycsin previously requested changes Nov 16, 2023
Copy link
Member

@ycsin ycsin left a comment

Choose a reason for hiding this comment

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

consider using year 2023, and probably just use .clang-format to format all these new files

drivers/sensor/lis2dux12/lis2dux12_trigger.c Outdated Show resolved Hide resolved
@kdunn926 kdunn926 force-pushed the drivers-sensors-add-lis2dux12-support branch from 4bb4096 to c8c0ed8 Compare November 16, 2023 02:51
drivers/sensor/lis2dux12/lis2dux12.c Outdated Show resolved Hide resolved
drivers/sensor/lis2dux12/lis2dux12.c Outdated Show resolved Hide resolved
@kdunn926 kdunn926 requested a review from ycsin November 16, 2023 03:37
@kdunn926 kdunn926 force-pushed the drivers-sensors-add-lis2dux12-support branch from c8c0ed8 to e243de1 Compare November 16, 2023 03:40
@kdunn926 kdunn926 force-pushed the drivers-sensors-add-lis2dux12-support branch from e243de1 to 55d5e05 Compare November 16, 2023 04:26
drivers/sensor/CMakeLists.txt Show resolved Hide resolved
drivers/sensor/Kconfig Show resolved Hide resolved
avisconti
avisconti previously approved these changes Mar 28, 2024
Copy link
Collaborator

@avisconti avisconti left a comment

Choose a reason for hiding this comment

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

It's been a long way but we are finally there...

drivers/sensor/CMakeLists.txt Show resolved Hide resolved
drivers/sensor/Kconfig Show resolved Hide resolved
@kdunn926 kdunn926 force-pushed the drivers-sensors-add-lis2dux12-support branch from dcf6590 to 6e2fbc4 Compare March 28, 2024 17:34
@kdunn926 kdunn926 requested review from kartben and avisconti March 29, 2024 03:50
avisconti
avisconti previously approved these changes Mar 29, 2024
@MaureenHelm
Copy link
Member

Please rebase

@kdunn926 kdunn926 force-pushed the drivers-sensors-add-lis2dux12-support branch from 6e2fbc4 to 8d69de7 Compare April 1, 2024 14:48
@kdunn926
Copy link
Contributor Author

kdunn926 commented Apr 1, 2024

Please rebase

Ok I just did. Sorry for the delay.

@kdunn926 kdunn926 requested a review from avisconti April 2, 2024 13:29
avisconti
avisconti previously approved these changes Apr 2, 2024
@kdunn926
Copy link
Contributor Author

kdunn926 commented Apr 2, 2024

consider using year 2023, and probably just use .clang-format to format all these new files

@ycsin - Have all the changes you requested been addressed?

@avisconti avisconti requested review from fabiobaltieri and removed request for erwango and nordicjm April 3, 2024 10:19
@avisconti
Copy link
Collaborator

@fabiobaltieri if/when you have time, can you possibly take a look at this PR? Thanks

@avisconti avisconti requested review from erwango and nordicjm April 3, 2024 10:20

static int lis2dux12_set_range(const struct device *dev, uint8_t range)
{
int err = 0;
Copy link
Member

Choose a reason for hiding this comment

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

drop the initializer, not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright


err = lis2dux12_mode_set(ctx, &val);

if (!err) {
Copy link
Member

Choose a reason for hiding this comment

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

invert the logic here:

if (err) {
        return err;
}

and just return 0; at the end of the function, keep the normal execution flow inline, easier to follow the code intended path

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. I made the change.


if (!err) {
switch (range) {
default:
Copy link
Member

Choose a reason for hiding this comment

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

sure you don't want to error out here? silently falling back to default does not strike me as a great idea, maybe add a LOG_ERR and return -EINVAL?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I changed it to be like this:

	default:
		LOG_ERR("range [%d] not supported.", range);
		return -EINVAL;

Comment on lines 68 to 84
#define FOREACH_ODR_ENUM(ODR_VAL) \
ODR_VAL(LIS2DUX12_DT_ODR_OFF, 0.0f) \
ODR_VAL(LIS2DUX12_DT_ODR_1Hz_ULP, 1.0f) \
ODR_VAL(LIS2DUX12_DT_ODR_3Hz_ULP, 3.0f) \
ODR_VAL(LIS2DUX12_DT_ODR_25Hz_ULP, 25.0f) \
ODR_VAL(LIS2DUX12_DT_ODR_6Hz, 6.0f) \
ODR_VAL(LIS2DUX12_DT_ODR_12Hz5, 12.50f) \
ODR_VAL(LIS2DUX12_DT_ODR_25Hz, 25.0f) \
ODR_VAL(LIS2DUX12_DT_ODR_50Hz, 50.0f) \
ODR_VAL(LIS2DUX12_DT_ODR_100Hz, 100.0f) \
ODR_VAL(LIS2DUX12_DT_ODR_200Hz, 200.0f) \
ODR_VAL(LIS2DUX12_DT_ODR_400Hz, 400.0f) \
ODR_VAL(LIS2DUX12_DT_ODR_800Hz, 800.0f)
Copy link
Member

Choose a reason for hiding this comment

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

nit: you can probably pull those \ a bit behind, no need to make lines unnecessarily wide

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, I shifted these over quite a bit in the latest commit.

drivers/sensor/lis2dux12/lis2dux12.c Outdated Show resolved Hide resolved
drivers/sensor/lis2dux12/lis2dux12.c Outdated Show resolved Hide resolved
drivers/sensor/lis2dux12/lis2dux12.c Outdated Show resolved Hide resolved
drivers/sensor/lis2dux12/lis2dux12.c Outdated Show resolved Hide resolved
drivers/sensor/lis2dux12/lis2dux12.c Outdated Show resolved Hide resolved
drivers/sensor/lis2dux12/lis2dux12_trigger.c Show resolved Hide resolved
@ycsin ycsin dismissed their stale review April 3, 2024 14:20

stale

@kdunn926 kdunn926 force-pushed the drivers-sensors-add-lis2dux12-support branch from 8d69de7 to 32fbe01 Compare April 3, 2024 15:33
Adds support for the STMicroelectronics LIS2DUX12 3-axis accelerometer.

Signed-off-by: Kyle Dunn <[email protected]>
@kdunn926 kdunn926 force-pushed the drivers-sensors-add-lis2dux12-support branch from 32fbe01 to 50bef4a Compare April 3, 2024 17:03
Copy link
Collaborator

@avisconti avisconti left a comment

Choose a reason for hiding this comment

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

Very good

@fabiobaltieri fabiobaltieri merged commit a846b81 into zephyrproject-rtos:main Apr 4, 2024
21 checks passed
Copy link

github-actions bot commented Apr 4, 2024

Hi @kdunn926!
Congratulations on getting your very first Zephyr pull request merged 🎉🥳. This is a fantastic achievement, and we're thrilled to have you as part of our community!

To celebrate this milestone and showcase your contribution, we'd love to award you the Zephyr Technical Contributor badge. If you're interested, please claim your badge by filling out this form: Claim Your Zephyr Badge.

Thank you for your valuable input, and we look forward to seeing more of your contributions in the future! 🪁

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: Sensors Sensors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants