I2C Read/Write problem

684 views
Skip to first unread message

Ernesto Torres

unread,
Feb 18, 2009, 11:21:11 AM2/18/09
to Beagle Board
Hi,

I'm working with the i2c2 from the beagle board, and I wrote a program
for user-space, the header used is "i2c-dev.h" from lm sensors.

The hardware I use requires the following:

Write:
Start->Chip address+write->Register address(msb)->Register address
(lsb)->Data to write->Stop

Read:
Start->Chip address+write->Register address(msb)->Register address
(lsb)->Chip address+read->Data to read->Stop

I'm using ioctl with I2C_RDWR, no error is shown, but the values read
doesn't match with the ones written(0x00 is alway read). Maybe I'm
missing something that is not allowing the correct i2c bus
communication.

The i2c2 bus was previously enabled.

The program acts this way:
1. Open file descriptor for i2c2
2. Bind I2C slave address(ioctl with I2C_SLAVE)
3. Write
4. Read
5. Close

Thanks for your help,
Ernesto

This is the code i'm using:
------------------------------------------------------------------

#include <fcntl.h>

#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "i2c-dev.h"

#define I2C_DEVICE "/dev/i2c-2"
#define I2C_SLAVE_ADDR 0x38
#define I2C_REGISTER 0x000A


void write_msg(int file, char msb, char lsb, char value)
{
char wbuf[5] = {0};
wbuf[0] = msb; // Register MSB
wbuf[1] = lsb; // Register LSB
wbuf[2] = value; // value write
if ( write(file,wbuf,3) != 3)
{
printf("Writing Error Register\n");
}
else
{
printf("Writing Successful on register\n");
}
}

int main()
{
int file = 0;
int adapter_nr = 2; /* probably dynamically determined */
char filename[20] = {0};
int addr = 0x38; /* The I2C address */

sprintf(filename,"/dev/i2c-%d",adapter_nr);
printf("Device File: %s\n",filename);
printf("Device Address(I2C): 0x%02x\n",addr);

//OPEN DEVICE
if ((file = open(filename,O_RDWR)) < 0)
{
printf("Error opening device\n");
exit(1);
}
else {
printf("Device Opened Successfully \n");
}

//BIND SLAVE ADDRESS
if (ioctl(file,I2C_SLAVE,addr) < 0) {
printf("ioctl error\n");
}
//WRITE
write_msg(file, 0x00, 0x0A, 0x01);//file, msb, lsb, value
//READ
char rbuf[5] = {0};//read buffer
char wbuf[5] = {0};//write buffer

wbuf[0] = 0x00;//reg MSB
wbuf[1] = 0x0A;//reg LSB

struct i2c_msg m[2]={0};

m[0].len = 2;//2 byte write
m[0].flags = 0;//prepare to write
m[0].addr = addr;//slave addr
m[0].buf = wbuf;//write buffer

m[1].len = 1;//1 byte read
m[1].flags = I2C_M_RD;//prepare to read
m[1].addr = addr;//slave addr
m[0].buf = rbuf;//read buffer

struct i2c_rdwr_ioctl_data rdwr;//Message container
rdwr.msgs= m;//first msg
rdwr.nmsgs=2;// number of msgs

if (ioctl(file,I2C_RDWR,&rdwr) < 0) {
printf("Could not read \n");
}
else
printf("Result %02x\n",rbuf[0]);
//CLOSE DEVICE
int err = close(file);
if(err != 0){
printf("Error Occured while closing Device File.!!\n");
}
else{
printf("Device File Closed Successfully !!\n");
}

return 0;
}

Ernesto Torres

unread,
Feb 18, 2009, 11:23:28 AM2/18/09
to Beagle Board
sry, one modification was made, to the code

m[0].buf = rbuf;//read buffer is actually
m[1].buf = rbuf;//read buffer

Ernesto Torres

unread,
Feb 19, 2009, 12:36:47 PM2/19/09
to Beagle Board
Any suggestion anyone on why no errors are displayed but read/write
isn't working?

Ernesto Torres

unread,
Mar 2, 2009, 7:18:02 PM3/2/09
to Beagle Board
The code is fully functional, the only problem was the address from
the register
Reply all
Reply to author
Forward
0 new messages