Performance Comparison of Code Based on Different STM32 Library Functions

Performance Comparison of Code Based on Different STM32 Library Functions

Introduction

ST has released three types of library functions to facilitate customers in quickly developing STM32 series MCUs. From the earliest standard peripheral driver library to the later Cube HAL, and then to Cube LL and direct register manipulation. How efficient is the code of these libraries? This article will provide a rough analysis of this issue and offer comparative data for reference.

Problem Analysis

We will implement four common functions: GPIO toggle, TIM PWM output, ADC DMA data acquisition, and DMA M2M, using different library functions to achieve the same functionality, ultimately comparing the performance of each library function. A brief description of the four project codes is as follows:

GPIO Toggle: Switching the output level of GPIO, which includes system clock initialization and GPIO toggle code. TIM PWM Output: Outputting a PWM frequency of 36KHz through TIM1 channel 1, cyclically modifying its duty cycle from 25% to 50%, which includes system clock initialization, TIM1 initialization, and duty cycle switching code. ADC DMA Data Acquisition: Collecting 100 ADC results through ADC analog channel 1 and using DMA to transfer them to the user buffer, which includes system clock initialization, ADC initialization, and DMA initialization code. DMA M2M: Using DMA1 channel 1 to transfer 100 bytes of data from Flash to on-chip SRAM, which includes system clock initialization and DMA initialization code.

The main comparison focuses on three parameters: Flash usage, SRAM usage, and execution code efficiency.

The Flash and SRAM usage can be understood by checking the *.map file generated by IAR.

Performance Comparison of Code Based on Different STM32 Library Functions

In the *.map file, there will be content as shown in the above image, where the sum of readonly code memory and readonly data memory is the Flash usage. The size of Readwrite data memory is the SRAM usage. Therefore, the Flash usage shown in the image is 3204=3174+30, and the SRAM usage is 1032. Since the user heap (Cstack) is set to 1024, the actual SRAM usage for application code is 8=1032-1024.

The code execution efficiency is calculated using the core cycle count (CYCLECONTER) provided by IAR. Breakpoints are set at the beginning and end of the functional function, and the difference in core cycle counts between the two is the execution cycles of the code.

Performance Comparison of Code Based on Different STM32 Library Functions

The test hardware used was the Nucleo-F302 evaluation board.

The software environment and library function details are as follows:

IAR V7.60

Optimizations Level High (Size)

STM32CubeMX V4.17

Create Project with Copy the necessary library files

STM32CubeF3 V1.60

STM32F30x_DSP_StdPeriph_Lib_V1.2.3

STM32F3xx CMSIS V2.3.0

The test results are as follows: [Click on the image in mobile mode to enlarge]

Performance Comparison of Code Based on Different STM32 Library Functions

Performance Comparison of Code Based on Different STM32 Library Functions

Overall, the pattern that code efficiency is inversely proportional to code compatibility and portability is evident. The efficiency of the Cube LL library is significantly better than that of the HAL library, almost comparable to direct register writing efficiency. The HAL library functions are relatively large due to the need to consider high compatibility and portability across the entire STM32 series. For those new to STM32, it is very easy to get started with basic evaluations and verifications, making it a quick entry point. The emergence of the LL library is a strong complement to the HAL library. Compared to HAL library users, LL library users need to have a more detailed understanding of the MCU and corresponding peripherals.

Currently, STM32CubeMX not only supports the generation of initialization files based on the HAL library but also supports the generation of initialization files and projects based on the LL library. For developers who are already familiar with STM32 applications or are concerned about code efficiency, it is advisable to prioritize using the LL library.

By the way, I will introduce how to choose to use the HAL library or LL library to generate initialization files and corresponding projects in the graphical interface of STM32CubeMX.

In the CubeMX interface, after selecting and configuring various peripherals, you can choose the library type to use in Project”>Project Setting”>Advanced Setting as shown in the image below: HAL/LL.

Performance Comparison of Code Based on Different STM32 Library Functions

Additionally, some people occasionally ask where the LL library is. In fact, the LL library functions are located in the same directory as the HAL library functions. For example, in STM32F4, after downloading and unzipping STM32CubeF4, you can find the HAL functions and LL functions library files in a directory similar to the following:

…\Drivers\STM32F4xx_HAL_Driver\Src

Performance Comparison of Code Based on Different STM32 Library Functions

========================

Previous topic links:

1. Case of abnormal peripheral operation when enabling D-CACHE

2. Sharing cases related to power supply during STM32 applications

3. Implementation of external serial NOR Flash using QuadSPI

4. Experience with STM32CubeF1’s USB CDC implementation of VCP functionality

5. A case of a crash caused by using external SRAM

Leave a Comment