adding I2C to asset_tracker_v2 in V2.5.0

I have V2.5.0 asset_tracker_v2 running on a nrf9160DK board. I need to add I2C capability. I tried copying the code from the i2c_api example. I added "CONFIG_I2C=y" to prj.conf. I added the following to the overlay file:


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

I get the following error when I try to compile the asset_tracker_v2. I have tried changing to i2c-1,i2c-2 and I still get the same error. what am I doing wrong? How do I add I2C capability to asset_tracker_v2 and what pins are the I2C signals (SCL/SDA) available?

In file included from C:/Nordic1/v2.5.0/zephyr/include/zephyr/toolchain.h:50,
from C:/Nordic1/v2.5.0/zephyr/include/zephyr/kernel_includes.h:19,
from C:/Nordic1/v2.5.0/zephyr/include/zephyr/kernel.h:17,
from C:/Nordic1/v2.5.0/zephyr/soc/arm/nordic_nrf/validate_enabled_instances.c:7:
C:/Nordic1/v2.5.0/zephyr/include/zephyr/toolchain/gcc.h:81:36: error: 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."
81 | #define BUILD_ASSERT(EXPR, MSG...) _Static_assert(EXPR, "" MSG)
| ^~~~~~~~~~~~~~
C:/Nordic1/v2.5.0/zephyr/soc/arm/nordic_nrf/validate_enabled_instances.c:51:1: note: in expansion of macro 'BUILD_ASSERT'
51 | BUILD_ASSERT(CHECK(2), MSG(2));
| ^~~~~~~~~~~~
[119/466] Building C object zephyr/CMakeFiles/zephyr.dir/subsys/logging/log_core.c.obj
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: 'C:\Nordic1\toolchains\c57af46cb7\opt\bin\cmake.EXE' --build 'C:\Nordic1\test_n160\asset_tracker_v2\build'

************zephyr.dts******************

aliases {
led0 = &led0;
led1 = &led1;
led2 = &led2;
led3 = &led3;
pwm-led0 = &pwm_led0;
sw0 = &button0;
sw1 = &button1;
sw2 = &button2;
sw3 = &button3;
bootloader-led0 = &led0;
mcuboot-button0 = &button0;
mcuboot-led0 = &led0;
watchdog0 = &wdt0;
spi-flash0 = &mx25r64;
i2c-0 = &i2c0;
ext-flash = &mx25r64;
};

i2c0: i2c@8000 {
compatible = "nordic,nrf-twim";
#address-cells = < 0x1 >;
#size-cells = < 0x0 >;
reg = < 0x8000 0x1000 >;
clock-frequency = < 0x186a0 >;
interrupts = < 0x8 0x1 >;
status = "disabled";
};
i2c1: i2c@9000 {
compatible = "nordic,nrf-twim";
#address-cells = < 0x1 >;
#size-cells = < 0x0 >;
reg = < 0x9000 0x1000 >;
clock-frequency = < 0x186a0 >;
interrupts = < 0x9 0x1 >;
status = "disabled";
};
i2c2: arduino_i2c: i2c@a000 {
compatible = "nordic,nrf-twim";
#address-cells = < 0x1 >;
#size-cells = < 0x0 >;
reg = < 0xa000 0x1000 >;
clock-frequency = < 0x61a80 >;
interrupts = < 0xa 0x1 >;
status = "okay";
pinctrl-0 = < &i2c2_default >;
pinctrl-1 = < &i2c2_sleep >;
pinctrl-names = "default", "sleep";
pcal6408a: pcal6408a@20 {
compatible = "nxp,pcal6408a";
status = "disabled";
reg = < 0x20 >;
gpio-controller;
#gpio-cells = < 0x2 >;
ngpios = < 0x8 >;
int-gpios = < &gpio0 0x6 0x11 >;
};
};
i2c3: i2c@b000 {
compatible = "nordic,nrf-twim";
#address-cells = < 0x1 >;
#size-cells = < 0x0 >;
reg = < 0xb000 0x1000 >;
clock-frequency = < 0x186a0 >;
interrupts = < 0xb 0x1 >;
status = "disabled";
};

Parents
  • Hello

    1) The peripheral you want to use should be enabled. I can see that I2c0 and I2c1 both are "disabled" and the i2c2 is enabled.

    2) Many instances share the same ID (please refer to the documentation / product specification to see the peripherals having same ID). As such, only one of such peripheral should be enabled. And that is also present in the error you are getting.

    So, if you want to use i2c0, the put the following in the overlay: (I am assuming you will not be using i2c1 and i2c2)

    / {
    aliases {
        i2c-0 = &i2c0;
        };
    };
    
    &i2c0{
        status = "okay";
    }
    &i2c1{
        status = "disabled";
    }
    &i2c2{
        status = "disabled";
    }
    

    After that, when you compile the code, please go and check the pins used by i2c0. If you want to change the pins then you can do so using the PINCTRL in the overlay.

    /BR, Naeem

  • Naeem thanks for the quick response. I do not want to change the pin assignments of the I2C ports. I just want to know what they are so I can connect a I2C device. Where do I find the pin assignment for the I2C ports?

  • if I enable CONFIG_I@C=y in the prj.conf file I get the error message about only one device allowed. 

  • If I do exactly what you said earlier in this ticket I get the following error message. This was trying to use i2c0 as you suggested. so far  nothing seems to work with merging I2C into asset_tracker. I am confused.

    -- Found devicetree overlay: C:/Nordic1/test_n160/asset_tracker_v2/boards/nrf9160dk_nrf9160_ns_0_14_0.overlay
    devicetree error: 'pinctrl-0' is marked as required in 'properties:' in C:/Nordic1/v2.5.0/zephyr/dts/bindings\i2c\nordic,nrf-twim.yaml, but does not appear in <Node /soc/peripheral@40000000/i2c@8000 in 'C:/Nordic1/v2.5.0/zephyr/misc/empty_file.c'>
    -

  • Hello Timothy,

    I started with the Asset Tracker V2 sample from NCS 2.5.0, and I modified prj.conf to add the config for i2c and then I edited the overlay file as shown below. I am enabling only i2c2 as you have asked and also I am using pin 30 and 31. (You can see both files in the image below)

    Compilation was successful without any issue. Copying below the content which I updated in the overlay: We are enabling i2c2 and using Pinctrl to define the pins for data and clock.

    &i2c2 {
    	status = "okay";
    	pinctrl-0 = <&i2c2_default>;
    	pinctrl-1 = <&i2c2_sleep>;
    	pinctrl-names = "default", "sleep";
    };
    
    &pinctrl {
    	i2c2_default: i2c2_default {
    		group1 {
    				psels = <NRF_PSEL(TWIM_SDA, 0, 30)>,
    						<NRF_PSEL(TWIM_SCL, 0, 31)>;
    		};
    	};
    
    	i2c2_sleep: i2c2_sleep {
    		group1 {
    				psels = <NRF_PSEL(TWIM_SDA, 0, 30)>,
    						<NRF_PSEL(TWIM_SCL, 0, 31)>;
    				low-power-enable;
    		};
    	};
    };

    /Regards, Naeem

  • I have attached my overlay file. I must be adding your code in the wrong place. I copied it and pasted it inside the existing overlay file. I have also added code for UART2 in there needed for BLE operation. what am I doing wrong?

    I see the following errors when I try to compile.

    -- Found BOARD.dts: C:/Nordic1/v2.5.0/zephyr/boards/arm/nrf9160dk_nrf9160/nrf9160dk_nrf9160_ns.dts
    -- Found devicetree overlay: C:/Nordic1/v2.5.0/zephyr/boards/arm/nrf9160dk_nrf9160/nrf9160dk_nrf9160_ns_0_14_0.overlay
    -- Found devicetree overlay: C:/Nordic1/test_n160/asset_tracker_v2/boards/nrf9160dk_nrf9160_ns.overlay
    -- Found devicetree overlay: C:/Nordic1/test_n160/asset_tracker_v2/boards/nrf9160dk_nrf9160_ns_0_14_0.overlay
    devicetree error: C:/Nordic1/test_n160/asset_tracker_v2/boards/nrf9160dk_nrf9160_ns.overlay:90 (column 1): parse error: expected node name, property name, or '}'
    -
    2364.nrf9160dk_nrf9160_ns.overlay

  • Timothy said:
    C:/Nordic1/test_n160/asset_tracker_v2/boards/nrf9160dk_nrf9160_ns.overlay:90 (column 1): parse error: expected node name, property name, or '}'

    There is a missing   };   on line number 89,

    and an extra   };   on line 114.

    Please remove it from line 114 and put at line 89

Reply Children
Related