Electronic – STM32F10xxx ODR register atomic write

microcontrollerstm32

I'm attending a university course on real time systems and we are using the STM32F10xxx MCU family for labs and examples. Every GPIO port has an Output Data Register (ODR) and a set/reset register (BSRR). In the manual it says that BSRR should be used to atomically set/reset single bits, "so there is no risk an IRQ occurs between the read and modify access".
If I understand this statement correctly it means that if I do something like GPIOB->ODR |= (1UL<<3) a read and write on the ODR is necessary, so an interrupt might occur in between and, to avoid that, it would be better to use BSRR to set the relevant bits… am I right?

The question is: what if I want to set all the pins on the ODR register in an atomic way? For example does the instruction GPIOB->ODR = 0x00000002 run atomically? In this case there is no need to read the value of the register, I'm simply setting all the bits to a specific value… is this write operation atomical or can it be interrupted? The only thing I found on the manual is "the I/O port registers have to be accessed as 32-bit words", but I don't know if this implies something about atomicity…

Can you help me? I'm completely new to this, so forgive me if the answer is trivial. I've found a lot of similar questions, but none of them clarified my doubts.

Thanks.

Best Answer

BSRR performs atomic operations on ODR. One half sets and the other half resets the bits in ODR.

I wrote this from memory so the polarity might be reversed, but the idea is the same.

For example this two statements are functionally equivalent, with the exception of BSRR write being atomic:

GPIOB->ODR |= 1UL<<3;
GPIOB->BSRR = 1UL<<3;

similarly this two:

GPIOB->ODR &= ~(1UL<<3)
GPIOB->BSRR = 1UL<<(3 + 16)