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

How to implement I2C on nRF9160?

I have tried to use I2C but it does't work out. nRF9160 doesn't start booting after executing "build and run" and pushing the reset button.

I open secure_boot project first and build and run. Then I open this project.

build and run is successful.

 Any help?

OS: macOS Mojave

<prf.conf>
CONFIG_TRUSTED_EXECUTION_NONSECURE=y

CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_UART_1_NRF_UARTE=y

CONFIG_I2C=y
CONFIG_I2C_NRFX=y
CONFIG_I2C_0=y
CONFIG_I2C_0_NRF_TWI=y
CONFIG_I2C_INIT_PRIORITY=60

<nrf9160_pca10090.dts>
...
	chosen {
		zephyr,console = &uart0;
        zephyr,debug = &uart1;
        zephyr,i2c-accel = &i2c0;
		zephyr,sram = &sram0;
		zephyr,flash = &flash0;
	};
...

&i2c0 {
	status = "ok";
	sda-pin = <10>;
	scl-pin = <11>;
	clock-frequency = <100000>; 
};

<main.c>
#include <nrf9160.h>
#include <zephyr.h>
#include <misc/printk.h>
#include <i2c.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#define I2C_ACCEL_WRITE_ADDR 0x32
#define I2C_ACCEL_READ_ADDR 0x33

struct device * i2c_accel;
uint8_t WhoAmI = 0u;

uint8_t init_accelerometer(){
        i2c_accel = device_get_binding("I2C_0");
        if (!i2c_accel) {
		printk("error\r\n");
                return -1;
	} else  {
                i2c_configure(i2c_accel, I2C_SPEED_SET(I2C_SPEED_STANDARD));
                return 0;
        }
}

void main(void)
{
        printk("Hello, World!\r\n");
        init_accelerometer();

	while (1) {
                printk("loop head\r\n");

                if (i2c_reg_read_byte(i2c_accel, I2C_ACCEL_READ_ADDR, 0x0F, WhoAmI) != 0) {
                        printk("Error on i2c_read()\n");
                }
                printk("WhoAmI = %u\r\n", WhoAmI);
                printk("enter sleep\r\n");
                k_sleep(1000);
	}
}

<when pushing the reset button SW2>
...
Secure Boot: SPU: set SRAM region 12 as Non-Secure
Secure Boot: SPU: set SRAM region 13 as Non-Secure
Secure Boot: SPU: set SRAM region 14 as Non-Secure
Secure Boot: SPU: set SRAM region 15 as Non-Secure
Secure Boot: SPU: set SRAM region 16 as Non-Secure
Secure Boot: SPU: set SRAM region 17 as Non-Secure
Secure Boot: SPU: set SRAM region 18 as Non-Secure
Secure Boot: SPU: set SRAM region 19 as Non-Secure
Secure Boot: SPU: set SRAM region 20 as Non-Secure
Secure Boot: SPU: set SRAM region 21 as Non-Secure
Secure Boot: SPU: set SRAM region 22 as Non-Secure
Secure Boot: SPU: set SRAM region 23 as Non-Secure
Secure Boot: SPU: set SRAM region 24 as Non-Secure
Secure Boot: SPU: set SRAM region 25 as Non-Secure
Secure Boot: SPU: set SRAM region 26 as Non-Secure
Secure Boot: SPU: set SRAM region 27 as Non-Secure
Secure Boot: SPU: set SRAM region 28 as Non-Secure
Secure Boot: SPU: set SRAM region 29 as Non-Secure
Secure Boot: SPU: set SRAM region 30 as Non-Secure
Secure Boot: SPU: set SRAM region 31 as Non-Secure
Secure Boot: configure peripherals
Secure Boot: MSP_NS 200205d0
Secure Boot: prepare to jump to Non-Secure image
// the device doesn't start booting

  • Hi,

    Are you on the master branch, or are you on the 0.3.0 tag ?

    What commands do you use to build the project? If you are on master branch, you can use the commands shown in this post.

    Also note that the peripheral(I2C, or TWIM in this case) need to be configured as non-secure in secure_boot sample. On the master branch, at least NRF_TWIM2 is configured to non-secure by default.

  • Hi, Sigurd. Thank you very much for your advice.

    >Are you on the master branch, or are you on the 0.3.0 tag ?

    Yes. I followed the instruction of Getting Started Assistant and run this command.
    cd <sourcecode_root>/ncs/nrf ; git checkout tags/v0.3.0

    >What commands do you use to build the project?
    I click "Build" > "Build and run" on SEGGER Embedded Studio. I don't know other ways to build.

    >Also note that the peripheral(I2C, or TWIM in this case) need to be configured as non-secure in secure_boot sample. On the master branch, at least NRF_TWIM2 is configured to non-secure by default.
    So, when I start to develop a new firmware, what should I do? Should I copy secure_boot sample and modify it? I usually copy asset_tracker sample and modify it.
    I really appreciate it if you show me any example code using I2C with nRF9160.
  • Hi,

    This sample runs fine for me on the nRF9160, i.e. the programs starts. Note that I have not tested it by connecting a I2C slave to it.

  • Thank you so much!

    I copied your project and it works.

    I used to copy asset_tracker project and overwrite code on main.c because I don't know how to make a project from scratch. This caused the problem. I copy secure_boot project and copy your code then I2c works.

  • Hi : i am trying to run the i2c sensor example from the master branch of your repo.

    I am having the following error with latest version of the tools. (ran a west update)
    Can you please help with this ? 
    Thank you.
    Seems like it cannot recognize the i2c3 some how ?

    2> Compiling ‘i2c_nrfx_twim.c’
    2> In file included from C:/Nord/ncs/zephyr/include/toolchain.h:39:0,
    2>                  from C:/Nord/ncs/zephyr/include/kernel_includes.h:19,
    2>                  from C:/Nord/ncs/zephyr/include/kernel.h:17,
    2>                  from C:/Nord/ncs/zephyr/include/device.h:11,
    2>                  from C:/Nord/ncs/zephyr/include/drivers/i2c.h:23,
    2>                  from C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:8:
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:221:4: error: 'DT_NORDIC_NRF_TWIM_I2C_3_CLOCK_FREQUENCY' undeclared here (not in a function); did you mean 'DT_NORDIC_NRF_TWIM_I2C_2_CLOCK_FREQUENCY'?
    2> C:/Nord/ncs/zephyr/include/toolchain/gcc.h:63:52: note: in definition of macro 'BUILD_ASSERT_MSG'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:220:3: note: in expansion of macro 'I2C_NRFX_TWIM_FREQUENCY'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:269:1: note: in expansion of macro 'I2C_NRFX_TWIM_DEVICE'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:213:3: error: expression in static assertion is not an integer
    2> C:/Nord/ncs/zephyr/include/toolchain/gcc.h:63:52: note: in definition of macro 'BUILD_ASSERT_MSG'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:220:3: note: in expansion of macro 'I2C_NRFX_TWIM_FREQUENCY'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:269:1: note: in expansion of macro 'I2C_NRFX_TWIM_DEVICE'
    2> In file included from C:/Nord/ncs/zephyr/include/arch/arm/irq.h:18:0,
    2>                  from C:/Nord/ncs/zephyr/include/arch/arm/arch.h:26,
    2>                  from C:/Nord/ncs/zephyr/include/arch/cpu.h:17,
    2>                  from C:/Nord/ncs/zephyr/include/kernel_includes.h:34,
    2>                  from C:/Nord/ncs/zephyr/include/kernel.h:17,
    2>                  from C:/Nord/ncs/zephyr/include/device.h:11,
    2>                  from C:/Nord/ncs/zephyr/include/drivers/i2c.h:23,
    2>                  from C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:8:
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:226:15: error: 'DT_NORDIC_NRF_TWIM_I2C_3_IRQ_0' undeclared (first use in this function); did you mean 'DT_NORDIC_NRF_TWIM_I2C_2_IRQ_0'?
    2> C:/Nord/ncs/zephyr/include/sw_isr_table.h:70:5: note: in definition of macro 'Z_ISR_DECLARE'
    2> C:/Nord/ncs/zephyr/include/irq.h:51:2: note: in expansion of macro 'Z_ARCH_IRQ_CONNECT'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:226:3: note: in expansion of macro 'IRQ_CONNECT'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:269:1: note: in expansion of macro 'I2C_NRFX_TWIM_DEVICE'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:226:15: note: each undeclared identifier is reported only once for each function it appears in
    2> C:/Nord/ncs/zephyr/include/sw_isr_table.h:70:5: note: in definition of macro 'Z_ISR_DECLARE'
    2> C:/Nord/ncs/zephyr/include/irq.h:51:2: note: in expansion of macro 'Z_ARCH_IRQ_CONNECT'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:226:3: note: in expansion of macro 'IRQ_CONNECT'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:269:1: note: in expansion of macro 'I2C_NRFX_TWIM_DEVICE'
    2> In file included from C:/Nord/ncs/zephyr/include/arch/arm/arch.h:26:0,
    2>                  from C:/Nord/ncs/zephyr/include/arch/cpu.h:17,
    2>                  from C:/Nord/ncs/zephyr/include/kernel_includes.h:34,
    2>                  from C:/Nord/ncs/zephyr/include/kernel.h:17,
    2>                  from C:/Nord/ncs/zephyr/include/device.h:11,
    2>                  from C:/Nord/ncs/zephyr/include/drivers/i2c.h:23,
    2>                  from C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:8:
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:227:8: error: 'DT_NORDIC_NRF_TWIM_I2C_3_IRQ_0_PRIORITY' undeclared (first use in this function); did you mean 'DT_NORDIC_NRF_TWIM_I2C_2_IRQ_0_PRIORITY'?
    2> C:/Nord/ncs/zephyr/include/arch/arm/irq.h:82:32: note: in definition of macro 'Z_ARCH_IRQ_CONNECT'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:226:3: note: in expansion of macro 'IRQ_CONNECT'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:269:1: note: in expansion of macro 'I2C_NRFX_TWIM_DEVICE'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:240:17: error: 'DT_NORDIC_NRF_TWIM_I2C_3_SCL_PIN' undeclared here (not in a function); did you mean 'DT_NORDIC_NRF_TWIM_I2C_2_SCL_PIN'?
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:269:1: note: in expansion of macro 'I2C_NRFX_TWIM_DEVICE'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:241:17: error: 'DT_NORDIC_NRF_TWIM_I2C_3_SDA_PIN' undeclared here (not in a function); did you mean 'DT_NORDIC_NRF_TWIM_I2C_2_SDA_PIN'?
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:269:1: note: in expansion of macro 'I2C_NRFX_TWIM_DEVICE'
    2> In file included from C:/Nord/ncs/zephyr/include/drivers/i2c.h:23:0,
    2>                  from C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:8:
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:247:9: error: 'DT_NORDIC_NRF_TWIM_I2C_3_LABEL' undeclared here (not in a function); did you mean 'DT_NORDIC_NRF_TWIM_I2C_2_LABEL'?
    2> C:/Nord/ncs/zephyr/include/device.h:107:11: note: in definition of macro 'DEVICE_AND_API_INIT'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:246:2: note: in expansion of macro 'DEVICE_DEFINE'
    2> C:/Nord/ncs/zephyr/drivers/i2c/i2c_nrfx_twim.c:269:1: note: in expansion of macro 'I2C_NRFX_TWIM_DEVICE'
    Build failed
    



Related