Hello John,
Thank you for writing.
Unfortunately, it is not possible to read the FIFO of the MMA8451Q the way that you want. It is needed to read all the FIFO using the repeat start condition. If you could not fill the FIFO in the correct way, you could try to disable the FIFO and read the data one by one.
Furthermore, I am going to copy a code that I made. What the code does is fill and read the full FIFO from the MMA8451Q.
uint8_t FIFO_DataReady=0;
uint8_t FIFO_Status;
unsigned char AccelData[6*Watermark_Val];
short Xout_Accel_14_bit[Watermark_Val], Yout_Accel_14_bit[Watermark_Val], Zout_Accel_14_bit[Watermark_Val];
float Xout_g[Watermark_Val], Yout_g[Watermark_Val], Zout_g[Watermark_Val];
void MMA8451_init();
void MCU_init();
int main(void){
int counter = 0;
uint8_t i=0;
MCU_init();
MMA8451_init();
for(;;) {
if (FIFO_DataReady)
{
FIFO_DataReady = 0;
FIFO_Status = I2C_ReadRegister(MMA845x_I2C_ADDRESS, STATUS_REG);
I2C_ReadMultiRegisters(MMA845x_I2C_ADDRESS, OUT_X_MSB_REG, 6*Watermark_Val, AccelData);
for (i = 0; i < Watermark_Val; i++)
{
Xout_Accel_14_bit[i] = ((short) (AccelData[0 + i*6]<<8 | AccelData[1 + i*6])) >> 2;
Yout_Accel_14_bit[i] = ((short) (AccelData[2 + i*6]<<8 | AccelData[3 + i*6])) >> 2;
Zout_Accel_14_bit[i] = ((short) (AccelData[4 + i*6]<<8 | AccelData[5 + i*6])) >> 2;
Xout_g[i] = ((float) Xout_Accel_14_bit[i]) / SENSITIVITY_2G;
Yout_g[i] = ((float) Yout_Accel_14_bit[i]) / SENSITIVITY_2G;
Zout_g[i] = ((float) Zout_Accel_14_bit[i]) / SENSITIVITY_2G;
}
}
counter++;
}
return 0;
}
void MCU_init()
{
SIM_SCGC4 |= SIM_SCGC4_I2C0_MASK;
SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK;
PORTE_PCR24 = PORT_PCR_MUX(5);
PORTE_PCR25 = PORT_PCR_MUX(5);
I2C0_F = 0x14;
I2C0_C1 = I2C_C1_IICEN_MASK;
SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK;
PORTA_PCR14 |= (0|PORT_PCR_ISF_MASK|
PORT_PCR_MUX(0x1)|
PORT_PCR_IRQC(0xA));
NVIC_ICPR |= 1 << ((INT_PORTA - 16)%32);
NVIC_ISER |= 1 << ((INT_PORTA - 16)%32); }
void MMA8451_init()
{
unsigned char reg_val = 0;
I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG2, 0x40);
do
{
reg_val = I2C_ReadRegister(MMA845x_I2C_ADDRESS, CTRL_REG2) & 0x40;
} while (reg_val);
I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG1, 0x00);
I2C_WriteRegister(MMA845x_I2C_ADDRESS, F_SETUP_REG, 0xA0);
I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG4, 0x40);
I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG5, 0x40);
I2C_WriteRegister(MMA845x_I2C_ADDRESS, XYZ_DATA_CFG_REG, 0x00);
I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG2, 0x02);
I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG1, 0x3D);
}
void PORTA_IRQHandler()
{
PORTA_PCR14 |= PORT_PCR_ISF_MASK;
FIFO_DataReady = 1;
}
P.S. I am using the FRDM-KL25Z microcontroller.