# bdring/grbl_esp32 architecture

> Architecture diagram of the GitHub repository bdring/grbl_esp32, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bdring/grbl_esp32
- Repository: https://github.com/bdring/grbl_esp32

## Overview

1. **What this repo is**
   - This is the legacy **Grbl_ESP32** firmware: a CNC motion controller for ESP32, built as an Arduino-style firmware project.
   - The architecture is firmware-centric, not an application server: the ESP32 runs the control loop, serves a web UI, and drives motors/spindles/sensors directly.

2. **Startup and top-level composition**
   - `Grbl_Esp32/Grbl_Esp32.ino` is the Arduino entrypoint.
   - Core runtime orchestration lives in `src/Grbl.cpp` and the supporting modules around it (`System`, `Protocol`, `Settings`, `Report`, `Exec`).
   - The project is configured through `platformio.ini`, but the codebase is structured for Arduino/ESP32 compilation.

3. **Command ingress paths**
   - The firmware accepts commands from multiple front doors: **USB/serial**, **Bluetooth serial**, **Wi‑Fi web UI**, **telnet**, and **SD card** jobs.
   - Those inputs converge into the same command-processing path via `Protocol`, `Serial`, `WebUI/Commands`, and related input buffering code.
   - This means the architecture has a single motion/control core with several transport adapters.

4. **Parsing, execution, and reporting pipeline**
   - G-code and real-time commands are parsed in `GCode`, with execution handled by `Exec` and coordinated by `Protocol`.
   - Status, alarms, error text, and machine-state reporting are split into `Report` and `Error`.
   - `Settings`, `SettingsDefinitions`, and `ProcessSettings` provide the configuration layer that feeds the runtime before motion is executed.

5. **Motion control core**
   - Motion is staged through `Planner` and then emitted through `Stepper`/`MotionControl`.
   - `Jog` handles interactive jogging, while `Limits` and `Probe` cover homing, limit switches, and probing.
   - `I2SOut` indicates the step generation/output path is abstracted so the ESP32 can produce high-rate step signals without relying on the main control loop timing alone.

6. **Hardware abstraction and machine profiles**
   - Pin mapping and board-specific behavior are separated into `CPUMap`, `Pins`, and `Machine`/`MachineCommon`.
   - The `src/Machines` directory contains many predefined machine profiles, showing that “machine definition” is a first-class boundary in this firmware.
   - The `Custom` directory and `CustomCode.cpp` support project-specific extensions without editing the core engine.

7. **Motor subsystem**
   - Motors are modeled separately from axes, which is important because the firmware supports multiple motors per axis and dynamic remapping.
   - `src/Motors` contains implementations for standard steppers, unipolar motors, servos, RC servos, null motors, Dynamixel, and Trinamic-based drivers.
   - This layer is the bridge between the motion planner and the physical driver electronics.

8. **Spindle and tool output subsystem**
   - Spindle/control output is abstracted behind `Spindle` with concrete implementations for PWM, relay, DAC/0-10V, VFDs, Huanyang, Teco, laser mode, and BESC-based brushless control.
   - `CoolantControl` and `UserOutput` handle other machine outputs like coolant and auxiliary signals.
   - This separation lets the same motion core drive both cutting spindles and nontraditional tools like lasers.

9. **Connectivity and remote management**
   - The `WebUI` subtree is a substantial subsystem: it includes the HTTP server, authentication, configuration pages, JSON encoding, input buffering, notifications, Wi‑Fi services/config, telnet, and serial-to-socket bridging.
   - `WebServer`, `TelnetServer`, `Serial2Socket`, `WifiConfig`, `WifiServices`, and `NotificationsService` form the network boundary.
   - The embedded frontend assets are packaged in `embedded/` and `Grbl_Esp32/data`, including compressed HTML/JS content served by the firmware.

10. **Persistent state and job storage**
   - Machine settings are persisted through the `Settings` layer, while SD card support is isolated in `SDCard`.
   - The firmware can run jobs from SD or via streamed commands, so storage is treated as an input source rather than a full filesystem application.
   - The `data/` and `embedded/` assets support the on-device UI, while `src/tests` provides sample G-code and parser/spindle test inputs.

11. **Safety and machine feedback**
   - `Limits`, `Probe`, `Exec`, `System`, and `CoolantControl` together implement the runtime safety behavior: homing, limit handling, feed hold/resume, door/probe interactions, and abort states.
   - The architecture clearly separates time-sensitive motion from asynchronous safety/IO monitoring, which is important for diagramming control boundaries.

12. **External/library boundaries**
   - The repo vendors its key dependencies under `libraries/`, notably `arduinoWebSockets` and `ESP32SSDP`, which support the network/web UI features.
   - This is not a generic app with many third-party services; the important external dependency boundary is mainly the ESP32 Arduino runtime plus these embedded networking libraries.

13. **Customization and extension points**
   - The `Custom` folder and `src/user_io.md`/custom machine headers show explicit extension hooks for machine-specific behavior, custom kinematics, macros, and startup/shutdown sequences.
   - Architecturally, these are plug-in style modules sitting beside the core rather than inside it, which is why the repo contains many named machine profiles and custom examples.

14. **Build/test/tooling**
   - The repository includes scripting for builds and machine conversion (`build-all.*`, `build-machine.py`, `configure-features.py`, GitHub workflows).
   - Docs and scripts under `doc/` and the test G-code files under `src/tests` indicate the project is validated through firmware compilation plus command/G-code behavior checks, not through a separate application test harness.

Main entry points: [`Grbl_Esp32/src/Exec.cpp`](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Exec.cpp) (Exec), [`Grbl_Esp32/src/Protocol.cpp`](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Protocol.cpp) (Protocol), [`Grbl_Esp32/src/Grbl.cpp`](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Grbl.cpp) (Grbl), [`Grbl_Esp32/src/Planner.cpp`](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Planner.cpp) (Planner)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_core["Core Runtime"]
  node_node_entry["Entry<br/>arduino sketch<br/>[Grbl_Esp32.ino]"]
  node_node_grbl["Grbl<br/>orchestrator<br/>[Grbl.cpp]"]
  node_node_protocol["Protocol<br/>command core<br/>[Protocol.cpp]"]
  node_node_exec["Exec<br/>[Exec.cpp]"]
  node_node_settings[("Settings<br/>config store<br/>[Settings.cpp]")]
  node_node_report["Report<br/>status output<br/>[Report.cpp]"]
  node_node_error["Error<br/>[Error.cpp]"]
  node_node_gcode["GCode<br/>parser<br/>[GCode.cpp]"]
  node_node_process["Process Settings<br/>init stage"]
end

subgraph group_group_motion["Motion Stack"]
  node_node_planner["Planner<br/>[Planner.cpp]"]
  node_node_stepper["Stepper<br/>step control<br/>[Stepper.cpp]"]
  node_node_motion["Motion Control<br/>motion core<br/>[MotionControl.cpp]"]
  node_node_i2s["I2S Out<br/>step output<br/>[I2SOut.cpp]"]
  node_node_limits["Limits<br/>safety io<br/>[Limits.cpp]"]
  node_node_probe["Probe<br/>safety io<br/>[Probe.cpp]"]
  node_node_jog["Jog<br/>manual motion<br/>[Jog.cpp]"]
end

subgraph group_group_hw["Hardware Abstraction"]
  node_node_cpu["CPU Map<br/>board map<br/>[CPUMap.h]"]
  node_node_machine["Machines<br/>profiles"]
  node_node_pins["Pins<br/>pin config<br/>[Pins.cpp]"]
  node_node_motors["Motors<br/>driver layer<br/>[Motors.cpp]"]
  node_node_spindle["Spindle<br/>tool output<br/>[Spindle.cpp]"]
  node_node_outputs["Aux Outputs<br/>io outputs<br/>[CoolantControl.cpp]"]
  node_node_custom["Custom Code<br/>extension hooks<br/>[CustomCode.cpp]"]
end

subgraph group_group_ui["Connectivity"]
  node_node_webui["WebUI<br/>network ui<br/>[WebServer.cpp]"]
  node_node_telnet["Telnet<br/>network ingress<br/>[TelnetServer.cpp]"]
  node_node_serial["Serial<br/>uart ingress<br/>[Serial.cpp]"]
  node_node_wifi["Wi-Fi<br/>network services<br/>[WifiServices.cpp]"]
end

subgraph group_group_storage["Storage & Assets"]
  node_node_storage[("SD Card<br/>job storage<br/>[SDCard.cpp]")]
  node_node_assets["Embedded UI<br/>assets<br/>[index.html.gz]"]
end

node_node_entry -->|"starts"| node_node_grbl
node_node_grbl -->|"drives"| node_node_protocol
node_node_grbl -->|"loads"| node_node_settings
node_node_grbl -->|"reports"| node_node_report
node_node_grbl -->|"coordinates"| node_node_exec
node_node_process -->|"applies"| node_node_settings
node_node_protocol -->|"parses"| node_node_gcode
node_node_gcode -->|"executes"| node_node_exec
node_node_protocol -->|"ingress"| node_node_serial
node_node_protocol -->|"ingress"| node_node_webui
node_node_protocol -->|"ingress"| node_node_telnet
node_node_protocol -->|"jobs"| node_node_storage
node_node_protocol -->|"errors"| node_node_error
node_node_exec -->|"queues"| node_node_planner
node_node_planner -->|"feeds"| node_node_stepper
node_node_stepper -->|"outputs"| node_node_i2s
node_node_exec -->|"checks"| node_node_limits
node_node_exec -->|"checks"| node_node_probe
node_node_exec -->|"supports"| node_node_jog
node_node_motion -->|"manages"| node_node_planner
node_node_motion -->|"manages"| node_node_stepper
node_node_machine -->|"defines"| node_node_pins
node_node_cpu -->|"supports"| node_node_pins
node_node_motors -->|"driven by"| node_node_planner
node_node_spindle -->|"tooling"| node_node_exec
node_node_outputs -->|"aux io"| node_node_exec
node_node_custom -->|"extends"| node_node_grbl
node_node_webui -->|"uses"| node_node_wifi
node_node_webui -->|"serves"| node_node_assets

click node_node_entry "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/Grbl_Esp32.ino"
click node_node_grbl "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Grbl.cpp"
click node_node_protocol "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Protocol.cpp"
click node_node_exec "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Exec.cpp"
click node_node_settings "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Settings.cpp"
click node_node_report "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Report.cpp"
click node_node_error "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Error.cpp"
click node_node_gcode "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/GCode.cpp"
click node_node_process "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/ProcessSettings.cpp"
click node_node_planner "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Planner.cpp"
click node_node_stepper "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Stepper.cpp"
click node_node_motion "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/MotionControl.cpp"
click node_node_i2s "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/I2SOut.cpp"
click node_node_limits "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Limits.cpp"
click node_node_probe "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Probe.cpp"
click node_node_jog "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Jog.cpp"
click node_node_webui "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/WebUI/WebServer.cpp"
click node_node_telnet "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/WebUI/TelnetServer.cpp"
click node_node_serial "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Serial.cpp"
click node_node_wifi "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/WebUI/WifiServices.cpp"
click node_node_storage "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/SDCard.cpp"
click node_node_assets "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/data/index.html.gz"
click node_node_cpu "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/CPUMap.h"
click node_node_machine "https://github.com/bdring/grbl_esp32/tree/main/Grbl_Esp32/src/Machines"
click node_node_pins "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Pins.cpp"
click node_node_motors "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Motors/Motors.cpp"
click node_node_spindle "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Spindles/Spindle.cpp"
click node_node_outputs "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/CoolantControl.cpp"
click node_node_custom "https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/CustomCode.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_entry,node_node_grbl,node_node_protocol,node_node_exec,node_node_settings,node_node_report,node_node_error,node_node_gcode,node_node_process toneBlue
class node_node_planner,node_node_stepper,node_node_motion,node_node_i2s,node_node_limits,node_node_probe,node_node_jog toneAmber
class node_node_cpu,node_node_machine,node_node_pins,node_node_motors,node_node_spindle,node_node_outputs,node_node_custom toneMint
class node_node_webui,node_node_telnet,node_node_serial,node_node_wifi toneRose
class node_node_storage,node_node_assets toneIndigo
```

## Components

### Core Runtime

Startup, command processing, settings, reporting, and execution flow.

- [Entry](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/Grbl_Esp32.ino): arduino sketch · Arduino firmware entrypoint. · `Grbl_Esp32/Grbl_Esp32.ino`
- [Grbl](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Grbl.cpp): orchestrator · Top-level firmware runtime coordination. · `Grbl_Esp32/src/Grbl.cpp`
- [Protocol](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Protocol.cpp): command core · Routes incoming commands and real-time events. · `Grbl_Esp32/src/Protocol.cpp`
- [Exec](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Exec.cpp): execution · Executes machine actions and state transitions. · `Grbl_Esp32/src/Exec.cpp`
- [Settings](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Settings.cpp): config store · Persistent machine configuration and runtime settings. · `Grbl_Esp32/src/Settings.cpp`
- [Report](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Report.cpp): status output · Machine status, alarms, and messages. · `Grbl_Esp32/src/Report.cpp`
- [Error](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Error.cpp): errors · Error text and fault reporting helpers. · `Grbl_Esp32/src/Error.cpp`
- [GCode](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/GCode.cpp): parser · Parses G-code and realtime commands. · `Grbl_Esp32/src/GCode.cpp`
- [Process Settings](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/ProcessSettings.cpp): init stage · Applies defaults and machine settings at startup. · `Grbl_Esp32/src/ProcessSettings.cpp`

### Motion Stack

Planning, step generation, jogging, probing, and safety limits.

- [Planner](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Planner.cpp): motion planner · Buffers and plans coordinated motion. · `Grbl_Esp32/src/Planner.cpp`
- [Stepper](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Stepper.cpp): step control · Generates step timing and motion stepping. · `Grbl_Esp32/src/Stepper.cpp`
- [Motion Control](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/MotionControl.cpp): motion core · Coordinates motion pipeline and timing-sensitive control. · `Grbl_Esp32/src/MotionControl.cpp`
- [I2S Out](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/I2SOut.cpp): step output · High-rate step output backend. · `Grbl_Esp32/src/I2SOut.cpp`
- [Limits](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Limits.cpp): safety io · Homing and limit switch handling. · `Grbl_Esp32/src/Limits.cpp`
- [Probe](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Probe.cpp): safety io · Probing and probe-trigger logic. · `Grbl_Esp32/src/Probe.cpp`
- [Jog](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Jog.cpp): manual motion · Interactive jogging support. · `Grbl_Esp32/src/Jog.cpp`

### Hardware Abstraction

Machine profiles, pins, motors, spindles, and outputs.

- [CPU Map](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/CPUMap.h): board map · Board-specific pin and CPU mapping. · `Grbl_Esp32/src/CPUMap.h`
- [Machines](https://github.com/bdring/grbl_esp32/tree/main/Grbl_Esp32/src/Machines): profiles · Predefined machine profiles and kinematics. · `Grbl_Esp32/src/Machines`
- [Pins](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Pins.cpp): pin config · Pin assignment and hardware signal mapping. · `Grbl_Esp32/src/Pins.cpp`
- [Motors](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Motors/Motors.cpp): driver layer · Motor abstractions and driver implementations. · `Grbl_Esp32/src/Motors/Motors.cpp`
- [Spindle](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Spindles/Spindle.cpp): tool output · Spindle, laser, and VFD output control. · `Grbl_Esp32/src/Spindles/Spindle.cpp`
- [Aux Outputs](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/CoolantControl.cpp): io outputs · Coolant and user-defined auxiliary outputs. · `Grbl_Esp32/src/CoolantControl.cpp`
- [Custom Code](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/CustomCode.cpp): extension hooks · Project-specific behavior and machine extensions. · `Grbl_Esp32/src/CustomCode.cpp`

### Connectivity

Web UI, network services, and command ingress adapters.

- [WebUI](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/WebUI/WebServer.cpp): network ui · HTTP and web control subsystem. · `Grbl_Esp32/src/WebUI/WebServer.cpp`
- [Telnet](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/WebUI/TelnetServer.cpp): network ingress · Telnet command ingress and session handling. · `Grbl_Esp32/src/WebUI/TelnetServer.cpp`
- [Serial](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/Serial.cpp): uart ingress · USB/serial transport and streaming. · `Grbl_Esp32/src/Serial.cpp`
- [Wi-Fi](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/WebUI/WifiServices.cpp): network services · Wi-Fi configuration and network services. · `Grbl_Esp32/src/WebUI/WifiServices.cpp`

### Storage & Assets

Persistent settings, SD jobs, and embedded UI assets.

- [SD Card](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/src/SDCard.cpp): job storage · SD-based job input and storage access. · `Grbl_Esp32/src/SDCard.cpp`
- [Embedded UI](https://github.com/bdring/grbl_esp32/blob/main/Grbl_Esp32/data/index.html.gz): assets · Compressed frontend assets served by firmware. · `Grbl_Esp32/data/index.html.gz`

## Connections

- Entry → Grbl: starts · Sketch entry initializes the firmware runtime.
- Grbl → Protocol: drives · Main runtime hands command handling to Protocol.
- Grbl → Settings: loads · Runtime reads persistent machine settings.
- Grbl → Report: reports · Runtime emits state and status reports.
- Grbl → Exec: coordinates · Core runtime coordinates execution and state changes.
- Process Settings → Settings: applies · Startup processing applies settings and defaults.
- Protocol → GCode: parses · Protocol routes incoming text to the parser.
- GCode → Exec: executes · Parsed commands become machine actions.
- Protocol → Serial: ingress · Serial is one of the command front doors.
- Protocol → WebUI: ingress · Web UI submits commands into the same path.
- Protocol → Telnet: ingress · Telnet sessions feed commands to Protocol.
- Protocol → SD Card: jobs · SD card jobs enter the command stream.
- Protocol → Error: errors · Protocol surfaces parse and runtime errors.
- Exec → Planner: queues · Execution stages motion into the planner.
- Planner → Stepper: feeds · Planned segments are consumed by stepping.
- Stepper → I2S Out: outputs · Stepper uses high-rate step output backend.
- Exec → Limits: checks · Execution monitors limit and homing safety.
- Exec → Probe: checks · Execution coordinates probing operations.
- Exec → Jog: supports · Interactive jogging is handled through execution.
- Motion Control → Planner: manages · Motion control manages planning flow.
- Motion Control → Stepper: manages · Motion control manages step generation.
- Machines → Pins: defines · Machine profiles bind hardware pin mapping.
- CPU Map → Pins: supports · CPU map feeds board-specific pin behavior.
- Motors → Planner: driven by · Motor drivers are commanded by motion planning.
- Spindle → Exec: tooling · Spindle and tool output are controlled during execution.
- Aux Outputs → Exec: aux io · Coolant and user outputs follow machine state.
- Custom Code → Grbl: extends · Custom code hooks into core runtime behavior.
- WebUI → Wi-Fi: uses · Web UI depends on Wi-Fi services and config.
- WebUI → Embedded UI: serves · Embedded frontend assets are delivered by the web server.
