Atomicity of STM32 register writes

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,652
Given that most of these registers are just collections of bits, the question arises of the validity of updating these bit sets one at a time, e.g.
C:
ctrl.ENABLE = 1;
ctrl.TICKINT = 1;
ctrl.CLKSOURCE = 1;
The generated code does its own AND/OR for these, but after each one the updated 32 bit value is written.

So inside the register itself we'd see 100 then 110, then 111 as each bit is set.

This begs the question, does the register get into some invalid intermediate state as we do this?

I suspect its not a problem, because all of the HAL code does two separate writes usually, one after its done the AND (to set bits off) and then another after its done the OR and the state in between could also be potentially invalid.

Of course we can easily copy a reg to a temp, setup that temp then write the temp to the reg if we want, but the HAL code I look at doesn't seem to care about doing this except in special cases like the AIRCR register in the System Control Block.

Thoughts...
 

BobTPH

Joined Jun 5, 2013
9,116
Generally, you disable the peripheral when updating its control registers. So the intermediate results would have no affect.
 

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,652
Generally, you disable the peripheral when updating its control registers. So the intermediate results would have no affect.
OK that's sensible sounding, the SYSTICK counter can be enabled and disabled so that should be used when updating that.

Thanks
 
Top