Hi I am trying to read and write data from the i2c bus on the beagle bone black. But I keep reading 0x00 whenever I try to access the Who Am I register on a MMA84152 (or any other register for that matter), which is a constant register which means its value does not change. I am trying to read i2c-1 character driver located in /dev and I connect the sda and scl of the MMA852 lines to pins 19 and 20 on the p9 header. The sda and scl lines are both pulled high with 10 k resistors. Both pin 19 and pin 20 show 00000073 for their pin mux which means it is set for i2c functionality and slew control is slow, reciever is active, the pin is using a pull up resistor, and pull up is enabled. I ran i2cdetect -r 1 and my device shows up as 0x1d which is its correct address. I also ran i2cdump 1 0x1d and 0x2a shows up under 0x0d which is the register I am trying to read from my device and contains the correct value according to the datasheet. But when I read it it returns 0x00 for me. I am also running the latest angstrom distribution and I am logged in under root so no need for sudo.I'm lost now. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string>
using namespace std;
int main(int argc, char **argv){
int X_orientation=0;
char buffer1[256];
string i2cDeviceDriver="/dev/i2c-1";
int fileHandler;
if((fileHandler=open(i2cDeviceDriver.c_str(),O_RDWR))<0){
perror("Failed To Open i2c-1 Bus");
exit(1);
}
if(ioctl(fileHandler,I2C_SLAVE,0x1d)<0){
perror("Failed to acquire i2c bus access and talk to slave");
exit(1);
}
char buffer[1]={0x0D};
if(write(fileHandler,buffer,1)!=1){
perror("Failed to write byte to accelerometer");
exit(1);
}
if(read(fileHandler,buffer1,1)!=1){
perror("Failed to read byte from accelerometer");
exit(1);
}
printf("Contents of WHO AM I is 0x%02X\n",buffer1[0]);
}