# LCLS-II LLRF Firmware

The LCLS-II LLRF system has its functions divided in 5 separate chassis, each containing a BMB7 board where the application firmware is build on a Kintex-7 (XC7K160T-2FFG676C) FPGA. The firmware builds for each one of these components can be found in the correspondent sub-directories:

- RF Station (rfs),
- Precision Receiver Chassis (prc),
- Resonance Control (resonance_control),
- Interlocks (interlock).

*Note*: Currently the RFS and PRC firmware are combined into a single build (in the `prc/` directory) and will eventually be split into separate builds.


## Firmware structure

In each sub-directory here you can find the firmware specific for each chassis. The top level module takes the name of each system (e.g. `prc.v` for the PRC in prc/), which in turn instantiates the top level module for each particular application (this module shares a common name across directories, `application_top.v`). Other common modules (with different implementations depending on the application) include `breakout.v` (module for the LPC FMC connector) and `breakout_hpc.v` (module for the HPC FMC connector).

## Programming

In the `bmb7/` directory and in the BMB7 documentation site (which can be found on [Dropbox](https://www.dropbox.com/sh/rbfcr47um3ty2t0/AADZCUw9kgjIqbv6FQa-_Hara?dl=0)) there is a script to program the Kintex-7 directly. This will allow or testing, but will not persist across power cycles. The default IP for the target is `192.168.1.127`. The `bmb7/` directory also includes other Python support code that is used across the different chassis.

Programming requires using the correct programming script, knowing the device IP address, and knowing the path to the correct bit file. A sample programming command looks like the command below, using the tools from the BMB7 sites:

	$ program_Xilinx_kintex7_direct.py 192.168.1.127 <project_name>.bit

More information on the programming process along with other features can be found in the BMB7 user guide in the Dropbox location indicated above.


## Configuration ROM

All the firmware builds have a `config_romx.v` file. This is an automatically generated ROM block with a standardize addressing which can be accessed from a host computer. It stores information like the Git commit ID of the firmware build, SHA-1 sum of the JSON file used to generate the record and then a compressed JSON register map. This is useful to identify the firmware version and build-related information in a live FPGA.

## Fiber connectivity

The 5 chassis are interconnected in one LLRF rack via fiber. We have 2 different setups at the moment to connect these modules. A reduced version for testing at the prototype cryomodule (pCM) test-stands and a complete version for production, both shown below. These are some common points to both diagrams:

- MTP connectors on the front connected directly to the BMB7s using MTP-MTP patch cables,
- LC pairs in the back connecting to external systems (subset in CMTx and full connectivity in production),
- Each QSFP has four bi-directional links, labeled `0` through `3` (see section below on GTX mapping for details),
- Each pair either has a pink label, orange, or labeled as `NONE`. Pairs labeled `NONE` are not connected, orange labels are wired to the LC connectors in the back of the patch panel and links with pink labels are intra-rack links. The latter are spliced inside the patch panel.

### GTX Mapping

There are 2 QSFPs connected to the Kintex-7 (K1 and K2 on the diagrams) and one to the Spartan-6 (S on the diagrams). Physically, if you start counting from the RJ45 connector on the BMB7, the links have the following order (`RJ45`, `S`, `K1` and `K2`). In the Kintex-7 Verilog builds, `K1` corresponds to `U50` and `K2` corresponds to `U32`. There is a generate statement in the Verilog that names each bi-directional link in one QUAD 0 through 3. These connections are common between the CMTx and production topology and you can find below a table summarizing the intra-rack link assignments:

|   **FROM** |   **TO**  |
|:----------:|:---------:|
| RFS1-U50-0 | PRC-U50-0 |
| RFS1-U50-2 | RES-U50-0 |
| RFS1-U50-3 | INT-U50-0 |
| RFS2-U50-0 | PRC-U50-1 |
| RFS2-U50-2 | RES-U50-1 |
| RFS2-U50-3 | INT-U50-1 |

### Chassis connectivity (pCM test-stands, CMTx)

<img src="../doc/LCLS-II-LLRF-Fiber-Patch-Panel-CMTF.png" width="100%">


### Chassis connectivity (LCLS-II production)

<img src="../doc/LCLS-II-LLRF-Fiber-Patch-Panel-Production.png" width="100%">