GPIO定义可参考方法

//
typedef enum 
{
    eLED_0,
    eLED_1,
    eLED_2,   
    eGPIO_MAX_COUNT,
}GPIO_ENUM_t;

//端口结构体
typedef const struct 
{
    GPIO_TypeDef    *port;      //IO 时钟
    uint32_t        clk;        //IO 端口
    uint16_t        pin;        //IO 引脚
    uint8_t         state;      //IO 输入输出状态 0)输出 1)输入 2)上拉输入 3)下拉输入 4)开漏输出
    uint8_t         id;         //IO id号
}PORT_INFO_t;


//端口定义 
static PORT_INFO_t m_PortInfo[] = 
{
    {GPIOF, RCC_APB2Periph_GPIOF,  GPIO_Pin_6,     0,     eLED_0},   
    {GPIOF, RCC_APB2Periph_GPIOF,  GPIO_Pin_7,     0,     eLED_1}, 
    {GPIOF, RCC_APB2Periph_GPIOF,  GPIO_Pin_8,     0,     eLED_2},
};

//端口个数
static uc16 m_PortCount = BSP_CONEOF(m_PortInfo);


/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    PORT_INFO_t * pPort = m_PortInfo;
    uint8_t i = 0;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    for (i = 0; i < m_PortCount; i++, pPort++)
    {
        RCC_APB2PeriphClockCmd(pPort->clk, ENABLE);
        GPIO_InitStructure.GPIO_Pin = pPort->pin;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;

        if (pPort->state == 0) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        else if (pPort->state == 1) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        else if (pPort->state == 2) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        else if (pPort->state == 3) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
        else if (pPort->state == 4) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
        else if (pPort->state == 5) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

        GPIO_Init(pPort->port, &GPIO_InitStructure);
    }
}

你可能感兴趣的:(STM32)