1

Hi,

I am new to embedded coding. I am working on ARM lm3s8962 controller.

Can anyone explain to me what the code below is doing? It is implementing bit banging but I want to know how it is implementing it.

//*****************************************************************************
//
// Macros for hardware access, both direct and via the bit-band region.
//
//*****************************************************************************
#define HWREG(x)                                                              \
        (*((volatile unsigned long *)(x)))
#define HWREGH(x)                                                             \
        (*((volatile unsigned short *)(x)))
#define HWREGB(x)                                                             \
        (*((volatile unsigned char *)(x)))
#define HWREGBITW(x, b)                                                       \
        HWREG(((unsigned long)(x) & 0xF0000000) | 0x02000000 |                \
              (((unsigned long)(x) & 0x000FFFFF) << 5) | ((b) << 2))
#define HWREGBITH(x, b)                                                       \
        HWREGH(((unsigned long)(x) & 0xF0000000) | 0x02000000 |               \
               (((unsigned long)(x) & 0x000FFFFF) << 5) | ((b) << 2))
#define HWREGBITB(x, b)                                                       \
        HWREGB(((unsigned long)(x) & 0xF0000000) | 0x02000000 |               \
               (((unsigned long)(x) & 0x000FFFFF) << 5) | ((b) << 2))
artless noise
  • 21,212
  • 6
  • 68
  • 105
  • Uh, what's there to get? They're just macros/programmer shortcuts to do things. They don't do anything on their own. – tangrs Mar 12 '13 at 06:30
  • thank you for replying , i want to know how the process goes on , like hWREG returns the address same is the case with the hwregh and b, but when it comes to HWREGBITB(X,b) it sets the bit (if m not wrong) i want to know how it is setting this bit , as i am getting confused in so many shift operations plz can u explain it ? thanx again – user2159417 Mar 12 '13 at 08:11

1 Answers1

1

Those are declarations for macros. They're not functions and they don't 'return' anything. They're simply a slightly-smarter search-and-replace.

In response to your comment, HWREG(x)is replaced with (*((volatile unsigned long *)(x))) (which is a pointer deference i.e. get the value at address x) and substitues x with its parameter when it is run through the preprocessor.

We can infer from this that HWREG takes some sort of address in some form. This will help us work out what HWREGBITB does.

HWREGBITB(x, b) takes it's x value,

  1. (((unsigned long)(x) & 0xF0000000)): Clears out all the bits except the most significant 4 bits (assuming longs are 32 bits wide)
  2. (| 0x02000000): Sets the next 4 bits to the value 2.
  3. (| (((unsigned long)(x) & 0x000FFFFF) << 5)): Gets the lower 20 bits of the original x and shifts it 5 bits to the left. Then, OR it into the result.
  4. (| ((b) << 2)): Take b, shift it 2 bits and OR it into the result.
  5. Treat the final result as an address and dereference it using HWREG

You can use a similar method to work out what the other macros mean.

What it does specifically in terms of hardware will depend on what hardware it is accessing and what the registers on the peripheral are. If I had to take a guess though, I would say that it's doing some sort of transaction on a AXI bus.

tangrs
  • 9,709
  • 1
  • 38
  • 53
  • it is to access memory , e.gHWREGBITW(&g_ulFlags, 0) ^= 1; here it is toggling the bit , now can you plz clear how it is accesing it . – user2159417 Mar 12 '13 at 09:08
  • Put it simply, those macros let you access the hardware registers as if it were a variable. Just think of `gHWREGBITW(&g_ulFlags, 0) ^= 1` as something like `var ^= 1`. – tangrs Mar 12 '13 at 23:31