Ground Embedded KO Subscribe

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.

GROUND EMBEDDED · NOTEFirmware Design & Quality 01Layered architecture 1volatile uint32_t *ODR 2 = (uint32_t*)0x40020014; 3*ODR |= (1u << 5); 4while (1) { 5 if (flag) toggle(); 6}
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
Applicationwhat to doServicesprotocols, filtersDriverssensors, motorsHAL · registershardware
An upper layer calls only the layer directly below. When the hardware changes, only the bottom is edited.

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.

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 →