title: Why is Read-Modify-Write necessary for registers on embedded systems?
body: <p>I was reading <a href="http://embeddedgurus.com/embedded-bridge/2010/03/different-bit-types-in-different-registers/" rel="noreferrer">http://embeddedgurus.com/embedded-bridge/2010/03/different-bit-types-in-different-registers/</a>, which said: </p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>So why read-modify-write? Is it still necessary?</p>