Ground Embedded KO Subscribe

Notes / MCU Firmware

Registers and HAL: when to use which

Writing registers directly is fast and exact; using HAL gets you there sooner. How the two are mixed in real projects.

GROUND EMBEDDED · NOTEMCU Firmware 02Registers versus HALAvg 30%D1 period

The same job, two ways

My codeHAL functionHAL_GPIO_WritePin()RegisterGPIOA-BSRRPin PA5My codeRegisterGPIOA-BSRRPin PA5
Two routes to turning on the same pin. HAL ends up writing the same register.
// HAL
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
// Register
GPIOA->BSRR = GPIO_BSRR_BS5;

Comparison

HAL Registers (including LL)
Development speed Fast Slow
Execution speed Slower: function calls and checks Fast
Code size Large Small
Porting between chips Easy Different on every chip
Understanding The inside is hidden You see the hardware as it is

How it is done in practice

  • Initialisation (clocks, pins, peripheral setup) uses the HAL code that CubeMX generates.
  • Control loops that run at 10 kHz or more, and register access inside interrupts, use registers or the LL (Low Layer) drivers.
  • When a HAL function behaves strangely, open its source and see which registers it touches. In the end you need to be able to read the reference manual to use HAL properly.

HAL is not a tool that lets you ignore the hardware. It is a tool that lets people who know the hardware work faster.

FREE PDFGet the 3 free ebooksIf you want to keep reading this topic as one book: BLDC motor control, serial communication and sensors at book length. PDFs are in Korean.Get them free →