# uofu-emb-26/ota architecture

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

- Interactive diagram: https://gitdiagram.com/uofu-emb-26/ota
- Repository: https://github.com/uofu-emb-26/ota

## Overview

1. **What this repo is**
   - This is an embedded OTA update system for an **STM32F072** target.
   - The core architectural pattern is **bootloader + dual application slots (A/B swap)**, so firmware can be updated without field intervention.

2. **System boundary**
   - The repository contains the **STM32-side firmware and build tooling**.
   - The transport path described in the README includes a **Raspberry Pi Pico bridge over UART** and a **Wi‑Fi server**, but those external components are not in this repo.
   - On the MCU, the firmware does not appear to implement Wi‑Fi itself; it relies on an incoming update stream from the external bridge.

3. **Execution model**
   - The code is split into a **bootloader path** and a **firmware/application path**.
   - `bootloader.c` / `bootloader_main.c` handle startup and slot selection.
   - `main.c` is the standard CubeMX-generated entrypoint for the firmware side.
   - `stm32f0xx_it.c`, `system_stm32f0xx.c`, and the HAL MSP file provide the low-level runtime scaffolding.

4. **Flash partitioning**
   - The linker scripts define the memory layout:
     - `STM32F072XX_FLASH.ld`
     - `STM32F072_BOOTLOADER.ld`
     - `STM32F072_APP_A.ld`
     - `STM32F072_APP_B.ld`
   - This is the main architectural mechanism for A/B swapping: the bootloader occupies one region, and the two firmware images live in separate flash slots.
   - The choice of linker scripts suggests separate build targets or image layouts for bootloader vs. application slots.

5. **Boot decision and failover**
   - The bootloader’s responsibility is to **decide which slot to run** based on stored metadata and image validity.
   - The expected behavior is: on reset, inspect OTA state, choose the active/healthy image, and fall back if the new image is incomplete or invalid.
   - This is the reliability boundary of the system: the bootloader must preserve a known-good image.

6. **Update/write path**
   - `flash_update.c` is the main boundary for programming flash.
   - It likely handles erase/program operations, page alignment, and write verification.
   - The OTA data path is therefore: **receive update bytes → write to inactive flash slot → verify → mark valid → reboot**.

7. **Metadata and state**
   - `ota_metadata.c` / `ota_metadata.h` store persistent OTA state such as slot status, validity, and possibly version or boot flags.
   - This metadata is what lets the bootloader know whether to boot slot A, slot B, or stay on the existing image.
   - In an A/B system, metadata is as important as the image bytes themselves because it controls rollback safety.

8. **Hardware/platform dependencies**
   - The project is built on **STM32Cube HAL/CMSIS** for STM32F0.
   - The driver stack under `Drivers/STM32F0xx_HAL_Driver` and `Drivers/CMSIS` is vendor-supplied support, not application logic.
   - The MCU peripherals visible in the dependencies include UART, flash, RCC, GPIO, CRC, and USB-related low-level support.

9. **Communication assumptions**
   - The README describes an update pipeline that goes **server → Wi‑Fi chip → Pico → UART → STM32**.
   - Architecturally, the STM32 side is the final receiver and flash writer; the upstream network protocol is abstracted away before it reaches this repo.
   - This keeps the firmware small and deterministic, which is appropriate for a bootloader.

10. **Build system and code generation**
   - The project uses **CMake** plus **STM32CubeMX-generated sources** (`OTA.ioc`, `cmake/stm32cubemx`).
   - `CMakePresets.json` and the toolchain files indicate cross-compilation for ARM embedded targets.
   - The repo is set up for reproducible firmware builds rather than a generic desktop application workflow.

11. **Developer/support tooling**
   - `flash_new_data.py` and `flash_commands.txt` are repository-side utilities for provisioning or testing firmware/update payloads.
   - `PROJECT_HANDOVER.md` and `notes.txt` suggest the repo is also being used as an engineering handoff artifact, which is useful for documenting flash layout and update procedure.

12. **How to draw the architecture**
   - A good diagram should show:
     - external update server,
     - Wi‑Fi/Pico transport bridge,
     - STM32 bootloader,
     - flash metadata/state store,
     - flash slot A,
     - flash slot B,
     - boot decision/fallback loop.
   - The key flows are **incoming firmware data to inactive slot** and **metadata-driven slot selection on reboot**.

Main entry points: [`OTA/Core/Src/bootloader_main.c`](https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/bootloader_main.c) (Boot entry), [`OTA/Core/Src/flash_update.c`](https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/flash_update.c) (Flash writer), [`OTA/Core/Src/main.c`](https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/main.c) (App entry), [`OTA/Core/Src/ota_metadata.c`](https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/ota_metadata.c) (OTA state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_external["External update path"]
  node_n_server(("Update server<br/>external source"))
  node_n_wifi_bridge["Wi‑Fi bridge<br/>transport bridge"]
  node_n_pico_uart["Pico UART<br/>serial bridge"]
end

subgraph group_group_firmware["STM32 firmware"]
  node_n_bootloader["Bootloader<br/>slot selector<br/>[bootloader.c]"]
  node_n_boot_main["Boot entry<br/>startup logic<br/>[bootloader_main.c]"]
  node_n_ota_meta[("OTA state<br/>persistent metadata<br/>[ota_metadata.c]")]
  node_n_flash_update["Flash writer<br/>programming path<br/>[flash_update.c]"]
  node_n_app_main["App entry<br/>application entrypoint<br/>[main.c]"]
  node_n_flash_a[("Slot A<br/>flash image<br/>[STM32F072_APP_A.ld]")]
  node_n_flash_b[("Slot B<br/>flash image<br/>[STM32F072_APP_B.ld]")]
  node_n_flash_boot[("Boot flash<br/>flash layout")]
end

subgraph group_group_platform["Platform support"]
  node_n_irq["Interrupts<br/>runtime handlers<br/>[stm32f0xx_it.c]"]
  node_n_msp["HAL MSP<br/>board support"]
  node_n_system["System init<br/>clock/runtime setup<br/>[system_stm32f0xx.c]"]
  node_n_startup["Startup asm<br/>reset vector"]
  node_n_hal["STM32 HAL<br/>vendor driver stack<br/>[stm32f0xx_hal.c]"]
  node_n_cmsis["CMSIS<br/>device support<br/>[stm32f072xb.h]"]
end

subgraph group_group_build["Build and tooling"]
  node_n_build["CMake build<br/>build system<br/>[CMakeLists.txt]"]
  node_n_cube["CubeMX config<br/>generated config<br/>[OTA.ioc]"]
  node_n_tooling["Flash tools<br/>operator tooling<br/>[flash_new_data.py]"]
end

node_n_server -->|"firmware stream"| node_n_wifi_bridge
node_n_wifi_bridge -->|"relay"| node_n_pico_uart
node_n_pico_uart -->|"incoming bytes"| node_n_flash_update
node_n_bootloader -->|"starts"| node_n_boot_main
node_n_boot_main -->|"reads state"| node_n_ota_meta
node_n_boot_main -->|"selects A"| node_n_flash_a
node_n_boot_main -->|"selects B"| node_n_flash_b
node_n_boot_main -->|"runs from"| node_n_flash_boot
node_n_flash_update -->|"writes inactive"| node_n_flash_b
node_n_flash_update -.->|"fallback write"| node_n_flash_a
node_n_flash_update -->|"marks valid"| node_n_ota_meta
node_n_ota_meta -->|"drives decision"| node_n_boot_main
node_n_app_main -->|"uses"| node_n_irq
node_n_app_main -->|"uses"| node_n_msp
node_n_app_main -->|"boots with"| node_n_system
node_n_system -->|"entry chain"| node_n_startup
node_n_hal -->|"flash access"| node_n_flash_update
node_n_cmsis -->|"core defs"| node_n_system
node_n_build -->|"generates"| node_n_cube
node_n_build -.->|"supports"| node_n_tooling

click node_n_bootloader "https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/bootloader.c"
click node_n_boot_main "https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/bootloader_main.c"
click node_n_ota_meta "https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/ota_metadata.c"
click node_n_flash_update "https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/flash_update.c"
click node_n_app_main "https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/main.c"
click node_n_irq "https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/stm32f0xx_it.c"
click node_n_msp "https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/stm32f0xx_hal_msp.c"
click node_n_system "https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/system_stm32f0xx.c"
click node_n_startup "https://github.com/uofu-emb-26/ota/blob/main/OTA/startup_stm32f072xb.s"
click node_n_hal "https://github.com/uofu-emb-26/ota/blob/main/OTA/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal.c"
click node_n_cmsis "https://github.com/uofu-emb-26/ota/blob/main/OTA/Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f072xb.h"
click node_n_flash_a "https://github.com/uofu-emb-26/ota/blob/main/OTA/STM32F072_APP_A.ld"
click node_n_flash_b "https://github.com/uofu-emb-26/ota/blob/main/OTA/STM32F072_APP_B.ld"
click node_n_flash_boot "https://github.com/uofu-emb-26/ota/blob/main/OTA/STM32F072_BOOTLOADER.ld"
click node_n_build "https://github.com/uofu-emb-26/ota/blob/main/OTA/CMakeLists.txt"
click node_n_cube "https://github.com/uofu-emb-26/ota/blob/main/OTA/OTA.ioc"
click node_n_tooling "https://github.com/uofu-emb-26/ota/blob/main/OTA/flash_new_data.py"

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_n_server,node_n_wifi_bridge,node_n_pico_uart toneBlue
class node_n_bootloader,node_n_boot_main,node_n_ota_meta,node_n_flash_update,node_n_app_main,node_n_flash_a,node_n_flash_b,node_n_flash_boot toneAmber
class node_n_irq,node_n_msp,node_n_system,node_n_startup,node_n_hal,node_n_cmsis toneMint
class node_n_build,node_n_cube,node_n_tooling toneRose
```

## Components

### External update path

Components outside the STM32 repo boundary that deliver update bytes.

- **Update server**: external source · Hosts firmware payloads upstream of the embedded transport chain.
- **Wi‑Fi bridge**: transport bridge · External network-facing hop that forwards update bytes toward the MCU.
- **Pico UART**: serial bridge · UART relay into the STM32; abstracts the network protocol away from the MCU.

### STM32 firmware

MCU-side bootloader, application entrypoints, and OTA flash control.

- [Bootloader](https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/bootloader.c): slot selector · Reset-time firmware that chooses the active or fallback image. · `OTA/Core/Src/bootloader.c`
- [Boot entry](https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/bootloader_main.c): startup logic · Bootloader main path that initializes decision flow and jump handling. · `OTA/Core/Src/bootloader_main.c`
- [OTA state](https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/ota_metadata.c): persistent metadata · Stored slot validity, boot flags, and related update state. · `OTA/Core/Src/ota_metadata.c`
- [Flash writer](https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/flash_update.c): programming path · Erases, programs, and verifies incoming bytes into flash memory. · `OTA/Core/Src/flash_update.c`
- [App entry](https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/main.c): application entrypoint · Standard CubeMX-generated firmware main function for the application side. · `OTA/Core/Src/main.c`
- [Slot A](https://github.com/uofu-emb-26/ota/blob/main/OTA/STM32F072_APP_A.ld): flash image · Primary application partition defined by its linker layout. · `OTA/STM32F072_APP_A.ld`
- [Slot B](https://github.com/uofu-emb-26/ota/blob/main/OTA/STM32F072_APP_B.ld): flash image · Secondary application partition used for OTA swaps and rollback. · `OTA/STM32F072_APP_B.ld`
- [Boot flash](https://github.com/uofu-emb-26/ota/blob/main/OTA/STM32F072_BOOTLOADER.ld): flash layout · Bootloader-reserved linker layout and MCU memory map. · `OTA/STM32F072_BOOTLOADER.ld`

### Platform support

Vendor HAL/CMSIS runtime and low-level startup scaffolding.

- [Interrupts](https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/stm32f0xx_it.c): runtime handlers · Exception and peripheral interrupt vectors for the MCU runtime. · `OTA/Core/Src/stm32f0xx_it.c`
- [HAL MSP](https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/stm32f0xx_hal_msp.c): board support · Peripheral and low-level HAL setup used by Cube-generated code. · `OTA/Core/Src/stm32f0xx_hal_msp.c`
- [System init](https://github.com/uofu-emb-26/ota/blob/main/OTA/Core/Src/system_stm32f0xx.c): clock/runtime setup · CMSIS system clock and core startup support. · `OTA/Core/Src/system_stm32f0xx.c`
- [Startup asm](https://github.com/uofu-emb-26/ota/blob/main/OTA/startup_stm32f072xb.s): reset vector · MCU reset and initial stack/vector table setup. · `OTA/startup_stm32f072xb.s`
- [STM32 HAL](https://github.com/uofu-emb-26/ota/blob/main/OTA/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal.c): vendor driver stack · ST-provided hardware abstraction layer used by the firmware. · `OTA/Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal.c`
- [CMSIS](https://github.com/uofu-emb-26/ota/blob/main/OTA/Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f072xb.h): device support · Core and device definitions for STM32F0 and Cortex-M0. · `OTA/Drivers/CMSIS/Device/ST/STM32F0xx/Include/stm32f072xb.h`

### Build and tooling

Cross-compilation, generated project files, and operator utilities.

- [CMake build](https://github.com/uofu-emb-26/ota/blob/main/OTA/CMakeLists.txt): build system · Cross-compilation and generated build orchestration for embedded targets. · `OTA/CMakeLists.txt`
- [CubeMX config](https://github.com/uofu-emb-26/ota/blob/main/OTA/OTA.ioc): generated config · Source of truth for pinout, peripherals, and generated project settings. · `OTA/OTA.ioc`
- [Flash tools](https://github.com/uofu-emb-26/ota/blob/main/OTA/flash_new_data.py): operator tooling · Repo-side scripts and commands used to provision or test update images. · `OTA/flash_new_data.py`

## Connections

- Update server → Wi‑Fi bridge: firmware stream · Update payload originates from the server.
- Wi‑Fi bridge → Pico UART: relay · External bridge forwards the stream over the transport chain.
- Pico UART → Flash writer: incoming bytes · STM32 receives update data for flash programming.
- Bootloader → Boot entry: starts · Reset flow enters bootloader main logic.
- Boot entry → OTA state: reads state · Bootloader inspects stored slot metadata before choosing an image.
- Boot entry → Slot A: selects A · May jump to slot A when it is valid and active.
- Boot entry → Slot B: selects B · May jump to slot B when it is valid and active.
- Boot entry → Boot flash: runs from · Bootloader executes from its reserved flash region.
- Flash writer → Slot B: writes inactive · Incoming image is programmed into the inactive application slot during OTA.
- Flash writer → Slot A: fallback write · Can target either slot depending on the current active image and update plan.
- Flash writer → OTA state: marks valid · Update completion updates persistent boot state and validity flags.
- OTA state → Boot entry: drives decision · Stored state governs next boot slot and rollback behavior.
- App entry → Interrupts: uses · Application runtime relies on interrupt handlers.
- App entry → HAL MSP: uses · Application runtime uses HAL board support initialization.
- App entry → System init: boots with · Application startup depends on core clock and system setup.
- System init → Startup asm: entry chain · Reset vector and startup assembly hand off to system initialization.
- STM32 HAL → Flash writer: flash access · Vendor HAL provides flash, UART, RCC, GPIO, and CRC primitives.
- CMSIS → System init: core defs · CMSIS supplies the MCU/device and core support definitions.
- CMake build → CubeMX config: generates · CMake build is paired with CubeMX-generated project configuration.
- CMake build → Flash tools: supports · Build ecosystem is complemented by repository-side flashing utilities.
