tarabyte
tarabyte

Reputation: 19172

STM32F HAL Library too large

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?

Upvotes: 2

Views: 4376

Answers (5)

tarabyte
tarabyte

Reputation: 19172

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

Upvotes: 5

Dark Sorrow
Dark Sorrow

Reputation: 1897

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

Upvotes: 4

ReAl
ReAl

Reputation: 1301

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.

Upvotes: 4

mxst4
mxst4

Reputation: 48

Did you try to set compiler flag to -Os?

Upvotes: 3

0___________
0___________

Reputation: 67476

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.

Upvotes: 2

Related Questions