MMA8452, reads returning 0xFF

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MMA8452, reads returning 0xFF

Jump to solution
1,365 Views
keefw2000
Contributor I

Hello, I am using the MMA8452Q Sparkfun Breakout board https://www.sparkfun.com/products/14587 connected to an STM32F401RE Nucleo board. I used the STM32CubeMX default configuration and just added an I2C bus to the setup. All peripheral initializations are auto-generated by CubeMX.

On the logic analyzer, I am able to write and read with the appropriate ACKs and NACKs, but the reads all return 0xFF. I read through the other forum posts, but was unable to get my device to read properly.

Any help would be much appreciated.

#define MMA8452Q_DEFAULT_ADDRESS 0x3A

uint8_t I2C_ByteRead(uint8_t reg)
{
   uint8_t result = 0;
   uint8_t dataBuffer[1]={0};
   //Specify the register to read from
   if(HAL_I2C_Master_Transmit(&hi2c2), MMA8452Q_DEFAULT_ADDRESS, &reg,
                             1, 100)==HAL_OK)
   {
      if(HAL_I2C_Master_Receive(&hi2c2, MMA8452Q_DEFAULT_ADDRESS, dataBuffer,
                                1, 100)==HAL_OK)
      {
         result = dataBuffer[0];
      }
   }
   return result;
}
Labels (1)
0 Kudos
1 Solution
1,350 Views
TomasVaverka
NXP TechSupport
NXP TechSupport

Hi Kayla,

 

Looking at your screenshot, there is the stop condition between the second (0x0D) and third byte (0x3B). This is incorrect.

 

You will likely need to modify the HAL_I2C_Master_Transmit() function as it sends the stop condition after the data (0x0D). So you need to change it so that SDA transitiones to high before the SCL goes high in order to eliminate this stop condition.

 

The transaction should look as follows:

 

1. Send a start sequence

2. Send 0x3A // MMA8452Q slave address with the R/W bit low or 0x1D << 1

3. Send 0x0D // Address of the WHO_AM_I register

4. Send a start sequence again (repeated start)

5. Send 0x3B // MMA8452Q slave address with the R/W bit high or (0x1D << 1) | 0x01

6. Read the data byte from WHO_AM_I register

7. Send NAK and the stop sequence

 

Here is an example for the MMA8652FC (Device ID = 0x4A) as I do not have access to the MMA8452Q right now.

 

Who_Am_I+register+read.jpg

 

I hope it helps!

 

Best regards,

Tomas

 

View solution in original post

0 Kudos
2 Replies
1,351 Views
TomasVaverka
NXP TechSupport
NXP TechSupport

Hi Kayla,

 

Looking at your screenshot, there is the stop condition between the second (0x0D) and third byte (0x3B). This is incorrect.

 

You will likely need to modify the HAL_I2C_Master_Transmit() function as it sends the stop condition after the data (0x0D). So you need to change it so that SDA transitiones to high before the SCL goes high in order to eliminate this stop condition.

 

The transaction should look as follows:

 

1. Send a start sequence

2. Send 0x3A // MMA8452Q slave address with the R/W bit low or 0x1D << 1

3. Send 0x0D // Address of the WHO_AM_I register

4. Send a start sequence again (repeated start)

5. Send 0x3B // MMA8452Q slave address with the R/W bit high or (0x1D << 1) | 0x01

6. Read the data byte from WHO_AM_I register

7. Send NAK and the stop sequence

 

Here is an example for the MMA8652FC (Device ID = 0x4A) as I do not have access to the MMA8452Q right now.

 

Who_Am_I+register+read.jpg

 

I hope it helps!

 

Best regards,

Tomas

 

0 Kudos
1,347 Views
keefw2000
Contributor I

Hi Tomas,

Thank you for the suggestion.

I will take a look at that and let you know how it goes.

Best,

Kayla

0 Kudos