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

      add_subdirectory_ifdef(CONFIG_<name of sensor> <sensor directory>)

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

        source "drivers/sensor/<sensor directory>/Kconfig"

        • 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:

        #if defined(DT_VENDOR_CHIPNAME_BUS_SPI)
            chip_spi_init(dev);
        #elif defined(DT_VENDOR_CHIPNAME_BUS_I2C)
            chip_i2c_init(dev);
        #else
            #error "BUS MACRO NOT DEFINED IN DTS"
        #endif

        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

        description: |
          ICM-20948 Nine Axis I2C/SPI MEMS
          This is a representation of the ICM-20948 Three Axis High-g I2C/SPI accelerometer
        
        compatible: "tdk,icm20948"
        
        include: spi-device.yaml
        
        #inherits:
        #  !include i2c-device.yaml
        
        properties:
            int1-gpios:
              type: phandle-array
              required: false
              description: |
                The INT1 signal defaults to active high as produced by the
                sensor.  The property value should ensure the flags properly
                describe the signal that is presented to the driver.
        

        tdk,icm20948-i2c.yaml

        description: |
          ICM-20948 Nine Axis I2C/SPI MEMS
          This is a representation of the ICM-20948 Three Axis High-g I2C/SPI accelerometer
        
        compatible: "tdk,icm20948"
        
        include: i2c-device.yaml
        
        #inherits:
        #  !include i2c-device.yaml
        
        properties:
            int1-gpios:
              type: phandle-array
              required: false
              description: |
                The INT1 signal defaults to active high as produced by the
                sensor.  The property value should ensure the flags properly
                describe the signal that is presented to the driver.
        

        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

        /* 	uart0 reserved for debug prints
        	uart1 reserved for modem traces
        */
        
        /*		status = "okay"; disable usart2 to use I2C_2 for ICM20948 */
        
        &uart2 {
        	status = "disabled";
        };
        
        &i2c2 {
        	compatible = "nordic,nrf-twim";
        	status = "okay";
        	sda-pin = <11>;
        	scl-pin = <12>;
        	clock-frequency = <I2C_BITRATE_STANDARD>; 
        
                /* The I2C address could be one of two, here 0x68 is assumed */
        	icm20948@68 {
        		compatible = "tdk,icm20948";
        		reg = <0x68>;
        		label = "ICM20948";
        	};
        };
        

      • Try to do this:

        // You need this in order for DT_ANY_INST_ON_BUS_STATUS_OKAY to work
        #define DT_DRV_COMPAT tdk_icm20948 
        
        // code
        // code
        // code
        
        
        #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
            //Do stuff
        #elif DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
            //Do stuff
        #else
        #error "BUS MACRO NOT DEFINED IN DTS"
        #endif

        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 Children
      No Data
      Related