この質問から:「構造体内のポインター(変数)構造体のメンバーへのアクセス」。(未定義?) 構造体を使用して、いくつかの (未知の型?) パラメータを受け入れることができます。 *BASE (構造体へのポインタ),ID,MODE; しかし、BASE は、インターフェイスに応じて、(たとえば) "struct a"、"struct b"、"..." として定義される可能性がある構造体です。前の質問での議論の後、最終的にこの構造体宣言にたどり着きました。
typedef struct _Interface_t{
struct PERIPH_BASE * BASE;
u32_t ID;
u32_t MODE;
} Interface_t; //Typo edit
interface_t USART0_DEV = {(struct PERIPH_BASE *)AT91C_BASE_US0, AT91C_ID_US0, 0}; // <-- AT91C_BASE_US0 struct as *BASE
interface_t TC0_DEV = {(struct PERIPH_BASE *)AT91C_BASE_TC0, AT91C_ID_TC0, 0}; // <-- AT91C_BASE_TC0 struct as *BASE
interface_t TWI_DEV = {(struct PERIPH_BASE *)AT91C_BASE_TWI, AT91C_ID_TWI, 0}; // <-- AT91C_BASE_TWI struct as *BASE
...
unsigned char ConfigureDevice(Interface_t Interface, u32_t config, u32_t Speed, u32_t IRQ_Trigger, u32_t IRQ_Mode, void (*Interface_irq_handler)(void)){
...
USART_Configure(Interface, config, Speed, PERIPHERALS_CLOCK); //ERROR0
if (*Interface_irq_handler != NULL){
((AT91S_USART)Interface->BASE)->US_IER = IRQ_Trigger; //ERROR1
}
USART_SetTransmitterEnabled(Interface->BASE, 1); //ERROR1
USART_SetReceiverEnabled(Interface->BASE, 1); //ERROR1
}
このようにして、Eclipse コード エディターは警告やエラーをスローしませんでしたが、すべてのプロジェクトをビルドし、コンパイラーは不平を言いました:
incompatible type for argument 1 of 'USART_Configure'
(エラー0)
invalid type argument of '->' (have 'Interface_t')
(エラー1)
次のエラーが表示される前に: "cast to union type from type not present in union"
;
だから、たぶん、私が「受け取っている」構造体のタイプを知った後だと思いますBASE
(AT91S_PWMC、AT91S_USART、AT91S_AIC、...:構造体内のポインター(変数)構造体のメンバーへのアクセスを読んでください)私は(再)PERIPH_BASE Struct を Recieved Struct と同じように定義します。間違っていたら訂正してください...
よろしくお願いします。回答ありがとうございます。
編集: USART_Configure コードの追加:
void USART_Configure(AT91S_USART *usart,
unsigned int mode,
unsigned int baudrate,
unsigned int masterClock)
{
// Reset and disable receiver & transmitter
usart->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX
| AT91C_US_RXDIS | AT91C_US_TXDIS;
// Configure mode
usart->US_MR = mode;
// Configure baudrate
// Asynchronous, no oversampling
if (((mode & AT91C_US_SYNC) == 0)
&& ((mode & AT91C_US_OVER) == 0)) {
usart->US_BRGR = (masterClock / baudrate) / 16;
}
}
編集 2:完全なコード
#include "Config_file.h"
#include "board.h"
#include "aic/aic.h"
#include "pmc/pmc.h"
#include "twi/twi.h"
#include "usart/usart.h"
typedef volatile unsigned int u32_t;
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};
Interface_t USART1_DEV = {(struct PERIPH_BASE *)AT91C_BASE_US1, AT91C_ID_US1, 0};
Interface_t TC0_DEV = {(struct PERIPH_BASE *)AT91C_BASE_TC0, AT91C_ID_TC0, 0};
Interface_t TC1_DEV = {(struct PERIPH_BASE *)AT91C_BASE_TC1, AT91C_ID_TC1, 0};
Interface_t TC2_DEV = {(struct PERIPH_BASE *)AT91C_BASE_TC2, AT91C_ID_TC2, 0};
Interface_t TWI_DEV = {(struct PERIPH_BASE *)AT91C_BASE_TWI, AT91C_ID_TWI, 0};
...
unsigned char ConfigureDevice(Interface_t *Interface, u32_t config, u32_t Speed, u32_t IRQ_Trigger, u32_t IRQ_Mode, void (*Interface_irq_handler)(void)){
PMC_EnablePeripheral(Interface->ID);
switch(Interface->ID){
case AT91C_ID_FIQ: // Interface is FIQ!
TRACE_ERROR("Attempt to Configure an FIQ as System Peripheral!");
break;
case AT91C_ID_SYS: // Interface is SYS!
if (*Interface_irq_handler != NULL){
((AT91S_SYS)Interface->BASE)->AIC_IECR = IRQ_Trigger;
}
break;
case AT91C_ID_PIOA|AT91C_ID_PIOB: // Interface is PIO!
// PIO_Configure(); ///XXX: PIO_Configure To Be Modified
if (*Interface_irq_handler != NULL){
((AT91S_PIO)Interface->BASE)->PIO_IER= IRQ_Trigger;
}
break;
case AT91C_ID_SPI0|AT91C_ID_SPI1: // Interface is SPI!
// SPI_Configure(Interface, config, Speed, PERIPHERALS_CLOCK); //XXX: SPI_Configure To Be Written
if (*Interface_irq_handler != NULL){
((AT91S_SPI)Interface->BASE)->SPI_IER = IRQ_Trigger;
}
break;
case AT91C_ID_US0|AT91C_ID_US1: // Interface is USART!
(AT91S_USART)Interface->BASE;
USART_Configure(Interface->BASE, config, Speed, PERIPHERALS_CLOCK);
if (*Interface_irq_handler != NULL){
((AT91S_USART)Interface->BASE)->US_IER = IRQ_Trigger;
}
USART_SetTransmitterEnabled(Interface->BASE, 1);
USART_SetReceiverEnabled(Interface->BASE, 1);
break;
case AT91C_ID_SSC: // Interface is SSC!
if (*Interface_irq_handler != NULL){
((AT91S_SSC)Interface->BASE)->SSC_IER = IRQ_Trigger;
}
break;
case AT91C_ID_TWI: // Interface is TWI!
if (*Interface_irq_handler != NULL){
((AT91S_TWI)Interface->BASE)->TWI_IER = IRQ_Trigger;
TWI_ConfigureMaster(Interface->BASE, Speed, PERIPHERALS_CLOCK);
}
break;
case AT91C_ID_PWMC: // Interface is PWM!
if (*Interface_irq_handler != NULL){
((AT91S_PWMC)Interface->BASE)->PWMC_IER = IRQ_Trigger;
}
break;
case AT91C_ID_UDP: // Interface is USB!
if (*Interface_irq_handler != NULL){
((AT91S_UDP)Interface->BASE)->UDP_IER = IRQ_Trigger;
}
break;
case AT91C_ID_TC0|AT91C_ID_TC1|AT91C_ID_TC2: // Interface is TC!
unsigned int div, tcclks;
PMC_EnablePeripheral(Interface->ID); // Enable TC0 peripheral clock
TC_FindMckDivisor(Speed, PERIPHERALS_CLOCK, &div, &tcclks); // Configure TC for a (u32_t Speed) In Hertz
TC_Configure(Interface->BASE, tcclks | config); // interrupt configuration
((AT91S_TC)Interface->BASE)->TC_RC = (PERIPHERALS_CLOCK / (2 * div));
if (*Interface_irq_handler != NULL){
((AT91S_TC)Interface->BASE)->TC_IER = IRQ_Trigger;
}
break;
case AT91C_ID_CAN: // Interface is CAN!
if (*Interface_irq_handler != NULL){
((AT91S_CAN)Interface->BASE)->CAN_IER = IRQ_Trigger;
}
break;
case AT91C_ID_EMAC: // Interface is EMAC!
if (*Interface_irq_handler != NULL){
((AT91S_EMAC)Interface->BASE)->EMAC_IER = IRQ_Trigger;
}
break;
case AT91C_ID_ADC: // Interface is ADC!
if (*Interface_irq_handler != NULL){
((AT91S_ADC)Interface->BASE)->ADC_IER = IRQ_Trigger;
}
break;
case AT91C_ID_IRQ0|AT91C_ID_IRQ1: // Interface is IRQ!
TRACE_ERROR("Attempt to Configure an IRQ as System Peripheral!");
break;
default:
TRACE_ERROR("Attempt to Configure an Undefined IRQ!"); // Unknown Interface!
break;
}
return 0;
}
void ConfigureAIC(Interface_t *Interface, u32_t IRQ_Mode, void (*Interface_irq_handler)(void)){
AIC_ConfigureIT(Interface->ID, IRQ_Mode, *Interface_irq_handler);
AIC_EnableIT(Interface->ID);
}
私はそれをgitにプッシュしました: