Why is Read-Modify-Write necessary for registers on embedded systems?

后端 未结 5 1902
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 20:17

I was reading http://embeddedgurus.com/embedded-bridge/2010/03/different-bit-types-in-different-registers/, which said:

With read/write bits, firmware se

5条回答
  •  -上瘾入骨i
    2021-02-07 20:52

    Modern processors can either set or clear bits with single instruction. However, these instructions can not both set and clear at the same time. There are instances when some of bits of an IO port must all change together and not affect other bits. As long as the sequence of read-modify-write can not be broken, there is no problem.

    The situation where the r-m-w can become a problem requires three conditions.

    1. The variable must be globally accessible such as an IO port or special function register or globally defined variable.

    2. The global variable can be modified in a function that can be preempted.

    3. The same global variable is modified while servicing a preemption.

    The only way to resolve multiple bit modifications using a r-m-w non-atomic sequence is to protect the sequence of instructions by disabling the interrupt for the interrupt service routine that can also modify the variable or register. This is similar to digine exclusive access to resources such as LCD or serial ports.

提交回复
热议问题