AnsweredAssumed Answered

MMA8451 on Raspberry Pi returns incorrect whoami

Question asked by Tim Nealon on May 30, 2018
Latest reply on May 31, 2018 by Tomas Vaverka

I'm currently working on connecting and reading an MMA8451 with a raspberry pi running C code. I've got the code up and running, and i can't find any reason why the accelerometer should be returning an incorrect whoami (currently sending 0xff). Here is the code i use to initialize the accelerometer:

 

 

mma8451 mma8451_initialise(int device int addr)
{
mma8451 handle;
handle.file = -1;
handle.address = addr;
char buf[15];
printf("Initalising MMA8451 sensor at address %#02x\n", addr);
//Open /dev/i2c-x file without a buffer
sprintf(buf, "/dev/i2c-%d", device);
if ((handle.file = open(buf, O_RDWR)) < 0)
{
handle.file = -2;
return handle;
}
//Configure slave i2c address via ioctl
if(ioctl(handle.file, I2C_SLAVE, addr) < 0)
{
handle.file = -3;
return handle;
}
//Check if we read correctly from the sensor
char whoami = mma8451_read_byte(&handle, 0x0D);
printf("whoami read %#02x\n", whoami);
//Undefined behavior for the rest of device operation if the device is not returning hex 1A
if(whoami != 0x1A) perror("mma451_pi warning: Device correctly intialized but not returning 0x1A at WHO_AM_I request.\n"
"Are you sure you are using a MMA8451 accelerometer on this address?");
//Send reset request
mma8451_write_byte(&handle, 0x2B, 0x40);
printf("Waiting for accelerometer to be reset\n");
while(mma8451_read_byte(&handle, 0x2B) & 0x40); //reset done
printf("Done\n");
mma8451_set_range(&handle, 2);
mma8451_write_byte(&handle, 0x2B, 0x02); //high resolution mode
mma8451_write_byte(&handle, 0x2A, 0x01 | 0x04); //high rate low noise
//Deactivate fifo
mma8451_write_byte(&handle, 0x09, 0);
//turn on orientation configuration
mma8451_write_byte(&handle, 0x11, 0x40);
printf("MMA8451 at address %#02x configured for real time sampling, in high rate, low noise mode, at high resolution, on a 2G max range\n", addr);
return handle;

}

This is directly from the mma8451_pi library found here: https://github.com/Ybalrid/mma8451_pi

 

Any ideas what could be causing it to return an incorrect whoami?

Outcomes