STM32 GPIO之 GPIO_Init() 函数

来源:互联网 发布:免费企业网管软件 编辑:程序博客网 时间:2024/05/21 10:49

今天,我们来分析一下GPIO_Init() 函数的实现,看看ST的工程师是如何实现初始化端口的(不对地方,请大家多多指正)。

每个GPIO端口有16个Pin, 每个端口的配置 有两个寄存器,高位和低位寄存器配置。
来看看,两个寄存器,这个函数主要就是对这两个寄存器的操作。
CRH
CRL

可以看到寄存器中每四位设置一个PIN。
在输入模式(MODE[1:0]=00):
00:模拟输入模式
01:浮空输入模式(复位后的状态)
10:上拉/下拉输入模式
11:保留
在输出模式(MODE[1:0]>00):
00:通用推挽输出模式
01:通用开漏输出模式
10:复用功能推挽输出模式
11:复用功能开漏输出模式
MODEy[1:0]:端口x的模式位(y = 8…15)
软件通过这些位配置相应的I/O端口,请参考表15端口位配置表。
00:输入模式(复位后的状态)
01:输出模式,最大速度10MHz
10:输出模式,最大速度2MHz
11:输出模式,最大速度50MHz
从上面,我们可以知道 :
***MODEy[1:0]: 来确定输入输出模式(在输出模式的时候设置速度)
CNFy[1:0] 根据MODEy里面的值 设置IO口 上拉,下拉,模拟,推挽。*
现在,贴出程序(下面的不是ST的源程序,我删减了些,在本文的最后,贴出源程序,现在只是为了方便讲解):
(下面的程序中,没有设置上/下拉时引脚的值,在本文最后贴出的程序是完整的)

void GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_InitStruct){   uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;  uint32_t tmpreg = 0x00, pinmask = 0x00;   //先把模式值得到,    currentmode=(uint32_t)(GPIO_InitStruct->GPIO_Mode)&(uint32_t)0x0F;      //判断是否为输出模式,如果是输出模式 则要设置输出速率      if((((uint32_t)GPIO_InitStruct->GPIO_Mode)&((uint32_t)0x10))!=0x00)        {           currentmode |=((uint32_t)GPIO_InitStruct->GPIO_Speed);        }        //判断是低/高位       if(((uint32_t)GPIO_InitStruct->GPIO_Pin)&((uint32_t)0x00FF) !=0x00)        {           tmpreg=(uint32_t)GPIOx->CRL;  //得到当前寄存器的值           for(pinpos=0x0;pinpos<0x08;pinpos++)            {              pos=(uint32_t)0x01<<pinpos;              currentpin=(uint32_t)GPIO_InitStruct->GPIO_Pin & pos;              if(currentpin==pos)                {                   pos=pinpos<<2;                   //清楚寄存器中要设置的位,其余的不变,就是这里把其余位的值保留下来了                   pinmask=((uint32_t)0x0F)<<pos;                    tmpreg &= ~pinmask;                   tmpreg |=currentmode<<pos;                }            }           GPIOx->CRL=tmpreg;        }   }

currentmode=(uint32_t)(GPIO_InitStruct->GPIO_Mode)&(uint32_t)0x0F;
这句,通过位 与 得到当前的模式。
我们来看看GPIO_Mode 的 枚举值 ,
typedef enum
{ GPIO_Mode_AIN = 0x0, //模拟输入
GPIO_Mode_IN_FLOATING = 0x04,//浮空输入
GPIO_Mode_IPD = 0x28, //上拉输入
GPIO_Mode_IPU = 0x48,//上拉输入
GPIO_Mode_Out_OD = 0x14, //通用开漏输出
GPIO_Mode_Out_PP = 0x10,//通用推挽输出
GPIO_Mode_AF_OD = 0x1C,//复用开漏输出
GPIO_Mode_AF_PP = 0x18//复用推挽输出
}GPIOMode_TypeDef;

解释下 这些枚举的具体值:
由于每个pin的设置 Bit 只有四位,上面的有值实际在填寄存器的值的时候是没有用的,可是这些值在做判断如何填的时候大有用处。
GPIO_Mode_IPD = 0x28, //上拉输入
GPIO_Mode_IPU = 0x48,//上拉输入
2,4是用来区分上/下拉的,因为上拉,下拉的初始值是不一样的,
GPIO_Mode_Out_OD = 0x14, //通用开漏输出
GPIO_Mode_Out_PP = 0x10,//通用推挽输出
GPIO_Mode_AF_OD = 0x1C,//复用开漏输出
GPIO_Mode_AF_PP = 0x18//复用推挽输出

0x1x ,使用来在程序来判断 输出输出。

if((((uint32_t)GPIO_InitStruct->GPIO_Mode)&((uint32_t)0x10))!=0x00)
用来判断是否是 输出模式。
如果是输出模式,执行下面这句:
currentmode |=((uint32_t)GPIO_InitStruct->GPIO_Speed);
把输出速度也加进来。

现在,我们把 要设置引脚的模式的值 放在了 currentmode里了。
只要把这个值填到,引脚对应 CRL/CRH中个的相应位,就行了。
if(((uint32_t)GPIO_InitStruct->GPIO_Pin)&((uint32_t)0x00FF) !=0x00)
判断是高位八位还是低八位.
如果是低八位的话:
tmpreg=(uint32_t)GPIOx->CRL; //得到当前寄存器的值。
for(pinpos=0x0;pinpos<0x08;pinpos++)
八次循环刚好把 八个引脚设置好。
***pos=(uint32_t)0x01<

define GPIO_Pin_1 ((uint16_t)0x0002) /!< Pin 1 selected /

define GPIO_Pin_2 ((uint16_t)0x0004) /!< Pin 2 selected /

define GPIO_Pin_3 ((uint16_t)0x0008) /!< Pin 3 selected /

define GPIO_Pin_4 ((uint16_t)0x0010) /!< Pin 4 selected /

define GPIO_Pin_5 ((uint16_t)0x0020) /!< Pin 5 selected /

define GPIO_Pin_6 ((uint16_t)0x0040) /!< Pin 6 selected /

define GPIO_Pin_7 ((uint16_t)0x0080) /!< Pin 7 selected /

define GPIO_Pin_8 ((uint16_t)0x0100) /!< Pin 8 selected /

define GPIO_Pin_9 ((uint16_t)0x0200) /!< Pin 9 selected /

define GPIO_Pin_10 ((uint16_t)0x0400) /!< Pin 10 selected /

define GPIO_Pin_11 ((uint16_t)0x0800) /!< Pin 11 selected /

define GPIO_Pin_12 ((uint16_t)0x1000) /!< Pin 12 selected /

define GPIO_Pin_13 ((uint16_t)0x2000) /!< Pin 13 selected /

define GPIO_Pin_14 ((uint16_t)0x4000) /!< Pin 14 selected /

define GPIO_Pin_15 ((uint16_t)0x8000) /!< Pin 15 selected /

define GPIO_Pin_All ((uint16_t)0xFFFF) /!< All pins selected

从上面的值,我们就可以理解,这三句话在做什么
if成立执行:
pos=pinpos<<2; 左移两位,等于乘以4 ,

***pinmask=((uint32_t)0x0F)<

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct){  uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;  uint32_t tmpreg = 0x00, pinmask = 0x00;  /* Check the parameters */  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));  assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));  assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));  /*---------------------------- GPIO Mode Configuration -----------------------*/  currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);  if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)  {     /* Check the parameters */    assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));    /* Output mode */    currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;  }/*---------------------------- GPIO CRL Configuration ------------------------*/  /* Configure the eight low port pins */  if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)  {    tmpreg = GPIOx->CRL;     for (pinpos = 0x00; pinpos < 0x08; pinpos++)    {      pos = ((uint32_t)0x01) << pinpos;      /* Get the port pins position */      currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;      if (currentpin == pos)      {        pos = pinpos << 2;        /* Clear the corresponding low control register bits */        pinmask = ((uint32_t)0x0F) << pos;        tmpreg &= ~pinmask;        /* Write the mode configuration in the corresponding bits */        tmpreg |= (currentmode << pos);        /* Reset the corresponding ODR bit */        if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)        {          GPIOx->BRR = (((uint32_t)0x01) << pinpos);        }        else        {          /* Set the corresponding ODR bit */          if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)          {            GPIOx->BSRR = (((uint32_t)0x01) << pinpos);          }        }      }    }    GPIOx->CRL = tmpreg;        }/*---------------------------- GPIO CRH Configuration ------------------------*/  /* Configure the eight high port pins */  if (GPIO_InitStruct->GPIO_Pin > 0x00FF)  {    tmpreg = GPIOx->CRH;    for (pinpos = 0x00; pinpos < 0x08; pinpos++)    {      pos = (((uint32_t)0x01) << (pinpos + 0x08));      /* Get the port pins position */      currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);      if (currentpin == pos)      {        pos = pinpos << 2;        /* Clear the corresponding high control register bits */        pinmask = ((uint32_t)0x0F) << pos;        tmpreg &= ~pinmask;        /* Write the mode configuration in the corresponding bits */        tmpreg |= (currentmode << pos);        /* Reset the corresponding ODR bit */        if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)        {          GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08));        }        /* Set the corresponding ODR bit */        if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)        {          GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08));        }      }    }    GPIOx->CRH = tmpreg;  }}
0 0
原创粉丝点击