Issues with setting up i2c for custom project in nrf connect SDK (VScode)

Hello,

I am trying to do my own i2c read/write from a sensor, and I followed the twim example available with the SDK. 

When I build the project, I get errors wherever I have I2C related macros:

\ncs\v1.7.0\zephyr\include\devicetree.h:177:36: error: 'DT_N_NODELABEL_i2c_addr_REG_IDX_0_VAL_ADDRESS' was not declared in this scope
  177 | #define DT_NODELABEL(label) DT_CAT(DT_N_NODELABEL_, label)

\ncs\v1.7.0\zephyr\include\devicetree.h:2542:24: note: in definition of macro 'DT_CAT'
 2542 | #define DT_CAT(a1, a2) a1 ## a2

\ncs\v1.7.0\zephyr\include\devicetree.h:1355:30: note: in expansion of macro 'DT_REG_ADDR_BY_IDX'
 1355 | #define DT_REG_ADDR(node_id) DT_REG_ADDR_BY_IDX(node_id, 0)

And so on. Essentially whenever I use these macro definitions in code it also spits out an error.

I made sure to define the device overlay and setup the i2c similar to the tutorial. The address of my sensor is 0x32:


&i2c1 {										/* a */
	compatible = "nordic,nrf-twim";			/* b */
	status = "okay";						/* c */
	clock-frequency = <100000>;				/* d */

	twis_device1:nRF52840@32 {				/* e */
		// device compatible				/* f */
		label = "zmod addr 0x32";			/* g */
       	reg = <0x32>;						/* h */
   	};
};

I also made sure to enable i2c in proj.conf:

CONFIG_I2C=y
CONFIG_I2C_NRFX=y
 
Parents Reply
  • I don't think so. Here is how I define and call DT_NODELABEL:

    #define MY_TWIM DT_NODELABEL(i2c1)
    const struct device *nrfx_twis_dev1;
    
    void nrf_i2c_setup()
    {
        int config_result = false;
    
        nrfx_twis_dev1 = device_get_binding(DT_LABEL(MY_TWIM));
    
        if (nrfx_twis_dev1 == NULL) {
            printk("\n\nI2C Slave: Device driver not found.\n");
        } else {
            printk("\nI2C device 1: %s\n", DT_PROP(DT_NODELABEL(twis_device1), label));
    
            config_result = i2c_configure(nrfx_twis_dev1, I2C_SPEED_SET(I2C_SPEED_FAST) | I2C_MODE_MASTER);
    
            if (!config_result) {
            printk("I2C Master: Slave ADDR: 0x%x SCL: %d, SDA: %d, CLK(Hz): %u\n\n",
                DT_REG_ADDR(DT_NODELABEL(twis_device1)), 
                DT_PROP(MY_TWIM, scl_pin),
                DT_PROP(MY_TWIM, sda_pin),
                DT_PROP(MY_TWIM, clock_frequency));
            } else
            printk("\n\nI2C: Configuration error code: %d\n", config_result);
        }
    }

    One thing to mention is this is not in my main.c, rather, it's in a library I wrote in a folder inside the project, and linked using cmake.

Children
  • Solved it! 

    Somewhere else in the code, I had used the i2c_write() function, and I passed the address of the device which I had defined in overlay. However, I did not use the node name, and used the actual address instead. This caused the issue:

    i2c_write(nrfx_twis_dev1, &reg_addr, sizeof(reg_addr), DT_REG_ADDR(DT_NODELABEL(some_address)))
    although some_address was the same address I had defined in the overlay, I should have used the node name. The correct code is the following:
    i2c_write(nrfx_twis_dev1, &reg_addr, sizeof(reg_addr), DT_REG_ADDR(DT_NODELABEL(twis_device1)))
    Where twis_device1 is defined in the overlay.
Related