Making I²C to work between Arduino Uno, Arduino Nano (ATmega 168) and Spykee

As a part of my research to extend my Spykee robot using the I²C pins, today I was finally able to make I²C to work between my two Arduino boards: Arduino Uno and Arduino Nano 2.x.

 

For information about the pins and wiring, here is a good start: http://www.instructables.com/id/I2C-between-Arduinos/

After that, I started the tests on Spykee. I was also able to create a small test program and cross-compile it to Spykee’s ARM architecture. This page has a very nice example of C code to deal with I²C in Linuxes:
https://www.kernel.org/doc/Documentation/i2c/dev-interface

To enable I²C on Spykee you’ll have to compile your own firmware after enabling the compilation of the I²C library. Drop me a line in the comments section if you need any help with that.

Then you’ll need to add the missing pins and wires to the Spykee mainboard. The I²C pins on Spykee are those unused ones close to the antenna, as shown in the picture below (for some reason this is a very hard information to find anywhere else on the internet). Also, notice the correct wiring for pins: +3.3v, Gnd, Rx and Tx, not necessarily in this order). You can infer which pin is which from the wiring:


Finally, here is the test code for the Spykee firmware. I just added it to the /buildroot/packages/wavestorm/spykee directory. Remember, this is just test code. Quality isn’t a priority here J:

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>

#define I2C_SLAVE       0x0703  /* Use this slave address */
#define I2C_SLAVE_FORCE 0x0706  /* Use this slave address, forced */

int main() {
  int file;
  int adapter_nr = 0; /* probably dynamically determined */
  char filename[20];

  snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
  file = open(filename, O_RDWR);
  if (file < 0) {
    /* ERROR HANDLING; you can check errno to see what went wrong */
    printf("Error opening file.");
    exit(1);
  }

  int addr = 	0x2; /* The I2C address */

  if (ioctl(file, I2C_SLAVE, addr) < 0) {
    /* ERROR HANDLING; you can check errno to see what went wrong */
    printf("Error ioctling.");
    exit(1);
  }

  //uint8_t register = 10; /* Device register to access */
  //__s32 res;
  char buf[10];

  ///* Using SMBus commands */
  //res = i2c_smbus_read_word_data(file, register);
  //if (res < 0) {
  //  /* ERROR HANDLING: i2c transaction failed */
  //} else {
  //  /* res contains the read word */
  //}

  /* Using I2C Write, equivalent of
     i2c_smbus_write_word_data(file, register, 0x6543) */
  buf[0] = 65;
  buf[1] = 66;
  buf[2] = 67;
  if (write(file, buf, 3) != 3) {
    /* ERROR HANDLING: i2c transaction failed */
  }

  ///* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
  //if (read(file, buf, 1) != 1) {
  //  /* ERROR HANDLING: i2c transaction failed */
  //} else {
  //  /* buf[0] contains the read byte */
  //}

  return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s