I am using imx6qsabresd board.
I want to develop user space application to access on board i2c based mma8451q 3-Axis Orientation/Motion Detection Sensor.
I am using eclipse for development.
Below is my code for that.
#include <stdlib.h>
#include <stdio.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#define SLAVE_ADDRESS 0x1D
// I2C Linux device handle
int g_i2cFile;
// open the Linux device
void i2cOpen()
{
g_i2cFile = open("/dev/i2c-0", O_RDWR);
if (g_i2cFile < 0) {
perror("i2cOpen");
puts("I2C Open Failed");
exit(1);
}
else
puts("I2C Open Success");
}
// close the Linux device
void i2cClose()
{
close(g_i2cFile);
}
// set the I2C slave address for all subsequent I2C device transfers
void i2cSetAddress(int address)
{
if (ioctl(g_i2cFile, I2C_SLAVE, address) < 0) {
perror("i2cSetAddress");
puts("I2C Set Address Failed");
exit(1);
}
else
puts("I2C Set Address Success");
}
int main(void) {
unsigned char res,i;
puts("MMA8451Q Accelerometer Test Program"); /* prints Hello World */
i2cOpen();
i2cSetAddress(SLAVE_ADDRESS);
res = i2c_smbus_read_byte_data(g_i2cFile, 0x0D);
printf("Result= %d\n\r\n\r", res);
i2cClose();
return 0;
}
First function i2c_smbus_read_byte_data() cannot be found by eclipse.
So i searched around and found solution. I run "sudo apt-get install libi2c-dev" and replace i2c_dev.h in "/opt/poky/1.8/sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi/usr/include/linux" from newly created i2c_dev.h in "/usr/include/linux".
Now i can successfully compile the code.
So is this a right step?
And the output of the code is
MMA8451Q Accelerometer Test Program
I2C Open Success
I2C Set Address Success
Result= 255
It should be 0x1A (Device ID).
I am getting 255 result fro all the address.
So whats wrong in this code?