This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to add new i2c based sensor device to ncs\v1.3.0\zephyr\drivers\sensor

How to add new i2c based sensor device to ncs\v1.3.0\zephyr\drivers\sensor.

Can you suggest any tutorial to do so as i want to use sensor subsystem which in turn uses i2c driver.

Thanks

Chandra

Parents
  • I tried to look around but could not find any such guides. However I have provided an answer for this earlier, take a look at it here.

    Down below I've provided a short guide how to add a custom sensor to Zephyr

    • Start with a senor most similar to your sensor and use it as a reference
    • Create a binding in zephyr\dts\bindings\sensor\<compatible string>.yaml
      • E.g. copy zephyr\dts\bindings\sensor\adi,adxl362.yaml, change the name and modify it according to your sensor
    • Create the driver in zephyr\drivers\sensor
      • E.g. copy zephyr\drivers\sensor\adxl362, change the name and modify it according to your sensor
    • In \zephyr\drivers\sensor\CMakeLists.txt add the following line

      • In \zephyr\drivers\sensor\Kconfig add the following line

        • Create a sample in zephyr\samples\sensor, to test if it works as expected

        Best regards,

        Simon

      • Hi

        Thanks for the info and this is verymuch useful.

        I have followed all the steps.

        in  the sensor  c file, i have some like below:

        These macros are not getting defined. This is some things to do with zephyr system, but any pointers that would greatly help to proceed.

      • Hi

        zephyr\dts\bindings\sensor\<compatible string>.yaml  -----> Contains tdk,icm20948-i2c.yaml , tdk,icm20948-spi.yaml

        tdk,icm20948-spi.yaml

        tdk,icm20948-i2c.yaml

        zephyr\samples\sensor\<your sensor>\boards\<board name>.overlay --> I donot have this file. but I have overlay file in my application:nrf9160dk_nrf9160ns.overlay

      • Try to do this:

        Take a look at the zephyr/include/devicetree.h for more information about the macro DT_ANY_INST_ON_BUS_STATUS_OKAY.

        Best regards,

        Simon

      • This part i got it. it works for me early this week.

        but i have following code in driver.

        #if defined(DT_TDK_ICM20948_BUS_SPI)
            icm20948_spi_init(dev);
        #elif defined(DT_TDK_ICM20948_BUS_I2C)
            icm20948_i2c_init(dev);
        #else
            #error "BUS MACRO NOT DEFINED IN DTS"
        #endif

        DT_TDK_ICM20948_BUS_SPI, DT_TDK_ICM20948_BUS_I2C: what are these?

        I just repalced these with DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) and DT_ANY_INST_ON_BUS_STATUS_OKAY(I2C).

        How to enable these macros : DT_TDK_ICM20948_BUS_SPI ,DT_TDK_ICM20948_BUS_I2C? we can just ignore.

      • I'm not sure where you get those DTS defines from, and why you think those should be generated. Could you give me some pointers?

        I think you should be fine using DT_ANY_INST_ON_BUS_STATUS_OKAY and DT_ANY_INST_ON_BUS_STATUS_OKAY, that is what all the other sensor drives in NCS v1.3.0 are using:

        https://github.com/nrfconnect/sdk-zephyr/blob/v2.3.0-rc1-ncs2/drivers/sensor/iis3dhhc/iis3dhhc.c#L235

        https://github.com/nrfconnect/sdk-zephyr/blob/v2.3.0-rc1-ncs1/drivers/sensor/lps22hh/lps22hh.c#L210-L225

        https://github.com/nrfconnect/sdk-zephyr/blob/v2.3.0-rc1-ncs1/drivers/sensor/stts751/stts751.c#L207

        https://github.com/nrfconnect/sdk-zephyr/blob/v2.3.0-rc1-ncs1/drivers/sensor/lsm6dso/lsm6dso.c#L755

        Best regards,

        Simon

      • OK thanks for all the clarifications. Thank you very much.

        I could succeed with DT_ANY_INST_ON_BUS_STATUS_OKAY , could communicate with sensor.

        Need to improve sensor driver.

        for now I am going to close this ticket.

        Thanks
        Chandra

      Reply
      • OK thanks for all the clarifications. Thank you very much.

        I could succeed with DT_ANY_INST_ON_BUS_STATUS_OKAY , could communicate with sensor.

        Need to improve sensor driver.

        for now I am going to close this ticket.

        Thanks
        Chandra

      Children
      No Data