This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

bit-banding

STM32f103 (cortex)
anyone seen any advantage from bit-banding. I have employed it a few places and always found it to be no savings in time or space.

Erik

  • One advantage of bit banding is that it allows code to write to a single bit in a register without regard for whether an interrupt or some other task might attempt to write other bits.

    Suppose, for example, that some code wanted to set an I/O pin high. The code:

      MY_PORT_REG |= MY_PIN_MASK;
    


    would be translated into a three-instruction sequence similar to:

      ldr r0,[r1,#MY_PORT_REG_displacement] ; Assumes R1 points somewhere close to MY_PORT_REG
      orr r0,#MY_PIN_MASK
      str r0,[r1,#MY_PORT_REG_displacement]
    


    If an interrupt routine writes to MY_PORT_REG (intending to change OTHER bits) between the first and third instruction, the value written to MY_PORT_REG would not reflect that change, and so those bits would be set back to the value they held before the interrupt.

    One could avoid this problem by using load-exclusive and store-exclusive, e.g.

      while(__strex(&MY_PORT_REG, __ldrex(&MY_PORT_REG) | MY_PIN_MASK)) {}  // Repeat until it works
    


    This code will execute like three instructions like the above, except that it uses special versions of the LDR and STR instructions which say that the store should be skipped if something else touches the register between the load and the store, and should return a flag indicating whether it was executed or skipped. The while() loop will cause the read-modify-write sequence to be repeated until it succeeds.

    That approach is workable for many applications, but it's little clunky. Bit banding allows the code to be greatly simplified in the common case where the objective is simply to set or clear a single bit.

  • Bit banding allows the code to be greatly simplified

    ... since I got into it for reasons of speed
    "anyone seen a better throughput using bit-banding"

    Erik

  • Bit banding for peripheral registers solves a problem that didn't exist, since vendors have implemented reasonable workarounds a long time ago. In most cases registers where bit manipulation matters (e.g. GPIO) are implemented as set/clear registers which already provide atomic access in an even better way, where you can modify multiple bits with a single access.

    Having said that, bit banding is still useful for SW flags and semaphores.

    Bit banding is an optional feature on newer Cortex-M processors and may not always be available.

    Kind regards
    Marcus
    http://www.doulos.com/arm/

  • Some peripheral registers have separate set/clear addresses, but not all registers that could use atomic bit updates include such features. I'm not sure that bit banding offers any advantage over declaring as a standard that writes to addresses 0x21000000-21FFFFFF behave as set-bits-only versions of corresponding addresses 0x20000000-20FFFFFF, and likewise writes to 0x22000000-0x22FFFFFF would behave as clear-bits-only; one could do likewise with RAM. Such an implementation could probably be simpler and cheaper than bit-banding (since it would only require feeding each peripheral or memory a write-high-bits and write-low-bits write-enable signals) but that's not how ARM did things.