Low Layer API for I2C Communication in Stm32L031
Hi,
I have created a code snippet to communicate with I2C using LL API instead of HAL. The code is given below
uint8_t temp[2]={0xF9,0x00};
while(LL_I2C_IsActiveFlag_BUSY(I2C1)); // Wait until the I2C bus is not busy
LL_I2C_HandleTransfer(I2C1, 0x94, LL_I2C_ADDRSLAVE_7BIT, 2, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
LL_mDelay(10);
for (int i = 0; i < 2; i++) {
while (!LL_I2C_IsActiveFlag_TXE(I2C1)); // Wait until the data register is empty
if (LL_I2C_IsActiveFlag_NACK(I2C1)) { // Check for NACK (Not Acknowledged)
return 1;
}
LL_I2C_TransmitData8(I2C1, temp[i]); // Transmit data byte
LL_mDelay(10);
LL_I2C_ClearFlag_TXE(I2C1);
}
LL_I2C_GenerateStopCondition(I2C1);
LL_mDelay(10);
LL_I2C_ClearFlag_STOP(I2C1); // Clear the stop flag
// Reception (Rx)
LL_I2C_HandleTransfer(I2C1, 0x94, LL_I2C_ADDRSLAVE_7BIT, 16, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_READ);
int i = 0;
// Wait for the stop condition or reception of 16 bytes
while (!LL_I2C_IsActiveFlag_STOP(I2C1)) {
if (LL_I2C_IsActiveFlag_RXNE(I2C1)) { // Check if data is received
Received[i++] = LL_I2C_ReceiveData8(I2C1); // Store received data
if (i >= 16) break; // Exit if 16 bytes have been received
}
}
// Ensure stop flag is cleared after reception is complete
if (LL_I2C_IsActiveFlag_STOP(I2C1)) {
LL_I2C_ClearFlag_STOP(I2C1);
}
i am able to transmit the data, but failed to read from the buffer. Anyone with prior experience in LL I2C can give me solution for this.
