# tangtangsix/modbus_tcp architecture

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

- Interactive diagram: https://gitdiagram.com/tangtangsix/modbus_tcp
- Repository: https://github.com/tangtangsix/modbus_tcp

## Overview

This project is an embedded firmware for an STM32F103C8 microcontroller that implements a Modbus TCP communication stack using the HAL library and a W5500 Ethernet controller. In other words, it is a network-enabled embedded application that uses hardware abstraction layers (CMSIS and STM32F1xx_HAL_Driver) along with custom application code to provide Modbus TCP over Ethernet.

##### Main Components
- Hardware Platform: The STM32F103C8 microcontroller board.
- External Networking: W5500 Ethernet controller, which connects to the microcontroller via the SPI interface.
- Software Layers:
– Low-Level Drivers: HAL library files (STM32F1xx_HAL_Driver) and CMSIS components.
– Middleware: Low-level device libraries, possibly RTOS components from the CMSIS/RTOS folders.
– Application Layer: The core Modbus TCP implementation and support code (main.c, gpio.c, spi.c, usart.c, etc.).
- Communication Protocols: Modbus protocol over TCP/IP and the SPI protocol for communication between STM32 and W5500.

##### Relationships and Interactions
- The STM32 microcontroller acts as the central processor.
- It communicates with the W5500 via the SPI bus to handle Ethernet communications.
- The HAL drivers abstract the microcontroller’s peripherals (GPIO, SPI, USART, etc.) and interface with hardware.
- The Modbus TCP stack sits on top of the networking layer and processes network packets to implement the Modbus protocol.
- Optionally, if the RTOS components are used, they manage tasks (like periodic network polling, interrupt handling, and protocol processing).

##### Architectural Patterns and Design Principles
- Layered Architecture: Clear separation between hardware abstraction (HAL/CMSIS drivers), device interface (SPI, GPIO, etc.), and application logic (Modbus TCP).
- Separation of Concerns: The hardware-specific libraries (CMSIS, HAL, STM32F1xx_HAL_Driver) are distinct from business logic (Modbus TCP handling) and network interfacing (W5500).

##### Relevant Technologies and Frameworks
- Keil MDK-ARM (MDK-ARM folders indicate the project is supported by Keil’s toolchain).
- STM32F1xx HAL drivers for hardware interaction.
- CMSIS libraries for device core initialization and abstraction.
- W5500 Ethernet driver located in the W5500 folder.
- Modbus TCP service implementation in the application’s “Src” folder files.

##### Elements
- Labels for each primary component:
– “STM32F103C8 Microcontroller”
– “W5500 Ethernet Controller”
– “HAL Drivers & CMSIS Core”
– “Modbus TCP Application Logic”
– “SPI Bus Interface”
– Indicate data/control flow from the microcontroller to the W5500 via SPI.
– Show how the HAL drivers feed into the application layer.
– For hardware components (e.g., rectangles for MCU and Ethernet chip).

##### Detailed Process
– Represent the STM32 microcontroller and separately, the W5500 Ethernet controller.
- Above this hardware layer, show the “HAL & CMSIS” layer that covers all the low-level drivers.
- “RTOS / Scheduler” if your build uses CMSIS-RTOS components, showing how it coordinates tasks among modules.
- Finally, indicate an “External Ethernet Network” block that connects to the W5500 via TCP/IP.

Remember to elaborate on each layer so that the principal software engineer can quickly understand the separation of concerns and interactions between hardware and software components.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Hardware Layer"
        MC["STM32F103C8 Microcontroller"]:::hardware
        SPI["SPI Bus Interface"]:::interface
        W5500["W5500 Ethernet Controller"]:::hardware
        MC -->|"SPI"| SPI
        SPI -->|"SPI"| W5500
    end

    subgraph "Driver Layer"
        HAL["HAL Drivers"]:::driver
        CMSIS["CMSIS Core"]:::driver
        MC --> HAL
        HAL --> CMSIS
    end

    subgraph "Middleware Layer"
        RTOS["RTOS/Middleware Components"]:::middleware
        CMSIS --> RTOS
    end

    subgraph "Application Layer"
        APP["Modbus TCP Application Logic"]:::application
        HAL --> APP
        CMSIS --> APP
        RTOS --> APP
        APP -->|"ModbusTCP"| W5500
    end

    subgraph "External Network"
        NET["External Ethernet Network"]:::external
        W5500 -->|"TCP/IP"| NET
    end

    %% Click Events
    click MC "https://github.com/tangtangsix/modbus_tcp/tree/main/Drivers/CMSIS/Device/ST/STM32F1xx"
    click W5500 "https://github.com/tangtangsix/modbus_tcp/tree/main/W5500/"
    click HAL "https://github.com/tangtangsix/modbus_tcp/tree/main/Drivers/STM32F1xx_HAL_Driver"
    click CMSIS "https://github.com/tangtangsix/modbus_tcp/tree/main/Drivers/CMSIS"
    click SPI "https://github.com/tangtangsix/modbus_tcp/blob/main/Inc/spi.h"
    click SPI "https://github.com/tangtangsix/modbus_tcp/blob/main/Src/spi.c"
    click APP "https://github.com/tangtangsix/modbus_tcp/tree/main/Src/"
    click APP "https://github.com/tangtangsix/modbus_tcp/tree/main/MDK-ARM/Modbus_TCP"
    click RTOS "https://github.com/tangtangsix/modbus_tcp/tree/main/Drivers/CMSIS/RTOS2"

    %% Styles
    classDef hardware fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef driver fill:#66ccff,stroke:#333,stroke-width:2px;
    classDef middleware fill:#ffa500,stroke:#333,stroke-width:2px;
    classDef application fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef interface fill:#ccccff,stroke:#333,stroke-width:2px;
    classDef external fill:#ff9999,stroke:#333,stroke-width:2px;
```
