Skip to content

Latest commit

 

History

History
217 lines (177 loc) · 11.8 KB

developer_guide.md

File metadata and controls

217 lines (177 loc) · 11.8 KB

Developer's Guide

This file provides guidance to developers when working with code in this repository.

Environment & Python Virtual Environment

Always use the local virtual environment at .venv/ (Python 3.13):

# Run python commands / pytest using .venv
.venv/bin/python <script>
.venv/bin/pytest

Required system & FPGA tools:

  • Verilator (v5.038+ tested) or Icarus Verilog (v12.0 tested)
  • cocotb (v2.0.1)
  • Yosys (for synthesis & CDC checks)
  • sv2v (v0.0.12-19-g7808819 in CI; v0.0.12 and v0.0.13 give identical output): converts the SystemVerilog shells (rtl/core/llrf_shell.sv, designs/lemp/llrf_shell.sv, rtl/{core,frontend,localbus}/*.sv) into llrf_shell_expand.v for the Yosys CDC check (rtl/tests/llrf_shell_cdc). Vivado, the top-level lint, topsim and the shell testbenches read the SystemVerilog directly, from the source lists (designs/<design>/llrf_shell.f, merged into top/<design>/marble_zest_top.f)
  • Xilinx Vivado 2022.1+ (for bitstream synthesis and top_sim)
  • Submodules: LBNL submodules/bedrock (must be initialized: git submodule update --init --recursive)

Common Development Commands

1. Python & Linting

# Install package in editable mode
.venv/bin/python -m pip install -e .

# Run pytest
.venv/bin/pytest

# Code style checking (per CI)
flake8 uspas_llrf rtl/tests examples marble_bsp/bsp_regmap.py

2. LLRF DSP Verification & Cocotb Simulation

The DSP testbenches use cocotb with Verilator (default) or Icarus Verilog.

# Run the RTL unit tests (ddc, dds, sin_dds, duc, pulse_gen, loop_ctrl, rx_chain, cic_waves)
make -C rtl/tests

# Run one suite (rtl/tests/<suite>)
make -C rtl/tests/dds
make -C rtl/tests/cic_waves

# Run the teaching examples (llrf_loop: one IF channel through the loop; bpm)
make -C examples
make -C examples/llrf_loop

# Lint the shell's SystemVerilog sources with Verilator (per CI job sv_lint): any
# warning fails, width warnings included. The Verilog library is waived in
# rtl/core/lint_waivers.vlt; the cocotb TBs build with -Wno-width for its sake.
make -C designs/uspas lint

# Build standalone design package / IP artifacts (e.g., uspas, pip-ii, awa, etc.)
cd ../designs/uspas && make
cd ../designs/awa && make
# Or build all designs at once
cd ../designs && make

# Run unified llrf_shell cocotb simulation for a specific design
cd ../designs/uspas/tb/llrf_shell && make
cd ../designs/alsu/tb/llrf_shell && make
cd ../designs/awa/tb/llrf_shell && make
cd ../designs/lemp/tb/llrf_shell && make

# Enable waveform tracing (generates FST/VCD)
make WAVES=1

# Run with Icarus Verilog instead of Verilator
make SIM=icarus

# Run the shell's Clock Domain Crossing (CDC) check via Yosys for a design
make -C rtl/tests/llrf_shell_cdc DESIGN=uspas

3. Board Support Package (BSP) Test

cd marble_bsp
make

4. RISC-V SoC Simulation

# Open-source RTL simulation (Icarus Verilog)
cd soc/common/sim
make

# Full simulation with Vivado UNISIM primitives (requires Vivado in PATH)
cd soc/common/top_sim
make

5. FPGA Bitstream Synthesis

# One directory per design: top/{uspas, alsu, lemp, awa, pip-ii}, each with its own _xilinx/
# (FSET is automatically mapped from DESIGN in settings.mk: USPAS, ALSU, LEMP, AWA)
# Shared rules: top/common/top_rules.mk; SoC firmware is built in soc/<design>/build/
make -C top/uspas
make -C top/awa
# Building marble_zest.json (part of every build) checks its base addresses
# against the localbus decode in top/common/marble_zest_mid.vh (lint/check_lb_map.py)

5a. Top-Level Checks (no Vivado)

# Lint marble_zest_top with every block's source list (CI job top_lint): widths,
# missing ports and multiple drivers in the top files (marble_zest_top.sv,
# marble_zest_mid.vh, the design_*.vh hooks); the blocks are linted on their own.
# Xilinx primitives come from Yosys's cell library. A constant second driver
# (assign x = 0) is folded away by Verilator and not reported.
make -C top/uspas lint

# Verilator simulation of the top (marble_zest_frame.sv: no Zest, no Xilinx I/O),
# then its CI test (topsim_test): boots the firmware's SIMULATION build and checks
# the config ROM, every block's registers, the self-test and system_bist_pass
# over LEEP, through a localhost UDP bridge (top/common/topsim/udp_tap.c; no tap device)
make -C top/uspas topsim
make -C top/uspas topsim_test
# By hand: ./Vmarble_zest_frame in top/uspas, then leep://127.0.0.1:8030.
# TOPSIM_FW=full runs the bitstream's firmware (its Marble and Zest init wait for
# devices topsim does not model); TOPSIM_TRACE=1 builds with FST tracing (+trace),
# which needs a lot of memory; TOPSIM_NET=tap uses a tap device instead.

# CDC check of the whole top, crossings between blocks included (about 2.5 min)
make -C top/uspas topsim_cdc.txt

6. Hardware Deployment & Testing (LEEP)

cd top/uspas

# Program FPGA over OpenOCD (set MARBLE_SERIAL to the board's serial number)
make system_config MARBLE_SERIAL=61

# Test Ethernet, the git ID and the system self-test via LEEP (IP 192.168.19.<SERIAL>);
# each check retries up to SYSTEM_TEST_TIMEOUT s (60), so no wait after system_config.
# Without make: python -m uspas_llrf.tools.system_test <IP> --git-id <hash> [--console <tty>]
make system_test MARBLE_SERIAL=61

# Post-boot firmware check over the USB UART console (answers '?' with this commit's GIT_REV_ID)
make system_console_test MARBLE_SERIAL=61

# Direct LEEP CLI access
.venv/bin/leep leep://192.168.18.61:803 list
.venv/bin/leep leep://192.168.18.61:803 reg <reg_name>
.venv/bin/leep leep://192.168.18.61:803 reg <reg_name>=<val>

# SoC console on the Marble's USB UART (115200 baud, exit with Ctrl+])
make console MARBLE_SERIAL=61

SoC console and reset. The Marble's USB UART RTS line drives the SoC reset: the top level resets the SoC on its release (UART_CTS edge, top/common/marble_zest_mid.vh). By default Linux releases RTS when a program closes the port ("hang up on close"), so quitting a terminal such as pyserial-miniterm reboots the SoC. make console turns this off with stty -F <port> -hupcl before starting miniterm. To make it the default for any terminal program, install the udev rules once per host. They also let members of the plugdev group use the Marble's FTDI device (JTAG for make system_config, and the console) without root:

sudo cp doc/udev/99-marble-console.rules /etc/udev/rules.d/
sudo udevadm control --reload   # applies when the Marble's USB port next appears

Add yourself to the group with sudo usermod -aG plugdev $USER (log in again to apply). Without the rules, run stty -F /dev/serial/by-id/usb-LBNL_Marble_<6-digit serial>-if02-port0 -hupcl once after each power cycle or USB re-plug.


High-Level Architecture & Codebase Structure

This repository contains the digital Low-Level RF (LLRF) firmware/gateware suite targeting the LBNL Marble FPGA carrier (Kintex-7) and Zest digitizer (FMC board).

uspas_llrf/
├── submodules/bedrock/        # LBNL Bedrock gateware library (localbus, CORDIC, badger Ethernet, RISC-V PicoRV32)
├── uspas_llrf/                # Python package
│   ├── model/                 # DSP analytical models (llrf_dsp.py: LLRFLoop; plant.py, cavity.json) used in cocotb & Python drivers
│   ├── app/                   # High-level Python application interfaces and BSP drivers
│   ├── tools/                 # Hardware utility scripts (RF synth, EVG generator)
│   ├── tests/                 # Unified testbench classes (test_llrf_shell.py: TB_llrf_shell)
│   └── settings.json          # Pre-defined facility LLRF DSP configuration parameters
├── designs/                   # Facility designs: shell selection (SHELL_SRC), register map extensions, testbenches
│   ├── rules.mk               # Centralized make rules for all designs (register file generation, sv2v expansion, CDC checks)
│   ├── README.md              # Detailed overview of design variants and frequency configuration matrix
│   ├── uspas/                 # USPAS design: Makefile, testbench, README
│   ├── lemp/                  # design.py: 1k signal buffers and the iq_buf window
│   └── alsu/, awa/, pip-ii/   # Designs on the base shell (the default SHELL_SRC)
├── rtl/                       # Portable RTL (plan 14d; what uspas_llrf_core ships, step 19)
│   ├── localbus/              # Localbus RTL shared by the shell, the BSP and the SoC: request types (localbus_pkg.sv), clock-domain copies (lb_xdomain.sv), readback mux (lb_rd_mux.sv)
│   ├── dsp/                   # Verilog DSP primitives: dds, ddc/noniq_ddc, dac_duc, interpolator, pi_scalar, dsp_core (baseband PI feedback), pulse_gen, cic_timing, sig_buf, slow_bridge_shell
│   ├── core/                  # The base SystemVerilog shell llrf_shell.sv, its packages and blocks: loops/loop_ctrl, diag/cic_waves, monitor_inlk, arc_inlk
│   ├── frontend/              # Baseband in and out of the core: rx_chain, tx_chain (Zest IF)
│   └── tests/                 # Cocotb unit tests, one directory per unit; llrf_shell/ (the base shell's TB wrapper), llrf_shell_cdc/ (CDC skin)
├── examples/                  # Teaching examples (USPAS): llrf_loop/ (one IF channel through the loop), bpm/
├── marble_bsp/                # Marble FPGA board support package (Local Bus, Packet Badger UDP/Ethernet, EVR timing, MMC)
├── soc/
│   ├── common/                # PicoRV32 RISC-V soft-core SoC (system.sv, firmware, sim/, top_sim/, synth/)
│   └── <design>/              # Per-design firmware: design.mk, settings_design.h, init_zest.c, init_marble.c
├── top/
│   ├── common/                # Top-level FPGA integration (`marble_zest_top.sv`, `marble_zest_mid.vh`), top_rules.mk; by task: synth/ (Vivado TCL, constraints, CDC waivers), lint/, topsim/, hw/ (programming)
│   └── <design>/              # Per-design build directory (Makefile; optional design_mid.vh, design.xdc)
├── epics/                     # EPICS Display Builder UI screens (BOB files)
└── doc/                       # System documentation and Jupyter demonstration notebooks

Key Architectural Concepts:

  1. IQ Convention: Signals use positive carrier frequency: $y(t) = \Re((I + jQ) e^{j\omega t}) = I\cos(\omega t) - Q\sin(\omega t)$.
  2. Clocking Structure:
    • $f_{\text{dac_clk}} = 2 f_{\text{adc_clk}} = 2 f_{\text{dsp_clk}}$
    • The reference clock feeds a clock distributor (LMK01801) driving ADCs (AD9653) and DACs (AD9781).
  3. Control Interface: Registers and circular buffers are memory-mapped through LBNL Local Bus and exposed over Ethernet UDP using LBNL Packet Badger and the leep Python library/CLI.
  4. Facility Configurations & Entry Key:
    • The top-level entry key for synthesis and build configuration is DESIGN (uspas, alsu, lemp, awa, pip-ii).
    • DESIGN automatically maps to FSET (USPAS, ALSU, LEMP, AWA) in settings.mk and uspas_llrf/settings.json.
  5. LLRF Shell:
    • Every design but LEMP uses the base SystemVerilog shell rtl/core/llrf_shell.sv (the default SHELL_SRC in designs/rules.mk; LEMP sets its own in its Makefile): shared blocks in rtl/core/ and rtl/frontend/ (rx_chain, diag, loop_ctrl with the amplitude and phase pulse modulation tables, tx_chain) and the register file generated from uspas_llrf/regmap.py (a design's design.py may extend it; LEMP sets 1k signal buffers).
    • Unified cocotb testbench uspas_llrf.tests.test_llrf_shell.TB_llrf_shell verifies all designs in designs/<design>/tb/llrf_shell/, including the shell-level pulse modulation test.
    • Roadmap (doc/llrf_shell_unification_plan.md): thin ALSU and LEMP shells over the same blocks.
  6. Analog Frontend TX Spectral Flip: Supports tx_afe_spectral_flip XORed with duc_spectral_flip for hardware RF mixing polarity compensation.