[STM32] STM32 GPIO_Init函数为什么传入的是结构体指针而不是变量

[复制链接]
 楼主| skw168 发表于 2022-5-24 09:46 | 显示全部楼层 |阅读模式
本帖最后由 skw168 于 2022-5-24 09:46 编辑

STM32 固件库GPIO初始化函数void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

1.此函数的形式参数中,为什么传入的是GPIO_InitTypeDef* GPIO_InitStruct指针,而不是结构体变量;
因为我感觉传入GPIO_InitTypeDef变量也能完成同样的功能

函数调用:        GPIO_Init(AMP_PORT, &GPIO_InitStructure);        

函数原型:
  1. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
  2. {
  3.   uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;

  4.   /* Check the parameters */
  5.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  6.   assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
  7.   assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  8.   assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));

  9.   /*-------------------------- Configure the port pins -----------------------*/
  10.   /*-- GPIO Mode Configuration --*/
  11.   for (pinpos = 0x00; pinpos < 0x10; pinpos++)
  12.   {
  13.     pos = ((uint32_t)0x01) << pinpos;

  14.     /* Get the port pins position */
  15.     currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;

  16.     if (currentpin == pos)
  17.     {
  18.       if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))
  19.       {
  20.         /* Check Speed mode parameters */
  21.         assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));

  22.         /* Speed mode configuration */
  23.         GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));
  24.         GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));

  25.         /* Check Output mode parameters */
  26.         assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));

  27.         /* Output mode configuration */
  28.         GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos));
  29.         GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));
  30.       }

  31.       GPIOx->MODER  &= ~(GPIO_MODER_MODER0 << (pinpos * 2));

  32.       GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));

  33.       /* Pull-up Pull down resistor configuration */
  34.       GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));
  35.       GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));
  36.     }
  37.   }
  38. }


elec0010 发表于 2022-5-24 10:02 | 显示全部楼层
结构体传指针,速度非常快,传内容需要额外的复制开销

评论

有道理!!  发表于 2022-5-24 18:25
zwsam 发表于 2022-5-25 08:59 | 显示全部楼层
学习了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

37

主题

137

帖子

6

粉丝
快速回复 返回顶部 返回列表