3

I'm following the guide given at micromouseonline . com/2010/07/14/bit-banding-in-the-stm32 . I'm using IAR EWARM and Cortex M3. Everything works fine but I'm not able to set the bits in a given address. Im using STM32L151xD and IAR EWARM compiler.

This is how they define the functions

#define RAM_BASE 0x20000000
#define RAM_BB_BASE 0x22000000
#define Var_ResetBit_BB(VarAddr, BitNumber) (*(vu32 *) (RAM_BB_BASE | ((VarAddr - RAM_BASE) << 5) | ((BitNumber) << 2)) = 0)
#define Var_SetBit_BB(VarAddr, BitNumber) (*(vu32 *) (RAM_BB_BASE | ((VarAddr - RAM_BASE) << 5) | ((BitNumber) << 2)) = 1)
#define Var_GetBit_BB(VarAddr, BitNumber) (*(vu32 *) (RAM_BB_BASE | ((VarAddr - RAM_BASE) << 5) | ((BitNumber) << 2)))


#define varSetBit(var,bit) (Var_SetBit_BB((u32)&var,bit))
#define varGetBit(var,bit) (Var_GetBit_BB((u32)&var,bit))

the call is :

uint32_t flags;
varSetBit(flags,1);

however, the bit 1 in flags is always 0 if I see using a debugger. The flags is assumed to be 0 at first. So, all the bits in flags will be 0. However, when i use varSetBit(flags,1), the answer at bit 1 is 0 again. I dont think im doing anything wrong. Is it a compiler problem? am i missing some settings? Any help will be appreciated.

artless noise
  • 21,212
  • 6
  • 68
  • 105
Abhishek Thakur
  • 16,337
  • 15
  • 66
  • 97

1 Answers1

2

I suspect that you misunderstand the purpose of the bit-banding feature.
With bit-banding, the application have access (read/write) to the micro-controller's registers bit by bit. This allows modifying a bit with a single store instruction instead of a read/modify/write sequence. For this to work, stm32 devices (or more generally Cortex M3 devices) have a specific address space where each bit of each register is mapped to a specific address.

Let's take an example, if you need to set the bit 3 of the register FOO:

  • Without bit-banding, you would have to write the following code:

    FOO = FOO | 0b100;

This results from the assembler instructions in a load of the register FOO, a bit-wise OR operation and a store of the register FOO.

  • With bit-banding, you write:

    varSetBit(FOO, 3);

which results to a simple store at the address computed by preprocessor from the varSetBit macro.

Now that is said, bit-banding only apply to the micro-controller's register. You can't use them to manipulate bits of your own variables as you do with your flags variable.

For more information read the ARM application note

greydet
  • 5,509
  • 3
  • 31
  • 51
  • 1
    This is correct; you can of course write your own macro to set a single bit in a user-defined variable but it will still be a read-modify-write operation and the macro simply makes it easier to see what is going on. – Vicky May 07 '13 at 15:33