• STM32F4 HAL库中是如何实现UART IO配置的?


    1.配置串口IO、中断等底层的东西需要在用户文件中重写HAL_UART_MspInit函数
    2.hal库是在stm32f4xx_hal_msp.c文件中重写的HAL_UART_MspInit函数,分析如下:
    stm32f4xx_hal_msp.c通过间接方式最终包含了stm32f4xx_hal_uart.h,包含关系如下:
    stm32f4xx_hal_msp.c
    -->#include "main.h"
      -->#include "stm32f4xx_hal.h"
        -->#include "stm32f4xx_hal_conf.h"
          -->#define HAL_UART_MODULE_ENABLED //是否使用串口模块的开关,打开开关则包含头文件 #include "stm32f4xx_hal_uart.h"


    3.stm32f4xx_hal_uart.h头文件中有HAL_UART_MspInit函数的声明
    -->void HAL_UART_MspInit(UART_HandleTypeDef *huart);//定义在stm32f4xx_hal_uart.h头文件中


    4.stm32f4xx_hal_uart.c源文件中有HAL_UART_MspInit的弱实现

    -->__weak修饰的函数其作用是将当前文件的对应函数声明为弱函数符号,如果外部文件出现相同的函数名,最终编译出来的
    文件会优先指向外部文件的函数符号;因此HAL_UART_MspInit函数实现了重写。

    /**
    * @brief UART MSP Init.
    * @param huart Pointer to a UART_HandleTypeDef structure that contains
    * the configuration information for the specified UART module.
    * @retval None
    */
    __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
    {
    /* Prevent unused argument(s) compilation warning */
    UNUSED(huart);
    /* NOTE: This function should not be modified, when the callback is needed,
    the HAL_UART_MspInit could be implemented in the user file
    */

    }

    4.重写后的函数 

     1 /**
     2   * @brief UART MSP Initialization 
     3   *        This function configures the hardware resources used in this example: 
     4   *           - Peripheral's clock enable
     5   *           - Peripheral's GPIO Configuration  
     6   * @param huart: UART handle pointer
     7   * @retval None
     8   */
     9 void HAL_UART_MspInit(UART_HandleTypeDef *huart)
    10 {  
    11     GPIO_InitTypeDef  GPIO_InitStruct;
    12 
    13     /*##-1- Enable peripherals and GPIO Clocks #################################*/
    14     /* Enable GPIO TX/RX clock */
    15     USARTx_TX_GPIO_CLK_ENABLE();
    16     USARTx_RX_GPIO_CLK_ENABLE();
    17     /* Enable USART2 clock */
    18     USARTx_CLK_ENABLE(); 
    19 
    20     /*##-2- Configure peripheral GPIO ##########################################*/  
    21     /* UART TX GPIO pin configuration  */
    22     GPIO_InitStruct.Pin       = USARTx_TX_PIN;
    23     GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
    24     GPIO_InitStruct.Pull      = GPIO_NOPULL;
    25     GPIO_InitStruct.Speed     = GPIO_SPEED_FAST;
    26     GPIO_InitStruct.Alternate = USARTx_TX_AF;
    27 
    28     HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
    29 
    30     /* UART RX GPIO pin configuration  */
    31     GPIO_InitStruct.Pin = USARTx_RX_PIN;
    32     GPIO_InitStruct.Alternate = USARTx_RX_AF;
    33 
    34     HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
    35 }
  • 相关阅读:
    DSP 数学工具回顾:从无穷级数 到 快速傅立叶变换
    用c++设计音效插件 : 10 基础DSP理论 (第一部分)
    用c++设计音效插件 : 10 基础DSP理论 (第二部分)
    用c++设计音效插件 :6 ASPiK Programming Guide
    python 如何在多层循环中使用break/continue
    李沐 如何判断(你自己的)研究工作的价值
    SpringBoot 2.x集成Elasticsearch
    SpringBoot 2.x集成AWS S3对象存储
    关于pycharm一直index这件事
    docker 安装 nacos
  • 原文地址:https://www.cnblogs.com/TonyJia/p/13513110.html
Copyright © 2020-2023  润新知