How to I2C

Hello, 

I want to control a CODEC via I2C.

I implemented the software following the DevAcademy below.

I2C Driver – Nordic Developer Academy (nordicsemi.com)

But I get the following build error:

C:\ncs\v2.3.0\zephyr\include\zephyr\device.h:83:41: error: '__device_dts_ord_134' undeclared here (not in a function); did you mean '__device_dts_ord_13'?

I implemented the source code as follows.

1) prj.conf

CONFIG_I2C=y

2) nrf5340dk_nrf5340_cpuapp.overlay

&i2c2 {
    mycodec: mycodec@1a{
        compatible = "i2c-device";
        reg = < 0x1a >;
        label = "MYCODEC";
        status = "okay";
        sda-pin = < 0x23 >;
        scl-pin = < 0x22 >;
    };
};

3) main.c

#include <zephyr/drivers/i2c.h>

#define I2C2_NODE DT_NODELABEL(mycodec)

static const struct i2c_dt_spec dev_i2c = I2C_DT_SPEC_GET(I2C2_NODE);

void main(void)
{
	if (!device_is_ready(dev_i2c.bus))
	{
		printk("I2C bus %s is not ready!\n\r",dev_i2c.bus->name);
	}

    while(1);
}

How do I resolve this build error?

Hardware: nRF5340 (custom board)

Software: NCS v2.3.0

IDE: VS Code v1.77.1

Regards, 

Gotoda

Parents Reply
  • Hi,

     

    Could you try moving "status = "okay";" one step up?

    &i2cX {
        status = "okay";
        ...
    };

     

    Also, since you are using ncs v2.x, you should use pinctrl for defining your pins. Here's an example of using I2C1, with P1.02/P1.03 as the pinout:

    &pinctrl {
    	i2c1_default: i2c1_default {
    		group1 {
    			psels = <NRF_PSEL(TWIM_SDA, 1, 2)>,
    				<NRF_PSEL(TWIM_SCL, 1, 3)>;
    		};
    	};
    
    	i2c1_sleep: i2c1_sleep {
    		group1 {
    			psels = <NRF_PSEL(TWIM_SDA, 1, 2)>,
    				<NRF_PSEL(TWIM_SCL, 1, 3)>;
    			low-power-enable;
    		};
    	};
    };
    
    &i2c1 {
    	compatible = "nordic,nrf-twim";
    	status = "okay";
    	pinctrl-0 = <&i2c1_default>;
    	pinctrl-1 = <&i2c1_sleep>;
    	...
    };

     

    Kind regards,

    Håkon

Children
Related