GD32中的初始化函数中不要有 XXXX_Init()的形式

[复制链接]
 楼主| sunmeat 发表于 2014-11-3 17:37 | 显示全部楼层 |阅读模式
以前习惯写函数时,把初始化函数写成GPIO_Init()/UART_Init()的形式,这样一目了然,可是ST的库中有这样的定义
  1. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
  2. {
  3.   uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
  4.   uint32_t tmpreg = 0x00, pinmask = 0x00;
  5.   /* Check the parameters */
  6.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  7.   assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  8.   assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));  
  9.   
  10. /*---------------------------- GPIO Mode Configuration -----------------------*/
  11.   currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
  12.   if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00) //输出模式
  13.   {
  14.     /* Check the parameters */
  15.     assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
  16.     /* Output mode */
  17.     currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
  18.   }
  19. /*---------------------------- GPIO CRL Configuration ------------------------*/  //低8位
  20.   /* Configure the eight low port pins */
  21.   if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00) //引脚有定义
  22.   {
  23.     tmpreg = GPIOx->CRL;
  24.     for (pinpos = 0x00; pinpos < 0x08; pinpos++)
  25.     {
  26.       pos = ((uint32_t)0x01) << pinpos;
  27.       /* Get the port pins position */
  28.       currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
  29.       if (currentpin == pos)         //判断每一个脚
  30.       {
  31.         pos = pinpos << 2;
  32.         /* Clear the corresponding low control register bits */
  33.         pinmask = ((uint32_t)0x0F) << pos;
  34.         tmpreg &= ~pinmask;
  35.         /* Write the mode configuration in the corresponding bits */
  36.         tmpreg |= (currentmode << pos);
  37.         /* Reset the corresponding ODR bit */
  38.         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)   // 端口置为高电平
  39.         {
  40.           GPIOx->BRR = (((uint32_t)0x01) << pinpos);
  41.         }
  42.         else
  43.         {
  44.           /* Set the corresponding ODR bit */
  45.           if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)        // 端口清0
  46.           {
  47.             GPIOx->BSRR = (((uint32_t)0x01) << pinpos);
  48.           }
  49.         }
  50.       }
  51.     }
  52.     GPIOx->CRL = tmpreg;
  53.   }
  54. /*---------------------------- GPIO CRH Configuration ------------------------*/
  55.   /* Configure the eight high port pins */
  56.   if (GPIO_InitStruct->GPIO_Pin > 0x00FF)
  57.   {
  58.     tmpreg = GPIOx->CRH;
  59.     for (pinpos = 0x00; pinpos < 0x08; pinpos++)
  60.     {
  61.       pos = (((uint32_t)0x01) << (pinpos + 0x08));
  62.       /* Get the port pins position */
  63.       currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
  64.       if (currentpin == pos)
  65.       {
  66.         pos = pinpos << 2;
  67.         /* Clear the corresponding high control register bits */
  68.         pinmask = ((uint32_t)0x0F) << pos;
  69.         tmpreg &= ~pinmask;
  70.         /* Write the mode configuration in the corresponding bits */
  71.         tmpreg |= (currentmode << pos);
  72.         /* Reset the corresponding ODR bit */
  73.         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
  74.         {
  75.           GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08));
  76.         }
  77.         /* Set the corresponding ODR bit */
  78.         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
  79.         {
  80.           GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08));
  81.         }
  82.       }
  83.     }
  84.     GPIOx->CRH = tmpreg;
  85.   }
  86. }
所以,我们在写初始化函数的时候可以写成如下这样的形式
  1. void GPIO_config(void)
  2. {
  3.   GPIO_InitTypeDef GPIO_InitStructure;
  4.        
  5.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);//


  6.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//只初始化PC6
  7.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  8.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//通用外推输出
  9.   GPIO_Init(GPIOC, &GPIO_InitStructure);//PC6控制IO       
  10.        
  11. }



 楼主| sunmeat 发表于 2014-11-3 17:42 | 显示全部楼层
补充:Systick中是不可以有 Systick_Config的形式,在库中有这样的形式
  1. static __INLINE uint32_t SysTick_Config(uint32_t ticks)
  2. {
  3.   if (ticks > SysTick_LOAD_RELOAD_Msk)  return (1);            /* Reload value impossible */
  4.                                                                
  5.   SysTick->LOAD  = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;      /* set reload register */
  6.   NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);  /* set Priority for Cortex-M0 System Interrupts */
  7.   SysTick->VAL   = 0;                                          /* Load the SysTick Counter Value */
  8.   SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
  9.                    SysTick_CTRL_TICKINT_Msk   |
  10.                    SysTick_CTRL_ENABLE_Msk;                    /* Enable SysTick IRQ and SysTick Timer */
  11.   return (0);                                                  /* Function successful */
  12. }
 楼主| sunmeat 发表于 2014-11-3 17:43 | 显示全部楼层
可以写成Systick_Init()的形式
  1. void SysTick_Init(void)
  2. {
  3.         if(SysTick_Config(72000000 / 1000)) //注意:3.5库中 SystemFrequency 被 SystemCoreClock 取代。
  4.         while(1);
  5. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

208

主题

2132

帖子

13

粉丝
快速回复 在线客服 返回列表 返回顶部

208

主题

2132

帖子

13

粉丝
快速回复 在线客服 返回列表 返回顶部