超子物联网 HAL库学习 汇总入口:
超子物联网HAL库笔记:[汇总]
写作不易,如果您觉得写的不错,欢迎给博主来一波点赞、收藏~让博主更有动力吧!
HAL库
HAL库的优点
对比标准固件库的不同
先找一个位置,存放工程模板
在工程模板中 添加 User、Library 、 Hardware 、 CMSIS 文件夹:
下载HAL库 源码 STM32Cube MCU和MPU包: 相关产品
解压并打开
拷贝HAL库(Library)
\\STM32Cube_FW_F1_V1.8.0\\Drivers\\STM32F1xx_HAL_Driver
把头文件夹 Inc 和源文件夹 Src 移动到自己的Library的对应的文件中拷贝启动文件(CMSIS\Include)
\\STM32Cube_FW_F1_V1.8.0\\Drivers\\CMSIS
\\CMSIS\\Include
把所有文件拷贝到自己的CMSIS/Include文件中 再进入\\CMSIS\\Device\\ST\\STM32F1xx\\Include
把所有文件拷贝到自己的CMSIS/Include文件中拷贝源文件(CMSYS\Source)
\\STM32Cube_FW_F1_V1.8.0\\Drivers\\CMSIS\\Device\\ST\\STM32F1xx\\Source\\Templates
\\Templates
文件的的system_stm32f1xx.c拷贝到自己的CMSIS/Source文件中编辑Hardware文件夹
分别在源文件家和头文件夹添加stm32f1xx_it.c 和stm32f1xx_it.h 代码如下
/*-------------------------------------------------*/
/* */
/* 实现各种中断服务函数的源文件 */
/* */
/*-------------------------------------------------*/
#include "stm32f1xx_hal.h"
#include "stm32f1xx_it.h"
/*-------------------------------------------------*/
/*函数名:不可屏蔽中断处理函数 */
/*参 数:无 */
/*返回值:无 */
/*-------------------------------------------------*/
void NMI_Handler(void)
{
}
/*-------------------------------------------------*/
/*函数名:硬件出错后进入的中断处理函数 */
/*参 数:无 */
/*返回值:无 */
/*-------------------------------------------------*/
void HardFault_Handler(void)
{
}
/*-------------------------------------------------*/
/*函数名:软中断,SWI 指令调用的处理函数 */
/*参 数:无 */
/*返回值:无 */
/*-------------------------------------------------*/
void SVC_Handler(void)
{
}
/*-------------------------------------------------*/
/*函数名:可挂起的系统服务处理函数 */
/*参 数:无 */
/*返回值:无 */
/*-------------------------------------------------*/
void PendSV_Handler(void)
{
}
/*-------------------------------------------------*/
/*函数名:SysTic系统嘀嗒定时器处理函数 */
/*参 数:无 */
/*返回值:无 */
/*-------------------------------------------------*/
void SysTick_Handler(void)
{
HAL_IncTick();
}
/**
******************************************************************************
* @file GPIO/GPIO_IOToggle/Inc/stm32f1xx_it.h
* @author MCD Application Team
* @brief This file contains the headers of the interrupt handlers.
******************************************************************************
* @attention
*
* © Copyright (c) 2016 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F1xx_IT_H
#define __STM32F1xx_IT_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void NMI_Handler(void);
void HardFault_Handler(void);
void MemManage_Handler(void);
void BusFault_Handler(void);
void UsageFault_Handler(void);
void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
#ifdef __cplusplus
}
#endif
#endif /* __STM32F1xx_IT_H */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
最后需要把自己的Library文件中的Include 文件中的stm32f1xx_hal_conf_template.h 更名为 stm32f1xx_hal_conf.h 。否则编译会找不到这个头文件
CMSIS
Include
cmsis_armcc.h
cmsis_armclang.h
cmsis_compiler.h
cmsis_gcc.h
cmsis_iccarm.h
cmsis_version.h
core_armv8mbl.h
core_armv8mml.h
core_cm0.h
core_cm0plus.h
core_cm1.h
core_cm23.h
core_cm3.h
core_cm33.h
core_cm4.h
core_cm7.h
core_sc000.h
core_sc300.h
mpu_armv7.h
mpu_armv8.h
stm32f100xb.h
stm32f100xe.h
stm32f101x6.h
stm32f101xb.h
stm32f101xe.h
stm32f101xg.h
stm32f102x6.h
stm32f102xb.h
stm32f103x6.h
stm32f103xb.h
stm32f103xe.h
stm32f103xg.h
stm32f105xc.h
stm32f107xc.h
stm32f1xx.h
system_stm32f1xx.h
tz_context.h
Source
startup_stm32f100xb.s
startup_stm32f100xe.s
startup_stm32f101x6.s
startup_stm32f101xb.s
startup_stm32f101xe.s
startup_stm32f101xg.s
startup_stm32f102x6.s
startup_stm32f102xb.s
startup_stm32f103x6.s
startup_stm32f103xb.s
startup_stm32f103xe.s
startup_stm32f103xg.s
startup_stm32f105xc.s
startup_stm32f107xc.s
system_stm32f1xx.c
DebugConfig
Project_STM32F103C8_1.0.0.dbgconf
Target_1_STM32F103C8_1.0.0.dbgconf
Hardware
Include
stm32f1xx_it.h
Source
stm32f1xx_it.c
Library
Include
Legacy
stm32f1xx_hal_can_ex_legacy.h
stm32f1xx_hal_can_legacy.h
stm32_hal_legacy.h
stm32f1xx_hal.h
stm32f1xx_hal_adc.h
stm32f1xx_hal_adc_ex.h
stm32f1xx_hal_can.h
stm32f1xx_hal_cec.h
stm32f1xx_hal_conf.h
stm32f1xx_hal_cortex.h
stm32f1xx_hal_crc.h
stm32f1xx_hal_dac.h
stm32f1xx_hal_dac_ex.h
stm32f1xx_hal_def.h
stm32f1xx_hal_dma.h
stm32f1xx_hal_dma_ex.h
stm32f1xx_hal_eth.h
stm32f1xx_hal_exti.h
stm32f1xx_hal_flash.h
stm32f1xx_hal_flash_ex.h
stm32f1xx_hal_gpio.h
stm32f1xx_hal_gpio_ex.h
stm32f1xx_hal_hcd.h
stm32f1xx_hal_i2c.h
stm32f1xx_hal_i2s.h
stm32f1xx_hal_irda.h
stm32f1xx_hal_iwdg.h
stm32f1xx_hal_mmc.h
stm32f1xx_hal_nand.h
stm32f1xx_hal_nor.h
stm32f1xx_hal_pccard.h
stm32f1xx_hal_pcd.h
stm32f1xx_hal_pcd_ex.h
stm32f1xx_hal_pwr.h
stm32f1xx_hal_rcc.h
stm32f1xx_hal_rcc_ex.h
stm32f1xx_hal_rtc.h
stm32f1xx_hal_rtc_ex.h
stm32f1xx_hal_sd.h
stm32f1xx_hal_smartcard.h
stm32f1xx_hal_spi.h
stm32f1xx_hal_sram.h
stm32f1xx_hal_tim.h
stm32f1xx_hal_tim_ex.h
stm32f1xx_hal_uart.h
stm32f1xx_hal_usart.h
stm32f1xx_hal_wwdg.h
stm32f1xx_ll_adc.h
stm32f1xx_ll_bus.h
stm32f1xx_ll_cortex.h
stm32f1xx_ll_crc.h
stm32f1xx_ll_dac.h
stm32f1xx_ll_dma.h
stm32f1xx_ll_exti.h
stm32f1xx_ll_fsmc.h
stm32f1xx_ll_gpio.h
stm32f1xx_ll_i2c.h
stm32f1xx_ll_iwdg.h
stm32f1xx_ll_pwr.h
stm32f1xx_ll_rcc.h
stm32f1xx_ll_rtc.h
stm32f1xx_ll_sdmmc.h
stm32f1xx_ll_spi.h
stm32f1xx_ll_system.h
stm32f1xx_ll_tim.h
stm32f1xx_ll_usart.h
stm32f1xx_ll_usb.h
stm32f1xx_ll_utils.h
stm32f1xx_ll_wwdg.h
stm32_assert_template.h
Source
Legacy
stm32f1xx_hal_can.c
stm32f1xx_hal.c
stm32f1xx_hal_adc.c
stm32f1xx_hal_adc_ex.c
stm32f1xx_hal_can.c
stm32f1xx_hal_cec.c
stm32f1xx_hal_cortex.c
stm32f1xx_hal_crc.c
stm32f1xx_hal_dac.c
stm32f1xx_hal_dac_ex.c
stm32f1xx_hal_dma.c
stm32f1xx_hal_eth.c
stm32f1xx_hal_exti.c
stm32f1xx_hal_flash.c
stm32f1xx_hal_flash_ex.c
stm32f1xx_hal_gpio.c
stm32f1xx_hal_gpio_ex.c
stm32f1xx_hal_hcd.c
stm32f1xx_hal_i2c.c
stm32f1xx_hal_i2s.c
stm32f1xx_hal_irda.c
stm32f1xx_hal_iwdg.c
stm32f1xx_hal_mmc.c
stm32f1xx_hal_msp_template.c
stm32f1xx_hal_nand.c
stm32f1xx_hal_nor.c
stm32f1xx_hal_pccard.c
stm32f1xx_hal_pcd.c
stm32f1xx_hal_pcd_ex.c
stm32f1xx_hal_pwr.c
stm32f1xx_hal_rcc.c
stm32f1xx_hal_rcc_ex.c
stm32f1xx_hal_rtc.c
stm32f1xx_hal_rtc_ex.c
stm32f1xx_hal_sd.c
stm32f1xx_hal_smartcard.c
stm32f1xx_hal_spi.c
stm32f1xx_hal_sram.c
stm32f1xx_hal_tim.c
stm32f1xx_hal_timebase_rtc_alarm_template.c
stm32f1xx_hal_timebase_tim_template.c
stm32f1xx_hal_tim_ex.c
stm32f1xx_hal_uart.c
stm32f1xx_hal_usart.c
stm32f1xx_hal_wwdg.c
stm32f1xx_ll_adc.c
stm32f1xx_ll_crc.c
stm32f1xx_ll_dac.c
stm32f1xx_ll_dma.c
stm32f1xx_ll_exti.c
stm32f1xx_ll_fsmc.c
stm32f1xx_ll_gpio.c
stm32f1xx_ll_i2c.c
stm32f1xx_ll_pwr.c
stm32f1xx_ll_rcc.c
stm32f1xx_ll_rtc.c
stm32f1xx_ll_sdmmc.c
stm32f1xx_ll_spi.c
stm32f1xx_ll_tim.c
stm32f1xx_ll_usart.c
stm32f1xx_ll_usb.c
stm32f1xx_ll_utils.c
Listings
Project.map
Objects
main.d
main.o
Project.axf
Project.build_log.htm
Project.htm
Project.lnp
Project.sct
Project_Project.dep
startup_stm32f101xb.o
stm32f1xx_hal.d
stm32f1xx_hal.o
stm32f1xx_hal_cortex.d
stm32f1xx_hal_cortex.o
stm32f1xx_hal_gpio.d
stm32f1xx_hal_gpio.o
stm32f1xx_hal_gpio_ex.d
stm32f1xx_hal_gpio_ex.o
stm32f1xx_hal_rcc.d
stm32f1xx_hal_rcc.o
stm32f1xx_hal_rcc_ex.d
stm32f1xx_hal_rcc_ex.o
system_stm32f1xx.d
system_stm32f1xx.o
User
main.c
创建工程、选择User为目录。
点击木箱子设置分组
导入文件
在魔术棒中设置:
C/C++中: 在Define输入STM32F103xB (目的是通知编译器我们用的是xb) 添加头文件路径(Hardware、CMSIS、Library)。以后有别的添加则再添加。 Target中:
选择ARM编译器为v6.16 否则会报错
在main.c中引入头文件stm32f1xx_hal.h (在hal.c中可找到)
最后编译即可。0错误0警告
以HAL库的串口为例来讲解
串口分为USART和UART 他们分别是同步通信和异步通信。一般我们用异步的
所以这里我们添加uart.c的文件。
hal库之中,串口包含三种方式: 轮询、中断、DMA。所以在库文件中必须添加DMA库
所以我们这里添加DMA.c的文件
在HAL库中每个外设都有两个关键的部分:
这些是可以在文件中验证的。hal_uart.c中 可见
我们对CubeMax的操作,实际上就是与这些结构体、成员变量一一对应的
一个外设的相关结构体会被定义在一个总的结构体中。比如串口是UART_HandleTypedef