This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Error in Adding I2C for nRF9160DK

Hi ,

      I need to add an I2C device in project , I referred sample project i2c_scanner .

changes done in nrf9160dk_nrf9160ns.overlay file 

&i2c3 {
	status = "okay";
	//compatible = "nordic,nrf-twim";
	sda-pin = < 12 >;
	scl-pin = < 11 >;
    clock-frequency = <I2C_BITRATE_STANDARD>;  
};

code added in prj.conf

CONFIG_I2C=y
CONFIG_I2C_NRFX=y

CONFIG_I2C_3=y
CONFIG_I2C_3_NRF_TWIM=y

code in main.c

#include <zephyr.h>
#include <stdio.h>
#include <string.h>
#include <drivers/i2c.h>

#define I2C_SLAVE_ADDR 0x46
#define I2C_DEV "I2C_2"


void I2C_test(void)
{
	struct device *i2c_dev;
	struct i2c_msg msgs[1];
        uint8_t dst = 1,error;

        /* Send the address to read from */
        msgs[0].buf = &dst;
        msgs[0].len = 1U;
        msgs[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP;

	printk("Starting i2c device...\n");

	i2c_dev = device_get_binding(I2C_DEV);
	if (!i2c_dev) {
		printk("I2C: Device driver not found.\n");
		return;
	}

        i2c_configure(i2c_dev, I2C_SPEED_SET(I2C_SPEED_STANDARD));

        error = i2c_transfer(i2c_dev, &msgs[0], 1, I2C_SLAVE_ADDR);
		if (error == 0) {
			printk("transfer complete");
		}
		else {
			printk("error %d \n", error);
		}
}

I am facing 2 issues. 

1. I am not able to import the project in Segger Embedded studio after adding following lines in prj.conf

 

CONFIG_I2C_3=y
CONFIG_I2C_3_NRF_TWIM=y

the error message is : 
error: I2C_3_NRF_TWIM (defined at drivers/i2c/Kconfig.nrfx:39) is assigned in a configuration file,
but is not directly user-configurable (has no prompt). It gets its value indirectly from other
symbols. See docs.zephyrproject.org/.../CONFIG_I2C_3_NRF_TWIM.html
and/or look up I2C_3_NRF_TWIM in the menuconfig/guiconfig interface. The Application Development
Primer, Setting Configuration Values, and Kconfig - Tips and Best Practices sections of the manual
might be helpful too.


2. If I dont add the above lines, I am able to import the project .

but getting compilation error as 

static assertion failed: "Only one of the following peripherals can be enabled: SPI2, SPIM2, SPIS2, TWI2, TWIM2, TWIS2, UARTE2. Check nodes with status \"okay\" in zephyr.dts."

What I am doing wrong? what changes should be made in prj.conf file?

Thank you

 

  • Hello,

    For the I2C peripheral, you only need this command in prj.conf:

    CONFIG_I2C=y

    All the other commands are deprecated, and not necessary to set anymore.

    In your nrf9160dk_nrf9160ns.overlay file, the compatible part should not be commented out since it is necessary for the devicetree to find the right drivers.

    Additionally, you must disable SPI3, since this peripheral is sharing the memory section with TWI3. You can check this via the following link:

    https://infocenter.nordicsemi.com/index.jsp?topic=%2Fps_nrf9160%2Fmemory.html&anchor=topic

    Search for ID11 in the table.

    The SPI3 peripheral is enabled by default on the nRF9160 DK, see: ../build/zephyr/zephyr.dts:

    spi3: spi@b000 {
                    #address-cells = < 0x1 >;
                    #size-cells = < 0x0 >;
                    reg = < 0xb000 0x1000 >;
                    interrupts = < 0xb 0x1 >;
                    status = "okay";
                    label = "SPI_3";
                    compatible = "nordic,nrf-spim";
                    sck-pin = < 0x13 >;
                    mosi-pin = < 0x12 >;
                    miso-pin = < 0x11 >;
                };

    So, your nrf9160dk_nrf9160ns.overlay file should look like this:

     &i2c3 {
        status = "okay";
        compatible = "nordic,nrf-twim";
        sda-pin = < 12 >;
        scl-pin = < 11 >;
        clock-frequency = <I2C_BITRATE_STANDARD>;  
    };
    
    &spi3 {
        status = "disabled";
    };

    In your main.c, the I2C_DEV definition is not correct if you would like to use i2c3. It should be:

    #define I2C_DEV "I2C_3"

    Let me know if you run into more problems.

    Regards,

    Markus

  • it check by ~/ncs/zephyr/soc/arm/nordic_nrf/validate_enabled_instances.c

    #define CHECK(idx) \
        !(I2C_ENABLED(idx) && SPI_ENABLED(idx)) && \
        !(I2C_ENABLED(idx) && UART_ENABLED(idx)) && \
        !(SPI_ENABLED(idx) && UART_ENABLED(idx))

    #define MSG(idx) \
        "Only one of the following peripherals can be enabled: " \
        "SPI"#idx", SPIM"#idx", SPIS"#idx", TWI"#idx", TWIM"#idx", TWIS"#idx \
        IF_ENABLED(CONFIG_SOC_SERIES_NRF53X, (", UARTE"#idx)) \
        IF_ENABLED(CONFIG_SOC_SERIES_NRF91X, (", UARTE"#idx)) \
        ". Check nodes with status \"okay\" in zephyr.dts."

    #if !IS_ENABLED(CONFIG_SOC_NRF52810)
    BUILD_ASSERT(CHECK(0), MSG(0));
    #endif
    BUILD_ASSERT(CHECK(1), MSG(1));
    BUILD_ASSERT(CHECK(2), MSG(2));
    BUILD_ASSERT(CHECK(3), MSG(3));


    The _idx can not repeat

    ex1: uart_2 can not with i2c_2
    you need remove uart_2 or use i2c_3

    ex1: i2c_3 can not with spi_3
    you need remove i2c_3

  • Thank you for the clarification.
     I am able to add I2C.

Related