I am using a define to read data from and write to flash or ram.
#define REG_READ(address) ((uint8_t)(*(vuint8_t *)(address)))
#define REG_WRITE(address, value) (*(vuint8_t *)(address) = (value))
But I'm unable to understand ((uint8_t)(*(vuint8_t *)(address))) and (*(vuint8_t *)(address) = (value)) this.
Could you please help me out on this? How it is typecasted and what this typecasting actually means.
Thanks in advance!!
Harshitha C B
Solved! Go to Solution.
Welcome to the C programming language :-)
#define REG_READ(address) ((uint8_t)(*(vuint8_t *)(address)))
you can use it as this:
uint8_t val = REG_READ(0x1000);
The macro takes the address (0x1000 in this case) and casts it to a pointer to volatile unsigned 8bit int ((vuint8_t*).
Then it dereferenciates the pointer (* operator) and casts the result to an unsigned 8bit int (uint8_t).
Finally, it assigns it to the variable val.
The WRITE macro is doing the same thing, must with assigning the value to that constant pointer cast.
You might see other examples of this in “Volatile” can be harmful… | MCU on Eclipse
I hope this helps,
Erich
Welcome to the C programming language :-)
#define REG_READ(address) ((uint8_t)(*(vuint8_t *)(address)))
you can use it as this:
uint8_t val = REG_READ(0x1000);
The macro takes the address (0x1000 in this case) and casts it to a pointer to volatile unsigned 8bit int ((vuint8_t*).
Then it dereferenciates the pointer (* operator) and casts the result to an unsigned 8bit int (uint8_t).
Finally, it assigns it to the variable val.
The WRITE macro is doing the same thing, must with assigning the value to that constant pointer cast.
You might see other examples of this in “Volatile” can be harmful… | MCU on Eclipse
I hope this helps,
Erich