Titanium Exposé

Main menu

Post navigation

Connect NGW100 Linux (AVR32) to Arduino with I2C

For a Linux embedded training, I need to find something fun to plug on an AVR32 board. The board used for the training is a NGW100 board. I decided to try to use the I2C bus. I have several I2C stuff on the desk, but nothing really fun and I don’t want to solder a lot of stuff, and spend a lot of time on this. I decided to use a Arduino board as a I2C slave device and plug this to the NGW100 board.

First step: Simply use the Arduino IDE and the Wire lib. I use an Seeeduino because I need an Arduino that works at 3.3v level. Here the code :

To test the Arduino I used a Bus Pirate, this is quite simple and fun, here a little snipset of my initial test with the BP (note the string are different). The I2C slave is at the 0×4 address (check the setup()).

Second step: Plug the Arduino to the NGW100. I used the wrapping technique. Simply connect SDA, SCL, and GND. (NGW100 pinouts : SDA=>9, SCL=>10, GND=>2)

On Linux, load the I2C-GPIO kernel module. On OpenWRT (used on the NGW100), simply load the kmod-i2c-gpio package.

Final step: If everything is Ok, we can now test the communication. I used a small piece of C code to deal with I2C on Linux.

#include <string.h>#include <stdio.h>#include <stdlib.h>#include <linux/i2c-dev.h>#include <sys/ioctl.h>#include <fcntl.h>voidi2c_run(void){intfile;charfilename[40];intaddr=0x4;charbuf[32]={0};inti;sprintf(filename,"/dev/i2c-0");if((file=open(filename,O_RDWR))<0){printf("Failed to open the bus.");exit(1);}if(ioctl(file,I2C_SLAVE,addr)<0){printf("Failed to acquire bus access and/or talk to slave.\n");exit(1);}i=24;
// readI2Cif(read(file,buf,i)!=i){printf("Failed to read from the i2c bus.\n");}else{printf("Read %d bytes from I2C: [%s]\n",i,buf);}sprintf(buf,"IC2 from Linux to Arduino");i=strlen(buf);if(write(file,buf,i)!=i){printf("Failed to write to the i2c bus.\n");}else{printf("Sent %d bytes to I2C: [%s]\n",i,buf);}}intmain(){i2c_run();return0;}

As you can see this code is a bit rude, but works really well : Read the I2C bus, and send a sample string, a proof ? :)

Of course, I used string values but in real life a small protocol shoud be used. Another important thing: I used a NGW100 but you can use the same idea on all Linux embedded board like the Fonera, or anything else.

Update: Of course you can use the i2c-tools on Linux to detect your own device. To do that : Grab the i2c-tools source, and cross compile it for the AVR32. (You only have to change the CC path in the Makefile).