将项目从 ICCAVR 移植到 Atmel Studio 7.0 _flash 问题

Porting project from ICCAVR to Atmel Studio 7.0 _flash problem

我目前正在将一个大型项目从 ICCAVR 移植到 Atmel Studio,因为我需要为我正在处理的项目使用一些 ASF 库。

在尝试将以下行转换为 Atmel Studio 时,我在下面的代码示例中遇到此错误

void debugoutf(__flash char * header, __flash char * msg);

Error pointer targeting address space '__flash' must be const in function parameter 'header'

我尝试使用文档中的宏,以便它可以在 Atmel Studio 中编译。

#ifndef FLASHVAR_H_
#define FLASHVAR_H_

    #include <avr/pgmspace.h>

    #if defined(__ICCAVR__) // IAR C Compiler
    #define FLASH_DECLARE(x) __flash x
    #endif
    #if defined(__GNUC__) // GNU Compiler
    #define FLASH_DECLARE(x) x __attribute__((__progmem__))
    #endif

IAR 到 AVR 的转换

void debugout(FLASH_DECLARE (char * header), char * msg);

我的问题是我是否正确完成了转换,因为我认为我没有正确完成转换,因为在我的 UART 调试中没有打印出任何内容。

在变量上使用 __attribute__((__progmem__)) 的问题在于,无论何时您想从这样的变量中读取数据,都不能像从 RAM 中访问变量那样以通常的方式访问它。相反,您必须使用 avr/pgmspace.h header in avr-libc 提供的特殊功能,例如 pgm_read_byte.

这很烦人,但您不必那样做,因为他们添加 named address spaces like __flash to recent versions of GCC (assuming you are using C, not C++). I recommend you stop using __attribute__((__progmem__)) and use __flash instead, if your version of GCC supports it. If the compiler gives you an error or warning because it expects items stored in flash to be marked as const, you can simply add const to the declaration/definition (in the right position). If you have trouble doing that, please post a new question or edit this question so it contains a MCVE.

的原因

对于您给出的示例代码,您应该尝试编写:

void debugoutf(const __flash char * header, const __flash char * msg);