STM32F4 HAL库中如何实现UART IO配置的?HAL_UART_MspInit有两个,__weak修饰的函数是将当前文件的对应函数声明为弱函数符号,如果外部文件出现相同的函数名,最终外部优先

本文详细介绍了STM32 HAL库中如何配置UART外设,包括在用户文件中重写HAL_UART_MspInit函数以定制硬件资源。在STM32F4xx_HAL_Msp.c中,该函数通过间接包含关系被调用,其弱实现允许用户在自己的文件中进行重写。重写后的函数主要负责开启GPIO和USART时钟,配置GPIO引脚模式和速度,以满足特定应用的需求。
摘要由CSDN通过智能技术生成

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 }

STM32F4 HAL库中是如何实现UART IO配置的? - Tony.Jia - 博客园

/* 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
   */ 

/*防止未使用的参数编译警告*/

未使用(huart);

/*注意:当需要回调时,不应修改此函数,

HAL\u UART\u MspInit可以在用户文件中实现

*/

这就是为什么用户文件中这个函数在调用函数后面定义而不报警告的原因,在库中事先定义声明了,此类函数还很多比如HAL_UART_RxCpltCallback()等等

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值