Follow+Star Public Account, don’t miss out on exciting content

Author | strongerHuang
WeChat Public Account | Embedded Column
The standard peripheral library, HAL, and LL software library of STM32 all have many clever aspects worth learning from.Today, we will discuss how the STM32Cube LL library cleverly uses “static inline” to make the code more efficient.
1 Overview
Some applications require the MCU to handle tasks efficiently, especially when running algorithms that demand high CPU execution efficiency.
There are many articles online stating that STM32Cube HAL has low execution efficiency and a large code size, which leads to various confusions for many beginners or those who have not yet started.To be honest, HAL does have issues with code efficiency and size compared to the standard peripheral library, and the corresponding STM32Cube LL library avoids these problems.
2 Reasons for LL’s Efficiency
In summary, the reason is: clever use of C language static and inline functions to directly manipulate registers.
Of course, this is one of the important reasons; there are other reasons that will not be described here.You will find a large number of functions in the LL library’s .h files that directly read and write registers using static and inline functions.For example, reading and writing IO ports:
__STATIC_INLINE uint32_t LL_GPIO_ReadOutputPort(GPIO_TypeDef *GPIOx){ return (uint32_t)(READ_REG(GPIOx->ODR));}
__STATIC_INLINE void LL_GPIO_SetOutputPin(GPIO_TypeDef *GPIOx, uint32_t PinMask){ WRITE_REG(GPIOx->BSRR, (PinMask >> GPIO_PIN_MASK_POS) & 0x0000FFFFU);}
Here, __STATIC_INLINE refers to static and inline:
#define __STATIC_INLINE static __inline
And the definition for reading and writing bits:

The macro definitions here are called in many peripheral .h files.For example, enabling USART:
LL library enabling USART:
__STATIC_INLINE void LL_USART_Enable(USART_TypeDef *USARTx){ SET_BIT(USARTx->CR1, USART_CR1_UE);}
Standard peripheral library enabling USART:
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState){ /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx)); assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE) { /* Enable the selected USART by setting the UE bit in the CR1 register */ USARTx->CR1 |= USART_CR1_UE; } else { /* Disable the selected USART by clearing the UE bit in the CR1 register */ USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_UE); }}
By comparing, you will clearly find that:the LL library has higher execution efficiency.
3 What is an Inline Function?
At this point, some readers may ask: what is an inline function?
An inline function is a programming language construct used to suggest to the compiler to inline certain special functions.— from Baidu Encyclopedia
Typically, during program execution, the processor reads code from memory to execute.When a function is called in the program, the program jumps to the location in memory where the function is stored, starts reading the code, and returns after execution.To improve speed, the C language defines inline functions, telling the compiler to directly copy the function code into the program during compilation, so that it does not need to read the function code separately during execution.Note:When inline functions are large, they can have the opposite effect, so generally, only smaller functions are used as inline functions.
4 Software Framework Thinking
The efficiency of LL is due to its clever use of some C language knowledge, with minimal encapsulation, directly or indirectly manipulating registers.
This is made possible by the ST development team designing such an intermediate layer software framework.
For those with experience in large project development, the framework of a project greatly impacts the entire project.
It’s like building a building; if the framework of the floors is not well constructed, do you think the quality of the building will be good?
Therefore, it is mentioned that when programming, especially for larger projects, one needs to consider the software framework; a good framework can make your project achieve twice the result with half the effort.
———— END ————

● Column “Embedded Tools”
● Column “Embedded Development”
● Column “Keil Tutorial”
● Selected Tutorials from the Embedded Column
Follow the public account and reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.


Click “Read Original” to see more shares.