Reputation: 527
What does this piece of code mean?
#define kb_Data \
(uint8_t)((volatile uint16_t*)0xF50010)
I understand what a #define
statement does, but what does it mean when it casts a uint16_t pointer to a uint8_t integer? This is meant to be an array of bytes, as can be seen in here, but I don't understand how an int can act as an array
Upvotes: 0
Views: 133
Reputation: 10504
Taken as a complete expression it's nonsense. I just did some testing and on g++ and clang++ by default it's a compile error. On g++ with -fpermissive it compiles and produces the value 0x10.
However macros are not necessarily complete expressions, they are text substitutions. As was pointed out by Ben Voigt in a comment this allows shenanigans with operator precedence.
So if someone writes something like.
a = kb_Data[2];
Then, because array indexing has a higher precedence than a C-style cast, it will read element 2 of an array of 16 bit numbers at address 0xf50010. Then convert the result to an 8 bit number.
So why would you do this? My best guess would be someone hooked up an 8 bit peripheral to the low bits of a 16 bit bus rather than implement a proper bus bridge, then used a macro hack to make the driver work.
Upvotes: 3