这是交易:我正在研究一个 C 函数,它配置一个包含许多成员的结构,作为 *BASE(指向结构的指针),ID,MODE;但是 BASE 是一个结构,可以定义为(比如)“struct a”、“struct b”、“...”,具体取决于接口。这是结构(和一些示例)声明:
typedef unsigned int u32_t;
typedef unsigned char u8_t;
typedef struct _interface_t{
u32_t *BASE;
u8_t ID;
u32_t MODE;
} interface_t;
interface_t USART0_DEV = {AT91C_BASE_US0, AT91C_ID_US0, 0}; // <-- Here we have a AT91C_BASE_US0 struct as *BASE
interface_t USART1_DEV = {AT91C_BASE_US1, AT91C_ID_US1, 0};
interface_t TC0_DEV = {AT91C_BASE_TC0, AT91C_ID_TC0, 0}; // <-- Here we have a AT91C_BASE_TC0 struct as *BASE
interface_t TC1_DEV = {AT91C_BASE_TC1, AT91C_ID_TC1, 0};
interface_t TC2_DEV = {AT91C_BASE_TC2, AT91C_ID_TC2, 0};
interface_t TWI_DEV = {AT91C_BASE_TWI, AT91C_ID_TWI, 0}; // <-- Here we have a AT91C_BASE_TWI struct as *BASE
如您所见,我使用u32_t
而不是结构,因为我声明了指向该结构的指针。后来我将我的功能定义为:
unsigned char ConfigureDevice(interface_t *Interface, u32_t config, u32_t Speed, u32_t IRQ_Trigger, void (*Interface_irq_handler)(void)){
...
Interface->BASE->US_IER = IRQ_Trigger; //Note: US_IER Must receive a vector to a function
...
}
但我收到错误“无法解决 US_IER”。所以我尝试了AT91S_USART InterfaceBASE = Interface->BASE; InterfaceBASE->US_IER = IRQ_Trigger;
而不是Interface->BASE->US_IER = IRQ_Trigger;
没有错误。但我不能使用它,因为我并不总是知道我正在处理什么样的接口(直到我阅读 ID 成员)。
更糟糕的是:当我尝试编译时,我不断在typedef中收到conflicting type qualifiers for 'u32_t'
错误,并在每个定义中收到警告。u32_t
initialization from incompatible pointer type
near initialization for USART0_DEV.BASE
interface_t
我在论坛中搜索,但我无法弄清楚我的错误。这些有点帮助:
我的代码有什么问题?我能做些什么来使用类似的东西Interface->BASE->US_IER
编辑:嗨!,所以现在这就是我得到的:
typedef struct _Interface_t{
struct PERIPH_BASE * BASE;
u32_t ID;
u32_t MODE;
Interface_t;
Interface_t USART0_DEV = {(struct PERIPH_BASE *)AT91C_BASE_US0, AT91C_ID_US0, 0};
...
unsigned char ConfigureDevice(Interface_t *Interface, u32_t config, u32_t Speed, u32_t IRQ_Trigger,...
...
((AT91S_SYS)Interface->BASE)->AIC_IECR = IRQ_Trigger; //No problems marked by Eclipse Code Editor
....
}
看起来不错,但是编译时出现cast to union type from type not present in union
错误...