How trovad/bldc_d works
Project type and purpose
• This is an embedded firmware project (“VESC firmware”) for DC/BLDC/FOC motor control. It’s neither a web app nor desktop tool, but a real-time, bare-metal/microcontroller application built on ChibiOS RTOS.
Top-level architectural components
a. Build system
• Makefiles & scripts (Makefile, make/*.mk, package_firmware.py, Qt Creator project)
• Configuration generator (confgenerator.c, conf_general.h, hwconf/)
• External tools (ARM SDK installer, Python scripts, Qt Creator integration)
b. Bootloader & debugger support
• Blackmagic SWD interface (blackmagic/)
• OpenOCD config files (stm32-bv_openocd.cfg, Project/Qt Creator)
c. RTOS & HAL
• ChibiOS kernel (ChibiOS_3.0.5/os/rt/)
• Hardware abstraction (ChibiOS_3.0.5/os/hal/)
• BSP & LLD drivers (ChibiOS_3.0.5/ext, ports for STM32 families)
d. Board configuration
• Hardware configurations (hwconf/…) for dozens of VESC variants
• MCU configuration (mcuconf.h, chconf.h, halconf.h)
e. Low-level drivers
• GPIO, ADC, DAC, timers, PWM, SPI, I²C, CAN, USB, etc.
• Utility libraries (util/, motor/, encoder/, imu/)
f. Communication stack
• USB serial & CDC (comm/comm_usb.c, terminal.c)
• CAN bus driver & packet layer (comm_can.c, packet.c)
• UAVCAN library (libcanard/)
g. Motor-control algorithms
• FOC & PWM (motor/mcpwm_foc.c, motor/foc_math.c)
• Speed/position sensing (encoder/, imu/)
h. Middleware & utilities
• Logging, CRC, digital filters (util/, comm/log.c)
• Shell & CLI (os/various/shell.c)
i. Application layer
• Pre-written apps (applications/*.c) for Nunchuk, PPM, UART-comm, custom templates
• Application selection via build target
j. Host-side UI & scripting
• Qt/QML UI templates (qmlui/)
• Python packaging scripts (package_firmware.py)
• LispBM interpreter integration (lispBM/) for dynamic expansion (optional)
Relationships and data flows
• Build system assembles configuration + HAL + ChibiOS + drivers + applications → firmware binary
• Debug tools (Blackmagic, OpenOCD) flash the binary to the MCU
• On-chip at reset: bootloader → jump to main firmware
• main.c initializes ChibiOS kernel and HAL → spawns threads
• High-priority control loop thread reads ADC/encoder/IMU → runs FOC math → updates PWM
• Comm thread handles USB & CAN interrupts → packet framing → invokes command handlers
• Utility threads (logging, shell) process background tasks via queues/semaphores
• Middleware (libcanard) sits between CAN driver and application for UAVCAN messages
• Application code registers callbacks into command/middleware layers
Architectural patterns & principles
• Layered design:
1. HAL & board support
2. Drivers & utilities
3. RTOS kernel
4. Middleware & comm stacks
5. Application
• Event-driven concurrency: ChibiOS threads + ISRs + message queues
• Hardware-abstraction: single HAL API + board-specific low-level drivers
• Build-time configuration: code-generation from conf files to tailor firmware to each board