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?
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
.
Analyzing (*((volatile uint32_t *)0x40000000))
0x40000000
is the address of register in your micro memory mapuint32_t *
volatile
is added to tell compile to avoid to optimize that variable because of could change, for example, in an interrupt routine.*
dereference the pointer: make you able to access the content of that specific register.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With