Notes / Firmware Design & Quality
Layered architecture: upper layers call only downwards
Split into HAL, drivers, services and application, and fix the direction of dependency one way. A structure in which the upper code survives a change of chip.
| Layer | What it does | Examples |
|---|---|---|
| Application | The product's behaviour rules | Balancing state machine, mode switching |
| Services | Reusable functions | PID, filters, protocol parser, logging |
| Device drivers | Handle one external chip | MPU6050, HC-05, motor driver |
| HAL / MCAL | MCU peripherals | GPIO, I2C, timers, ADC |
Rules
- Upper layers call lower ones, and lower layers know nothing of the upper. A driver must not call an application function directly. Notifications from below go upwards through callbacks or events.
- Keep calls between modules in the same layer to a minimum too.
- Register names and pin numbers must not appear in the application.
What you gain
- Change the MCU from an STM32F4 to a G4 and only the HAL needs fixing.
- The service and application layers can be tested on a PC without hardware.
- Several people can work in parallel, layer by layer.
Link to automotive software
The AUTOSAR structure of MCAL, ECU abstraction, services, RTE and SWC is this idea set down as a standard. Separating BSW from ASW follows the same principle.