Two questions on I2C flags SB and BTF in STM32F446RE
Hi, I'm stepping through the code on Nucleo-F446RE board and doing the following:
LL_I2C_GenerateStartCondition(I2C1);
step++;
while(!LL_I2C_IsActiveFlag_SB(I2C1));
LL_I2C_TransmitData8(I2C1,BMP180_ADDRESS_WRITE);
step++;
while(!LL_I2C_IsActiveFlag_ADDR(I2C1));
LL_I2C_ClearFlag_ADDR(I2C1);
LL_I2C_TransmitData8(BMP180_REG_OUT);
step++;
while(!LL_I2C_IsActiveFlag_TXE(I2C1));
step++;
LL_I2C_GenerateStartCondition();
step++;The documentation says that SB flag is "Cleared by software by reading the SR1 register followed by writing the DR register, or by hardware when PE=0"
But at the before the 5th line of my code the debugger shows the SB flag is still 0b1, but LL_I2C_IsActiveFlag_SB reads the SR1 register, and LL_I2C_TransmitData8 is writing to DR register. So why is the flag still set? Am I missing anything here?
And my second question is about BTF flag.
Again documentation states that BTF flag is
"– Set by hardware when NOSTRETCH=0 and:
– In reception when a new byte is received (including ACK pulse) and DR has not been read
yet (RxNE=1).
– In transmission when a new byte should be sent and DR has not been written yet (TxE=1).
– Cleared by software by either a read or write in the DR register or by hardware after a start or
a stop condition in transmission or when PE=0.
Note: The BTF bit is not set after a NACK reception
The BTF bit is not set if next byte to be transmitted is the PEC (TRA=1 in I2C_SR2
register and PEC=1 in I2C_CR1 register)"
So why would I use TXE and RNXE flags instead of BTF? Shouldn't BTF be universal way of saying that transmission or receiving where complete? Or is the difference only in the NACK at the reception, ie. TXE might be set after transmission, but BTF will be only if ACK is received?
But then in line 10 of my code above I used to check BTF flag which caused this part of the code to hang, whereas switching to checking TXE flag makes is run just fine. This got me to the point where I just got absolutely lost why reading the former ended up in such behaviour.
I would be grateful if someone could explain the difference. Thank you!
The quotes are from RM0390 chapter 24, but it after reading it I can't grasp the difference on my own.
