HAL库_源码阅读

库版本:STM32Cube_FW_F4_V1.25.0

1> 函数入口

1

1.1> 启动文件 startup_stm32f407xx.s

》配置异常向量表;

1.1

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)后,会被覆盖;
为用户提供一个模板;

参考手册:
在这里插入图片描述



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> 容易忽略的宏

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 在软件中定义;
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值