I2C communication with two NRF DK

Hi,

I want to make an I2C communication with an NRF5340 DK as Master and an NRF52840 as Slave, but all the example i found in I2C communication it use the slave adress to communicate,

But i don't know my NRF52840 DK I2C adress, is there a way to make this communication possible ? 

Thank you for your help,

Best Regards,

Daroussi 

  • After some research i found this post which say that i can put my NRF52840 as a slave adding this line on prj.conf

    CONFIG_I2C_NRFX=y
    CONFIG_NRFX_TWIS0=y

     Configuring 52832 for I2C slave mode using zephyr and VS code 

    i try but it doesn't work 

  • There is some details of my code for the Slave configuration : 

    my prj.conf :  

    #
    # Copyright (c) 2016 Intel Corporation
    #
    # SPDX-License-Identifier: Apache-2.0
    #
    
    # STEP 2 - Enable the I2C driver
    CONFIG_I2C=y
    # STEP 4.2 - Enable floating point format specifiers
    CONFIG_CBPRINTF_FP_SUPPORT=y
    
    CONFIG_I2C_NRFX=y
    CONFIG_NRFX_TWIS0=y
    --------------------------
    my overlay : 
    // To get started, press Ctrl+Space to bring up the completion menu and view the available nodes.
    // For more help, browse the DeviceTree documentation at https://docs.zephyrproject.org/latest/guides/dts/index.html
    
    &i2c1 {
        status = "okay";
        pinctrl-0 = <&i2c1_default>;
    	pinctrl-1 = <&i2c1_sleep>;
        pinctrl-names = "default", "sleep";
        mysensor: mysensor@77{
            compatible = "i2c-device";
            status = "okay";
            reg = < 0x77 >;
        };
    };
    
    &pinctrl {
    	/omit-if-no-ref/ i2c1_default: i2c1_default {
    		group1  {
    			psels = <NRF_PSEL(TWIM_SCL, 1, 14)>,
    					<NRF_PSEL(TWIM_SDA, 1, 15)>;
    		};
    	};
    
    	/omit-if-no-ref/ i2c1_sleep: i2c1_sleep {
    		group1  {
    			psels = <NRF_PSEL(TWIM_SCL, 1, 14)>,
    					<NRF_PSEL(TWIM_SDA, 1, 15)>;
    			low-power-enable;
    		};
    	};
    };
    -------------------------
    my main.c 
    /*
     * Copyright (c) 2016 Intel Corporation
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/devicetree.h>
    /* STEP 3 - Include the header file of the I2C API */
    #include <zephyr/drivers/i2c.h>
    /* STEP 4.1 - Include the header file of printk() */
    #include <zephyr/sys/printk.h>
    /* 1000 msec = 1 sec */
    #define SLEEP_TIME_MS 1000
    
    /* STEP 8 - Define the addresses of relevant registers */
    
    
    /* STEP 6 - Get the node identifier of the sensor */
    #define I2C_NODE DT_NODELABEL(mysensor)
    
    
    int main(void)
    {
    
    	/* 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(I2C_NODE);
    
    	if (!device_is_ready(dev_i2c.bus)) {
    		printk("I2C bus %s is not ready!\n", dev_i2c.bus->name);
    		return -1;
    	}
    
    	return 0;
    }
    
  • For the master config i have this : 

    prj.conf : 

    #
    # Copyright (c) 2016 Intel Corporation
    #
    # SPDX-License-Identifier: Apache-2.0
    #
    
    # STEP 2 - Enable the I2C driver
    CONFIG_I2C=y
    # STEP 4.2 - Enable floating point format specifiers
    CONFIG_CBPRINTF_FP_SUPPORT=y
    
    

    my overlay: 

    // To get started, press Ctrl+Space to bring up the completion menu and view the available nodes.
    // For more help, browse the DeviceTree documentation at https://docs.zephyrproject.org/latest/guides/dts/index.html
    
    &i2c0 {
        status = "okay";
        pinctrl-0 = <&i2c0_default>;
    	pinctrl-1 = <&i2c0_sleep>;
        pinctrl-names = "default", "sleep";
        mysensor: mysensor@77{
            compatible = "i2c-device";
            status = "okay";
            reg = < 0x77 >;
        };
    };
    
    &pinctrl {
    	/omit-if-no-ref/ i2c0_default: i2c0_default {
    		group1  {
    			psels = <NRF_PSEL(TWIM_SCL, 1, 14)>,
    					<NRF_PSEL(TWIM_SDA, 1, 15)>;
    		};
    	};
    
    	/omit-if-no-ref/ i2c0_sleep: i2c0_sleep {
    		group1  {
    			psels = <NRF_PSEL(TWIM_SCL, 1, 14)>,
    					<NRF_PSEL(TWIM_SDA, 1, 15)>;
    			low-power-enable;
    		};
    	};
    };

    my main.c

    /*
     * Copyright (c) 2016 Intel Corporation
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/devicetree.h>
    /* STEP 3 - Include the header file of the I2C API */
    #include <zephyr/drivers/i2c.h>
    /* STEP 4.1 - Include the header file of printk() */
    #include <zephyr/sys/printk.h>
    /* 1000 msec = 1 sec */
    #define SLEEP_TIME_MS 1000
    
    /* STEP 6 - Get the node identifier of the sensor */
    #define I2C_NODE DT_NODELABEL(mysensor)
    
    
    
    
    
    int main(void)
    {
    	static const struct i2c_dt_spec dev_i2c = I2C_DT_SPEC_GET(I2C_NODE);
    
    	if (!device_is_ready(dev_i2c.bus)) {
    		printk("I2C bus %s is not ready!\n", dev_i2c.bus->name);
    		return -1;
    	}
    
    	
    
    	uint8_t data;
    	int ret = i2c_read_dt(&dev_i2c, &data, sizeof(data));
    	if(ret != 0){
    		printk("Failed to read from I2C device address");
    	}
    
    
    }
    

  • To resume my code :  

    I try to put my NRF5340 DK as a Slave using the adress 0x77 and my NRF52840 as Master.

    And after i try to read my NRF5340 Adress to be sure connection is ok but it doesn't work :  

    *** Using Zephyr OS v3.7.99-1f8f3dc29142 ***
    Failed to read from I2C device address

  • But i don't know my NRF52840 DK I2C adress, is there a way to make this communication possible ? 

    There is no particular address and you can select any address which is not being used by any other peripheral/register.

    Also,

    could you try to set the compatible = "nordic,nrf-twim"; for the slave? 

    I would suggest you to define them according to  this: https://github.com/nrfconnect/sdk-nrf/blob/v2.9.1/tests/benchmarks/i2c_endless/boards/nrf52840dk_nrf52840.overlay#L61 

    -Priyanka

Related