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

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

          Children
          • 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.

          • Could you upload these files:

            • zephyr\dts\bindings\sensor\<compatible string>.yaml
            • zephyr\samples\sensor\<your sensor>\boards\<board name>.overlay
              • If you have placed the sample or overlay file somewhere else, please provide that instead. 

            Best regards,

            Simon

          • 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

          Related