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

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
    评论
HAL_UART_MspInit是一个函数,它在stm32f4xx_hal_uart.h头文件声明和定义。在这个函数,我们可以对UART进行初始化和配置。这个函数是在文件使用__weak关键字定义的,这意味着它可以在用户文件进行自定义实现。这个函数的参数是一个指向UART_HandleTypeDef结构的指针,该结构包含了UART模块的配置信息。在这个函数,我们可以对UART的时钟使能、GPIO引脚配置等进行初始化操作。根据具体需求,用户可以在自己的文件实现这个函数以进行特定的配置和初始化操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [HAL_UART_MspInit有两个,__weak修饰函数是将当前文件对应函数声明函数符号,如果外部文件出现相同...](https://blog.csdn.net/qq_25814297/article/details/125870262)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [[STM32] NOTE07-STM32使用HAL之Msp回调函数理解](https://blog.csdn.net/chenhanxuan1999/article/details/101225079)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值