St provides us HAL – Hardware Abstraction Layer that ensures maximized portability across the STM32 portfolio. So let investigate it and see how it helps our work.
1. What is HAL?
Hardware Abstraction Layer, a layer inside STM32Cube F4 firmware solution, provides the low-level drives and the hardware interfacing methods to interact with the upper layers (application, libraries and stacks).
It allows to offload our application implementation by providing ready to use processes.
To understand detail information and how to download and use it, you should see my post about STM32CubeF4 architecture overview.
2. Add HAL library into your project
2.1. Create new project
Follow guidance on GPIO LED Control – Accessing Register Directly to create new project with main.c
2.2. Create HAL library folders
- Create Driver and Include folder in our project folder
- Now we will use HAL library from STM32CubeF4 packet:
- Copy all files from STM32Cube_FW_F4_V1.24.0\Drivers\STM32F4xx_HAL_Driver\Src to our Driver folder
- Copy all files from STM32Cube_FW_F4_V1.24.0\Drivers\STM32F4xx_HAL_Driver\Inc to our Include folder
- You can create the folder in anywhere you want:
- The Driver and Include folder created inside project makes your project more independent, but the project size is larger
- The Driver and Include folder is out of project folder will save the project size, but when moving project you have to remember moving the driver folder also
2.3. Add library files into project
- Right click on STM32F411VET target and choose Add Group
- Add Driver group into project
- Right click on Driver -> choose Add Existing -> Choose file from Driver folder -> Click Add
- You should add all files into our project, reason is it will save your time to create project for other modules
- Note that you have to remove all *_template.c file to make the build success. These files are used for user to config project, we will investigate them later on each corresponding module implementation.
- Now we need to add the path to header files
- Choose Option for Target (Option to control debug tool and flash option in GPIO LED Control – Accessing Register Directly
- Choose C/C++ tag and click browser button of Include Paths
- Select New(Insert) -> Click Browser button -> Select our Include folder that has HAL lib header files
- Click Select Folder -> OK then we added Path to HAL header files
2.4. Add config file to our project
Follow HAL Architecture, we need to add stm32f4xx_hal_conf.h header files to config our project
- Create new group Include in project
- Create a new file stm32f4xx_hal_conf.h in our Include folder then add it into our project also under Include group
- Copy content from stm32f4xx_hal_conf_template.h to stm32f4xx_hal_conf.h
- Modify it from line 30 to for the feature we use for GPIO implementation
/* Exported types ------------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/ /* ########################## Module Selection ############################## */ /** * @brief This is the list of modules to be used in the HAL driver */ #define HAL_MODULE_ENABLED #define HAL_GPIO_MODULE_ENABLED #define HAL_RCC_MODULE_ENABLED #define HAL_FLASH_MODULE_ENABLED #define HAL_CORTEX_MODULE_ENABLED
3. Add code to check library
- Add code to main.c file to verify our HAL library:
#include "stm32f4xx_hal.h" #include "stm32f4xx_hal_gpio.h" int main(void) { return 0; }
- Check all Options for Target as GPIO LED Control – Accessing Register Directly
- Click Rebuild and see succeed result
4. What should we do next?
Now you created your own project based on HAL layer. Save it as your template project will make the implementation become faster.
This post is not finished here, I will show you more detail about HAL GPIO feature in next post. See you soon.