Topics / Embedded C / Q34
What are a HAL and a BSP, and how do they differ?
Both are layers that keep application code independent of the hardware, but at different levels.
- A HAL (hardware abstraction layer) gives a uniform API for a type of peripheral (
uart_write(),gpio_set(),spi_transfer()), hiding the registers of one chip family. Vendor examples are STM32 HAL/LL, NXP MCUXpresso SDK drivers and TI DriverLib; CMSIS-Driver standardises the API across vendors. - A BSP (board support package) adapts software to a specific board: which pins connect to which device, clock and memory setup, external chips (PMIC, flash, sensors), and the start-up code. In embedded Linux, the BSP is the bootloader, kernel configuration and device tree for that board.
| HAL | BSP | |
|---|---|---|
| Abstracts | A peripheral or chip family | A whole board |
| Changes when | You switch MCU family | You change the board design |
| Example | uart_init(&cfg) |
"Debug UART is USART2 on PA2/PA3; sensor on I2C1 at 0x48" |
A good layering is: application → services/middleware → BSP → HAL → registers. Interviewers often follow up with how you would port firmware to a new MCU: you rewrite or replace the HAL and update the BSP, and the application should not change.