# esp8266/arduino architecture

> Architecture diagram of the GitHub repository esp8266/arduino, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/esp8266/arduino
- Repository: https://github.com/esp8266/arduino

## Overview

##### Project type and purpose
- This is an Embedded Firmware SDK (“Arduino Core”) for the ESP8266 WiFi microcontroller.
- It provides Arduino‐style APIs and libraries on top of Espressif’s NONOS SDK, plus build/upload tools, documentation, and tests.

##### File structure
- Top‐level domains (clusters):
- bootloaders/eboot – secondary bootloader source, linker scripts for flashing and startup
- cores/esp8266 – Arduino runtime implementation (wiring, time, serial, networking wrappers)
- libraries/… – dozens of Arduino‐style libraries (WiFi, HTTPClient, EEPROM, SPIFFS, LittleFS, OTA, etc.)
- variants/… plus boards.txt, platform.txt, programmers.txt – board‐specific pin mappings and packaging metadata
- tools/… – build and upload scripts (Python, esptool, makeEspArduino, platformio‐build), certificate managers, elf2bin, upload.py
- tools/sdk/… – Espressif NONOS SDK headers, libs (multiple versions), linker scripts, lwIP, BearSSL, etc.
- doc/… – Sphinx/ReadTheDocs sources, tutorials, API reference
- tests/… – CI workflows (GitHub Actions), host‐side mocks and unit tests, device‐side test sketches

##### README insights
- Confirms it’s the ESP8266 core for Arduino IDE & PlatformIO.
- Describes supported features: WiFi (TCP/UDP), HTTP, mDNS, SSDP, DNS, OTA updates, filesystems (SPIFFS, LittleFS), SD, SPI, I²C, peripherals.
- Mentions build options: Boards Manager, git version, PlatformIO, make.

##### Main system components
a. Bootloader
- eboot: builds with its own linker script (.ld), outputs eboot.elf/.bin, runs on reset, loads main firmware.
b. Firmware Build Pipeline
- Build Tools (Python scripts, platformio‐build, makeEspArduino) read platform.txt & boards.txt → invoke xtensa‐gcc → compile cores/esp8266 + selected libraries + tools/sdk headers + SDK libs → elf2bin → final firmware.bin + optional filesystem image.
c. Core Runtime
- C++/C code in cores/esp8266 implements Arduino APIs, wiring, scheduling, serial, stream classes, and wraps SDK OS calls (system_*, esp_*, lwIP).
d. Arduino Libraries
- Each subfolder in libraries/ is a modular, Arduino‐compatible library built alongside core → provides high-level services (WiFiClient, HTTPServer, ArduinoOTA, SPIFFS/LittleFS, I²C, SPI, etc.).
e. Board Variants & Packaging
- variants/ define pins_arduino.h for each board
- platform.txt & boards.txt describe compiler/linker flags, upload protocols
- package/ scripts build and publish the Boards Manager JSON
f. SDK & Third-Party Dependencies
- tools/sdk/ contains Espressif SDK headers, precompiled .a files, BearSSL, lwIP, uzlib, umm_malloc, etc.
g. Documentation & Tests
- doc/ for user docs, API reference
- tests/ for host‐side mocks and device‐side test sketches, plus CI config in .github/workflows and tests/ci

##### Relationships & data flows
- Developer → Build Tools → Compiler/Linker (xtensa toolchain + SDK libs) → Firmware.bin
- Firmware.bin + SPIFFS.bin → flashed by Bootloader into ESP8266 flash
- On reset: Bootloader → jumps to main firmware → Core Runtime initializes HW and calls user sketch
- At runtime: Sketch and Libraries call Core APIs → Core wraps SDK OS functions → hardware/peripherals (WiFi radio, flash, GPIO, timers)
- Tests:
- Host tests import cores/esp8266 code plus mocks → run on desktop
- Device tests flash binaries via bootloader → execute on ESP8266 → report via serial

##### Architectural patterns & principles
- Modularity: clear separation of bootloader, core runtime, libraries, variants, SDK, tools, tests, docs
- Abstraction layers: Arduino API → Core Runtime → Espressif SDK HAL → hardware
- Extensibility: easily add new libraries under libraries/, new boards under variants/
- CI/CD: automated GitHub Actions build/test/doc pipelines

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Board Variants & Packaging
    subgraph "Board Variants & Packaging"
        direction TB
        VVariants["variants/"]:::buildtools
        VBoards["boards.txt"]:::buildtools
        VPlatform["platform.txt"]:::buildtools
        VProgrammers["programmers.txt"]:::buildtools
        VPackage["package/"]:::buildtools
    end

    %% Build & Upload Tools
    subgraph "Build & Upload Tools" 
        direction TB
        Tools["tools/ (build & upload scripts)"]:::buildtools
        Compiler["xtensa-gcc"]:::buildtools
        Elf2Bin["elf2bin"]:::artifact
        FirmwareBin["firmware.bin"]:::artifact
        SPIFFSImg["SPIFFS.img"]:::artifact
    end

    %% Core, Libraries & SDK
    subgraph "Core, Libraries & SDK"
        direction TB
        subgraph "Core Runtime"
            CoreRuntime["cores/esp8266"]:::coreruntime
        end
        subgraph "Arduino Libraries"
            Libraries["libraries/"]:::ardlibraries
        end
        subgraph "Espressif SDK"
            SDK["tools/sdk/"]:::sdk
        end
    end

    %% Bootloader
    subgraph "Bootloader"
        Bootloader["bootloaders/eboot"]:::bootloader
    end

    %% Hardware
    ESP8266HW["ESP8266 Hardware"] 

    %% Docs & Tests
    subgraph "Documentation & Tests"
        direction TB
        Doc["doc/"]:::docs
        TestsHost["tests/ (host-side mocks)"]:::docs
        TestsDevice["tests/ (device-side sketches)"]:::docs
        CIWorkflows[".github/workflows/"]:::docs
    end

    %% Runtime Sketch
    Sketch["User Sketch"]

    %% Connections
    VPlatform -->|provides config| Tools
    VBoards -->|provides config| Tools
    VVariants -->|defines pins| Tools
    VProgrammers -->|defines upload protocols| Tools
    VPackage -->|publishes JSON| Tools

    Tools -->|invokes| Compiler
    Compiler -->|compiles code| CoreRuntime
    Compiler --> Libraries
    Compiler --> SDK
    Compiler -->|generates| Elf2Bin
    Elf2Bin -->|creates| FirmwareBin
    Elf2Bin -->|creates| SPIFFSImg

    FirmwareBin -->|flashed by| Bootloader
    SPIFFSImg -->|flashed by| Bootloader
    Bootloader -->|boots| ESP8266HW

    %% Runtime data flows
    Sketch -->|calls APIs| CoreRuntime
    Libraries -->|use core| CoreRuntime
    CoreRuntime -->|wraps| SDK
    SDK -->|controls| ESP8266HW

    %% Tests
    TestsHost -->|imports| CoreRuntime
    TestsDevice -->|flashes via| Bootloader
    CIWorkflows -->|runs| TestsHost
    CIWorkflows -->|runs| TestsDevice

    %% Click Events
    click VVariants "https://github.com/esp8266/arduino/tree/master/variants/"
    click VBoards "https://github.com/esp8266/arduino/blob/master/boards.txt"
    click VPlatform "https://github.com/esp8266/arduino/blob/master/platform.txt"
    click VProgrammers "https://github.com/esp8266/arduino/blob/master/programmers.txt"
    click VPackage "https://github.com/esp8266/arduino/tree/master/package/"
    click Tools "https://github.com/esp8266/arduino/tree/master/tools/"
    click CoreRuntime "https://github.com/esp8266/arduino/tree/master/cores/esp8266/"
    click Libraries "https://github.com/esp8266/arduino/tree/master/libraries/"
    click SDK "https://github.com/esp8266/arduino/tree/master/tools/sdk/"
    click Bootloader "https://github.com/esp8266/arduino/tree/master/bootloaders/eboot"
    click Doc "https://github.com/esp8266/arduino/tree/master/doc/"
    click TestsHost "https://github.com/esp8266/arduino/tree/master/tests/"
    click TestsDevice "https://github.com/esp8266/arduino/tree/master/tests/"
    click CIWorkflows "https://github.com/esp8266/arduino/tree/master/.github/workflows/"

    %% Styles
    classDef buildtools fill:#FFA500,stroke:#333,stroke-width:1px
    classDef coreruntime fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef ardlibraries fill:#90EE90,stroke:#333,stroke-width:1px
    classDef sdk fill:#D8BFD8,stroke:#333,stroke-width:1px
    classDef bootloader fill:#FFFFE0,stroke:#333,stroke-width:1px
    classDef docs fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef artifact fill:#FFFFFF,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
```
