Asset Tracker v2 - Add new sensor with NRF9160 board

Hello, 

I'm trying to connect a sensor to the nRF9160 board using the example Asset Tracker v2. 

My sensor (SHT30) is connect in I2C to the pins 31 (SCL) and 30 (SDA).

In the nrf9160dk_nrf9160_ns.overlay I added : 

&i2c2 {
	compatible = "nordic,nrf-twim";
	status = "okay";
	sda-pin = < 30 >;
	scl-pin = < 31 >;
    clock-frequency = <I2C_BITRATE_FAST>;
	label = "I2C_2" ;

	sht30: sht30@44 {
		label = "SHT30";
		reg = <0x44>;
	};
};

In the file prj.conf, I added : 

CONFIG_SENSOR=y
CONFIG_I2C=y

I also added a file which define the driver : 

int sht30_init(const struct device *dev)
{
	struct sht30_data *data = dev->data;

	data->i2c_master = device_get_binding("SHT30");
	if (!data->i2c_master) {
		LOG_ERR("I2C master not found");
		return -EINVAL;
	}

	return 0;
}

static struct sht30_data sht30_data;

DEVICE_DEFINE(sht30, "SHT30", sht30_init, NULL, &sht30_data,
		    NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
			NULL);


The code build correctly but device_get_binding("SHT30") returns an error. The device is not initialized correctly.

Also, when I look at the signals with Sigrok of pins 30 and 31 on the board, there is nothing. But I will be able to see the clock on the pin 31 even if my sensor is not initialized correctly, no?

In the deviceTree, the bus i2c2 is well configured in the mcuboot with my sht30 node but not in the spm build (it's still the standard configuration of i2c2). I guess the error come from here. Do I need to had a configuration in the prj.conf of the child_image?

Thanks in advance for your help !

Elisa

Parents
  • Hi,

    Is that the entire driver file? Where is it located, and how are you including it in the project?

    It would be great to have as much information as possible on the changes you have made, so that I can reproduce this on my board.

  • In the src folder I added a folder "drivers" with sht30.c, sht30.h and the CMakeLists. These files only do the init for now.

    sht30.c:

    #include <drivers/sensor.h>
    #include <drivers/i2c.h>
    #include <sys/__assert.h>
    #include <sys/byteorder.h>
    #include <init.h>
    #include <kernel.h>
    #include <string.h>
    #include <sys/printk.h>
    #include <errno.h>
    #include <logging/log.h>
    
    #include "sht30.h"
    
    LOG_MODULE_REGISTER(sht30, CONFIG_SENSOR_LOG_LEVEL);
    
    
    int sht30_init(const struct device *dev)
    {
    	struct sht30_data *data = dev->data;
    	uint8_t error = 0u;
    	
    	data->i2c_master = device_get_binding("I2C_2");
    	if (!data->i2c_master) {
    		LOG_ERR("I2C master not found");
    		return -EINVAL;
    	}
    	return 0;
    }
    
    static struct sht30_data sht30_data;
    
    DEVICE_DEFINE(sht30, "SHT30", sht30_init, NULL, &sht30_data,
    		    NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
    			NULL);


    sht30.h:
    #include <stdint.h>
    #include <device.h>
    
    
    struct sht30_data {
    	const struct device *i2c_master;
    	uint16_t i2c_slave_addr;
    
    	/* Calculated sensor values. */
    	int32_t calc_temp;
    	uint32_t calc_humidity;
    
    };


    CMakeLists.txt:

    target_sources(app PRIVATE
    	       ${CMAKE_CURRENT_SOURCE_DIR}/sht30.c
    )
    

    I also added 

    add_subdirectory(src/drivers)
    to the CMakeLists.txt of the project. 


    I followed the example of asset tracker v2 for the thingy91 board with the sensor bme680. 

    If I do device_get_binding("I2C_2") instead of device_get_binding("SHT30") the function doesn't return an error but i2c_2 have a clock frequency of  I2C_SPEED_STANDARD instead of I2C_BITRATE_FAST. The changes in my nrf9160dk_nrf9160_ns.overlay file are not taken into account.  

  • With your changes, device_get_binding("SHT30") seems to work for me. If you are using SES, remember to re-open the project or run Project->Run CMake after any changes to prj.conf or overlay files.

  • Thank you for your answer. 

    I'm using Visual Studio Code and when I flash the nrf9160 board, device_get_binding("SHT30") returns NULL.
    If you check the DeviceTree in build/mcuboot and build/spm, do you see the node sht30 in i2c2 ? I do not, only in the DeviceTree of the build. 

Reply Children
  • The nodes have the default values in mcuboot and spm for me too, that is expected. You are calling device_get_binding() from the application, so that is the one that is important. It works for me when I build with VS Code.

    Can you double-check that you have implemented all the changes you have posted here and that the files are saved?
    Which SDK version are you using?
    Are you using Windows 10, and is your SDK installed through the Toolchain Manager?
    Are there any other changes to your SDK? For example check by running git status in the nrf and zephyr folders.

Related