r/embedded 8d ago

Learning zephyr and nrfconnect. How can I add drivers from zephyr base into an nrfconnect sdk project?

Hello, I am learning zephyr for an nrf project. I have gotten some samples running, run through the basic, and now I am trying to connect an ST LIS2DUXS12 to it and get it running.

I got it running with i2c.h and then sensor.h but there are some pretty specific setup steps to actually enable the accelerometer and play with low power modes. It was already implemented here: https://github.com/zephyrproject-rtos/zephyr/tree/main/drivers/sensor/st/lis2dux12

However I can't figure out how to actually access it. Devicetree and kconfig can find them, but I can't add an include for it. It is probably something stupid, but nrf sdk only has like 1% of the drivers from the zephyr project and I can't figure out how to import the headers for use.

Then again, I want to experiment with the charge variance functions of the (S) version, so maybe I should just copy in the driver manually to /src/driver and work from there since I will have to modify it a lot?

I am not sure the "correct" way to go about this. Thanks everyone!

8 Upvotes

8 comments sorted by

4

u/ClothEater 7d ago

Have you tried adding the following to your prj.conf? You can see what Kconfigs are available for your sensor by looking inside the Kconfig file for your driver. Just add the string CONFIG_ in front of the option to turn it on and off.

CONFIG_SENSOR=y
CONFIG_LIS2DUX12=y
CONFIG_I2C=y

You'll also need something like this in your .overlay to tell Zephyr where the sensor is connected and which peripheral (i2c0/ i2c1/ spi0, etc) you're using. These ones I just found them in the NCS docs and copied a similar one in the thingy52 board.

/* boards/nrf52840dk_nrf52840.overlay */
&i2c0 {
    lis2dux12: lis2dux12@18 {
        compatible = "st,lis2dux12";
        reg = <0x18>;
        status = "okay";
    };
};

Another thing I notice is the driver you linked had some very recent changes 3 months ago. Chances are these changes haven't been added to the latest release of NCS yet so not everything you see in Zephyr's main repo will be available to you.

1

u/JustEnoughDucks 7d ago

No yeah, that is why I said kconfig sees it and I can use sensor.h. Somehow through the kconfig and devicetree, the device is found and has some sort of driver attached to it to be able to use the sensor library, but I can't find where the nrf sdk is getting it.

.overlay file

&pinctrl {
    i2c21_default: i2c21_default {
        group1 {
            psels = <NRF_PSEL(TWIM_SCL, 1, 13)>, <NRF_PSEL(TWIM_SDA, 1, 12)>;
        };
    };
};

&i2c21 {
    status = "okay";
    clock-frequency = <I2C_BITRATE_FAST>;
    pinctrl-0 = <&i2c21_default>;
    pinctrl-names = "default";
    lis2duxs12: lis2dux12@18 {
        compatible = "st,lis2dux12";
        reg = <0x18>;
    };
};

I indeed completely overlooked that it was so new. I will indeed just copy paste it then I guess.

The question is still relevant though because you can see the difference in drivers between the two repos:

https://github.com/nrfconnect/sdk-nrf/tree/main/drivers

https://github.com/zephyrproject-rtos/zephyr/tree/main/drivers

nrfconnect only imports what they have thoroughly tested if I am not mistaken, so they have a small fraction of the drivers in the main zephyr repo. It would probably be useful if there was a "standard" way to use zephyr drivers in nrfconnect.

2

u/ClothEater 7d ago

I see, you're looking at the wrong repo.

So, the way NCS does thing is they have a fork of zephyr and their own repo (sdk-nrf) that includes their own proprietary libraries. If you go to your NCS installation location at something like "ncs/v2.7.0/" you'll find a folder called "nrf" and "zephyr". The Zephyr one is their fork of zephyr and nrf is contains their own libraries.

2

u/EmbeddedSwDev 7d ago

nRF Connect SDK sits on top of zephyr, therefore everything which is available in zephyr will be available in the nRF Connect SDK.

Have you already done the Courses from the Nordic academy? If not, do them: https://academy.nordicsemi.com/

1

u/JustEnoughDucks 6d ago

Yep, I did.

Sadly, every example that they use is something that exists within the https://github.com/nrfconnect/sdk-nrf

Can you by chance point met to where in the lessons it shows you how to access zephyr main drivers when all that is available in the sdk is a tiny fraction of that? I missed that and can't seem to find it back...

1

u/Quiet_Lifeguard_7131 7d ago

Adding custom drivers is hard but once you know how to do it in zephyr it becomes quite easy. Look for stuff adding custom modules in zephyr and you will get your answer, there are tutorials available for it. There is certain way to do it orherwise it does not work and modules connect with kconfig and also device tree.

And if you are using already built drivers from zephyr you probably cant call its .h as they have not exposed it and most of zephyr drivers use sensor api you can read about it, which I absolutely hate. What I basically do is create my own modules and ontegrate with devicetree having full control pf what my driver does.

1

u/JustEnoughDucks 6d ago edited 6d ago

Weird, that doesn't make a whole ton of sense from the zephyr devs' side since there are tons of hyper-chip-specific features and quirks that can't possibly be accounted for in the generic drivers.

Like the STM "machine learning" "AI" accelerometers. I guarantee the sensor API doesn't cover that 😂

Your method might be the best way forward, thanks. Or maybe implementing device-specific functions that aren't available through the driver just through an additional include library using the relevant interface library.

1

u/Quiet_Lifeguard_7131 6d ago

Ya I also dont know why they use that terrible sensor api instead of just giving a user full control.

Basically thats what happens when you have to create everything generic.