xyc666 发表于 2022-11-22 09:38:44

请问C学到什么程度才能开始学板子呀

请问C学到什么程度才能开始学板子呀

eric2013 发表于 2022-11-22 10:14:31

HAL库有个GPIO_Init函数,你能把这个函数的实现捋顺,我觉得大部分代码都可以了
/**
* @briefInitializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
* @paramGPIOx: where x can be (A..K) to select the GPIO peripheral.
* @paramGPIO_Init: pointer to a GPIO_InitTypeDef structure that contains
*         the configuration information for the specified GPIO peripheral.
* @retval None
*/
void HAL_GPIO_Init(GPIO_TypeDef*GPIOx, GPIO_InitTypeDef *GPIO_Init)
{
uint32_t position = 0x00U;
uint32_t iocurrent;
uint32_t temp;
EXTI_Core_TypeDef *EXTI_CurrentCPU;

#if defined(DUAL_CORE) && defined(CORE_CM4)
EXTI_CurrentCPU = EXTI_D2; /* EXTI for CM4 CPU */
#else
EXTI_CurrentCPU = EXTI_D1; /* EXTI for CM7 CPU */
#endif

/* Check the parameters */
assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
assert_param(IS_GPIO_PULL(GPIO_Init->Pull));

/* Configure the port pins */
while (((GPIO_Init->Pin) >> position) != 0x00U)
{
    /* Get current io position */
    iocurrent = (GPIO_Init->Pin) & (1UL << position);

    if (iocurrent != 0x00U)
    {
      /*--------------------- GPIO Mode Configuration ------------------------*/
      /* In case of Output or Alternate function mode selection */
      if ((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
          (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
      {
      /* Check the Speed parameter */
      assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
      /* Configure the IO Speed */
      temp = GPIOx->OSPEEDR;
      temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2U));
      temp |= (GPIO_Init->Speed << (position * 2U));
      GPIOx->OSPEEDR = temp;

      /* Configure the IO Output Type */
      temp = GPIOx->OTYPER;
      temp &= ~(GPIO_OTYPER_OT0 << position) ;
      temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
      GPIOx->OTYPER = temp;
      }

      /* Activate the Pull-up or Pull down resistor for the current IO */
      temp = GPIOx->PUPDR;
      temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2U));
      temp |= ((GPIO_Init->Pull) << (position * 2U));
      GPIOx->PUPDR = temp;

      /* In case of Alternate function mode selection */
      if ((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
      {
      /* Check the Alternate function parameters */
      assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
      assert_param(IS_GPIO_AF(GPIO_Init->Alternate));

      /* Configure Alternate function mapped with the current IO */
      temp = GPIOx->AFR;
      temp &= ~(0xFU << ((position & 0x07U) * 4U));
      temp |= ((GPIO_Init->Alternate) << ((position & 0x07U) * 4U));
      GPIOx->AFR = temp;
      }

      /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
      temp = GPIOx->MODER;
      temp &= ~(GPIO_MODER_MODE0 << (position * 2U));
      temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
      GPIOx->MODER = temp;

      /*--------------------- EXTI Mode Configuration ------------------------*/
      /* Configure the External Interrupt or event for the current IO */
      if ((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
      {
      /* Enable SYSCFG Clock */
      __HAL_RCC_SYSCFG_CLK_ENABLE();

      temp = SYSCFG->EXTICR;
      temp &= ~(0x0FUL << (4U * (position & 0x03U)));
      temp |= (GPIO_GET_INDEX(GPIOx) << (4U * (position & 0x03U)));
      SYSCFG->EXTICR = temp;

      /* Clear EXTI line configuration */
      temp = EXTI_CurrentCPU->IMR1;
      temp &= ~(iocurrent);
      if ((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
      {
          temp |= iocurrent;
      }
      EXTI_CurrentCPU->IMR1 = temp;

      temp = EXTI_CurrentCPU->EMR1;
      temp &= ~(iocurrent);
      if ((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
      {
          temp |= iocurrent;
      }
      EXTI_CurrentCPU->EMR1 = temp;

      /* Clear Rising Falling edge configuration */
      temp = EXTI->RTSR1;
      temp &= ~(iocurrent);
      if ((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
      {
          temp |= iocurrent;
      }
      EXTI->RTSR1 = temp;

      temp = EXTI->FTSR1;
      temp &= ~(iocurrent);
      if ((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
      {
          temp |= iocurrent;
      }
      EXTI->FTSR1 = temp;
      }
    }

    position++;
}
}

killalljp 发表于 2022-11-23 09:00:26

用过标准库的稍微查一下相关寄存器表应该就懂了

armcc 发表于 2022-11-30 15:05:38

eric2013 发表于 2022-11-22 10:14
HAL库有个GPIO_Init函数,你能把这个函数的实现捋顺,我觉得大部分代码都可以了
/**
...

我也想问这个问题,没想到楼主捷足先登了。:lol
页: [1]
查看完整版本: 请问C学到什么程度才能开始学板子呀