2

I just ran out of flash on my microcontroller project. How can I reduce the size of the HAL library or otherwise make it take less of a huge percentage of my project?

tarabyte
  • 17,837
  • 15
  • 76
  • 117

5 Answers5

5

In addition to all of these great responses, I'd like to add that the HAL can be configured in stm32f3xx_hal_conf.h to disable unused modules.

/**
  * @brief This is the list of modules to be used in the HAL driver 
  */
#define HAL_MODULE_ENABLED
#define HAL_ADC_MODULE_ENABLED
#define HAL_CAN_MODULE_ENABLED
/* #define HAL_CAN_LEGACY_MODULE_ENABLED */
#define HAL_CEC_MODULE_ENABLED
#define HAL_COMP_MODULE_ENABLED
#define HAL_CORTEX_MODULE_ENABLED
#define HAL_CRC_MODULE_ENABLED
#define HAL_DAC_MODULE_ENABLED
#define HAL_DMA_MODULE_ENABLED
#define HAL_FLASH_MODULE_ENABLED
#define HAL_GPIO_MODULE_ENABLED
#define HAL_HRTIM_MODULE_ENABLED
#define HAL_I2C_MODULE_ENABLED
#define HAL_I2S_MODULE_ENABLED
#define HAL_IRDA_MODULE_ENABLED
#define HAL_IWDG_MODULE_ENABLED
#define HAL_OPAMP_MODULE_ENABLED
#define HAL_PCD_MODULE_ENABLED
#define HAL_PWR_MODULE_ENABLED
#define HAL_RCC_MODULE_ENABLED
#define HAL_RTC_MODULE_ENABLED
#define HAL_SDADC_MODULE_ENABLED
#define HAL_SMARTCARD_MODULE_ENABLED
#define HAL_SMBUS_MODULE_ENABLED
#define HAL_SPI_MODULE_ENABLED
#define HAL_TIM_MODULE_ENABLED
#define HAL_TSC_MODULE_ENABLED
#define HAL_UART_MODULE_ENABLED
#define HAL_USART_MODULE_ENABLED
#define HAL_WWDG_MODULE_ENABLED
tarabyte
  • 17,837
  • 15
  • 76
  • 117
  • Worth noting that while you should verify, the MX config tool integrated in STM32CubeIDE that creates the boilerplate project comments out the relevant lines automatically. – Escher May 15 '21 at 13:51
  • @tarabyte Be aware that (by the nature of the C/C++ build tools)any unused symbols will not be used by the linker inside the final binary image(unless specified)! so it doesn't matter how many modules or files are included – IMAN4K Jul 17 '23 at 16:10
4

Try to:

  • eliminate unused code and data ("garbage collection") using -ffunction-sections and -fdata-sections for compiler, --gc-sections for linker .
  • use linker-time optimization (LTO) using -O2 -flto both for linker and compiler.
ReAl
  • 1,231
  • 1
  • 8
  • 19
  • Standard options when you limport project from the cube. Standard options on the most IDEs. – 0___________ Feb 09 '19 at 16:44
  • @P__J__ **1.** I use neither HAL nor LL API. Some of the projects which I saw was without garbage collection enabled and I concluded that it's not enabled by default. **2.** In my opinion, if some program (non-esoteric but esoteric ones are not written by novice) crashes if LTO enabled than it malformed and even without LTO it will be crashed after one or two dozen of changes. – ReAl Feb 12 '19 at 09:30
  • what garbage collection. It is only not linking the dead code and data. – 0___________ Feb 12 '19 at 20:18
  • @P__J__ From gcc documentation: [Together with a linker **garbage collection** (linker --**gc**-sections option) these options may lead to smaller statically-linked executables (after stripping).](https://gcc.gnu.org/onlinedocs/gcc-8.2.0/gcc/Optimize-Options.html#Optimize-Options) – ReAl Feb 12 '19 at 21:20
3

Did you try to set compiler flag to -Os?

mxst4
  • 48
  • 1
  • 4
3

If you are ready compromise on portability and ease of use you can use Low Level(LL) drivers provided by ST. As an added benefit your performance may also increase.

A post from ST forums :

The Low Layer (LL) drivers are designed to offer a fast light-weight expert-oriented layer which is closer to the hardware than the HAL. Contrary to the HAL, LL APIs are not provided for peripherals where optimized access is not a key feature, or those requiring heavy software configuration and/or complex upper-level stack (such USB).

The HAL and LL drivers are complementary and cover a wide range of applications requirements:

  1. The HAL offers high-level and feature-oriented APIs, with a high-portability level. They hide the MCU and peripheral complexity to end-user.
  2. The LL offers low-level APIs at registers level, with better optimization but less portability. They require deep knowledge of the MCU and peripherals specifications

The LL drivers feature:

  • A set of functions to initialize peripheral main features according to the parameters specified in data structures

  • A set of functions used to fill initialization data structures with the reset values of each field

  • Functions to perform peripheral de-initialization (peripheral registers restored to their default values)

  • A set of inline functions for direct and atomic register access

  • Full independence from HAL since LL drivers can be used either in standalone mode (without HAL drivers) or in mixed mode (with HAL drivers)

The Low Layer drivers provide hardware services based on the available features of the STM32 peripherals. These services reflect exactly the hardware capabilities and provide one-shot operations that must be called following the programming model described in the microcontroller line reference manual. As a result, the LL services do not implement any processing and do not require any additional memory resources to save their states, counter or data pointers: all the operations are performed by changing the associated peripheral registers content.

Link

Dark Sorrow
  • 1,681
  • 14
  • 37
2

There is only one way - stop using the HAL library and do it on the registers level. If the size of the HAL is important you use small micro and this the only way of doing it.

0___________
  • 60,014
  • 4
  • 34
  • 74