Software Architecture

Purpose

This document describes the firmware and host-side software architecture of the MOTSEN Tool: the layers, the modules within each layer, the threading and timing model, and the seams that make the firmware portable across MCUs. It sits between System Description and Software Requirements.

The architecture is intentionally MCU-agnostic. Every MCU-specific concern is confined to the hardware abstraction layer (HAL). Application, services, and measurement code use only HAL interfaces, never vendor SDK calls or register access.

Architecture Overview

Software Architecture: Layered architecture SWARCH_001
status: draft
derived from: DESC_033

The firmware is organized in four layers, lower layers do not call upward:

  1. HAL — thin abstraction over MCU peripherals (GPIO, UART, timer, ADC, PWM, NVM).

  2. Drivers — peripheral-oriented modules built on the HAL (PWM driver, ADC sampler, Hall reader, comms framer).

  3. Services — cross-cutting facilities (system tick, scheduler, parameter store, safety supervisor, comms protocol, logger).

  4. Application — operating-mode state machine, measurement procedures, control loops.

The host side adds two further layers above the link: a local web server and a browser UI. The on-wire protocol is the seam between firmware and host.

Software Architecture: Layer diagram SWARCH_002
status: draft
derived from: DESC_033
digraph sw_layers { rankdir=BT; node [shape=box, style="rounded,filled", fillcolor="#D5F5E3"]; HAL [label="HAL\n(GPIO, UART, Timer, ADC, PWM, NVM)"]; Drv [label="Drivers\n(PWM, ADC, Hall, Comms framer)"]; Svc [label="Services\n(Tick, Scheduler, Params, Safety, Protocol, Logger)"]; App [label="Application\n(State machine, Measurement, Control)"]; Wire [label="On-wire protocol", shape=ellipse, fillcolor="#FCF3CF"]; Host [label="Host: local web server + browser UI", fillcolor="#D6EAF8"]; HAL -> Drv; Drv -> Svc; Svc -> App; App -> Wire [style=dashed, dir=both]; Wire -> Host [style=dashed, dir=both]; }

Hardware Abstraction Layer

Software Architecture: HAL scope SWARCH_010
status: draft
derived from: DESC_033
is derived by: SWREQ_010

The HAL exposes interfaces for the peripheral classes required by MCU peripheral classes used (HWARCH_060): GPIO, UART, timer, ADC, PWM, and NVM. Each interface is defined in pure C and contains no MCU-specific identifiers.

Software Architecture: HAL portability seam SWARCH_011
status: draft
derived from: DESC_033
is derived by: SWREQ_011, SWREQ_012

The HAL is the only place where MCU vendor headers, SDK calls, or register access may appear. Layers above the HAL #include only HAL headers. A new MCU is supported by adding a new HAL implementation; no change above the HAL is required.

Software Architecture: HAL build-time configuration SWARCH_012
status: draft
derived from: DESC_033
is derived by: SWREQ_013, SWREQ_014

The HAL implementation for a given MCU is selected at build time. Pin assignments and peripheral instances are passed in as build-time configuration, not hard-coded in the HAL source.

Software Architecture: HAL minimal surface SWARCH_013
status: draft
links to: RISK_002
derived from: DESC_033

The HAL is sized to the minimum needed by the current phase. New HAL functions are added only when a driver or service needs them. This is the mitigation for RISK_002 (HAL over-engineered prematurely).

Software Architecture: HAL implementation strategy SWARCH_014
status: placeholder
links to: DEC_005
derived from: DESC_063
is derived by: SWREQ_902

Whether each HAL implementation wraps the MCU vendor’s RTD/SDK or sits directly on registers is pending DEC_005.

Drivers Layer

Software Architecture: PWM driver SWARCH_020
status: draft
derived from: DESC_030
is derived by: SWREQ_020, SWREQ_021

The PWM driver translates the application’s duty-cycle commands into HAL calls. It configures center-aligned mode, complementary outputs, dead-time, and ADC trigger generation. It exposes a single apply_duties(a, b, c) entry point and an enable / disable pair.

Software Architecture: ADC sampler SWARCH_021
status: draft
derived from: DESC_031
is derived by: SWREQ_022, SWREQ_023

The ADC sampler runs in the ADC end-of-conversion interrupt and delivers a struct of fresh phase currents and DC-bus voltage to the control loop within one PWM period. It applies calibration scale and offset before publishing the values.

Software Architecture: Hall reader SWARCH_022
status: draft
derived from: DESC_032
is derived by: SWREQ_024

The Hall reader samples Hall inputs on edge interrupts and on a periodic poll. It maintains the current commutation sector and an inferred direction, and exposes them as read-only state.

Software Architecture: Encoder reader SWARCH_023
status: placeholder
links to: MIL_042
derived from: DESC_032
is derived by: SWREQ_200

Incremental encoder driver. Scheduled for Phase 2 (MIL_042).

Software Architecture: Resolver driver SWARCH_024
status: placeholder
links to: MIL_077
derived from: DESC_032
is derived by: SWREQ_201

Resolver excitation and demodulation driver. Scheduled for Phase 3 (MIL_077).

Software Architecture: Comms framer SWARCH_025
status: draft
derived from: DESC_035
is derived by: SWREQ_025

The comms framer encodes and decodes frames on the host link using the selected on-wire format (pending DEC_003). It exposes a byte-stream interface to the protocol service and a frame interface to the link’s HAL UART instance.

Services Layer

Software Architecture: System tick and scheduler SWARCH_030
status: draft
derived from: DESC_033
is derived by: SWREQ_030, SWREQ_031, SWREQ_032

A single timer-driven system tick provides non-blocking timing primitives and drives a cooperative scheduler for periodic work that is not time-critical (telemetry assembly, host comms, status updates).

Software Architecture: Real-time execution model SWARCH_031
status: draft
derived from: DESC_033
is derived by: SWREQ_033

Real-time work runs in interrupt context, not on the scheduler:

  • ADC end-of-conversion ISR → control / measurement step.

  • Hall edge ISR → sector update.

  • Comms RX ISR → byte intake.

The application loop runs at lower priority and is preempted by these ISRs. No RTOS is required.

Software Architecture: Parameter store SWARCH_040
status: draft
derived from: DESC_036
is derived by: SWREQ_040, SWREQ_041

The parameter store holds the runtime-tunable values (limits, measurement settings, calibration). Parameters are accessible by ID from the host link. RAM-only in Phase 1; non-volatile persistence is a placeholder until the NVM-bearing HAL is required.

Software Architecture: Calibration store SWARCH_041
status: draft
derived from: DESC_031
is derived by: SWREQ_042

Sensing-chain calibration coefficients (scale, offset for each channel) live in the calibration store and are applied by the ADC sampler. The measurement engine refuses to publish a characterization result if the calibration store is empty (see Sensing chain calibrated be... (SYS_044)).

Software Architecture: Comms protocol SWARCH_050
status: draft
derived from: DESC_035
is derived by: SWREQ_050, SWREQ_051

The protocol service runs on top of the comms framer. It defines three message classes:

  • Parameter read / write requests and responses.

  • Telemetry streaming records published periodically.

  • Command request / response for measurement procedures and mode transitions.

Protocol versioning is included in the frame header so that host and firmware can detect a mismatch.

Software Architecture: Safety supervisor SWARCH_060
status: draft
derived from: DESC_034
is derived by: SWREQ_060, SWREQ_061, SWREQ_062

The safety supervisor runs on every ADC sample and checks instantaneous current and DC-bus voltage against firmware-configurable limits. On any limit breach it:

  1. Disables PWM via the PWM driver.

  2. Transitions the state machine to Fault.

  3. Captures the fault cause and the contents of the latest sample frame to the logger.

Software Architecture: Logger SWARCH_070
status: draft
derived from: DESC_036
is derived by: SWREQ_070, SWREQ_071

The logger collects diagnostic events into a small in-RAM ring buffer and forwards them on the telemetry channel. Events include faults, mode transitions, and procedure start/end markers. The logger has no blocking calls and never allocates.

Application Layer

Software Architecture: Operating-mode state machine SWARCH_080
status: draft
derived from: DESC_040

The state machine is the single owner of the operating mode (Idle, Sensor Check, Characterization, Run, Fault). It accepts transition requests from the protocol service, validates them against the rules in Firmware-enforced transitions (SYS_111) to No direct Characterization ... (SYS_114), and drives mode-entry / exit actions (PWM enable/disable, current limit selection).

Software Architecture: Sensor health check procedure SWARCH_090
status: draft
derived from: DESC_037
is derived by: SWREQ_090, SWREQ_091

The sensor health check is a sequence of low-energy stimuli that validates phase sequence, Hall alignment, and basic connectivity. It runs entirely in firmware, with the host receiving only command and result frames.

Software Architecture: Rs measurement procedure SWARCH_091
status: draft
derived from: DESC_037
is derived by: SWREQ_100, SWREQ_101

Rs is measured by applying a controlled DC current along a known electrical axis and computing R = V / I after settling. The procedure declares its own current limit, ramps to the test current, and aborts the procedure if the limit is breached.

Software Architecture: Ld / Lq measurement procedure SWARCH_092
status: placeholder
links to: MIL_040
derived from: DESC_037
is derived by: SWREQ_202

Ld / Lq measurement. Scheduled for Phase 2 (MIL_040).

Software Architecture: Back-EMF / Kt measurement procedure SWARCH_093
status: placeholder
links to: MIL_041
derived from: DESC_037
is derived by: SWREQ_203

Back-EMF and torque-constant measurement. Scheduled for Phase 2 (MIL_041).

Software Architecture: Open-loop commutation control SWARCH_100
status: draft
derived from: DESC_040
is derived by: SWREQ_110, SWREQ_111

The Phase 1 control loop drives open-loop sinusoidal commutation with an enforced current limit. The control loop runs synchronously with the ADC sampler (see Real-time execution model (SWARCH_031)).

Software Architecture: FOC control SWARCH_101
status: placeholder
links to: MIL_045
derived from: DESC_040
is derived by: SWREQ_204

Field-oriented control. Scheduled for Phase 2 (MIL_045).

Host-Side Software

Software Architecture: Local web server SWARCH_110
status: placeholder
links to: DEC_002, MIL_017
derived from: DESC_036
is derived by: SWREQ_124, SWREQ_125

A local web server on the host PC serves the UI static assets and bridges a real-time channel to the firmware over the host link. The backend language and framework are pending DEC_002.

Software Architecture: Browser UI SWARCH_111
status: draft
derived from: DESC_036
is derived by: SWREQ_120, SWREQ_121, SWREQ_122

The browser UI provides a single-page application showing live phase currents, sensor state, the current operating mode, and any active fault. It triggers measurement procedures via the protocol and displays their results.

Software Architecture: Host protocol client SWARCH_112
status: draft
derived from: DESC_035
is derived by: SWREQ_123

The host has its own implementation of the comms protocol defined by Comms protocol (SWARCH_050). Frame format and physical layer match the firmware side (pending DEC_003).

Software Architecture: Scripting interface SWARCH_113
status: placeholder
links to: MIL_044
derived from: DESC_036
is derived by: SWREQ_205

A scriptable interface on the host allows running measurement sequences without the browser UI. Scheduled for Phase 2 (MIL_044).

Software Architecture: Host parameter & result persistence SWARCH_114
status: placeholder
links to: MIL_047
derived from: DESC_036
is derived by: SWREQ_206

Persistent storage of motor parameters and measurement results on the host filesystem. Scheduled for Phase 2 (MIL_047).

Build and Configuration

Software Architecture: Build configuration SWARCH_120
status: draft
derived from: DESC_033
is derived by: SWREQ_130

The firmware build accepts a “target” parameter that selects the HAL implementation, the pin/peripheral mapping, and the feature set for the target phase. The same source tree builds for all supported targets.

Software Architecture: Compile-time feature gating SWARCH_121
status: draft
derived from: DESC_050
is derived by: SWREQ_131

Optional features (CAN, encoder, resolver, FOC, etc.) are gated at compile time so that the Phase 1 build does not pull in Phase 2 or 3 code.

Software Architecture: No dynamic allocation in firmware SWARCH_122
status: draft
derived from: DESC_033
is derived by: SWREQ_132

The firmware shall not use dynamic memory allocation after startup. All buffers are sized at build time.

Open Items

Software Architecture: Host backend stack — pending SWARCH_900
status: placeholder
links to: DEC_002
derived from: DESC_062
is derived by: SWREQ_900

Backend language and framework for the local web server are pending DEC_002.

Software Architecture: Host link protocol — pending SWARCH_901
status: placeholder
links to: DEC_003
derived from: DESC_061
is derived by: SWREQ_901

The exact frame format and physical layer of the host link are pending DEC_003. The architecture above assumes the existence of a framed bi-directional channel but does not bind it.

Software Architecture: Sensorless control — pending SWARCH_902
status: placeholder
links to: DEC_006
derived from: DESC_065
is derived by: SWREQ_903

Whether observer-based sensorless control is in scope is pending DEC_006.

Traceability

Each swarch node above declares derived_from exactly one DESC_* node in System Description. The forward direction is populated by swreq nodes in Software Requirements, which declare derived_from either a SYS_* requirement or a SWARCH_* block here.