Q:

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? 

A:

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. 

A:

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. 

A:

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:

The HAL offers high-level and feature-oriented APIs, with a high-portability level.  They hide the MCU and peripheral complexity to end-user. 
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

