I2C not working on NCS 2.1.0 (nRF32840DK)

Greetings,

We are developing a new application based on NCS 2.1.0. Right now we are using the nRF52840DK and we are performing some tests on the basic examples/samples and functionality such as BLE, Thread communication, and finally I2C.

We tried communicating with an I2C sensor (LSM6DSO Evaluation kit) that is operating correctly when run with our verified drivers on nRF5 SDK (17.1.0) and we get back a correct response ( we are reading the device ID and data correctly ) which has been verified so our hardware connection is correct (as seen in the capture below ).

When we run the simple example code found in nRF Connect SDK Fundamentals course, Lesson 6 Exercise 1 we get back error from the i2c function

Below is our code:

prj.conf

# STEP 2 - Enable the I2C driver
CONFIG_I2C=y
# STEP 4.2 - Enable floating point format specifiers
CONFIG_CBPRINTF_FP_SUPPORT=y

nrf52840dk_nrf52840.overlay   (I2C slave address of our sensor is 0x6a)

// To get started, press Ctrl+Space to bring up the completion menu and view the available nodes.

// You can also use the buttons in the sidebar to perform actions on nodes.
// Actions currently available include:

// * Enabling / disabling the node
// * Adding the bus to a bus
// * Removing the node
// * Connecting ADC channels

// For more help, browse the DeviceTree documentation at https://docs.zephyrproject.org/latest/guides/dts/index.html
// You can also visit the nRF DeviceTree extension documentation at https://nrfconnect.github.io/vscode-nrf-connect/devicetree/nrfdevicetree.html
&i2c0 {
    
    mysensor: mysensor@6a{
        compatible = "i2c-device";
        reg = < 0x6a >;
        label = "MYSENSOR";
    };
};

main.c

/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 * 
 * Note: Tested on nRF52833 DK
 */

#include <zephyr.h>
#include <device.h>
#include <devicetree.h>
/* STEP 3 - Include the header file of the I2C API */
#include <drivers/i2c.h>
/* STEP 4.1 - Include the header file of printk() */
#include <sys/printk.h>
/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS   1000

/* STEP 8 - Define the I2C slave device address and the addresses of relevant registers */
#define STTS751_TEMP_HIGH_REG            0x00
#define STTS751_TEMP_LOW_REG             0x02
#define STTS751_CONFIG_REG               0x03

/* STEP 6 - Get the node identifier of the sensor */
#define I2C0_NODE DT_NODELABEL(mysensor)

void main(void)
{

	int ret;

/* STEP 7 - Retrieve the API-specific device structure and make sure that the device is ready to use  */
	static const struct i2c_dt_spec dev_i2c = I2C_DT_SPEC_GET(I2C0_NODE);
	if (!device_is_ready(dev_i2c.bus)) {
		printk("I2C bus %s is not ready!\n\r",dev_i2c.bus->name);
		return;
	}

/* STEP 9 - Setup the sensor by writing the value 0x8C to the Configuration register */
	uint8_t config_tx = 0x0F;
	ret = i2c_write_dt(&dev_i2c, &config_tx, sizeof(config_tx));
	if(ret != 0){
		printk("Failed to write to I2C device address %x at Reg. %x \n\r", dev_i2c.addr,config_tx);
	}
	uint8_t config_rx[1];
	ret = i2c_read_dt(&dev_i2c, config_rx, sizeof(config_rx));
	printk("Read from I2C device address %x at Reg. %x the value %x\n\r",dev_i2c.addr,config_tx, config_rx[0]);

	while (1) {
/* STEP 10 - Read the temperature from the sensor */


/* STEP 11 - Convert the two bytes to a 12-bits */

	}
}

The serial output is:

Failed to write to I2C device address 6a at Reg. f
Read from I2C device address 6a at Reg. f the value 0

And the scope from the logic analyzer shows that only the second command (i2c_read_dt) was executed:

What is the issue here? We have tried running the same code in different projects with various i2c commands( i2c_write_read , i2c_burst_read etc. ) but got the same results.

Are we missing something? It seems that we followed the exercise steps exactly and our hardware connection has been verified to work correctly.

What could be the issue?

Thank you for your time and I'm looking forward to hearing from you!

Best regards,

Stavros Filippas

  • Hi Stravos, 

    Check the example application on my github page. https://github.com/iFransL/i2c-scanner It is a simple I2C scanner. And you can configure it for any of the Nordic boards. 

    Also have a look at the Zephyr Sample for the LSM6DSO (\ncs\vx.x.x\zephyr\samplers\sensor\lsm6dso).



    Regards, Frans

  • Hi Frans,

    Thank you very much for your immediate response!

    First I would like to mention that we tested the example you sent by setting an overlay as the readme instructs with the code below in an overlay file (we are using i2c0, we also tried without the overlay as this node has the label arduino_i2c) 

    / {
    	aliases {
    		my-i2c = &i2c0;
    	};
    };

    and when building we always get the error:

    \i2c-scanner-main\build\zephyr\include\generated\devicetree_unfixed.h:7237:36: error: 'DT_N_S_soc_S_i2c_40003000_P_label' 
    undeclared (first use in this function); did you mean 'DT_N_S_soc_S_i2c_40003000_P_reg'?

    How do we fix this?

    -----------------------------------------------

    Second, for our product, we need to have a general implementation of i2c so it can be used for communication on a multitude of sensors that we already have developed the drivers for.

    In your example application, the i2c_transfer function is used, this is not ideal.

    Is the exercise code not functional? can we not use the functions of the SDK (i2c_write, i2c_read, i2c_write_read) ?

    How do I configure the I2C so I can use these functions? Are they not functional for all cases? What about a custom board?

    Thank you very much for your help and I look forward to hearing from you!

    Best regards,

    Stavros

  • Hi Frans,

    I have found the reason all the attempts to communicate via I2C failed,

    I had to add the property bias-pull-up to my pinctrl configuration used by my i2c node (using overlay)

    For anyone else having the same issues here is the overlay code:

    &pinctrl {
        i2c0_default: i2c0_default {
    		group1 {
    			psels = <NRF_PSEL(TWIM_SDA, 0, 26)>,
    				<NRF_PSEL(TWIM_SCL, 0, 27)>;
                bias-pull-up;
    		};
    	};
    };

    Thanks again for the help

    Regards, Stavros

Related