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

nrf9160 I2C only sending out 1 byte

I'm trying to get my nrf9160 DK to communicate with an i2c device so I setup a project to enable the i2c module and use TWIM to send i2c data.  But then I run it and it only sends out 1 byte of data.  Any thoughts as to what I might be doing wrong?
I followed this: devzone.nordicsemi.com/.../164005

You add an .overlay file in the application folder (where the applications CMakeLists.txt is located). And when using the nrf91DK you call this file "nrf9160_pca10090.overlay".
In this file you add what pins you would like to use for the peripherals.
ex. to activate TWI
- add this to the .overlay file
&i2c2 {
 status = "ok";
 sda-pin = <11>;
 scl-pin = <12>;
 clock-frequency = <I2C_BITRATE_STANDARD>;
};
- Activate the TWI in the prj.conf file (same as ninja menuconfig or the settings in SES)
# Enable I2C
CONFIG_I2C=y
CONFIG_I2C_NRFX=y
CONFIG_I2C_2=y
CONFIG_I2C_2_NRF_TWIM=y

Then I wrote a simple main.c
#include <zephyr.h>
#include <bsd.h>
#include <nrf_socket.h>
#include <net/socket.h>
#include <stdio.h>
#include <i2c.h>
#define I2C_DEV_NAME    "I2C_2"
struct device * i2c_dev;
struct device * gpio_dev;
static void i2c_init(void)
{
  i2c_dev = device_get_binding(I2C_DEV_NAME);
  if (i2c_dev == NULL)
  {
    printk("Could not get %s device\n", I2C_DEV_NAME);
    return;
  }
  else
  {
    printk("Well, we got the binding for %s device\n", I2C_DEV_NAME);
  }
}
static bool verify_id(void)
{
 u8_t val = 0;
 int err = i2c_reg_read_byte(i2c_dev, 0x60, 0x10, &val);
        if (err)
        {
          printk("i2c_reg_read_byte err = %d, value = %d\n", err, val);
        }
        else
        {
          printk("i2c read (0x%x from this address 0x%x)\n", val, REG_HARDWARE_ID);
 }
 return true;
}
int main(void)
{
  printk("Staring I2C application\n");
  i2c_init();
  verify_id();
  while (1)
  {
    verify_id();
    printk("Sleepy\n");
    k_sleep(500);
  }
  return 0;
}
I was expecting the i2c_reg_read_byte was going to send out the device address, then the register address and clock out another byte for the slave to respond.  But it sends out the first byte and that's it.  I'm sure I'm missing something simple.  
Parents
  • If anyone was following along.  It was the target i2c device.  So when you send out an i2c read, you send out an device address byte and then a memory address byte.  And then clock out for a response.  Well my i2c device I was trying to connect to wasn't operating properly, so when I sent out the device address byte, there was nothing listening so there was no ack to that address byte.  I got a different i2c device and hooked it up and it sent out both bytes.  I assumed it was something on the Nordic side, but it appears it was on the "thing the Nordic was connected to" side.

Reply
  • If anyone was following along.  It was the target i2c device.  So when you send out an i2c read, you send out an device address byte and then a memory address byte.  And then clock out for a response.  Well my i2c device I was trying to connect to wasn't operating properly, so when I sent out the device address byte, there was nothing listening so there was no ack to that address byte.  I got a different i2c device and hooked it up and it sent out both bytes.  I assumed it was something on the Nordic side, but it appears it was on the "thing the Nordic was connected to" side.

Children
Related