6

I am analyzing a peripheral driver's files and found some register mapping code. I have basic knowledge about pointers, but am unable to understand the below code.

#define WATCHDOG0_LOAD_R        (*((volatile uint32_t *)0x40000000))

I have understood that it defines the identifier WATCHDOG0_LOAD_R to the register's memory address. But I am not able to understand the syntax on the right side. Could anyone explain me in detail why this pointer is written in such a way?

Quentin
  • 62,093
  • 7
  • 131
  • 191
DombleMaza
  • 89
  • 1
  • 1
  • 4
  • 1
    What don't you understand specifically the `0x40000000` or what `volatile uint32_t *` means? – πάντα ῥεῖ Jan 13 '16 at 13:48
  • 5
    Please chose one of the `C` and `C++` tags, unless you're asking specifically about an interaction between these two languages. – Quentin Jan 13 '16 at 13:53
  • You might also benefit from reading [this question](http://stackoverflow.com/q/4166201/212858) which ends up with similar code. – Useless Jan 13 '16 at 13:55
  • 2
    Possible duplicate [1](http://stackoverflow.com/questions/15267001/how-to-store-a-variable-at-a-specific-memory-location/15268780#15268780) [2](http://stackoverflow.com/questions/5029938/c-pointers-and-the-physical-address/5031056#5031056) and [3](http://stackoverflow.com/questions/31262865/what-does-port-0x41004400ul-mean-here/31266754#31266754). And so on. Please do some research. – Lundin Jan 13 '16 at 13:57
  • @Lundin I choose 2 here as coming closest to resemble. Well, to be fair it might be hard to find these dupes, unless you have answered them yourselves ;-) ... – πάντα ῥεῖ Jan 13 '16 at 14:04
  • If i tell frankly in simple words than i am not able to understand why there is two '*' in right-side statement? I mean we are declaring a pointer to that particular memory address like `volatile uint32_t *ptr = 0x40000000`. What is the purpose of another pointer? – DombleMaza Jan 13 '16 at 14:19
  • @DombleMaza To clarify finally: The outer `*` is not _a pointer_, but used to dereference that formerly declared (casted) pointer, thus the memory address is accessed to read/write a value from/to it. – πάντα ῥεῖ Jan 13 '16 at 14:24
  • The purpose is to use that macro as: `WATCHDOG0_LOAD_R= 0xAA55AA55` or `uint32_t reg_value = WATCHDOG0_LOAD_R;` In other words you directly access the content of the resister to set or read it. – LPs Jan 13 '16 at 14:24

2 Answers2

23

Let's take it one step at the time:

0x40000000

is your memory address.

(uint32_t *)0x40000000

casts this to a pointer to that memory address, of type uint32_t, meaning 32 bit without sign.

(volatile uint32_t *)0x40000000

volatile means, basically, "hey compiler, don't do any optimization; I really want to go every time to that memory address and fetch it, without any prefetch or anything particular".

*((volatile uint32_t *)0x40000000)

means: take the value contained at the address identified by that pointer, so the four bytes starting at 0x40000000.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Andrea Bergia
  • 5,502
  • 1
  • 23
  • 38
  • @DombleMaza refering to your comment: which "following syntax" ? – Jabberwocky Jan 13 '16 at 14:01
  • 1
    Thanks a lot. But i am still not able to understand the purpose of making this whole this as a pointer. `(volatile uint32_t *)0x40000000` is not a pointer itself? May i write a same code as `volatile uint32_t *ptr = 0x40000000; #define WATCHDOG0_LOAD_R *ptr `? – DombleMaza Jan 13 '16 at 14:13
9

Analyzing (*((volatile uint32_t *)0x40000000))

  • 0x40000000 is the address of register in your micro memory map
  • the register is 32 bits wide, that means must be uint32_t *
  • volatile is added to tell compile to avoid to optimize that variable because of could change, for example, in an interrupt routine.
  • last: the * dereference the pointer: make you able to access the content of that specific register.
LPs
  • 16,045
  • 8
  • 30
  • 61
  • 2
    Note also the dereference. Assigning this definition to an integer will load the value at the address. –  Jan 13 '16 at 13:52
  • @TruthSerum True, I edited. I was hoping that OP understand that on his/her own, at least. – LPs Jan 13 '16 at 13:55