Note
Looking for the Brazilian Portuguese version? Read it here.
Modular Hardware Abstraction Architecture (MHAA) is a C++ architectural study for embedded software. It explores a layered and modular approach to hardware abstraction and source-level portability across compatible microcontrollers.
The project studies how established architectural concepts, C++ templates, and interfaces with pure virtual functions can work together to create reusable peripheral APIs while keeping hardware-specific details explicit.
- Layered hardware abstraction: organize software from hardware-specific definitions and MCU adapters through Peripheral Contracts and reusable Core modules to application-level APIs.
- Microcontroller compatibility: explore source-level portability between microcontrollers that provide compatible capabilities.
- Reusable components: keep common peripheral functionality in independent Core modules with clearly defined Peripheral Contracts.
- Hybrid C++ abstraction: combine templates and pure virtual interfaces, using compile-time specialization and runtime polymorphism where each is appropriate.
- Clear dependencies: separate hardware-specific implementation from reusable functionality and make component composition explicit.
graph TD
APP[Application] --> COMPOSITION[MCU Composition Layer]
COMPOSITION --> CORE[Core]
COMPOSITION --> ADAPTER[MCU Adapter]
CORE --> CONTRACT[Peripheral Contract]
ADAPTER --> CONTRACT
ADAPTER --> HW[Hardware Definitions]
Note
The arrows represent code dependencies, not a sequence of runtime steps. The Core and the MCU Adapter both depend on the same Peripheral Contract. The Peripheral Contract defines an abstraction boundary; it is not a separate object that every operation must pass through.
| Component | Responsibility |
|---|---|
| Application | Contains firmware logic and uses the available Core APIs. |
| MCU Composition Layer | Acts as the Composition Root, selecting the target MCU, instantiating the required Core modules and MCU Adapters, and connecting their dependencies to compose the MCU interface exposed to the application. |
| Core | Provides reusable peripheral operations without depending on MCU registers. |
| Peripheral Contract | Defines the interface and expected behavior for a peripheral's implementations. |
| MCU Adapters | Implement Peripheral Contracts for a specific microcontroller. |
| Hardware Definitions | Describe hardware-specific registers, pins, mappings, and capabilities. |
MHAA combines six established architectural concepts:
| Concept | Role in MHAA |
|---|---|
| Layered Architecture | Separates application logic, composition, reusable functionality, Peripheral Contracts, and hardware-specific code. |
| Ports and Adapters | Uses Peripheral Contracts as ports and MCU-specific implementations as adapters. |
| SOLID | Guides responsibility boundaries, interface design, substitutability, and dependency direction. |
| Hardware Abstraction Layer (HAL) | Provides consistent APIs for hardware capabilities supported by each target. |
| Composition Root | Places target MCU selection and dependency composition in the MCU Composition Layer. |
| Modular Architecture | Organizes functionality into independent components, including separate Core and Peripheral Contract modules. |
These are established practices. MHAA studies how they can be combined and applied to embedded C++ software.
The project currently provides basic Core modules and their corresponding Peripheral Contracts for common peripheral capabilities. The current components focus on foundational functionality rather than complex peripheral features.
The next stage is to develop MCU-specific adapters and validate the architecture across the following targets:
- ATmega328P
- ATmega2560
- STM32
- RP2040
- ESP32
Important
These are planned validation targets. Their inclusion does not mean that adapters or complete hardware support are already available.
Architectural clarity and source-level portability guide the design.
MHAA explores a hybrid approach to C++ abstraction:
- Templates provide compile-time specialization where it is useful.
- Pure virtual interfaces define Peripheral Contracts and allow implementations to be substituted.
- Core modules use these abstractions to keep reusable peripheral behavior independent of a specific MCU.
Memory usage and runtime overhead remain relevant design considerations. However, avoiding object-oriented design or virtual dispatch is not an absolute goal. The project evaluates these trade-offs in the context of clarity, modularity, and portability.