Hi,

I do not know if there will be any code over head due to C++.
Unfortunately my forte is 'C'. Can I trouble you for some
examples/sample code please? Thank you once again.

Use a 32-bit unsigned integer and a typedef as it has been suggested and You're done. It would be foolish to move on to C++ in an existing project.

For fun You can try to translate Your code with avr-g++. It should compile and library functions seem to work even without declaring them as extern "C", but may have side effects. Most of the language core is backward compatible to C.

Many stereotypes exist about the inefficiency of C++ and some of them contain a grain of truth. Try it Yourself and have a deep look into Stroustrup's "The C++ programming language", which is worth every cent.

The following code example may look dumb, because it does nothing really interesting. But the point is: By defining all those operators manually You gain full control over type conversion and You can not mix up internal and external addresses. Many operators are missing, so take it as a hint for further experiments.

// Everything related to address calculation and conversion
class ExtFlashAddr
{
public:
        // ExtFlashAddr myAddr = ExtFlashAddr(0x4711)
        inline explicit ExtFlashAddr(uint32_t address = 0);
        // Copy constructor
        inline ExtFlashAddr(const ExtFlashAddr& other);
        // ExtFlashAddr myAddr = otherAddr
        inline ExtFlashAddr& operator=(const ExtFlashAddr& other);
        // myAddr++
        inline ExtFlashAddr& operator++();

        friend ExtFlashAddr operator+(const ExtFlashAddr&, const ExtFlashAddr&);

private:
        uint32_t addr;
};

inline ExtFlashAddr operator+(const ExtFlashAddr& addr1, const ExtFlashAddr& addr2);

// Access functions
class ExtFlashTools
{
public:
        static void read(void* dst, const ExtFlashAddr& src, size_t size);
        static void erase(const ExtFlashAddr& src, size_t size);
        static void write(const ExtFlashAddr& dst, void const* src, size_t 
size);

};


ExtFlashAddr::ExtFlashAddr(uint32_t address)
{
        this->addr = address;
}

ExtFlashAddr::ExtFlashAddr(const ExtFlashAddr& other)
{
        this->addr = other.addr;
}


ExtFlashAddr& ExtFlashAddr::operator=(const ExtFlashAddr& other)
{
        this->addr = other.addr;
        return *this;
}

ExtFlashAddr& ExtFlashAddr::operator++()
{
        addr++;
        return *this;
}

ExtFlashAddr operator+(const ExtFlashAddr& addr1, const ExtFlashAddr& addr2)
{
        return ExtFlashAddr(addr1.addr + addr2.addr);
}


The code above should be optimized away with -Os.

_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Reply via email to