Solved
LL Driver For I2C while integrating BN0086
Hi
I am using STM32L031F6 microcontroller and trying to read data from BNO086. The HAL driver i2c code is working fine but LL driver code doens't work. I want to use LL driver for i2c. Following is my LL driver code for reading and writing.
Read Function:
void LL_I2C_Master_Receive(uint8_t i2cAddr, uint8_t *aReceiveBuffer, uint16_t size)
{
uint32_t startTime = SysTick_Get(); // Get the current SysTick value
LL_I2C_HandleTransfer(I2C1, (i2cAddr<<1), LL_I2C_ADDRSLAVE_7BIT, size, LL_I2C_MODE_RELOAD, LL_I2C_GENERATE_START_READ);
if(LL_I2C_GetAddressMatchCode(I2C1) == i2cAddr << 1)
{
/* Verify the transfer direction, a write direction, Slave enters receiver mode */
if(LL_I2C_GetTransferDirection(I2C1) == LL_I2C_DIRECTION_READ)
{
/* Clear ADDR flag value in ISR register */
LL_I2C_ClearFlag_ADDR(I2C1);
}
else
{
/* Clear ADDR flag value in ISR register */
LL_I2C_ClearFlag_ADDR(I2C1);
}
}
else
{
/* Clear ADDR flag value in ISR register */
LL_I2C_ClearFlag_ADDR(I2C1);
}
// /* (2) Loop until end of transfer received (STOP flag raised) ***************/
while(!LL_I2C_IsActiveFlag_STOP(I2C1))
{
if(LL_I2C_IsActiveFlag_RXNE(I2C1))
{
aReceiveBuffer[ubReceiveIndex++] = LL_I2C_ReceiveData8(I2C1);
}
}
/* (3) Clear pending flags, Check Data consistency **************************/
LL_I2C_ClearFlag_STOP(I2C1);
}
Write function:
void LL_I2C_Master_Transmit(uint8_t i2cAddr, const uint8_t *pData, uint16_t size)
{
uint32_t startTime = SysTick_Get(); // Get the current time from SysTick or another timer
/* (1) Initiate a Start condition to the Slave device ***********************/
LL_I2C_HandleTransfer(I2C1, (i2cAddr << 1), LL_I2C_ADDRSLAVE_7BIT, size, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
/* (2) Loop until end of transfer received (STOP flag raised) ***************/
while (!LL_I2C_IsActiveFlag_STOP(I2C1))
{
// Check TXIS flag value in ISR register to ensure we can transmit data
if (LL_I2C_IsActiveFlag_TXE(I2C1))
{
if (size > 0) // Ensure there is data left to transmit
{
LL_I2C_TransmitData8(I2C1, *pData++); // Transmit data
size--; // Decrease the number of bytes to transmit
}
}
}
/* (3) Clear pending flags, Data consistency are checking into Slave process */
LL_I2C_ClearFlag_STOP(I2C1); // Clear the STOP flag to complete the I2C communication
}
But the data is not properly Receiving, if any Mistake in these code? Does LL actually work with I2C?
