I ve just put my head on a problem where I was trying to get the adress of an arduino port into an expression and now just sharing with the community the trick I found for this:
Basically all the arduino SFR like PORTB or TIFR0 are declared from the sfr_defs.h.
and then each register is defined like this: #define PORTB _SFR_IO8(0x05) #define TIFR0 _SFR_IO8(0x15)
by this way, you can read a register directly like:
uint8_t XX = PORTB;
but if you just want to get the adress of PORTB (0x25) and TIFR0 (0x35) you cannot just write
uint8_t ADDR = &TIFR0;
as you get a compiler error:
invalid conversion from 'volatile uint8_t* {aka volatile unsigned char*}' to 'uint8_t {aka unsigned char}'
so the trick is to typecast all of that but with using the void cast in between, here is a macro:
Well,
I can propose a typical example of using this trick for a class which gives the possibility to declare digitalpins with direct access to ports, while providing a nice way of abstracting it in the source code:
it is possible to declare each IO with the sfr_bit tempate, and then to access each instances either in an expression or using the = assignement operator
Thanks to GCC the resulting code is just as compact as it can be using bit instructions
hope this is usefull and as fun as I wanted it to be
This is a nice method for fast GPIO. I have used it a few years back to create the following library. Arduino-GPIO/GPIO.h at master · mikaelpatel/Arduino-GPIO · GitHub There are a few more details needed to make it portable to Arduino Mega and Due. More details in the library. Cheers!