stm32 AW9523B IO扩展芯片扩展io口驱动程序

1、核心驱动为:

(1).c 文件

typedef struct
{
    uint8_t InputReg[GPIO_PORT_NUM];
    uint8_t OutputReg[GPIO_PORT_NUM];
    uint8_t ConfigReg[GPIO_PORT_NUM];
    uint8_t IntReg[GPIO_PORT_NUM];
}AW95_REG_T;

 typedef struct
{
    uint8_t IcId;
    uint8_t IrqFlag;
    uint8_t Input[GPIO_PORT_NUM];
    uint8_t Output[GPIO_PORT_NUM];
    uint8_t Config[GPIO_PORT_NUM];
    uint8_t Int[GPIO_PORT_NUM];
}AW95_IC_T;


const AW95_REG_T Aw95Reg = {{P0_INPUT,P1_INPUT},{P0_OUTPUT,P1_OUTPUT},\
                {P0_CONFIG,P1_CONFIG},{P0INT_MSK,P1INT_MSK}};

AW95_IC_T Aw95Ic1 = 
{
    .IcId = 0xB0,
}; 

AW95_IC_T Aw95Ic2 = 
{
    .IcId = 0xB4,
}; 

AW95_IC_T *Aw95IcBuf[GPIO_IC_NUM] = {&Aw95Ic1,&Aw95Ic2};


void EXIO_Init(void)
{
    uint8_t gpio_mode = 0x10;
    
    bsp_InitI2C(); /*模拟iic初始化*/
    GpioConfig(); /*外部中断初始化*/
    I2C_CheckOk(Aw95Ic1.IcId);
    I2C_CheckOk(Aw95Ic2.IcId);
    Aw95_ReadChipId(Aw95Ic1.IcId);
    Aw95_ReadChipId(Aw95Ic2.IcId);
    i2c_WriteBytesResend(Aw95Ic1.IcId,GCR_REG,&gpio_mode,1);/*配置ic1-gpio0为推挽输出模式*/
    i2c_WriteBytesResend(Aw95Ic2.IcId,GCR_REG,&gpio_mode,1);/*配置ic2-gpio0为推挽输出模式*/
}
void EXIO_PinInit(EXIO_IC_E _ic, EXIO_PORT_E _port,EXIO_PIN_E _pin,EXIO_DIRE_E _dire)
{
    AW95_IC_T * point = NULL;

    point = Aw95IcBuf[_ic];
    /*对内部存储值进行修改*/
    if(_dire == EXIO_OUTPUT)
    {
        point->Config[_port] &= ~_pin;
        point->Int[_port] |= _pin;
        
    }
    else
    {
        point->Config[_port] |= _pin;
        point->Int[_port] &= ~_pin;
    }
    /*将输入输出配置值写入到ic中*/
    i2c_WriteBytesResend((uint8_t)point->IcId,(uint8_t)Aw95Reg.ConfigReg[_port],&(point->Config[_port]),1);
    /*将中断状态写入到ic中*/
    i2c_WriteBytesResend((uint8_t)point->IcId,(uint8_t)Aw95Reg.IntReg[_port],&(point->Int[_port]),1);
}


uint8_t EXIO_ReadBit(EXIO_IC_E _ic, EXIO_PORT_E _port,EXIO_PIN_E _pin)
{
    uint8_t bitstatus = 0x00;
    AW95_IC_T * point = NULL;

    point = Aw95IcBuf[_ic];
    if(point->Input[_port] & _pin)
    {
        bitstatus = (uint8_t)Bit_SET;
    }
    else
    {
        bitstatus = (uint8_t)Bit_RESET;
    }
    
    return bitstatus;
}

uint8_t EXIO_ReadByte(EXIO_IC_E _ic, EXIO_PORT_E _port)
{
    AW95_IC_T * point = NULL;

    point = Aw95IcBuf[_ic];

    return point->Input[_port];
}


void EXIO_WriteBit(EXIO_IC_E _ic, EXIO_PORT_E _port,EXIO_PIN_E _pin,BitAction _value)
{
    AW95_IC_T * point = NULL;

    point = Aw95IcBuf[_ic];
    /*对内部存储值进行修改*/
    if(_value == Bit_SET)
    {
        point->Output[_port] |= _pin;
    }
    else
    {
        point->Output[_port] &= ~_pin;
    }
    /*将输出状态写入到ic中*/
    i2c_WriteBytesResend((uint8_t)point->IcId,(uint8_t)Aw95Reg.OutputReg[_port],&(point->Output[_port]),1);
}
void EXIO_Handle(void)
{
    if(Aw95Ic1.IrqFlag == 1)
    {
        Aw95Ic1.IrqFlag = 0;
        i2c_ReadBytesResend(Aw95Ic1.IcId,Aw95Reg.InputReg[EXIO_PORT_0],&(Aw95Ic1.Input[EXIO_PORT_0]),1);
        i2c_ReadBytesResend(Aw95Ic1.IcId,Aw95Reg.InputReg[EXIO_PORT_1],&(Aw95Ic1.Input[EXIO_PORT_1]),1);

    }
    if(Aw95Ic2.IrqFlag == 1)
    {    
        Aw95Ic2.IrqFlag = 0;
        i2c_ReadBytesResend(Aw95Ic2.IcId,Aw95Reg.InputReg[EXIO_PORT_0],&(Aw95Ic2.Input[EXIO_PORT_0]),1);
        i2c_ReadBytesResend(Aw95Ic2.IcId,Aw95Reg.InputReg[EXIO_PORT_1],&(Aw95Ic2.Input[EXIO_PORT_1]),1);
    }    
}

(2).h 文件

#ifndef __AW95_DEV__
#define __AW95_DEV__

/*外部io口ic编号*/
typedef enum
{
    EXIO_IC_1 = 0,
    EXIO_IC_2 = 1,
}EXIO_IC_E;

/*外部io口端口编号*/
typedef enum
{
    EXIO_PORT_0 = 0,
    EXIO_PORT_1 = 1,
}EXIO_PORT_E;

/*外部io口引脚编号*/
typedef enum
{
    EXIO_PIN_0 = 0x01,
    EXIO_PIN_1 = 0x02,
    EXIO_PIN_2 = 0x04,
    EXIO_PIN_3 = 0x08,
    EXIO_PIN_4 = 0x10,
    EXIO_PIN_5 = 0x20,
    EXIO_PIN_6 = 0x40,
    EXIO_PIN_7 = 0x80,
}EXIO_PIN_E;

/*外部io口方向选择*/
typedef enum
{
    EXIO_OUTPUT = 0,
    EXIO_INPUT = 1,
}EXIO_DIRE_E;


void EXIO_Init(void);
void EXIO_Handle(void);
void EXIO_PinInit(EXIO_IC_E _ic, EXIO_PORT_E _port,EXIO_PIN_E _pin,EXIO_DIRE_E _dire);
uint8_t EXIO_ReadBit(EXIO_IC_E _ic, EXIO_PORT_E _port,EXIO_PIN_E _pin);
void EXIO_WriteBit(EXIO_IC_E _ic, EXIO_PORT_E _port,EXIO_PIN_E _pin,BitAction _value);




#endif /*__AW95_DEV__*/

2、关键接口说明

(1)void EXIO_Init(void);

对AW9523B驱动模块进行初始化

(2)void EXIO_Handle(void);

对AW9523B驱动模块进行操作

(3)void EXIO_PinInit(EXIO_IC_E _ic, EXIO_PORT_E _port,EXIO_PIN_E _pin,EXIO_DIRE_E _dire);

初始化AW9523B单个引脚

(4)uint8_t EXIO_ReadBit(EXIO_IC_E _ic, EXIO_PORT_E _port,EXIO_PIN_E _pin);

读取AW9523B单个引脚

(5)void EXIO_WriteBit(EXIO_IC_E _ic, EXIO_PORT_E _port,EXIO_PIN_E _pin,BitAction _value);

读取AW9523B单个端口

3、关键数据结构说明

typedef struct
{
    uint8_t IcId;
    uint8_t IrqFlag;
    uint8_t Input[GPIO_PORT_NUM];
    uint8_t Output[GPIO_PORT_NUM];
    uint8_t Config[GPIO_PORT_NUM];
    uint8_t Int[GPIO_PORT_NUM];
}AW95_IC_T;

用于存储单个ic中的数据,如需管理更多的结构体,可以再增加定义

4、如需完整工程请联系

[email protected] 

你可能感兴趣的:(stm32,stm32,单片机,嵌入式硬件)