2020-10-11 7:07 AM
Hello,
I am trying to implement a low level I2C readbyte function, currently trying to read out register 0x41 (TEMP_H) and 0x42 (TEMP_L) from an MPU9250. The issue is that when I debug print the values, it seems that TEMP_H_Buffer appears to have the data from TEMP_L and vica versa (values are swapped)
Here is the function:
uint8_t I2C_readByte(I2C_TypeDef *I2Cx, uint8_t u8_registerAdress, uint8_t u8_slaveAdress, uint8_t *buffer)
{
// Generate Write Request
LL_I2C_HandleTransfer(I2Cx, u8_slaveAdress << 1, LL_I2C_ADDRSLAVE_7BIT, 1, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
while(!LL_I2C_IsActiveFlag_STOP(I2Cx))
{
if(LL_I2C_IsActiveFlag_TXE(I2Cx))
{
LL_I2C_TransmitData8(I2Cx, u8_registerAdress);
}
}
LL_I2C_ClearFlag_STOP(I2Cx);
//Generate Read Request
LL_I2C_HandleTransfer(I2Cx, u8_slaveAdress << 1, LL_I2C_ADDRSLAVE_7BIT, 1,
LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_READ);
while(!LL_I2C_IsActiveFlag_STOP(I2Cx))
{
if(LL_I2C_IsActiveFlag_RXNE(I2Cx))
{
*buffer = LL_I2C_ReceiveData8(I2Cx);
}
}
LL_I2C_ClearFlag_STOP(I2Cx);
return 0;
}And here is the main function implementation in freeRTOS:
void Start_I2CTask(void *argument)
{
/* USER CODE BEGIN Start_I2CTask */
uint8_t u8_slaveAdress = 0x68;
uint8_t TEMP_H = 0x41;
uint8_t TEMP_L = 0x42;
uint8_t TEMP_H_Val[] = {0};
uint8_t TEMP_L_Val[] = {0};
/* Infinite loop */
uint8_t buffer[64];
for(;;)
{
//I2C_readByte(I2C_TypeDef *I2Cx, uint8_t u8_registerAdress, uint8_t u8_slaveAdress, uint8_t *buffer);
I2C_readByte(I2C1, TEMP_H, u8_slaveAdress, TEMP_H_Val);
sprintf(buffer, "Value in register H is : %d \n \r", TEMP_H_Val[0]);
HAL_UART_Transmit(&huart3, buffer, strlen(buffer), 100);
osDelay(1000);
I2C_readByte(I2C1, TEMP_L, u8_slaveAdress, TEMP_L_Val);
sprintf(buffer, "Value in register L is : %d \n \r", TEMP_L_Val[0]);
HAL_UART_Transmit(&huart3, buffer, strlen(buffer), 100);
osDelay(1000);
}
/* USER CODE END Start_I2CTask */
}However, when I look at the serial output I see this:
Using WaveForms I capture following sequences (all seems normal to me)
TEMP_H:
TEMP_L:
It seems that old data from the I2C buffer is somehow transfered to the next read?
Anyone know what the issue could be?
Thanks in advance,
Sebastiaan
We’re moving the ST Community to a new platform to give you a better and more reliable community experience.