Problem configuring I2C on CMWX1ZZABZ (that is to say STM32L072CZ) for accelerometer MMA8652.
Hi there! I am actually fighting again i2c communication on the B-L072Z-LRWAN1 evaluation board (STM32L072CZ). Basically I would like to interface a NXP MMA8652 accelerometer over the I2C bus. I was able to do that (get the "who am I" register, get x/y/z acceleration values) with an Arduino board but could not get it properly run on the ST. I can actually see the 100kHz SCL/SDA on the scope but the accelerometer only answers 0xff to all requests.
I used CubeMX to set the board, (I2C base freq is 2,048MHz), and based my code on the "I2C_TwoBoards_AdvComIT" example from ST.
The code is pretty simple, IT based.
Init:
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x00000708;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}Read:
uint8_t MMA8652_read(uint8_t address)
{
uint8_t aTransferRequest[2];
// Ask
// ##-5- Master sends read request for slave ##############################
do
{
if(HAL_I2C_Master_Transmit_IT(&hi2c1, (uint16_t)I2C_ADDRESS, (uint8_t*)&address, 1)!= HAL_OK)
{
Error_Handler();
}
while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
{
}
}
while(HAL_I2C_GetError(&hi2c1) == HAL_I2C_ERROR_AF);
// Get
//##-7- Master receives aRxBuffer from slave #############################
do
{
if(HAL_I2C_Master_Receive_IT(&hi2c1, (uint16_t)I2C_ADDRESS, (uint8_t*)aTransferRequest, 1)!= HAL_OK)
{
Error_Handler();
}
while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
{
}
}
while(HAL_I2C_GetError(&hi2c1) == HAL_I2C_ERROR_AF);
return aTransferRequest[0];
}Here is the "detect accelerometer" function: the MMA8652 (address 0x1D) has a "who_am_i" register (address 0x0D) that should contains 0x4A.
From Arduino:

From the ST:

Did I miss something?
Thanks!
Bezac
