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.
The same job, two ways
// 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.