Why is Read-Modify-Write necessary for registers on embedded systems?
- by Adam Shiemke
I was reading http://embeddedgurus.com/embedded-bridge/2010/03/different-bit-types-in-different-registers/, which said:
With read/write bits, firmware sets and clears bits when needed. It typically first reads the register, modifies the desired bit, then writes the modified value back out
and I have run into that consrtuct while maintaining some production code coded by old salt embedded guys here. I don't understand why this is necessary.
When I want to set/clear a bit, I always just or/nand with a bitmask. To my mind, this solves any threadsafe problems, since I assume setting (either by assignment or oring with a mask) a register only takes one cycle. On the other hand, if you first read the register, then modify, then write, an interrupt happening between the read and write may result in writing an old value to the register.
So why read-modify-write? Is it still necessary?