Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:78 Xiaozhi esp32 Board Abstraction Layer

From Leeroopedia
Knowledge Sources
Domains Software_Architecture, Embedded_Systems
Last Updated 2026-10-17 00:00 GMT

Overview

A hardware abstraction layer (HAL) pattern that decouples core firmware logic from board-specific hardware implementations through a set of abstract interfaces and a factory macro, enabling a single firmware codebase to support dozens of distinct hardware boards.

Description

The Board Abstraction Layer is an application of the Dependency Inversion Principle to embedded hardware. Core firmware modules (audio, protocol, display, state machine) depend only on abstract interfaces (Board, AudioCodec, Display, NetworkInterface), never on concrete board classes or board-specific config.h files. A build selects exactly one concrete board implementation via the DECLARE_BOARD macro, which expands to a create_board() factory function.

This pattern solves the problem of maintaining a single firmware codebase across 40+ hardware variants with different chips (ESP32-S3, ESP32-P4, ESP32-C3, ESP32-C6), network transports (WiFi, cellular, Ethernet), audio codecs (ES8311, ES8388, ES8374), and display types (OLED, LCD, e-ink). Without abstraction, board-specific code would permeate every module, making the codebase unmaintainable.

Key design constraints:

  • Exactly one board per build: DECLARE_BOARD is invoked once, creating the sole board factory.
  • Board identity is immutable: pins and board type must never be altered for existing boards, as this affects OTA compatibility.
  • Optional capabilities are nullable: GetDisplay(), GetCamera(), GetBacklight() return nullptr when not present.

Usage

Use this principle when:

  • A firmware codebase must support multiple hardware variants with differing peripherals
  • Core logic should not be coupled to specific pin assignments or driver implementations
  • New board variants are added frequently without modifying core modules
  • Build-time selection of a single hardware target is acceptable (no runtime board switching)

Theoretical Basis

The pattern implements the Dependency Inversion Principle (SOLID):

Traditional (coupled):          Abstracted (inverted):

  Core → ES8311Codec              Core → AudioCodec (interface)
  Core → ST7789Display            Core → Display (interface)
  Core → WiFiDriver               Core → NetworkInterface (interface)
                                        ↑                        ↑
                                  ES8311Codec implements   WiFiDriver implements

The Abstract Factory pattern provides the concrete instance:

// Factory macro — one per build
#define DECLARE_BOARD(BOARD_CLASS_NAME) \
void* create_board() { return new BOARD_CLASS_NAME(); }

// Core code retrieves the singleton
Board& Board::GetInstance() {
    static Board* instance = static_cast<Board*>(create_board());
    return *instance;
}

The Null Object pattern handles optional capabilities: methods like GetDisplay() return nullptr, and callers must guard against absent hardware.

Practical Guide

When adding a new board:

  • Create a unique board identity: never modify an existing board's pins or type.
  • Inherit from the appropriate base: WifiBoard, Ml307Board, EthernetBoard, DualNetworkBoard, or RndisBoard.
  • Implement all pure virtual methods: GetBoardType(), GetAudioCodec(), GetNetwork(), StartNetwork(), SetPowerSaveLevel(), GetNetworkStateIcon(), GetBoardJson(), GetDeviceStatusJson().
  • Place exactly one DECLARE_BOARD in the board's .cc file.
  • Update the build chain: config.json → Kconfig.projbuild → CMakeLists.txt.

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment