[Top][All Lists]

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

Re: [avr-gcc-list] External 256K FLASH - memory pointer

From: Richard Weickelt
Subject: Re: [avr-gcc-list] External 256K FLASH - memory pointer
Date: Tue, 02 Oct 2012 00:20:22 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120827 Thunderbird/15.0


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
        // 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&);

        uint32_t addr;

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

// Access functions
class ExtFlashTools
        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 


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++()
        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.

reply via email to

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