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

What is better as mutex on Cortex-M4 - Bitband or LDREX/STREX

The two options are available.

What is the difference in respect of "cost" "speed" and "complexity" for the two method?

When we need more than few mutexs like that, say 100, is the answer different?

  • Hi amiry,

    is there an example to use the bitband for the synchronization? I think the bitband would not be used for the synchronization because we could not know the previous value of the bitband target area although the bitband initiates a read-modify-write transaction (i.e. atomic access). The previous value would show the transaction had succeeded or failed.

    Best regards,
    Yasuhiko Koumoto.

  • As bitband performs RMW I guess it can serve as primitive mutex.

    You are right about don't know what was happend before, however, think of:

    Startup:

    Select a word and Assign bit1 to Task1, Assign bit2 to Task2...and so on

    Task1:

    Read all word, if <> 0 resource is taken

    Write task1 bit, atomic

    Read all word again

    if == excactly Task1.bit --> all well. resource is taken by task1.

    if <> excactly Task1.bit --> bad. not mine. Clear task1 bit, atomic

    try again.

    Ha?

  • There are several limitations with the bit-band method:

    - the semaphore data are limited to bit type. With exclusive accesses you can have byte, halfword and word size. (For simple mutex operations bit data is fine).

    - Since bit band data is limited to 32-bit, and use each bit for each application task, so limited to 32 tasks to share the data.  Exclusive accesses don't have this limitation.

    - Bit band can only be used in bit band regions. Exclusive accesses can be used in other memory space.

    - Bit band is not supported in Cortex-M7 processor (chip designer can add bit band on the peripheral space using additional bus level component).

    In terms of cost and speed they are fairly similar.  Given that semaphore and mutex don't happen frequenctly, even if one of the methods is faster the benefit is small.

    In terms of complextity, you can do bit band easily with C, and for exclusive accesses you need to use intrinsic functions.

    Hope this helps.

    regards,

    Joseph

  • Hi amiry,

    ok, thank you. I understood your method. It seems to be performed successfully. However the number of read and write times will be bigger than LDREX/STREX method. As Joseph Yiu said, the benefit of the bitband method would be small.

    Best regards,

    Yasuhiko Koumoto.

  • Hi amiry,

    I think you have gotten answered by Joseph. If it is correct, please click "Correct Answer" for the Joseph's answer and close this post.

    Best regards,
    Yasuhiko Koumoto.

  • Note: If you don't have Bit Band, then you can still use 4 bytes of a 32-bit word as a 'mini bitband' functionality.

    There is also another option which was not mentioned: You could disable interrupts, modify the value and enable interrupts.

    Disabling interrupts will of course disturb the program flow, but it will not stop timers, etc; it'll just postpone the execution of the interrupt.

    The pending bits will still be set, so you will not lose any interrupt servicing.