Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to volatile data (*((volatile uint32_t *)0x40000000)) [duplicate]

Tags:

c++

c

arm

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?

like image 260
DombleMaza Avatar asked Jan 13 '16 13:01

DombleMaza


2 Answers

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.

like image 179
Andrea Bergia Avatar answered Sep 27 '22 23:09

Andrea Bergia


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.
like image 22
LPs Avatar answered Sep 27 '22 21:09

LPs