c++11通用属性与avr __programme__
c++11 generalized attributes with avr __progmem__
以下代码在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.cp:8:32: warning: ' program ' attribute指令被忽略[- watattributes]"并且代码被破坏,因为var x被存储到ram而不是flash中。
下一个问题是使用类方法,该类方法必须处理不同于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中。有什么诀窍吗?
我问题的第一部分的解决方案很简单:
extern const X x __attribute__ ((__progmem__)) = { 1 };
extern const X x [[gnu::__progmem__]] = { 1 };
问题是属性前缺少gnu::
相关文章:
- 为什么我在 AVR 中的中断无法正常工作?
- 在arduino项目中使用ArduinoCore avr库
- 将 Arduino 库添加到 Atmel Studio 7 AVR C++项目 - 缺少 Arduino.h
- 使用 avr-gcc 隐式转换为浮点数:uint8_t vs. uint16_t
- AVR-GCC:(看似)简单功能中不需要的序言/尾声
- 如何优化C++ avr 代码
- GCC为AVR上的简单ISR产生不必要的寄存器推送
- 定义的参数在以下包含的标头 [AVR C++] 中不可见
- 组合宏定义以一次获得多个定义 [avr C++]
- C++ AVR 上的编译器差异?
- 是否可以为 AVR 编写一个 constexpr 舍入函数?
- 如何导入<chrono>以使用 avr-gcc
- 致命错误: avr/io.h: 没有这样的文件或目录
- 一种避免pgm_read访问闪存的方法(avr 微控制器)
- AVR G++:执行超过 128 Kb ROM 边界的函数
- 为AVR构建编译时间任意长度阵列
- 我的AVR PWM库代码中的错误
- constexpr引用AVR端口地址
- AVR ASM从变量出发到端口
- 如何在AVR(例如:ATMega)或Arduino微控制器上制作2D PROGMEM阵列的1D PROGMEM数组(存储