使用avr __progmem__的c ++ 11通用属性

时间:2013-06-07 08:03:49

标签: c++ c++11 avr

以下代码使用avr-g ++ 4.8.1按预期编译: (代码必须以不同的翻译单位分割才能看到任何结果,因为优化程序会在内联指令时删除所有效果)

x.h:

struct X
{
    uint8_t a;
};

x.cpp:

extern const X x __attribute__ ((__progmem__)) = { 1 };

main.cpp中:

#include "x.h"
extern const X x __attribute__ (( __progmem__ )); 

int main()
{   
    PORTB = pgm_read_byte(& (x.a));
    return 0;
}

结果为(objdump -d):

0000001a <x>:
  1a:   01 00                                               ..
  ...

  2e:   ea e1           ldi r30, 0x1A   ; 26
  30:   f0 e0           ldi r31, 0x00   ; 0
  32:   c8 95           lpm

结果很好。

问:为什么不能用c ++ 11编写:

 extern const X x [[__progmem__]] = { 1 };

这导致警告“x.cpp:8:32:警告:' progmem '属性指令被忽略[-Wattributes]”并且代码被破坏,因为var x存储到ram而不是闪光灯。

下一个问题是使用类方法,它必须处理与flash或ram中的存储不同的数据成员。

A.H:

    class A
    {
        private:
            uint8_t a;

        public:
            constexpr A( uint8_t _a): a(_a) {}

            void Store() const; // Q:should be something like const __attribute__((__PROGMEM__)) ???
            void Store();
    };

a.cpp:

   #include "a.h"

    void A::Store() const
    {   
        PORTB=pgm_read_byte(&a);
    }   

    void A::Store() 
    {   
        PORTB=a;
    }   

    extern const A a_const PROGMEM ={ 0x88 };
    A a_non_const={ 0x99 };

main.cpp中:

extern const A a_const;
extern A a_non_const;


int main()
{
    a_const.Store();
    a_non_const.Store();

    return 0;
}

代码工作正常,但如果var声明为:

则会中断
extern const A a_const_non_flash={ 0x11 };

因为限定符const为“void Store()const”不足以确定var存储在flash / ram中。对此有什么诀窍吗?

1 个答案:

答案 0 :(得分:1)

我的问题第一部分的解决方案非常简单:

extern const X x __attribute__ ((__progmem__)) = { 1 };
extern const X x [[gnu::__progmem__]] = { 1 };

问题是在属性之前缺少gnu ::