Chumby Hacker - Arduino Uno I2C Communication

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
djrecipe
 
Posts: 1
Joined: Wed Mar 07, 2012 3:45 pm

Chumby Hacker - Arduino Uno I2C Communication

Post by djrecipe »

I finally got the Arduino and Chumby Hacker talking to eachother via I2C. It requires a slight deviation from the Ladyada i2c tutorials out there. No additional hardware or modifications are required to get the 5v Arduino talking to the 3.3v Chumby.

Chumby C Code
This code needs to be transferred to a directory on your Chumby Hacker. If you edit this file in Windows, be sure to save in Unix format (without Carriage Returns). I use a free application called ‘EditPad Lite’ to accomplish this.

#define BUFSIZ 4
#define IADDR 23

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

int main(int argc, char **argv)
{
int i2cfd;
int8_t tempbuf[BUFSIZ];

// open connection to i2c file
if ((i2cfd = open("/dev/i2c-0", O_RDWR)) < 0)
exit(1);

if (ioctl(i2cfd,I2C_SLAVE,IADDR<<1) < 0)
{
printf("Write address is bad.");
exit(2);
}

unsigned char reg=7;
write(i2cfd, &reg, 1);

if (ioctl(i2cfd,I2C_SLAVE,((IADDR<<1)+1)) < 0)
{
printf("Read address is bad.");
exit(2);
}

while(1)
{
read(i2cfd,tempbuf,BUFSIZ);
if(tempbuf[0]!=-1)
printf("Type: %d,Channel: %d,Data 1: %d,Data 2: %d,\n", tempbuf[0], tempbuf[1], tempbuf[2], tempbuf[3]);
sleep(.01);
}

close(i2cfd);
return 0;
} // end of C code

Arduino Sketch Code
Simply upload this to your Arduino via the free Arduino application. Be sure to modify your Wire library to NOT enable 5v i2c pull-up resistors by default. The file you need to modify is located at ..\arduino-1.0\libraries\Wire\utility\twi.c See below appendices for more information.

#define BUFSIZ 4

#include <MIDI.h>
#include <Wire.h>

byte dat[BUFSIZ]={-1,-1,-1,-1};
boolean boo=false;

void setup()
{
Wire.onReceive(ReceiveEvent);
Wire.onRequest(RequestEvent);
Wire.begin(23);
MIDI.turnThruOff();
MIDI.begin(MIDI_CHANNEL_OMNI);

}

void loop()
{
boo=MIDI.read(11);
if(boo && MIDI.getType()!=ActiveSensing)
{
dat[0]=(byte)MIDI.getType();
dat[1]=MIDI.getChannel();
dat[2]=MIDI.getData1();
dat[3]=MIDI.getData2();
}
}

void ReceiveEvent(int howMany)
{
Wire.read();
}

void RequestEvent(void)
{
Wire.write(dat,BUFSIZ);
dat[0]=-1;
} // end of Arduino sketch code







Appendices
The Arduino library you need to modify is located at ..\arduino-1.0\libraries\Wire\utility\twi.c Find and comment-out the following segment of code:

// activate internal pullups for twi.
// digitalWrite(SDA, 1);
// digitalWrite(SCL, 1);


For more information on how to install gcc and compile C programs, visit http://www.ladyada.net/learn/chumby/compiler.html. Look around the website for other useful information as well.

I also have Python 2.6 running on a Chumby Hacker as well, if it interests you.

This is all posted on Google Docs @ https://docs.google.com/document/d/1LRJ ... __g7c/edit

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”