# davidmolony/mesc_firmware architecture

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

- Interactive diagram: https://gitdiagram.com/davidmolony/mesc_firmware
- Repository: https://github.com/davidmolony/mesc_firmware

## Overview

MESC Firmware is an embedded BLDC field-oriented-control (FOC) motor-control project for STM32 targets. Its principal runtime is motor firmware initialization, followed by acquisition of motor measurements and closed-loop control that drives the inverter; the README documents sensorless, HFI, encoder and Hall sensing, plus torque, speed and duty modes. Shared control code is paired with target-specific hardware and several RTOS interfaces. The excerpts show shared data structures, target setup and one target’s peripheral configuration, but not the complete control loop or interface dispatch, so those links are left unverified. Major README capabilities are retained without asserting unsampled call paths.

Main entry points: [`MESC_Common/Src/MESCfoc.c`](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCfoc.c) (FOC controller), [`MESC_F405RG/Core/Src/main.c`](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_F405RG/Core/Src/main.c) (Target firmware entry), [`MESC_RTOS/AXIS/MESCmotor_state.h`](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/AXIS/MESCmotor_state.h) (Motor state machine), [`MESC_RTOS/Tasks/apps.c`](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/Tasks/apps.c) (RTOS app registry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_control["Motor control"]
  node_foc["FOC controller<br/>[MESCfoc.c]"]
  node_motor_state["Motor state machine<br/>[MESCmotor_state.h]"]
  node_motor_model["Motor initialization<br/>[MESCmotor.c]"]
  node_bldc["BLDC commutation<br/>[MESCBLDC.c]"]
  node_pwm["PWM generation<br/>[MESCpwm.c]"]
  node_sin_lut["Sine and inductance lookup<br/>[MESCsin_lut.c]"]
end

subgraph group_sensing["Sensing and safety"]
  node_measurements["ADC measurements<br/>[MESCBLDC.h]"]
  node_observers["Position observers<br/>[MESCfluxobs.c]"]
  node_hfi["HFI sensing<br/>[MESChfi.c]"]
  node_temperature["Thermal limits<br/>[MESCtemp.c]"]
  node_errors["Fault handling<br/>[MESCerror.c]"]
end

subgraph group_platform["STM32 platforms"]
  node_target_main["Target firmware entry<br/>[main.c]"]
  node_hw_setup["Hardware setup<br/>[MESChw_setup.h]"]
  node_hw_f405["F405 target peripherals<br/>[main.c]"]
end

subgraph group_interfaces["User interfaces"]
  node_rtos_apps["RTOS app registry<br/>[apps.c]"]
  node_rtos_init["RTOS task setup<br/>[init.h]"]
  node_interface_axis["Axis interface<br/>[MESCinterface.c]"]
  node_interface_dash["Dashboard interface<br/>[MESCinterface.c]"]
  node_interface_mesc["MESC interface<br/>[MESCinterface.c]"]
  node_terminal["Terminal<br/>[TTerm.c]"]
end

node_operator(("Operator or host"))

node_target_main -->|"initializes"| node_motor_model
node_target_main -->|"uses"| node_temperature
node_target_main -->|"uses"| node_motor_state
node_target_main -->|"starts tasks"| node_rtos_init
node_target_main -.->|"configures peripherals"| node_hw_f405
node_hw_setup -->|"configures"| node_foc
node_measurements -.->|"provides samples"| node_foc
node_foc -.->|"sets modulation"| node_pwm
node_motor_state -.->|"selects control mode"| node_foc
node_foc -.->|"uses lookup"| node_sin_lut
node_foc -.->|"uses estimates"| node_observers
node_foc -.->|"supports HFI"| node_hfi
node_foc -.->|"thermal rollback"| node_temperature
node_foc -.->|"reports faults"| node_errors
node_operator -.->|"sends commands"| node_interface_axis
node_operator -.->|"sends commands"| node_interface_dash
node_operator -.->|"sends commands"| node_interface_mesc
node_rtos_init -->|"registers apps"| node_rtos_apps
node_rtos_apps -->|"uses terminal"| node_terminal
node_interface_axis -.->|"commands control"| node_foc
node_interface_dash -.->|"commands control"| node_foc
node_interface_mesc -.->|"commands control"| node_foc

click node_target_main "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_F405RG/Core/Src/main.c"
click node_hw_setup "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Inc/MESChw_setup.h"
click node_foc "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCfoc.c"
click node_motor_state "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/AXIS/MESCmotor_state.h"
click node_motor_model "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCmotor.c"
click node_bldc "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCBLDC.c"
click node_pwm "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCpwm.c"
click node_sin_lut "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCsin_lut.c"
click node_measurements "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Inc/MESCBLDC.h"
click node_observers "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCfluxobs.c"
click node_hfi "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESChfi.c"
click node_temperature "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCtemp.c"
click node_errors "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCerror.c"
click node_hw_f405 "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_F405RG/Core/Src/main.c"
click node_rtos_apps "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/Tasks/apps.c"
click node_rtos_init "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/Tasks/init.h"
click node_interface_axis "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/AXIS/MESCinterface.c"
click node_interface_dash "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/Dash/MESCinterface.c"
click node_interface_mesc "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/MESC/MESCinterface.c"
click node_terminal "https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/TTerm/Core/TTerm.c"

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_foc,node_motor_state,node_motor_model,node_bldc,node_pwm,node_sin_lut toneBlue
class node_measurements,node_observers,node_hfi,node_temperature,node_errors toneAmber
class node_target_main,node_hw_setup,node_hw_f405 toneMint
class node_rtos_apps,node_rtos_init,node_interface_axis,node_interface_dash,node_interface_mesc,node_terminal toneRose
class node_operator toneIndigo
```

## Components

### Motor control

- [FOC controller](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCfoc.c): `MESC_Common/Src/MESCfoc.c`
- [Motor state machine](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/AXIS/MESCmotor_state.h): `MESC_RTOS/AXIS/MESCmotor_state.h`
- [Motor initialization](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCmotor.c): `MESC_Common/Src/MESCmotor.c`
- [BLDC commutation](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCBLDC.c): `MESC_Common/Src/MESCBLDC.c`
- [PWM generation](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCpwm.c): `MESC_Common/Src/MESCpwm.c`
- [Sine and inductance lookup](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCsin_lut.c): `MESC_Common/Src/MESCsin_lut.c`

### Sensing and safety

- [ADC measurements](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Inc/MESCBLDC.h): `MESC_Common/Inc/MESCBLDC.h`
- [Position observers](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCfluxobs.c): `MESC_Common/Src/MESCfluxobs.c`
- [HFI sensing](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESChfi.c): `MESC_Common/Src/MESChfi.c`
- [Thermal limits](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCtemp.c): `MESC_Common/Src/MESCtemp.c`
- [Fault handling](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Src/MESCerror.c): `MESC_Common/Src/MESCerror.c`

### STM32 platforms

- [Target firmware entry](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_F405RG/Core/Src/main.c): `MESC_F405RG/Core/Src/main.c`
- [Hardware setup](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_Common/Inc/MESChw_setup.h): `MESC_Common/Inc/MESChw_setup.h`
- [F405 target peripherals](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_F405RG/Core/Src/main.c): `MESC_F405RG/Core/Src/main.c`

### User interfaces

- [RTOS app registry](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/Tasks/apps.c): `MESC_RTOS/Tasks/apps.c`
- [RTOS task setup](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/Tasks/init.h): `MESC_RTOS/Tasks/init.h`
- [Axis interface](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/AXIS/MESCinterface.c): `MESC_RTOS/AXIS/MESCinterface.c`
- [Dashboard interface](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/Dash/MESCinterface.c): `MESC_RTOS/Dash/MESCinterface.c`
- [MESC interface](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/MESC/MESCinterface.c): `MESC_RTOS/MESC/MESCinterface.c`
- [Terminal](https://github.com/davidmolony/mesc_firmware/blob/master/MESC_RTOS/TTerm/Core/TTerm.c): `MESC_RTOS/TTerm/Core/TTerm.c`

### Other components

- **Operator or host**

## Connections

- Target firmware entry → Motor initialization: initializes (evidence: `MESC_F405RG/Core/Src/main.c`)
- Target firmware entry → Thermal limits: uses (evidence: `MESC_F405RG/Core/Src/main.c`)
- Target firmware entry → Motor state machine: uses (evidence: `MESC_F405RG/Core/Src/main.c`)
- Target firmware entry → RTOS task setup: starts tasks (evidence: `MESC_F405RG/Core/Src/main.c`)
- Target firmware entry → F405 target peripherals: configures peripherals (evidence: `MESC_F405RG/Core/Src/main.c`)
- Hardware setup → FOC controller: configures (evidence: `MESC_Common/Inc/MESChw_setup.h`)
- ADC measurements → FOC controller: provides samples
- FOC controller → PWM generation: sets modulation
- Motor state machine → FOC controller: selects control mode
- FOC controller → Sine and inductance lookup: uses lookup
- FOC controller → Position observers: uses estimates
- FOC controller → HFI sensing: supports HFI
- FOC controller → Thermal limits: thermal rollback
- FOC controller → Fault handling: reports faults
- Operator or host → Axis interface: sends commands
- Operator or host → Dashboard interface: sends commands
- Operator or host → MESC interface: sends commands
- RTOS task setup → RTOS app registry: registers apps (evidence: `MESC_RTOS/Tasks/init.h`)
- RTOS app registry → Terminal: uses terminal
- Axis interface → FOC controller: commands control
- Dashboard interface → FOC controller: commands control
- MESC interface → FOC controller: commands control
