Folk,
I spent a few hours playing with this approach, and when you go to
"extern" definitions with the address provided to the linker, the compiler
no longer recognises that it can substitute "sbi" for "ldw", "or" and "stw",
so you get long-form code.
I tried to force the issue using inline "asm" calls to the SBI instruction,
but then gcc won't coerce the (unknown, but possibly 16-bit) address into
the 6-bit field, even when I try various ways to force it. I think that Atmel
have hacked gcc just enough to work for the cases they care about.
The upshot of that is I can't make proper use of a "struct" (because I
can't locate it in memory).
I.e. I can't see any way to use SBI/CLI instructions on registers in this
struct:
struct __attribute__((packed)) AvrIOPort {
uint8_t pin;
uint8_t ddr;
uint8_t data;
};
extern volatile AvrIOPort PortB; // Address set by a linker option
or the low 0x20 bytes of my much larger "CPU" structure (which maps
the entire 0xFF block).
Here is the code which fails:
template <volatile AvrIOPort& Port, uint8_t Number>
class Pin
{
public:
Pin& operator=(bool b)
{
if (b)
// Port.data |= (01<<Number);
asm volatile(
" sbi %[portdata],%[portbit]\n"
: // Output Operands
: // Input Operands
[portdata] "I" (&Port.data),
[portbit] "I" (01<<Number)
:
);
else
// Port.data &= ~(01<< Number);
asm volatile(
" cbi %[portdata],%[portbit]\n"
: // Output Operands
: // Input Operands
[portdata] "I" (&Port.data),
[portbit] "I" (01<<Number)
: // Clobbers
);
return *this;
}
void output() { }
};
Pin<PortB, 0> portBp0;
The compiler can't see that the (external) address of "Port.data" can
be fit into a 6-bit field (specified by the "I" parameter type), so it
complains "impossible constraint".
I can still make this all work using #defines for all the register addresses,
but it's a lot uglier than using structs.
I hope I don't have the same problem with the ARM version of gcc.
Perhaps someone will get value from my discussion above.
Clifford Heath.
> ----- Original Message ----- From: "Clifford Heath" <
cliffor...@gmail.com>
> To: "Connected Community HackerSpace" <
connected-commu...@googlegroups.com>
> Sent: Monday, May 16, 2016 10:28 AM
> Subject: Arduino APIs, performance, and C++ templates (long)
> -----
> No virus found in this message.
> Checked by AVG -
www.avg.com
> Version: 2016.0.7597 / Virus Database: 4568/12237 - Release Date: 05/15/16
>
>