Avr-libc explains how to pass an IO port as a parameter:
http://www.nongnu.org/avr-libc/u...
By declaring the parameter as
volatile uint8_t *port
one can do
*port |= mask;
.
Now I wish to have C++ templates specialized by SFR like:
typedef volatile uint8_t Register; template< Register ® > class RegBit { public: static inline void bitOn( uint8_t bitNo ) { reg |= _BV( bitNo ); } static inline void bitOff( uint8_t bitNo ) { reg &= ~_BV( bitNo ); } inline bool operator( )( uint8_t bitNo ) const { return reg & _BV( bitNo ); } }; //... // So I can do bit handling and other things like: RegBit< DDRB >::bitOn( 0 );
But I see those compilation errors:
templ.cpp: In function 'int main()':
templ.cpp:44: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
templ.cpp:44: error: `*' cannot appear in a constant-expression
templ.cpp:44: error: template argument 1 is invalid
templ.cpp:44: error: invalid type in declaration before '(' token
Changing the reference template parameter to a pointer one gives the same error.
From "avr/io.h" and its included files:
#define _SFR_IO8(io_addr) ((io_addr) + __SFR_OFFSET) #define DDRB _SFR_IO8(0x04)
__SFR_OFFSET is 0 or 0x20 depending on the MCU.
It would led to DDRB being replaced by the preprocessor with ((0x04) + 0x20).
So, the following two lines are equivalent:
RegBit< *(volatile uint8_t *)((0x04) + 0x20) >::bitOn( 0 ); RegBit< DDRB >::bitOn( 0 );
And I find out the '*' complained by the compiler.
If I do:
RegBit< (volatile uint8_t *)((0x04) + 0x20) >::bitOn( 0 );
Compiler now says:
templ.cpp: In function 'int main()':
templ.cpp:51: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
templ.cpp:51: error: template argument 1 is invalid
templ.cpp:51: error: invalid type in declaration before '(' token
Can anyone here help me, please???
I'm using the latest WinAVR (20081205, prior I was using 20080610).
Of course I've already tried several other things that I didn't post here, but if any aditional information could be useful, please ask.
Thanks!