Howto read I2C on MPC5200 Lite
Matthias Fechner
idefix at fechner.net
Wed Mar 7 23:24:30 EST 2007
Hi,
I started playing with I2C on my MPC5200 Lite board now.
For this a attached a MAX6633 to the I2C bus.
With u-boot I can read the chip so I think in hardware is everything ok.
Now I booted my board and loaded the module i2c-mpc and i2c-dev.
Create the devices with major number 89 and minor number 0 and 1.
I checked now for the kernel docu the file dev-interface.
They suggested to use i2c_smbus_read_word_data, but with this I was
not successfully.
So i tried to open the device and read with read from the device, but
no success.
Here is the final code i tried:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
int main(void)
{
int file;
int adapter_nr=0;
char filename[20];
// open connection to device driver
sprintf(filename,"/dev/i2c-%d",adapter_nr);
if((file=open(filename,O_RDWR)) < 0)
{
printf("Cannot open i2c device\n");
exit(1);
}
// open connection to I2C device with address addr
int addr=0x40;
if(ioctl(file,I2C_SLAVE,addr) <0)
{
printf("Cannot access slave device\n");
exit(1);
}
// comunicate with the I2C device
// device register to access
__u8 memToRead = 0x0;
__s32 res;
char buf[10];
if(read(file,buf,1) != 1)
{
printf("Cannot read from device\n");
}else{
printf("Got %s\n",buf[0]);
}
close(file);
return 0;
}
But I always got Cannot read from device.
Can anyone help me here please?
Best regards,
Matthias
--
"Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the universe trying to
produce bigger and better idiots. So far, the universe is winning." --
Rich Cook
More information about the Linuxppc-embedded
mailing list