cancel
Showing results for 
Search instead for 
Did you mean: 

Bit Banding Explained

jschatz
Associate II
Posted on August 18, 2009 at 13:36

Bit Banding Explained

6 REPLIES 6
jschatz
Associate II
Posted on May 17, 2011 at 13:20

I am having a little trouble understanding the bit band operation as it applies to the IAR implementation of C code.

Could somebody explain bit banding? I have read the literature, but I still don't grasp the concept...

Do I need to assign a value into a register to create a region that the bit banding offset uses? I could use an example that explains how to set a value onto a GPIO port.

This seems simple, but in the context of the libraries (V3 or later) it is not making great sense.

Also, is there an advantage to using the bit band region to access peripheral registers over some sort of direct access?

akaiser9
Associate II
Posted on May 17, 2011 at 13:20

While it might save some space too, the probably most important advantage of bitbanded operations is that they are atomic. Which means setting or clearing a bit without the unlucky chance of being interrupted in the middle of it.

Except for the GPIO which has separate provisions for this matter, setting or clearing bits the usual way involves at least 3 distinct operations at the machine level: load, modify, store. When this sequence is interrupted in the middle and the interrupt changes different bits in the same peripheral register, those changes get lost when the value loaded in the main program before the interrupt is written back to the peripheral register after the interrupt returns. Without bitbanding you'd have to disable interrupts to be safe.

jschatz
Associate II
Posted on May 17, 2011 at 13:20

In the embedded controllers that I have worked with in the past the read-modify-write sequence was always considered a single machine cycle. Interrupts would only occur at the beginning of a machine cycle (or after the end of the last machine cycle). I guess I will have to get used to this new style.

I actually tried to make GPIOs switch using pointers from the libraries. I think that they are pointing into the bit-band region however. The GPIOs do not appear to be changing. I guess that I can change them using the set/reset registers, but I am trying to conform to the methods that I think are prescribed by the libraries. This may be the source of my confusion. I am new to the ARM products.

However, I still have a problem understanding the actual operation of the bit-banding. Do I need to point the banding region to some location before I can use it? Does it change during operation for different peripherals? Should I avoid using the pointers from the libraries (V3.x) for GPIO interface?

Thank you for any replies!

akaiser9
Associate II
Posted on May 17, 2011 at 13:20

Bitbanding does not have to be initialized. It is a hardwired property of the separate address spaces reserved for this purpose.

For GPIO pin control it is usually easier to use the BRR+BSRR registers. Bitbanding could be useful as abstraction layer to hide the exact pin locations, because you can define a name for each pin attached to some peripheral in a central place and later forget about the port and the position within the port, as it is all the same kind of access.

Wrt library: I tend to avoid those libraries and prefer to access the registers directly. But this is my personal style, not a hint. To me, the lib does not make things easier, but adds one more doc to study and one more possible source of error. Also sometimes the lib implements things awefully complicated even though it could have be done a lot easier (look at the implementation of the USART bit rate calculation, the lib author definitely did not understand what the hardware designer intended).

[ This message was edited by: prx on 11-08-2009 00:06 ]

hl
Associate II
Posted on May 17, 2011 at 13:20

Hi,

Bit banding seems hard but is not really. Basically, it means each and every single bit in a peripheral register (eg. HSION in the RCC_CR register) has a corresponding 32-bit value in the 'Bit-band' region. This is called 'mapping' and the bit-band address region is called an 'alias'. Seems strange, doesn't it. If the HSION bit is set, then its address in the bit-band region contains the value 0x00000001 (a 4-byte location for every aliased bit); if it is reset, then the corresponding value in the bit-band region is 0x00000000. If you read-modify-write the RCC_CR location to do something with the HSION bit, then all the bits in that register are read-modified-written - this can lead to unintended results in some cases. However, in the bit-band region you can read a byte (or, for that matter, 32-bit) location without affecting other bytes (aliased bits). The mapping is described on page 43 of the June 2009 version of RM0008, the STM32F10xxx Reference manual. Or find the Cortex-M3 revision r1p1 Technical Reference manual. It's covered in Chapter 4. You can find a link to it on page 1 of RM0008.

Unfortunately, I don't think the C-SPY simulator uses this alias region. At least I can't see any evidence of it.

Hope this helps.

Harry

jschatz
Associate II
Posted on May 17, 2011 at 13:20

Thank you both for your reply.

I have been having difficulty with a lot of 'little' things about the STM32F103ZCH while trying to get my project going. I am using C-Spy (IAR compiler) and it has been a great help. I am impressed at the amount of peripherals that are available in my processor, but this is my first venture into using an ARM processor.

Some of the little things are enabling the clocks on the GPIOs and the significance of the bit-banding. I have not been seeing suggestions about doing these little things in comments in the example programs.