This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Reading external sensor using I2C on nrf9160dk

Hello,

I want to read external accelerometer on nrf9160dk using I2C. But when I connect SDA and SCL pins of sensor to nrf91, there is no output.

The Code is:

#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_READ_ADDR   60

struct device * i2c_accel;
u8_t I2C_BUFFER[35];


void init_accelerometer(){
    i2c_accel = device_get_binding("I2C_2");
    if (!i2c_accel) {
		printk("error\r\n");
	} 
    i2c_configure(i2c_accel, I2C_SPEED_SET(I2C_SPEED_FAST));

}


void main(void)
{
        printk("Start I2C comminication\r\n");
        init_accelerometer();

	while (1) {

                i2c_read(i2c_accel, I2C_BUFFER, 29, I2C_ACCEL_READ_ADDR);

                printk("I2C_BUFFER = %s\n", I2C_BUFFER);
	}
}

When I don't connect sensor to nrf91, then continuously it prints "I2C_BUFFER ="

When I connect the sensor, everything stops and when I again remove the pins it prints "I2C_BUFFER ="

What I am doing wrong? How to read these sensors value?

Thank you

  • I am using ICM20648. Actually I encountered the problem.

    Atmel SAM is interfaced to ICM20648 by using I2C. Now I just want to read this data on nRF91. It means SAM is master and ICM, nRF91 are slaves. But when I ran the code, nRF91 was acting as Master and it read the address of ICM which is 0x69 and it stopped the communication between SAM and ICM.

    So now the question is how to configure nRF91 as slave and read the sensor data received at SAM which is Master?

  • Impossible with the TWI hardware on NRF chips - these want to be bus master or act as a slave.

    Almost impossible (read: very hard) to do in software.

    By extensively using the I²C clock streching feature (and GPIOs in open drain mode), one could implement I²C in software that can read and track another I²C master's transactions.

    A more sensible solution would use the NRF chip as pure slave and have the SAM transfer the data it just read from the accelerometer back to the NRF chip in another bus transaction (maybe even on a second bus).

  • Yes I want to use nRF91 as pure slave. So how to configure it, that is the question. What are the settings required in prj.conf file? 

    nRF91 will be slave and SAM will be Master. SAM will write data on address 60 and nRF91 will read at that address. So how to configure nRF9160 as slave for I2C? 

  • Hello,

    I am trying to configure I2C_3 as slave on nrf9160. But I couldn't found any driver for that. There is i2c_nrfx_twim.c for master. But there is no such driver for slave. In i2c.h and nrf9160.h files there are definitions and registers for slave. But how to use them. I am not understanding how to do configuration. Please guide me for this.

    Thanks

  • The I2C-slave driver support seems to be missing in Zephyr for nrf. You can create a feature request in Zephyr if this is something you need. Note that you might be able to use the nrfx TWIS driver directly instead, see this post on how to use nrfx directly. Alternately, you can use the nRF9160 as I2C master, and the SAM as slave. Or you can use a different protocol between the nRF9160 and the SAM. E.g. UART or SPI.

Related