# 1141170704/buck-boost architecture

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

- Interactive diagram: https://gitdiagram.com/1141170704/buck-boost
- Repository: https://github.com/1141170704/buck-boost

## Overview

##### Project Type and Purpose
- This is an embedded firmware project for a Buck-Boost digital power supply based on an STM32G4 series MCU. The device reads voltages/currents, runs a PID control loop, drives the power stage (buck-boost converter), provides a user interface (keys + OLED), and persists settings to external flash.

##### File Structure Analysis
- Top-level:
- .cmsis/… – ARM CMSIS core headers for startup and core access
- Core/Hardware – Driver implementations for external hardware modules (Key, OLED, W25Q64 flash)
- Core/Inc – HAL peripheral wrappers (ADC, DMA, GPIO, I2C, SPI, TIM, USART, watchdog, HRTIM, PID)
- Core/Src – Source files that implement peripheral initialization and ISR handlers, plus main.c and system startup
- EIDE configs & workspace files – Project metadata for the IDE/build environment
- Patterns:
- Layered architecture:
– Hardware Abstraction (CMSIS + STM32 HAL)
– Board-level Drivers (Core/Hardware)
– Peripheral Wrappers & Middleware (Core/Inc & Core/Src)
– Application Logic (main.c + PID controller + UI logic)

##### Key Components
a. STM32G4 MCU Block
– Cortex-M4 core, flash, SRAM
– On-chip peripherals: ADC, DMA, TIM/HRTIM, SPI, I2C, USART, IWDG
b. External Hardware
– Buck-Boost power stage (power MOSFETs + inductors + capacitors)
– OLED display (SPI or I2C interface)
– User keys (GPIO inputs)
– W25Q64 external flash (SPI)
c. Firmware Layers
– CMSIS (core startup, vector table)
– STM32 HAL (low-level register drivers)
– Board Drivers (Core/Hardware/*.c/.h)
– Middleware: PID control, system functions (Core/Inc + Core/Src)
– Application (main.c loop, UI state machine)

##### Interactions & Data Flows
- ADC → DMA → PID algorithm → Timer/HRTIM PWM outputs → Power stage
- Key presses (GPIO interrupts) → UI state machine → update OLED via SPI/I2C
- Settings save/load → W25Q64 flash via SPI
- Watchdog timer resets on main loop “heartbeat”
- USART for debugging/parameter tuning (optional)

##### Architectural Patterns & Principles
- Layered separation: hardware abstraction, drivers, middleware, application
- Interrupt-driven peripherals with DMA for real-time sampling
- Modular drivers per hardware block (single responsibility)
- Persistent storage abstraction for settings

##### Essential Elements to Capture
- Clear component names (e.g., “STM32G4 ADC1 + DMA1” vs. generic “ADC”)
- Data flows with arrowheads and labels
- Notes on critical timing (e.g., sampling rate, PWM frequency) if desired

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Hardware Layer
    subgraph "Hardware Layer"
        style HardwareLayer fill:#e0ffe0,stroke:#008000,stroke-width:1px
        MCU["STM32G4 MCU\nCortex-M4, Flash, SRAM"]:::hardware
        ADC1["ADC1"]:::hardware
        DMA1["DMA1"]:::hardware
        HRTIM["HRTIM"]:::hardware
        TIM["TIM"]:::hardware
        PowerStage["Buck-Boost Power Stage"]:::hardware
        KeysHW["User Keys"]:::hardware
        OLED_HW["OLED Display"]:::hardware
        Flash_HW["W25Q64 Flash"]:::hardware
    end

    %% HAL & Board Drivers Layer
    subgraph "HAL & Board Drivers" 
        style HALLayer fill:#e0f0ff,stroke:#0055ff,stroke-width:1px
        CMSIS["CMSIS Core"]:::hal
        HALConf["HAL Config\nstm32g4xx_hal_conf.h"]:::hal
        HALMSP["HAL MSP Init\nstm32g4xx_hal_msp.c"]:::hal
        SystemStart["System Startup\nsystem_stm32g4xx.c"]:::hal

        subgraph "Peripheral Wrappers"
            ADC_drv["ADC Wrapper\nadc.h/.c"]:::hal
            DMA_drv["DMA Wrapper\ndma.h/.c"]:::hal
            TIM_drv["TIM Wrapper\ntim.h/.c"]:::hal
            HRTIM_drv["HRTIM Wrapper\nhrtim.h/.c"]:::hal
            SPI_drv["SPI Wrapper\nspi.h/.c"]:::hal
            I2C_drv["I2C Wrapper\ni2c.h/.c"]:::hal
            USART_drv["USART Wrapper\nusart.h/.c"]:::hal
            IWDG_drv["IWDG Wrapper\niwdg.h/.c"]:::hal
        end

        subgraph "Board Drivers"
            Key_drv["Key Driver\nCore/Hardware/Key.h/.c"]:::hal
            OLED_drv["OLED Driver\nCore/Hardware/OLED.h/.c"]:::hal
            Flash_drv["W25Q64 Driver\nCore/Hardware/W25Q64.h/.c"]:::hal
        end
    end

    %% Middleware Layer
    subgraph "Middleware"
        style MiddlewareLayer fill:#fff0d0,stroke:#ff8000,stroke-width:1px
        PID_mod["PID Control\nPID.h/.c"]:::middleware
        SysUtil["System Utils\nfunction.h/.c"]:::middleware
    end

    %% Application Layer
    subgraph "Application"
        style AppLayer fill:#f0e0ff,stroke:#8000ff,stroke-width:1px
        MainLoop["Main Loop & UI\nmain.h/.c"]:::app
        IRQHandlers["Interrupt Handlers\nstm32g4xx_it.h/.c"]:::app
    end

    %% Flows
    MCU -->|"/api: ADC data"| ADC1
    ADC1 -->|"via DMA1"| DMA1
    DMA1 -->|"to PID"| PID_mod
    PID_mod -->|"PWM update"| HRTIM
    HRTIM -->|"PWM signal"| PowerStage

    KeysHW -->|"GPIO IRQ"| IRQHandlers
    IRQHandlers -->|"UI event"| MainLoop
    MainLoop -->|"I2C/SPI"| OLED_HW

    MainLoop -->|"heartbeat"| IWDG_drv
    MainLoop -->|"save/load"| Flash_HW
    Flash_HW -->|"SPI"| Flash_drv

    USART_drv -->|"debug"| MainLoop

    %% Initialization Flow
    CMSIS --> HALConf --> HALMSP --> SystemStart --> MainLoop

    %% Click Events
    click CMSIS "https://github.com/1141170704/buck-boost/tree/master/.cmsis/include"
    click HALConf "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/stm32g4xx_hal_conf.h"
    click HALMSP "https://github.com/1141170704/buck-boost/blob/master/Core/Src/stm32g4xx_hal_msp.c"
    click SystemStart "https://github.com/1141170704/buck-boost/blob/master/Core/Src/system_stm32g4xx.c"
    click ADC_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/adc.h"
    click ADC_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Src/adc.c"
    click DMA_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/dma.h"
    click DMA_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Src/dma.c"
    click TIM_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/tim.h"
    click TIM_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Src/tim.c"
    click HRTIM_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/hrtim.h"
    click HRTIM_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Src/hrtim.c"
    click SPI_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/spi.h"
    click SPI_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Src/spi.c"
    click I2C_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/i2c.h"
    click I2C_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Src/i2c.c"
    click USART_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/usart.h"
    click USART_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Src/usart.c"
    click IWDG_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/iwdg.h"
    click IWDG_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Src/iwdg.c"
    click PID_mod "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/PID.h"
    click PID_mod "https://github.com/1141170704/buck-boost/blob/master/Core/Src/PID.c"
    click SysUtil "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/function.h"
    click SysUtil "https://github.com/1141170704/buck-boost/blob/master/Core/Src/function.c"
    click Key_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Hardware/Key.h"
    click Key_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Hardware/Key.c"
    click OLED_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Hardware/OLED.h"
    click OLED_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Hardware/OLED.c"
    click Flash_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Hardware/W25Q64.h"
    click Flash_drv "https://github.com/1141170704/buck-boost/blob/master/Core/Hardware/W25Q64.c"
    click MainLoop "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/main.h"
    click MainLoop "https://github.com/1141170704/buck-boost/blob/master/Core/Src/main.c"
    click IRQHandlers "https://github.com/1141170704/buck-boost/blob/master/Core/Inc/stm32g4xx_it.h"
    click IRQHandlers "https://github.com/1141170704/buck-boost/blob/master/Core/Src/stm32g4xx_it.c"

    %% Styles
    classDef hardware fill:#e0ffe0,stroke:#008000
    classDef hal fill:#e0f0ff,stroke:#0055ff
    classDef middleware fill:#fff0d0,stroke:#ff8000
    classDef app fill:#f0e0ff,stroke:#8000ff
    class HardwareLayer,HALLayer,MiddlewareLayer,AppLayer internal
```
