VESC Firmware Documentation

repository·master·Indexed 25 days ago

https://github.com/vedderb/bldc

Open-source firmware for DC, BLDC, and FOC motor controllers used in robotics and electric vehicles. Includes documentation for Black Magic Probe SWD programming and debugging for NRF and STM32 series, custom hardware adaptation based on VESC6 architecture, and detailed driver integration for the Bosch Sensortec BMI160 sensor and BMM150 auxiliary magnetometer.

Tokens
67.5K
Snippets
243
Records
475
Agent score
85%

What's inside VESC

  1. Overview of LispBM for VESC

    master

    LispBM is a VESC integration of the lispBM language that allows the VESC to run Lisp programs in a sandboxed environment. This sandboxing is designed to prevent Lisp code from freezing or crashing the core VESC firmware, even in cases of infinite loops or memory exhaustion (heap/stack).

    Key features include:

    • Live Development: Use VESC Tool for live variable monitoring, plotting, and CPU/memory monitoring.
    • REPL: A Read-Eval-Print Loop in VESC Tool allows for live code execution and testing with access to the program's current functions and bindings.
    • Standalone Execution: Applications are stored in flash memory and run directly on the VESC at boot without requiring a connection to VESC Tool.
    • Automatic Startup: Once written to the VESC, Lisp applications start automatically on every boot.
  2. Overview of lispBM (LBM)

    master

    lispBM (LBM) is a Lisp or Scheme-like programming language designed specifically for microcontrollers. It incorporates concurrency, message passing, pattern matching, and process monitoring concepts inspired by Erlang.

    Key features:

    • Embeddable: Designed as a sandboxed scripting language to be integrated into larger applications.
    • Cross-platform: The runtime can be compiled for 32-bit or 64-bit platforms, supporting hardware like STM32, NRF52, ESP32, or X86.
    • Flexible OS Support: Can run on ChibiOS, FreeRTOS, ZephyrOS, bare-metal, or standard Linux.
  3. Identify imperative operations in LispBM

    master

    LispBM is a multiparadigm language. While it supports functional programming, it provides several core imperative operations for destructive updates and sequencing. Use these when you need to modify state directly, but be aware that they can break data persistence properties.

    Core imperative operations:

    • set: Destructively update a binding (similar to C's =).
    • setq: Destructively update a binding (similar to C's =).
    • setix: Destructive update of an element in a list.
    • setcar: Destructive update of the car field in a cons cell.
    • sercdr: Destructive update of the cdr field in a cons cell.
    • setassoc: Destructive update of a field in an association list.
    • bufset: Destructively updates ByteArrays (part of the bufset family).
    • bufclear: Destructive clear of a ByteArray.
    • progn: Executes a sequence of operations.
    • define: Defines a variable. Note that a second define of the same variable name acts as a destructive update.
  4. Use Black Magic Probe for SWD programming and debugging

    master

    The VESC firmware includes integration for the Black Magic Probe, allowing the VESC to function as an SWD programmer and limited debugger.

    Supported targets include:

    • NRF51 series
    • NRF52 series
    • STM32F0, STM32F1, STM32F2, STM32F3, STM32F4, STM32F7
    • STM32L0, STM32L1, STM32L4

    While some targets may not be available in the VESC Tool GUI, you can use the VESC terminal to perform operations on unsupported targets, such as reading/manipulating option bytes or reading the device ID.

  5. LispBM Image Format Overview

    master

    The LispBM image format is a binary serialization format used to store runtime state in persistent storage like flash memory. It is designed for embedded systems with the following characteristics:

    • Endianness: Images are stored in little-endian format.
    • Writing Direction: Fields are written top-down (from high addresses to low addresses).
    • Architecture Support: Supports both 32-bit and 64-bit architectures.
    • Incremental Builds: Images can be built incrementally by appending new fields.
    • Relocatability: The same image can be loaded at different memory addresses.
    • Sharing Recovery: Includes mechanisms to handle complex data structures via sharing tables.
  6. Understand ChibiOS/RT directory structure

    master

    The ChibiOS/RT repository is organized into several key directories:

    • demos/: Contains demo projects, with one directory per supported platform.
    • docs/: Contains documentation build resources, including Doxygen project files and local HTML documentation for HAL, NIL, and RT components.
    • ext/: External libraries that are not part of the core ChibiOS/RT.
    • os/: The core ChibiOS components:
      • hal/: Hardware Abstraction Layer, including board support files, OSAL implementations, and driver templates.
      • nil/: NIL RTOS component.
      • rt/: RT RTOS component.
      • various/: Various portable support files.
    • test/: Kernel test suite source code, including test engines and specific suites for HAL, NIL, and RT.
    • testhal/: HAL integration test demos.
  7. Understand ChibiOS products directory organization

    master

    The ChibiOS products directory is organized into several core products and shared components. If you are working with the ChibiOS source tree, you will find the following structure:

    • os/rt/: ChibiOS/RT product (Real-Time kernel). Contains include/ (headers), src/ (sources), templates/ (port templates), ports/ (port files), and osal/ (OSAL module for HAL interface).
    • os/nil/: ChibiOS/NIL product (Non-Interrupt Library). Contains include/, src/, templates/, ports/, and osal/.
    • os/hal/: ChibiOS/HAL product (Hardware Abstraction Layer). Contains include/ (high level headers), src/ (high level sources), templates/ (port templates), ports/ (low level driver implementations), and boards/ (board files).
    • os/common/: Shared files used by multiple ChibiOS products, including ports for various architectures and compilers.
    • os/various/: Portable support files.
    • os/ext/: Vendor files used by ChibiOS products.
  8. Understand LispBM S-Expressions and Atoms

    master

    LispBM programs are composed of S-expressions, which are tree structures built from two types of elements:

    1. Atoms: Basic units that cannot be further evaluated. These include:
      • Numbers: e.g., 1, 3.14, 65b, 2u32
      • Strings: e.g., "hello world"
      • Byte Arrays: e.g., [1 2 3 4 5]
      • Symbols: e.g., a, lambda, define
    2. Pairs: Created using cons. A pair (a . b) is created via (cons a b).

    Lists are a convention of right-leaning pairs ending in the symbol nil. A list (e0 e1 ... eN) is shorthand for (e0 . (e1 . ... (eN . nil))).

  9. Perform gyroscope bias correction

    master
    The gyroscope bias correction algorithm provides run-time calibration of the gyroscope bias. The algorithm automatically detects when the gyroscope has remained stationary for a specified period and then calculates the bias by averaging sampled gyroscope measurements.
  10. Initialize the BMI160 sensor (SPI or I2C)

    master

    Initialize the sensor by creating an instance of struct bmi160_dev and filling in the interface parameters. Call bmi160_init(&sensor) to complete initialization. After initialization, accel_cfg and gyro_cfg are set to default values.

    SPI 4-Wire Interface

    Set sensor.interface to BMI160_SPI_INTF and provide user-defined functions for read, write, and delay_ms.

    I2C Interface

    Set sensor.interface to BMI160_I2C_INTF and set sensor.id to BMI160_I2C_ADDR.

    /* Example for SPI 4-Wire */
    struct bmi160_dev sensor;
    
    sensor.id = 0;
    sensor.interface = BMI160_SPI_INTF;
    sensor.read = user_spi_read;
    sensor.write = user_spi_write;
    sensor.delay_ms = user_delay_ms;
    
    int8_t rslt = bmi160_init(&sensor);
    
    /* Example for I2C */
    struct bmi160_dev sensor;
    
    sensor.id = BMI160_I2C_ADDR;
    sensor.interface = BMI160_I2C_INTF;
    sensor.read = user_i2c_read;
    sensor.write = user_i2c_write;
    sensor.delay_ms = user_delay_ms;
    
    int8_t rslt = bmi160_init(&sensor);
  11. Resolve import paths in LispBM

    master

    Import paths can be absolute or relative:

    • Absolute paths: Looked up from the root of the file system.
    • Relative paths:
      • If the Lisp file is saved, paths are relative to the file's location.
      • If not found there, they are relative to the location where VESC Tool was started.
      • If the file is unsaved (new tab), only paths relative to VESC Tool are looked up.