预处理器的 If 条件问题

Problem with If-Condition for Preprocessor

我为 AVR XMega 微控制器编写了一个引导加载程序,该引导加载程序由配置文件配置:

Config_Bootloader.h

#ifndef CONFIG_BOOTLOADER_H_ 
#define CONFIG_BOOTLOADER_H_ 

 #include <avr/io.h>

 #define BOOTLOADER_INTERFACE                   &USARTE0
 #define BOOTLOADER_BAUD                        115200
 #define BOOTLOADER_TX                          3       

#endif /* CONFIG_BOOTLOADER_H_ */

这个配置文件应该被另一个包含文件预处理以获得一些寄存器值等。

Bootloader_Preprocessing.h

#ifndef BOOTLOADER_PREPROCESSING_H_
#define BOOTLOADER_PREPROCESSING_H_

 #include <avr/io.h>

 #ifdef USARTE0
     #if(BOOTLOADER_INTERFACE == &USARTE0)
        #define BOOTLOADER_PORT     &PORTE
     #else
        #error "Invalid bootloader interface!"
     #endif
 #endif

 #if(BOOTLOADER_BAUD == 9600)
     #define BOOTLOADER_BRREG_VALUE         12
     #define BOOTLOADER_SCALE_VALUE         0
 #elif(BOOTLOADER_BAUD == 19200)
     #define BOOTLOADER_BRREG_VALUE         11
     #define BOOTLOADER_SCALE_VALUE         -1
 #elif(BOOTLOADER_BAUD == 38400)
     #define BOOTLOADER_BRREG_VALUE         9
     #define BOOTLOADER_SCALE_VALUE         -2
 #elif(BOOTLOADER_BAUD == 57600)
     #define BOOTLOADER_BRREG_VALUE         75
     #define BOOTLOADER_SCALE_VALUE         -6
 #elif(BOOTLOADER_BAUD == 115200)
     #define BOOTLOADER_BRREG_VALUE         11
     #define BOOTLOADER_SCALE_VALUE         -7
 #else
     #error "Invalid baud rate for bootloader!"
 #endif

#endif /* BOOTLOADER_PREPROCESSING_H_ */

我将这两个文件都包含在我的 Bootloader.h

#ifndef BOOTLOADER_H_
#define BOOTLOADER_H_

 #include "Config_Bootloader.h"
 #include "Bootloader_Preprocessing.h"

#endif /* BOOTLOADER_H_ */

我收到以下错误和警告:

> #define BOOTLOADER_INTERFACE                  &USARTE0

operator '&' has no left operand

> #if(BOOTLOADER_INTERFACE == &USARTE0)

in expansion of macro 'BOOTLOADER_INTERFACE'
#error "Invalid bootloader interface!"

那么为什么地址比较不起作用?

预处理器中没有 "address" 这样的东西,因此它们不能在 #if 预处理器指令中进行比较。

有关它能做什么和不能做什么的详细信息,请参阅 the GCC docs for #IF。请查阅您正在使用的预处理器的文档,additional/different 限制可能适用(您将其标记为 AVR)。

您的预处理器似乎得出结论,运算符 & 必须是按位运算符 &,这是一个二元运算符,因此需要一个左操作数。

好的,在与 C 预处理器苦苦挣扎后,我有了一个解决方案。 我在配置中定义符号 BOOTLOADER_INTERFACE=E,0 并处理输入:

 #define CATENATE(Prefix, Name)             Prefix##Name
 #define FIRST_ARG(A, B)                    A
 #define SECOND_ARG(A, B)                   B

 #define MAKE_USART_NAME(Uart)              CATENATE(USART, Uart)
 #define MAKE_PORT_NAME(Port)               CATENATE(PORT, Port)
 #define USART_NAME(Name)                   MAKE_USART_NAME(CATENATE(Name))
 #define PORT_NAME(Name)                    MAKE_PORT_NAME(FIRST_ARG(Name))

结果是 PORT- 和 USART-Structure 的地址,具体取决于给定的 USART 接口。