ARM Cortex M3 Macro Limitation

Richard Earnshaw
Mon Apr 19 17:45:00 GMT 2010

On Sun, 2010-04-18 at 10:14 -0700, Thomas M. Alldread wrote: 
> Greetings:
>     I would like to learn a macro technique for the
> address assignment of single bit variables to the
> bitBand region of an ARM Cortex M3 MPU.
>      I wrote the following macro for defining and accessing
> single bit semaphore variables:
> #define SRBB(varName, bit) (*((volatile unsigned int *)\
> ((((uint32_t)&varName - SRAM_BASE)*32)+(bit*4)+SRAM_BB_BASE)))
> where:
> &varName       =   0x20000298 // for this example
> bit            =   1          // for this example
> SRAM_BASE      =   0x20000000
> SRAM_BB_BASE   =   0x22000000
>      For a reference I pre-calculated the bitBand address
> and took a look at the associated ASM steps. As shown in
> example 1 the GCC compiler matched the textbook
> efficiency of 3 ASM instructions for setting the bit.
> ....
> 1) Example with manually calculated bitBand address:
> (*((volatile unsigned int *)0x22005304)) = (uint16_t)1;;
>         4A20                  ldr r2, [pc, #0x080]
>         F04F0301               mov.w r3, #0x00000001
>         6013                   str r3, [r2, #0x00]
> ....
>      I then decided to generate the bitBand address with my
> macro and discovered that it took 7 instruction cycles to
> set the bit as some of the address arithmetic was assigned
> by GCC to the STM32 processor.
> ....
> 2)Example that uses the macro:
>         HEARTBEAT_500MS_BBFLG = 1;
>         4B22          ldr r3, [pc, #0x088]
>         EA4F1343      mov.w r3, r3, lsl #0x05
>         F1035308      add.w r3, r3, #0x22000000
>         F1030304      add.w r3, r3, #0x00000004
>         461A          mov r2, r3
>         F04F0301      mov.w r3, #0x00000001
>         6013          str r3, [r2, #0x00]
> ....
>      It appears the compiler pre calculates the 0x298 byte
> offset and bit offsets. Then the 32 times multiplication
> and offset sum steps are passed to the STM32.
>      I wonder if someone here knows why the GCC
> pre-processor does not complete my macro calculation?
> If so please advise me or point me to information that
> describes what I need to change so that the GCC
> pre-processor will complete the macro calculation
> without adding STM32 instructions.
>       Thanks in advance for any help!

I'm afraid there isn't much that can be done about this without adding
explicit bit-band support to the compiler (a lot of work).  The problem
is at least in part a representational limit of object files. 

The compiler does not know where your variable will eventually be placed
(the linker is the only thing that will know that).  So it can't
simplify your expression statically.  Further, it can't represent the
entire expression as a relocation because they cannot get this
complicated (being limited essentially to a symbol plus a simple
constant offset).  So the compiler has to leave your expression pretty
much as you've written it.


More information about the Gcc-help mailing list