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

nrf9160 i2c_scanner example

Hello,

I am working on a custom pcb with nrf9160-SICA. I have UART SPI and GPIOs running fine but I am having a very difficult time with I2c (TWI).

Pins 8 and 9 are occupied by switch1 and switch 2 on the nrf9160DK so even simple pin toggle (GPIO low high every second) is not possible.

On my board pins 8 and 9 are clear and can do simple GPIO toggle. because of this I assume my issue is software.

When I use my logic analyzer I don't see any output except for some odd low bits every few seconds or no activity (pins 8 and 9 are HIGH).

I am using this example: https://github.com/sigurdnev/fw-nrfconnect-nrf/tree/i2c/samples/nrf9160/i2c_scanner

CMakeLists:

cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(i2c_scanner)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

nrf9160_pca10090.overlay

&i2c2 {
	status = "ok";
	sda-pin = <9>; 
	scl-pin = <8>; 
	clock-frequency = <I2C_BITRATE_STANDARD>; 
};

nrf9160_pca10090.dts

/*
 * Copyright (c) 2017 Linaro Limited
 * Copyright (c) 2018 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/dts-v1/;
#include <nordic/nrf9160_xxaa.dtsi>

/ {
	model = "Nordic PCA10090 Dev Kit";
	compatible = "nordic,pca10090-dk", "nordic,nrf9160-xxaa";

	chosen {
		zephyr,console = &uart0;
                zephyr,i2c-accel = &i2c2;
		zephyr,sram = &sram0;
		zephyr,flash = &flash0;
	};

	aliases {
//		led0 = &led0;
//		led1 = &led1;
//		led2 = &led2;
//		led3 = &led3;
//		sw0 = &button2;
//		sw1 = &button3;
		sw2 = &button0;
		sw3 = &button1;
	};

	leds {
		compatible = "gpio-leds";
//		led0: led_0 {
//			gpios = <&gpio0 2 GPIO_INT_ACTIVE_LOW>;
//			label = "Green LED 0";
//		};
//		led1: led_1 {
//			gpios = <&gpio0 3 GPIO_INT_ACTIVE_LOW>;
//			label = "Green LED 1";
//		};
//		led2: led_2 {
//			gpios = <&gpio0 4 GPIO_INT_ACTIVE_LOW>;
//			label = "Green LED 2";
//		};
//		led3: led_3 {
//			gpios = <&gpio0 5 GPIO_INT_ACTIVE_LOW>;
//			label = "Green LED 3";
//		};
	};

	buttons {
		compatible = "gpio-keys";
		button0: button_0 {
			gpios = <&gpio0 6 GPIO_PUD_PULL_UP>; //gpios = <&gpio0 8 GPIO_PUD_PULL_UP>;
			label = "Push button 1"; //label = "Switch 1";
		};
		button1: button_1 {
			gpios = <&gpio0 7 GPIO_PUD_PULL_UP>; //gpios = <&gpio0 9 GPIO_PUD_PULL_UP>;
			label = "Push button 2"; //label = "Switch 2";
		};
//		button2: button_2 {
//			gpios = <&gpio0 6 GPIO_PUD_PULL_UP>;
//			label = "Push button 1";
//		};
//		button3: button_3 {
//			gpios = <&gpio0 7 GPIO_PUD_PULL_UP>;
//			label = "Push button 2";
//		};
	};

	sram0_bsd: memory@20010000 {
		device_type = "memory";
		compatible = "mmio-sram";
	};
};

&uart0 {
	status = "ok";
	current-speed = <115200>;
	tx-pin = <23>; //29
	rx-pin = <24>; //28
	//rts-pin = <27>;
	//cts-pin = <26>;
};

&uart1 {
	status = "ok";
	current-speed = <9600>; //<115200>;
	tx-pin = <11>; //1
	rx-pin = <10>; //0
	//rts-pin = <15>;
	//cts-pin = <18>;
};

&spi3 {
	status = "ok";
	sck-pin = <0>;
	mosi-pin = <30>;
	miso-pin = <2>;
	//ss-pin = <14>;
	spi-max-frequency = <4000000>;
};

&gpiote {
	status = "ok";
};

&gpio0 {
	status = "ok";
};

//&i2c2 {
//	status = "ok";
//	sda-pin = <9>;
//	scl-pin = <8>;
//	clock-frequency = <I2C_BITRATE_STANDARD>; 
//};

&timer0 {
	status = "ok";
};

&flash0 {
	/*
	 * For more information, see:
	 * http://docs.zephyrproject.org/devices/dts/flash_partitions.html
	 */
	partitions {
		compatible = "fixed-partitions";
		#address-cells = <1>;
		#size-cells = <1>;

		boot_partition: partition@0 {
			label = "mcuboot";
			reg = <0x00000000 0x10000>;
		};
		slot0_partition: partition@10000 {
			label = "image-0";
		};
#if defined(CONFIG_ARM_TRUSTZONE_M)
		slot0_ns_partition: partition@40000 {
			label = "image-0-nonsecure";
		};
#endif
		slot1_partition: partition@80000 {
			label = "image-1";
		};
#if defined(CONFIG_ARM_TRUSTZONE_M)
		slot1_ns_partition: partition@b0000 {
			label = "image-1-nonsecure";
		};
#endif
		scratch_partition: partition@f0000 {
			label = "image-scratch";
			reg = <0x000f0000 0xa000>;
		};

		storage_partition: partition@fa000 {
			label = "storage";
			reg = <0x000fa000 0x00006000>;
		};
	};
};

#if defined(CONFIG_ARM_TRUSTZONE_M)
/ {
	sram0_ns: memory@20020000 {
		device_type = "memory";
		compatible = "mmio-sram";
	};
};
#endif

/* Include partition configuration file */
#include "nrf9160_pca10090_partition_conf.dts"

main.c

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

#include <gpio.h>
#define I2C_ACCEL_WRITE_ADDR 0x32
#define I2C_ACCEL_READ_ADDR 0xAA //0x33

struct device * i2c_accel;
uint8_t WhoAmI = 0xaa; //0u;

uint8_t init_accelerometer(){
        const char* const i2cName = "I2C_2";
        i2c_accel = device_get_binding(i2cName);
//        i2c_accel = device_get_binding("I2C_2");
        if (!i2c_accel) {
		printk("error\r\n");
                return -1;
	} else  {
                i2c_configure(i2c_accel, I2C_SPEED_SET(I2C_SPEED_STANDARD));
                return 0;
        }
}
	struct device *dev;


void main(void)
{
       	dev = device_get_binding("GPIO_0");

	/* Set LED pin as output */
        gpio_pin_configure(dev, 16, GPIO_DIR_OUT); 
        gpio_pin_configure(dev, 17, GPIO_DIR_OUT); 
        gpio_pin_write(dev, 16, 1); 
        gpio_pin_write(dev, 17, 1); 

        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");
                }
                //i2c_reg_read_byte(i2c_accel, X, 0x0F, WhoAmI);
                printk("WhoAmI = %x\r\n", WhoAmI);
                printk("enter sleep\r\n");
                k_sleep(1000);
	}
}

Related