【问题标题】:c++11 generalized attributes with avr __progmem__c++11 带有 avr __progmem__ 的通用属性
【发布时间】:2013-06-07 08:03:48
【问题描述】:

以下代码在 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: warning: 'progmem' attribute directive ignored [-Wattributes]”并且代码被破坏,因为 var x 存储到 ram而不是闪光灯。

下一个问题是使用类方法,这些方法必须处理与闪存或内存中的存储有关的不同数据成员。

啊哈:

    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 };

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

【问题讨论】:

    标签: c++ c++11 avr


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 2018-12-18
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      相关资源
      最近更新 更多