cancel
Showing results for 
Search instead for 
Did you mean: 

Two questions on I2C flags SB and BTF in STM32F446RE

kj.obara
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions

> 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?

Maybe the debugger shows a cached value. Read SR1 in software.

But maybe not. I never checked this.

> BTF

I dont' know, I don't use it. The "older" I2C module as is in the 'F4 is overtly complex and quirky; I wouldn't be surprised if there would be numerious deviations from the description in the RM. Some of them may be in the errata - did you check them?

JW

View solution in original post

3 REPLIES 3

> 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?

Maybe the debugger shows a cached value. Read SR1 in software.

But maybe not. I never checked this.

> BTF

I dont' know, I don't use it. The "older" I2C module as is in the 'F4 is overtly complex and quirky; I wouldn't be surprised if there would be numerious deviations from the description in the RM. Some of them may be in the errata - did you check them?

JW

Hi JW, yes I also have Nucleo=F767 which I haven't used yet and I noticed I2C module is significantly different.

For F446RE I'm using RM0390 Revision 4 from February 2018.

Since you mentioned errata - I found this https://www.st.com/resource/en/errata_sheet/dm00155929-stm32f446xcxe-device-limitations-stmicroelectronics.pdf

But it's older than the reference manual. It mentions 7 bugs in I2C implementation, but it wouldn't seem as any of them directly answers my question. Nothing on TXE and BTF per my quick skim through the I2C section.

Although one of them related to repeated start issue seems vaguely similar to a quirk I had (that's not related to this question, however).

Since you mention older I2C module - is there any place you could direct me to see some history of changes that STM did to their lineup? I'm just curious what quirks await me with F446RE. Or would you care to suggest a Nucleo board that in your opinion is a more decent one that that?

Since you mention older I2C module - is there any place you could direct me to see some history of changes that STM did to their lineup?

No. We are asking ST to provide this, among many other things, in the annual wishlist (which for 2020 is nowhere yet).

JW