[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] template library for c++

From: Ron
Subject: RE: [avr-gcc-list] template library for c++
Date: Sun, 2 Oct 2005 12:46:35 +1000

> I started to work on a c++ template library for (avr) 
> microcontrollers, which should extend the c++ low level 
> support for microcontroller environments. The ATmega16 is the 
> only supported mcu at the moment. I post on this newsgroup, 
> because the templating and inlining mechanism of this library 
> heavily depends on the optimization process of the c++ compiler.

> I would appreciate any feedback to my approach.
> Regards,
> Nils

Sorry for the delay Nils. The IO parts I have tested all work well.
However I am not convinced that templates are all that useful for IO.
Consider the following (partial) base class:

    namespace io
    class IOClass
        IOClass(void) { 
            m_Pin = NULL; m_Ddr = NULL; m_Port = NULL; }
        void Setup(const uint16 address, const uint8 ddr, const uint8
state) {
            m_Pin = (uint8*)address; m_Ddr = m_Pin + 1; m_Port = m_Pin +
            *m_Ddr = ddr; *m_Port = state; }
        void SetDirection(const uint8 dir) { 
            *m_Ddr = dir; }
        void SetBit(const uint8 bitNumber) { 
            *m_Port |= _BV(bitNumber); }
        void ClrBit(const uint8 bitNumber) { 
            *m_Port &= ~_BV(bitNumber); }
        bool IsBitSet(const uint8 bitNumber) const { 
            return (GetBit(bitNumber) == _BV(bitNumber)); } 
        uint8 GetBit(const uint8 bitNumber) const { 
            return *m_Pin & _BV(bitNumber); }
        uint8* m_Pin; uint8* m_Ddr; uint8* m_Port;
    } // namespace

A standard AVR port can be derived from this like:

    #include "IOClass.h"
    namespace io
    class PortA : public IOClass
        PortA(const uint8 ddr, const uint8 state) {
            Setup((uint16)&PINA, ddr, state); }
    } // namespace    

An instance can be instantiated like:

    io::PortA porta(0xff, 0x55);

And used:

    flag = porta.IsBitSet(6);

The implementation has about the same cost and, in particular, uses the
existing WinAVR port definitions. Would you like to comment on the
advantages of using templates over this method?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]