參考
struct MyInterface {
void (*foo)();
void (*bar)();
};
void foo_impl() {
printf("Inside foo implementation.\n");
}
void bar_impl() {
printf("Inside bar implementation.\n");
}
int main() {
struct MyInterface my_interface;
my_interface.foo = foo_impl;
my_interface.bar = bar_impl;
my_interface.foo();
my_interface.bar();
return 0;
}
===========================================
函數原型
void I2C2_Initialize(void);
void I2C2_WriteByte(uint8_t wrByte);
uint8_t I2C2_ReadByte(void);
void I2C2_CallbackRegister(bool (*callback)(i2c_client_transfer_event_t clientEvent));
介面宣告
struct I2C_CLIENT_INTERFACE
{
void (*Initialize)(void);
void (*WriteByte)(uint8_t wrByte);
uint8_t(*ReadByte)(void);
void (*CallbackRegister)(bool (*handler)(i2c_client_transfer_event_t clientEvent));
void (*Tasks)(void);
};
程式定義橋接
const struct I2C_CLIENT_INTERFACE I2C2_Client = {
.Initialize = I2C2_Initialize,
.WriteByte = I2C2_WriteByte,
.ReadByte = I2C2_ReadByte,
.CallbackRegister = I2C2_CallbackRegister,
.Tasks = NULL
};
之後可以使用以下方式撰寫
I2C2_Client.WriteByte(0X55);
===================================
陣列行介面宣告,必須驗證其結果
typedef enum {
I2C1_IDLE = 0,
I2C1_SEND_ADR_READ,
I2C1_SEND_ADR_WRITE,
I2C1_TX,
I2C1_RX,
I2C1_RCEN,
I2C1_TX_EMPTY,
I2C1_SEND_RESTART_READ,
I2C1_SEND_RESTART_WRITE,
I2C1_SEND_RESTART,
I2C1_SEND_STOP,
I2C1_RX_ACK,
I2C1_RX_NACK_STOP,
I2C1_RX_NACK_RESTART,
I2C1_RESET,
I2C1_ADDRESS_NACK,
} i2c1_fsm_states_t;
static i2c1_fsm_states_t I2C1_DO_IDLE(void);
static i2c1_fsm_states_t I2C1_DO_SEND_ADR_READ(void);
static i2c1_fsm_states_t I2C1_DO_SEND_ADR_WRITE(void);
static i2c1_fsm_states_t I2C1_DO_TX(void);
static i2c1_fsm_states_t I2C1_DO_RX(void);
static i2c1_fsm_states_t I2C1_DO_RCEN(void);
static i2c1_fsm_states_t I2C1_DO_TX_EMPTY(void);
static i2c1_fsm_states_t I2C1_DO_SEND_RESTART_READ(void);
static i2c1_fsm_states_t I2C1_DO_SEND_RESTART_WRITE(void);
static i2c1_fsm_states_t I2C1_DO_SEND_RESTART(void);
static i2c1_fsm_states_t I2C1_DO_SEND_STOP(void);
static i2c1_fsm_states_t I2C1_DO_RX_ACK(void);
static i2c1_fsm_states_t I2C1_DO_RX_NACK_STOP(void);
static i2c1_fsm_states_t I2C1_DO_RX_NACK_RESTART(void);
static i2c1_fsm_states_t I2C1_DO_RESET(void);
static i2c1_fsm_states_t I2C1_DO_ADDRESS_NACK(void);
static i2c1_fsm_states_t I2C1_DO_IDLE(void)
{
I2C1_Status.busy = false;
I2C1_Status.error = I2C1_NOERR;
return I2C1_RESET;
}
typedef i2c1_fsm_states_t (*i2c1FsmHandler)(void);
const i2c1FsmHandler i2c1_fsmStateTable[] = {
I2C1_DO_IDLE,
I2C1_DO_SEND_ADR_READ,
I2C1_DO_SEND_ADR_WRITE,
I2C1_DO_TX,
I2C1_DO_RX,
I2C1_DO_RCEN,
I2C1_DO_TX_EMPTY,
I2C1_DO_SEND_RESTART_READ,
I2C1_DO_SEND_RESTART_WRITE,
I2C1_DO_SEND_RESTART,
I2C1_DO_SEND_STOP,
I2C1_DO_RX_ACK,
I2C1_DO_RX_NACK_STOP,
I2C1_DO_RX_NACK_RESTART,
I2C1_DO_RESET,
I2C1_DO_ADDRESS_NACK,
}; |