# Klystron Application

## !!!WARNING!!!
Do **NOT** attempt to run this release on sioc-li28-ky02!  
Version Klystron-R1-6-0 contains a redesign to support the latest firmware, and is incompatible with the hardware at K-28-2.
sioc-li28-ky02 development is currently frozen, and the station will remain on Klystron-R1-5-7 until new hardware is available.

## Environment

This application requires EPICS 3.15 64-bit, you must build on a rhel6-x86_64 machine such as lcls-dev3.  
For development, run `source /afs/slac/g/lcls/epics/setup/go_epics_3.15.5-1.0.bash` **before** checkout to source the proper environment.  
To checkout, run `eco` then enter "Klystron" and the desired version at the prompts.

## Development setup

In the LCLS Development environment, there are currently two test ATCA crates being used by LLRF, one in B34 and one in B84.
Both crates are capable of housing multiple instances of LLRF hardware, for testing multiple IOCs.

At present, the following IOCs have been setup with corresponding hardware:

| IOC Name       | CPU           | ATCA Crate     | Slot # | Slot IP    | Revision  | PV Prefix    |
| -------------- | ------------- | -------------- | ------ | ---------- | --------- | ------------ |
| sioc-b34-ky01  | cpu-b34-sp02  | crat-b34-sp02  | 3      | 10.0.1.103 | Latest    | KLYS:LI24:71 |
| sioc-b34-ky02  | cpu-b34-sp02  | crat-b34-sp02  | 2      | 10.0.1.102 | Prototype | KLYS:LI28:91 |
| sioc-b084-ky03 | cpu-b084-sp01 | crat-b084-sp02 | 5      | 10.0.2.105 | Latest    | KLYS:LI28:21 |

(The corresponding shelf manager for an ATCA crate can be found by replacing "crat-" with "shm-")

Each IOC has been given a different PV prefix to prevent naming conflicts with other IOCs and each other.  
In general, you can see the appropriate screens by running `edm -eolc -x -m "DEVICE=<PV Prefix>" Klystron_debugging.edl` from inside the application's edm directory.  
For convenience, there a several scripts located in the edm directory of the "NewFirmware" branch:
- launch_gui.sh: View sioc-b084-ky03 general debug screens
- launch_edm.sh: View sioc-b084-ky03 timing debug screens
- launch_b34_gui.sh: View sioc-b34-ky01 general debug screens
- launch_b34_timing.sh: View sioc-b34-ky01 timing debug screens

**Protoype** hardware has been used to develop this application up to version Klystron-R1-5-7.
From now on, development effort is focused on the **Latest** hardware revision.
Work on the new revision can be found in the "NewFirmware" branch of this application
(To checkout this branch, run `git checkout NewFirmware` from inside your application directory).

## Production Setup

There is currenlty one Klystron station (K-28-2) running in LCLS Production that uses this application:

| IOC Name       | CPU           | ATCA Crate     | Slot # | Slot IP    | Revision  | PV Prefix    |
| -------------- | ------------- | -------------- | ------ | ---------- | --------- | ------------ |
| sioc-li28-ky02 | cpu-li28-sp02 | crat-li28-sp02 | 2      | 10.0.1.102 | Prototype | KLYS:LI28:21 |

The next installation will put the **Latest** hardware revision into Sector 26, starting with K-26-3.

## Firmware configuration

There are two components for the firmware configuration of this application: the hierarchy and the default configuration.

Inside the firmware directory, there are two subdirectories:
- The "yaml" directory contains all the hierarchy register mappings for the firmware
- The "config" directory contains the default register values

Note that all IOCs share the same files in the "yaml" directory, but each IOC expects its own <IOC Name>.yaml file in the config directory.  
Edit the <IOC Name>.yaml file to adjust the initial values of firmware registers.

## Running and connecting to an IOC

To connect to an already running IOC, use `iocConsole <IOC Name>` if it has been configured in screeniocs.  
You can also manually connect to the screen session by ssh'ing to the CPU and running `screen -r <IOC Name>`

To start or restart an IOC, use the command `viocRestart -u laci -c <CPU Name> -v <IOC Name>`
(The `-u` and `-c` arguments are optional if the IOC has been configured in screeniocs).

If viocRestart fails, you must kill and restart the IOC manually:  
```
ssh laci@<CPU Name>
screen -r <IOC Name>
exit
linuxRT_startup_vioc.sh <IOC Name>
```

