cancel
Showing results for 
Search instead for 
Did you mean: 

LL Driver For I2C while integrating BN0086

Anusree_k
Associate II

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Imen.D
ST Employee

Sorry, I2C example with LL driver is for NUCLEO-L073RZ:

STM32CubeL0/Projects/NUCLEO-L073RZ/Examples_LL/I2C at master · STMicroelectronics/STM32CubeL0 · GitHub

 

For NUCLEO-L031K6 board, you can use the project LL template provided here:

STM32CubeL0/Projects/NUCLEO-L031K6/Templates_LL at master · STMicroelectronics/STM32CubeL0 · GitHub and follow the readme file to build your application.

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

View solution in original post

4 REPLIES 4
Imen.D
ST Employee

Hello @Anusree_k ,

You can follow the I2C examples (LL driver) provided in the STM32CubeL0 firmware package for the NUCLEO-L031K6 board:

STM32CubeL0/Projects/NUCLEO-L031K6/Examples/I2C at master · STMicroelectronics/STM32CubeL0 · GitHub

This will help you on your project implementation.

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

Hello,

I didn't see the LL Driver under the link you suggested. I checked the link, but it uses HAL for the I2C. Could you please verify this?

Imen.D
ST Employee

Sorry, I2C example with LL driver is for NUCLEO-L073RZ:

STM32CubeL0/Projects/NUCLEO-L073RZ/Examples_LL/I2C at master · STMicroelectronics/STM32CubeL0 · GitHub

 

For NUCLEO-L031K6 board, you can use the project LL template provided here:

STM32CubeL0/Projects/NUCLEO-L031K6/Templates_LL at master · STMicroelectronics/STM32CubeL0 · GitHub and follow the readme file to build your application.

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Saket_Om
ST Employee

Hello @Anusree_k 

 

what is the size of the data to be read? 

The reload mode is used when the data size to be read is greater than 255, however it is mandatory to call LL_I2C_HandleTransfer with LL_I2C_MODE_AUTOEND mode for the last amount of data less than 255 bytes. 

 

please refer to this post for more details 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar