# xiaoshijourney/ydlidar_x2_tof_stm32 architecture

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

- Interactive diagram: https://gitdiagram.com/xiaoshijourney/ydlidar_x2_tof_stm32
- Repository: https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32

## Overview

This firmware fuses YDLIDAR X2 scans and two TOF ranges on an STM32F103. The main loop decodes sensor data; a 100 ms timer path encodes a framed SPI report and applies timeout handling. README documents wall-angle fitting, OLED rendering, and optional USB CDC debugging. The TOF implementation files are not present in the supplied tree, so that subsystem is represented by its enclosing directory; relationships below follow the README and sampled main.c, not an inspection of those missing sources.

Main entry points: [`Core/Src/main.c`](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/main.c) (Main loop), [`xiaoshi_func/LIDAR.c`](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/xiaoshi_func/LIDAR.c) (Scan decoding), [`xiaoshi_func`](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/tree/main/xiaoshi_func) (SPI frame encode), [`xiaoshi_func/OLED.c`](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/xiaoshi_func/OLED.c) (OLED driver)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["Sensor application"]
  node_main_loop["Main loop<br/>[main.c]"]
  node_timer_handler["100 ms handler<br/>[main.c]"]
  node_tof_decode["TOF decoding"]
  node_tof_queue["TOF buffering"]
  node_timeout_guard["Sensor timeouts"]
end

subgraph group_lidar["LIDAR processing"]
  node_lidar_decode["Scan decoding<br/>[LIDAR.c]"]
  node_wall_fit["Wall angle fit<br/>[LIDAR.c]"]
  node_oled_render["Radar rendering<br/>[LIDAR.c]"]
end

subgraph group_output["Output interfaces"]
  node_frame_encode["SPI frame encode"]
  node_spi_output["SPI transmit<br/>[main.c]"]
  node_oled_driver["OLED driver<br/>[OLED.c]"]
  node_usb_debug["USB CDC debug<br/>[usbd_cdc_if.c]"]
end

subgraph group_platform["Board peripherals"]
  node_peripheral_init["Peripheral setup<br/>[main.c]"]
  node_uart_dma["UART and DMA<br/>[usart.c]"]
  node_timer_peripheral["TIM3 setup<br/>[tim.c]"]
  node_spi_peripheral["SPI2 setup<br/>[spi.c]"]
end

node_lidar_sensor(("YDLIDAR X2"))
node_tof_sensors(("Dual TOF"))
node_spi_consumer(("SPI host"))
node_oled_screen["OLED display"]
node_usb_host(("USB host"))

node_lidar_sensor -->|"sends scan"| node_lidar_decode
node_main_loop -->|"calls decode"| node_lidar_decode
node_lidar_decode -->|"fits wall"| node_wall_fit
node_tof_sensors -->|"sends ranges"| node_tof_queue
node_tof_decode -->|"reads queue"| node_tof_queue
node_main_loop -->|"calls decode"| node_tof_decode
node_main_loop -.->|"optional draw"| node_oled_render
node_oled_render -->|"renders with"| node_oled_driver
node_oled_driver -->|"updates display"| node_oled_screen
node_timer_handler -->|"calls encode"| node_frame_encode
node_lidar_decode -->|"supplies distances"| node_frame_encode
node_wall_fit -->|"supplies yaw"| node_frame_encode
node_tof_decode -->|"supplies ranges"| node_frame_encode
node_timer_handler -->|"checks timeouts"| node_timeout_guard
node_timer_handler -->|"transmits frame"| node_spi_output
node_spi_output -->|"sends SPI frame"| node_spi_consumer
node_main_loop -.->|"optional debug"| node_usb_debug
node_usb_debug -.->|"exposes serial"| node_usb_host
node_main_loop -->|"initializes board"| node_peripheral_init
node_peripheral_init -->|"configures UART"| node_uart_dma
node_peripheral_init -->|"configures timer"| node_timer_peripheral
node_peripheral_init -->|"configures SPI"| node_spi_peripheral

click node_main_loop "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/main.c"
click node_timer_handler "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/main.c"
click node_lidar_decode "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/xiaoshi_func/LIDAR.c"
click node_wall_fit "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/xiaoshi_func/LIDAR.c"
click node_tof_decode "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/tree/main/xiaoshi_func"
click node_tof_queue "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/tree/main/xiaoshi_func"
click node_frame_encode "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/tree/main/xiaoshi_func"
click node_timeout_guard "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/tree/main/xiaoshi_func"
click node_spi_output "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/main.c"
click node_oled_render "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/xiaoshi_func/LIDAR.c"
click node_oled_driver "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/xiaoshi_func/OLED.c"
click node_usb_debug "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/USB_DEVICE/App/usbd_cdc_if.c"
click node_peripheral_init "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/main.c"
click node_uart_dma "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/usart.c"
click node_timer_peripheral "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/tim.c"
click node_spi_peripheral "https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/spi.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_main_loop,node_timer_handler,node_tof_decode,node_tof_queue,node_timeout_guard toneBlue
class node_lidar_decode,node_wall_fit,node_oled_render toneAmber
class node_frame_encode,node_spi_output,node_oled_driver,node_usb_debug toneMint
class node_peripheral_init,node_uart_dma,node_timer_peripheral,node_spi_peripheral toneRose
class node_lidar_sensor,node_tof_sensors,node_spi_consumer,node_oled_screen,node_usb_host toneIndigo
```

## Components

### Sensor application

- [Main loop](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/main.c): `Core/Src/main.c`
- [100 ms handler](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/main.c): `Core/Src/main.c`
- [TOF decoding](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/tree/main/xiaoshi_func): `xiaoshi_func`
- [TOF buffering](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/tree/main/xiaoshi_func): `xiaoshi_func`
- [Sensor timeouts](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/tree/main/xiaoshi_func): `xiaoshi_func`

### LIDAR processing

- [Scan decoding](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/xiaoshi_func/LIDAR.c): `xiaoshi_func/LIDAR.c`
- [Wall angle fit](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/xiaoshi_func/LIDAR.c): `xiaoshi_func/LIDAR.c`
- [Radar rendering](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/xiaoshi_func/LIDAR.c): `xiaoshi_func/LIDAR.c`

### Output interfaces

- [SPI frame encode](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/tree/main/xiaoshi_func): `xiaoshi_func`
- [SPI transmit](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/main.c): `Core/Src/main.c`
- [OLED driver](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/xiaoshi_func/OLED.c): `xiaoshi_func/OLED.c`
- [USB CDC debug](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/USB_DEVICE/App/usbd_cdc_if.c): `USB_DEVICE/App/usbd_cdc_if.c`

### Board peripherals

- [Peripheral setup](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/main.c): `Core/Src/main.c`
- [UART and DMA](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/usart.c): `Core/Src/usart.c`
- [TIM3 setup](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/tim.c): `Core/Src/tim.c`
- [SPI2 setup](https://github.com/xiaoshijourney/ydlidar_x2_tof_stm32/blob/main/Core/Src/spi.c): `Core/Src/spi.c`

### Other components

- **YDLIDAR X2**
- **Dual TOF**
- **SPI host**
- **OLED display**
- **USB host**

## Connections

- YDLIDAR X2 → Scan decoding: sends scan
- Main loop → Scan decoding: calls decode (evidence: `Core/Src/main.c`)
- Scan decoding → Wall angle fit: fits wall
- Dual TOF → TOF buffering: sends ranges
- TOF decoding → TOF buffering: reads queue
- Main loop → TOF decoding: calls decode (evidence: `Core/Src/main.c`)
- Main loop → Radar rendering: optional draw
- Radar rendering → OLED driver: renders with
- OLED driver → OLED display: updates display
- 100 ms handler → SPI frame encode: calls encode (evidence: `Core/Src/main.c`)
- Scan decoding → SPI frame encode: supplies distances
- Wall angle fit → SPI frame encode: supplies yaw
- TOF decoding → SPI frame encode: supplies ranges
- 100 ms handler → Sensor timeouts: checks timeouts (evidence: `Core/Src/main.c`)
- 100 ms handler → SPI transmit: transmits frame (evidence: `Core/Src/main.c`)
- SPI transmit → SPI host: sends SPI frame
- Main loop → USB CDC debug: optional debug
- USB CDC debug → USB host: exposes serial
- Main loop → Peripheral setup: initializes board (evidence: `Core/Src/main.c`)
- Peripheral setup → UART and DMA: configures UART
- Peripheral setup → TIM3 setup: configures timer
- Peripheral setup → SPI2 setup: configures SPI
