I am trying to configure the STS4x temperature sensor by Sensirion on I2C on a nRF5340DK, I have setup my the coding exactly how sensirion suggested except few changes such as I2C address. Here is the github link : https://github.com/Sensirion/embedded-i2c-sts4x.git
In the github it is suggested that to only edit the sensirion_i2c_hal.c file and sensirion already gave an example of it in the github. However, i have changed the header files that suits current zephyr. Below is the sensirion_i2c_hal.c , in here i used I2C_1 instead of I2C_0 already given.
#include <zephyr/drivers/i2c.h> #include <zephyr/kernel.h> // For k_usleep and other kernel functions #include <zephyr/device.h> #include <zephyr/devicetree.h> #include "sensirion_common.h" #include "sensirion_config.h" #include "sensirion_i2c_hal.h" /* I2C device. */ static const struct device *i2c_dev; /** * Select the current i2c bus by index. * All following i2c operations will be directed at that bus. * * @param bus_idx Bus index to select * @returns 0 on success, an error code otherwise */ int16_t sensirion_i2c_hal_select_bus(uint8_t bus_idx) { char bus_name[6] = "I2C_1"; if (bus_idx > 9) { /* Invalid bus index */ return -1; } bus_name[4] = bus_idx + '0'; i2c_dev = device_get_binding(bus_name); if (i2c_dev == NULL) { /* No valid device found */ return -1; } return 0; } /** * Initialize all hard- and software components that are needed for the I2C * communication. */ void sensirion_i2c_hal_init(void) { /* Device (specified by sps30_i2c_dev) is already initialized by the Zephyr * boot-up process. Nothing to be done here. */ } /** * Release all resources initialized by sensirion_i2c_hal_init(). */ void sensirion_i2c_hal_free(void) { i2c_dev = NULL; } /** * Execute one read transaction on the I2C bus, reading a given number of bytes. * If the device does not acknowledge the read command, an error shall be * returned. * * @param address 7-bit I2C address to read from * @param data pointer to the buffer where the data is to be stored * @param count number of bytes to read from I2C and store in the buffer * @returns 0 on success, error code otherwise */ int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) { return i2c_read(i2c_dev, data, count, address); } /** * Execute one write transaction on the I2C bus, sending a given number of * bytes. The bytes in the supplied buffer must be sent to the given address. If * the slave device does not acknowledge any of the bytes, an error shall be * returned. * * @param address 7-bit I2C address to write to * @param data pointer to the buffer containing the data to write * @param count number of bytes to read from the buffer and send over I2C * @returns 0 on success, error code otherwise */ int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t* data, uint16_t count) { return i2c_write(i2c_dev, data, count, address); } /** * Sleep for a given number of microseconds. The function should delay the * execution for at least the given time, but may also sleep longer. * * Despite the unit, a <10 millisecond precision is sufficient. * * @param useconds the sleep time in microseconds */ void sensirion_i2c_hal_sleep_usec(uint32_t useconds) { int32_t remaining = useconds; while (remaining > 0) { remaining = k_usleep(remaining); } }
Here is my overlay file
&pinctrl { i2c1_default_alt: i2c1_default_alt { group2 { psels = <NRF_PSEL(TWIM_SDA, 1, 02)>, <NRF_PSEL(TWIM_SCL, 1, 03)>; bias-pull-up; }; }; i2c1_default_sleep: i2c1_default_sleep { group3 { psels = <NRF_PSEL(TWIM_SDA, 1, 02)>, <NRF_PSEL(TWIM_SCL, 1, 03)>; low-power-enable; }; }; }; &i2c1 { status = "okay"; compatible = "nordic,nrf-twim"; pinctrl-0 = <&i2c1_default_alt>; pinctrl-1 = <&i2c1_default_sleep>; pinctrl-names = "default", "sleep"; clock-frequency = < I2C_BITRATE_STANDARD >; }; // sts4x: sts4x@44 { // compatible = "i2c-device"; // reg = <0x44>; // label = "STS4X"; // status = "okay"; // };
my prj.conf file
CONFIG_STDOUT_CONSOLE=y CONFIG_I2C=y CONFIG_SENSOR=y CONFIG_CBPRINTF_FP_SUPPORT=y # I2C CONFIG_NRFX_TWIM1=y # Rebooot CONFIG_REBOOT=n CONFIG_RESET_ON_FATAL_ERROR=n
And main file
#include <stdio.h> // printf #include "sensirion_common.h" #include "sensirion_i2c_hal.h" #include "sts4x_i2c.h" /* * TO USE CONSOLE OUTPUT (PRINTF) YOU MAY NEED TO ADAPT THE INCLUDE ABOVE OR * DEFINE IT ACCORDING TO YOUR PLATFORM: * #define printf(...) */ int main(void) { int16_t error = 0; /** * select the proper i2c address for your sensor (defaults to 0x46) * * STS40-CD1B: ADDR_STS4X (I2C address: 0x46) * STS40-AD1B: ADDR_STS4X_ALT (I2C address: 0x44) * STS40-BD1B: ADDR_STS4X_ALT2 (I2C address: 0x45) */ init_driver(ADDR_STS4X_ALT); uint32_t serial_number; error = sts4x_serial_number(&serial_number); if (error) { printf("Error executing sts4x_serial_number(): %i\n", error); } else { printf("Serial number: %u\n", serial_number); } // Start Measurement for (;;) { int32_t temperature; // Read Measurement error = sts4x_measure_high_precision(&temperature); if (error) { printf("Error executing sts4x_measure_high_precision(): %i\n", error); } else { printf("Temperature: %.1f °C\n", temperature / 1000.0f); } sensirion_i2c_hal_sleep_usec(1000000); } return 0; }
I do not get an output only the booting screen,
*** Booting nRF Connect SDK v3.5.99-ncs1-1 ***
My question is how can i setup my sensor and is there any obvious mistakes i have done in the setting up i2c device? because the code could not setup the i2c device properly.
This is the project file folder : 2084.sts4x.zip