HAL库_源码阅读

HAL_Lib

  • 1> 函数入口
    • 1.1> 启动文件 startup_stm32f407xx.s
    • 1.2> 复位函数 Reset_Handler()
    • 1.3> 初始化函数 Systeminit()
  • 2> HAL库_模块配置
  • 3> __weak作用
  • 4> 中断处理
  • 5> 容易忽略的宏

库版本:STM32Cube_FW_F4_V1.25.0

1> 函数入口

HAL库_源码阅读_第1张图片

1.1> 启动文件 startup_stm32f407xx.s

》配置异常向量表;

HAL库_源码阅读_第2张图片

1.2> 复位函数 Reset_Handler()

》复位函数,调用子函数 【Systeminit】,再调用【__main】 ;
__main -> main

;;----------------------------- Reset handler ------------------------------------;;
Reset_Handler    PROC
                 EXPORT  Reset_Handler             [WEAK]
        IMPORT  SystemInit
        IMPORT  __main

                 LDR     R0, =SystemInit
                 BLX     R0
                 LDR     R0, =__main
                 BX      R0
                 ENDP

1.3> 初始化函数 Systeminit()

void SystemInit(void)
{

/* FPU settings 配置浮点运算单元---------------*/
/* External memory 配置外部SDRAM---------------*/
/* Configure the Vector Table location add offset address 配置异常向量表地址-------------*/

}

会改变吗:,在CubeMx工具配置过程中?

2> HAL库_模块配置

文件:stm32f4xx_hal_conf.h

> 重点 : HAL_GPIO_MODULE_ENABLED
> 
#ifndef __STM32F4xx_HAL_CONF_H
#define __STM32F4xx_HAL_CONF_H


/* 开启模块 */

#define HAL_GPIO_MODULE_ENABLED

/* 未开启的模块 */
/* #define HAL_ADC_MODULE_ENABLED   */
/* #define HAL_CRYP_MODULE_ENABLED   */

// 开启那个模块,实际就是把那个模块头文件包含
#ifdef HAL_GPIO_MODULE_ENABLED
  #include "stm32f4xx_hal_gpio.h"
#endif /* HAL_GPIO_MODULE_ENABLED */

#endif /* __STM32F4xx_HAL_CONF_H */

开启的模块,进行判断

文件:stm32f4xx_hal_gpio.C

#include "stm32f4xx_hal.h"
// 
#ifdef HAL_GPIO_MODULE_ENABLED

/* Driver Code Begin*/
/* Driver Code End */

#endif /* HAL_GPIO_MODULE_ENABLED */



3> __weak作用

/**
  * @brief  EXTI line detection callbacks.
  * @param  GPIO_Pin Specifies the pins connected EXTI line
  * @retval None
  */
__weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(GPIO_Pin);
  /* NOTE: This function Should not be modified, when the callback is needed,
           the HAL_GPIO_EXTI_Callback could be implemented in the user file
   */
}

弱定义函数,
用户在用户层重新定义HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)后,会被覆盖;
为用户提供一个模板;

参考手册:
HAL库_源码阅读_第3张图片



4> 中断处理

以EXTI中断为例

/**--- 启动File:【startup_stm32f407xx.s】 ----**/

 DCD     EXTI0_IRQHandler                  ; EXTI Line0   

/**--- 用户中断处理File:【stm32f4xx_it.c】 ----**/
void EXTI0_IRQHandler(void)
{
  
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  
}
/**--- 外设驱动File:stm32f4_hal_gpio.c ----**/

// 中断处理函数
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
{
  /* EXTI line interrupt detected */
  if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
  {
    __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);   // 清中断
    HAL_GPIO_EXTI_Callback(GPIO_Pin); 
  }
}

// 中断回调函数
__weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(GPIO_Pin);
  /* NOTE: This function Should not be modified, when the callback is needed,
           the HAL_GPIO_EXTI_Callback could be implemented in the user file
   */
}

总结: HAL库已为用户提供了整套流程,
只需要在用户层定义void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
不需要在清中断。



5> 容易忽略的宏

HAL库_源码阅读_第4张图片

/**--- HAL总 文件:【stm32f4xx.h】 ----**/

#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx)|| defined(STM32F417xx)
/** @brief  FSMC Bank1 (NOR/PSRAM 1 and 2) mapped at 0x00000000
  */
#define __HAL_SYSCFG_REMAPMEMORY_FSMC()       do {SYSCFG->MEMRMP &= ~(SYSCFG_MEMRMP_MEM_MODE);\
                                                  SYSCFG->MEMRMP |= (SYSCFG_MEMRMP_MEM_MODE_1);\
                                                 }while(0);
#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx */
  • STM32F407xx 在软件中定义;

你可能感兴趣的:(#,22》,HAL库,单片机,stm32,嵌入式硬件)