大碴子味 发表于 2020-4-25 10:38

STM32库函数个人浅析实现之以void GPIO_Init()开始

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当前IO配置模式 ; currentpin 当前pin脚; tmpreg目标寄存器;
/* 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));

/* **********************************************************************
typedef enum
{ GPIO_Mode_AIN = 0x0,                        //      0000   0000    模拟输入
GPIO_Mode_IN_FLOATING = 0x04,          //      0000   0100    浮空输入
GPIO_Mode_IPD = 0x28,                        //         0010   1000    下拉输入
GPIO_Mode_IPU = 0x48,                        //         0100   1000    上拉输入
GPIO_Mode_Out_OD = 0x14,                  //         0001   0100   通用开漏输出
GPIO_Mode_Out_PP = 0x10,                  //          0001   0000   通用推挽输出
GPIO_Mode_AF_OD = 0x1C,                   //         0001   1100   复用开漏输出
GPIO_Mode_AF_PP = 0x18                     //         0001   1000   复用推挽输出
}GPIOMode_TypeDef;




***********************************************************************/
/*---------------------------- GPIO Mode Configuration -----------------------*/
currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
   //cureenmode = 0000 xx00 ;   00 为 mode值,   xx 为CNF值;
   // GPIO_InitStruct->GPIO_Mode 为输入模式时(GPIO_Mode_AIN,GPIO_Mode_IN_FLOATING,GPIO_Mode_IPD,GPIO_Mode_IPU),xx00 为设置数据;
   //00表明输入模式,xx 为具体输入模式;

if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)//判断是否是输出模式;
// GPIO_InitStruct->GPIO_Mode   的 bit4 为 1时,即为 输出模式 时,进入if
{
    /* Check the parameters */
    assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
    /* Output mode */
    currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;    //currentmode =0000 xxyy ;xx 与 yy 分别取决于初始化 GPIO_Mode 设置 与 speed设置;   //即 GPIO_InitStruct 结构体 初始化数据 ; yy 表明为输出 ,xx为具体输出模式;
}

/*---------------------------- GPIO CRL Configuration ------------------------*/
/* Configure the eight low port pins */
if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)//GPIO_Pin 为 0~7 时,使用CRL 寄存器;
{
    tmpreg = GPIOx->CRL;//当前 CRL数据放到 tmpreg中;
页: [1]
查看完整版本: STM32库函数个人浅析实现之以void GPIO_Init()开始