#==================================
#----------------------------------
# prc_prl.template:
#
# Phase Reference Line support
# for a single PRC chassis
#
# PREF - rack or chassis name
#        LCLS2 example: ACCL:L1B:0200:RACKA
# NAME - FEED device name
#        LCLS2 example: prca
#----------------------------------
#==================================

record(longin, "$(CM)00:RACKA:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(PRCA=prca)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
  field(FLNK, "$(CM)00:RACKA:PRL_ENABLE")
}

# Indicates which PRL inputs are used
# EPICS owns this; lcls2_llrf phs_avg.py
# reads this PV
record(mbbi, "$(CM)00:RACKA:PRL_MODE") {
  field(VAL,  "$(PRLMODE)")
  field(ZRVL, "0")
  field(ZRST, "Fwd/Rev")
  field(ONVL, "1")
  field(ONST, "Fwd Only")
  field(TWVL, "2")
  field(TWST, "Rev Only")
  field(THVL, "3")
  field(THST, "None")
  field(PINI, "YES")
  field(FLNK, "$(CM)00:RACKA:PRL_MODE_STAT")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKA:PRL_MODEDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCA=prca) reg=mbox_PRL_MODE")
  info(feed:info:input, "$(CM)00:RACKA:PRL_MODE")
  info(feed:info:output, "$(CM)00:RACKA:PRL_MODE")
}

# Expected mode setting for this location
# Value uses same macro as PRL_MODE
# Used to raise alarm if it does not match
# the actual mode
# ('CON' from 'config' - LCLS convention)
record(mbbi, "$(CM)00:RACKA:PRL_MODE_CON") {
  field(VAL,  "$(PRLMODE)")
  field(ZRVL, "0")
  field(ZRST, "Fwd/Rev")
  field(ONVL, "1")
  field(ONST, "Fwd Only")
  field(TWVL, "2")
  field(TWST, "Rev Only")
  field(THVL, "3")
  field(THST, "None")
  field(PINI, "YES")
  field(FLNK, "$(CM)00:RACKA:PRL_MODE_STAT")
}

# Default is for a mismatch to be MINOR_ALARM
record(calc, "$(CM)00:RACKA:PRL_MODE_STAT") {
  field(DESC, "PRL mode matches expected")
  field(CALC, "A=B?1:0")
  field(INPA, "$(CM)00:RACKA:PRL_MODE")
  field(INPB, "$(CM)00:RACKA:PRL_MODE_CON")
  field(LOLO, "0")
  field(HIHI, "1")
  field(LLSV, "1")
}

# Nominal PRL forward/reverse offset
# Set by expert, once or rarely
# Also called unique phase offset
record(longin, "$(CM)00:RACKA:PRL_OFFSET_NOM") {
  field(DESC, "Nominal PRL fwd-rev phase offset")
  field(SCAN, "Passive")
  field(VAL,  "0")
  field(EGU,  "degrees")
  field(LOPR, "-180")
  field(HOPR, "180")
  field(PINI, "YES")
  field(FLNK, "$(CM)00:RACKA:PRL_OFFSET_NOM_TS1_")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKA:PRL_OFFSET_NOMDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCA=prca) reg=mbox_PRL_OFFSET_NOM")
  info(feed:info:input, "$(CM)00:RACKA:PRL_OFFSET_NOM")
  info(feed:info:output, "$(CM)00:RACKA:PRL_OFFSET_NOM")
}

# Records to update timestamp only when nominal
# offset is changed (and not over-written at IOC startup)
record(calcout, "$(CM)00:RACKA:PRL_OFFSET_NOM_TS1_") {
  field(DESC, "PRL nominal offset change")
  field(CALC, "A")
  field(INPA, "$(CM)00:RACKA:PRL_OFFSET_NOM")
  field(OOPT, "On Change")
  field(OUT, "$(CM)00:RACKA:PRL_OFFSET_NOM_TS2_ PP")
  info(autosaveFields, "A VAL")
}

record(stringin, "$(CM)00:RACKA:PRL_OFFSET_NOM_TS2_") {
  field(DESC, "PRL nominal offset change")
  field(DTYP, "Soft Timestamp")
  field(INP,  "@%m/%d/%Y %H:%M:%S")
  field(FLNK, "$(CM)00:RACKA:PRL_OFFSET_NOM_TS3_")
}

record(stringout, "$(CM)00:RACKA:PRL_OFFSET_NOM_TS3_") {
  field(DESC, "PRL nominal offset change")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM)00:RACKA:PRL_OFFSET_NOM_TS2_")
  field(OUT,  "$(CM)00:RACKA:PRL_OFFSET_NOM_TS PP")
  field(FLNK, "$(CM)00:RACKA:PRL_OFFSET_RAW")
}

record(stringout, "$(CM)00:RACKA:PRL_OFFSET_NOM_TS") {
  field(DESC, "PRL nominal offset last changed")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# In unscaled units (0-1 range)
# Default value from phs_avg.py
record(ai, "$(CM)00:RACKA:PRL_AMPL_THRESH") {
  field(DESC, "Minimum ampl to calc offset")
  field(VAL,  ".05")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(aSub, "$(CM)00:RACKA:PRL_OFFSET_CALC") {
  field(SNAM, "Calculate PRL Offset")
# If subroutine returns -1, set to INVALID
# Downstream records inherit this severity
  field(BRSV, "INVALID")
# PRLREV updates last in the set, so
# should have synchronous data
  field(INPA, "$(CM)00:RACKA:PRLFWD:AMEAN MSI NPP")
  field(INPB, "$(CM)00:RACKA:PRLREV:AMEAN MSI NPP")
  field(INPC, "$(CM)00:RACKA:PRLFWD:PMEAN MSI NPP")
  field(INPD, "$(CM)00:RACKA:PRLREV:PMEAN MSI CP")
  field(FTE,  "SHORT")
  field(INPE, "$(CM)00:RACKA:PRL_MODE MSI")
  field(INPF, "$(CM)00:RACKA:PRL_OFFSET_NOM MSI")
  field(INPG, "$(CM)00:RACKA:PRLFWD:SCALE MSI")
  field(INPH, "$(CM)00:RACKA:PRLREV:SCALE MSI")
  field(INPI, "$(CM)00:RACKA:PRL_AMPL_THRESH")
  field(NOVA, "1") # Phase offset [degrees]
  field(OUTA, "$(CM)00:RACKA:PRL_OFFSET_RAW PP MS")
  field(FTVB, "SHORT")
  field(OUTB, "$(CM)00:RACKA:PRL_OFFSET_STAT PP MS")
  field(FTVC, "STRING")
  field(NOVC, "1")
  field(OUTC, "$(CM)00:RACKA:PRL_OFFSET_MSG PP MS")
  field(FTVD, "STRING")
  field(NOVD, "1")
  field(OUTD, "$(CM)00:RACKA:PRL_OFFSET_TS PP MS")
  field(TSEL, "$(CM)00:RACKA:PRLREV:AWF.TIME CA")
}

record(mbbi, "$(CM)00:RACKA:PRL_OFFSET_STAT") {
  field(VAL,  "0")
  field(ZRVL, "0")
  field(ZRST, "Good")
  field(ONVL, "1")
  field(ONST, "Warning")
  field(ONSV, "MINOR")
  field(TWVL, "2")
  field(TWST, "Error")
  field(TWSV, "MAJOR")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(stringin, "$(CM)00:RACKA:PRL_OFFSET_MSG") {
  field(VAL,  "")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# Timestamp last successfully calculated OFFSET
record(stringin, "$(CM)00:RACKA:PRL_OFFSET_TS") {
  field(VAL,  "")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# PRL forward/reverse offset
# Calculated from waveforms
# LD prefers integer for PRL tracking alarms/status
# Non-integer for users/archiving
record(ai, "$(CM)00:RACKA:PRL_OFFSET_RAW") {
  field(DESC, "PRL fwd-rev phase offset")
  field(SCAN, "Passive")
  field(VAL,  "0")
  field(EGU,  "degrees")
  field(LOPR, "-180")
  field(HOPR, "180")
  field(FLNK, "$(CM)00:RACKA:PRL_OFFSET")
  field(TSEL, "$(CM)00:RACKA:PRLREV:AWF.TIME CA")
  info(autosaveFields, "VAL")
}

record(longin, "$(CM)00:RACKA:PRL_OFFSET") {
  field(DESC, "PRL fwd-rev phase offset")
  field(SCAN, "Passive")
  field(VAL,  "0")
  field(EGU,  "degrees")
  field(LOPR, "-180")
  field(HOPR, "180")
  field(INP,  "$(CM)00:RACKA:PRL_OFFSET_RAW MS")
  field(FLNK, "$(CM)00:RACKA:PRL_OFFSET_DIFF")
  field(TSEL, "$(CM)00:RACKA:PRLREV:AWF.TIME CA")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKA:PRL_OFFSETDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCA=prca) reg=mbox_PRL_OFFSET")
  info(feed:info:input, "$(CM)00:RACKA:PRL_OFFSET")
  info(feed:info:output, "$(CM)00:RACKA:PRL_OFFSET")
}

record(calc, "$(CM)00:RACKA:PRL_OFFSET_DIFF") {
  field(DESC, "PRL offset - nominal")
  field(CALC, "A-B")
  field(INPA, "$(CM)00:RACKA:PRL_OFFSET")
  field(INPB, "$(CM)00:RACKA:PRL_OFFSET_NOM")
  field(LLSV, "MAJOR")
  field(LSV,  "MINOR")
  field(HSV,  "MINOR")
  field(HHSV, "MAJOR")
  field(LOLO, "-10")
  field(LOW,  "-9")
  field(HIGH, "9")
  field(HIHI, "10")
  field(PREC, "0")
  field(EGU,  "degrees")
  field(FLNK, "$(CM)00:RACKA:PRL_OFFSET_DIAGSUB")
}

record(aSub, "$(CM)00:RACKA:PRL_OFFSET_DIAGSUB") {
  field(DESC, "Graphical diagnostic")
  field(SNAM, "PRL Offset Diag")
# Inputs
  field(FTA, "DOUBLE") # PRL offset current
  field(INPA, "$(CM)00:RACKA:PRL_OFFSET")
  field(FTB, "DOUBLE") # PRL offset nominal
  field(INPB, "$(CM)00:RACKA:PRL_OFFSET_NOM")
  field(FTC, "DOUBLE") # PRL offset last rack checkout
  field(INPC, "$(CM)00:RACKA:PRL_OFFSET_INIT")
  field(FTD, "DOUBLE") # Fwd PRL phase
  field(INPD, "$(CM)00:RACKA:PRLFWD:PMEAN")
  field(FTE, "DOUBLE") # Rev PRL phase
  field(INPE, "$(CM)00:RACKA:PRLREV:PMEAN")
# Outputs
  field(NOVA, "2") # PRL offset current X
  field(NOVB, "2") # PRL offset current Y
  field(NOVC, "2") # PRL offset nominal X
  field(NOVD, "2") # PRL offset nominal Y
  field(NOVE, "2") # PRL offset last rack checkout X
  field(NOVF, "2") # PRL offset last rack checkout Y
  field(NOVG, "2") # Fwd PRL phase X
  field(NOVH, "2") # Fwd PRL phase Y
  field(NOVI, "2") # Rev PRL phase X
  field(NOVJ, "2") # Rev PRL phase Y
  field(NOVK, "2") # Zero, two elements
  field(NOVL, "2") # Graph x axis
  field(NOVM, "2") # Graph y axis
  field(NOVN, "200") # Unit circle x
  field(NOVO, "200") # Unit circle y
}

record(aSub, "$(CM)00:RACKA:PRL_OFFSET_DIAGSUB") {
  field(DESC, "Graphical diagnostic")
  field(SCAN, "1 second")
  field(SNAM, "PRL Offset Diag")
# Inputs
  field(FTA, "DOUBLE") # PRL offset current
  field(INPA, "$(CM)00:RACKA:PRL_OFFSET")
  field(FTB, "DOUBLE") # PRL offset nominal
  field(INPB, "$(CM)00:RACKA:PRL_OFFSET_NOM")
  field(FTC, "DOUBLE") # PRL offset last rack checkout
  field(INPC, "$(CM)00:RACKA:PRL_OFFSET_INIT")
  field(FTD, "DOUBLE") # Fwd PRL phase
  field(INPD, "$(CM)00:RACKA:PRLFWD:PMEAN")
  field(FTE, "DOUBLE") # Rev PRL phase
  field(INPE, "$(CM)00:RACKA:PRLREV:PMEAN")
  field(FTF, "DOUBLE") # Tracking zero phase
  field(INPF, "$(CM)00:RACKA:PRL_ZERO")
  field(FTG, "DOUBLE") # Zero phase - Offset (LO?)
  field(INPG, "$(CM)00:RACKA:PRL_ZERO_OFFSET_DIFF")
# Outputs
  field(NOVA, "2") # PRL offset current X
  field(NOVB, "2") # PRL offset current Y
  field(NOVC, "2") # PRL offset nominal X
  field(NOVD, "2") # PRL offset nominal Y
  field(NOVE, "2") # PRL offset last rack checkout X
  field(NOVF, "2") # PRL offset last rack checkout Y
  field(NOVG, "2") # Fwd PRL phase X
  field(NOVH, "2") # Fwd PRL phase Y
  field(NOVI, "2") # Rev PRL phase X
  field(NOVJ, "2") # Rev PRL phase Y
  field(NOVK, "2") # Zero, two elements
  field(NOVL, "2") # Graph x axis
  field(NOVM, "2") # Graph y axis
  field(NOVN, "200") # Unit circle x
  field(NOVO, "200") # Unit circle y
  field(NOVP, "2") # Zero phase X
  field(NOVQ, "2") # Zero phase Y
  field(NOVR, "2") # Zero phase - offset X
  field(NOVS, "2") # Zero phase - offset Y
}

record(bo, "$(CM)00:RACKA:PRL_OFFSET_RESET") {
  field(DESC, "Set current to nominal")
  field(SCAN, "Passive")
  field(ZNAM, "Reset")
  field(ONAM, "Reset")
  field(FLNK, "$(CM)00:RACKA:PRL_OFFSET_RESET_")
}

record(seq, "$(CM)00:RACKA:PRL_OFFSET_RESET_") {
  field(DESC, "Set current to nominal")
  field(SCAN, "Passive")
  field(SELM, "All")
  field(DOL0, "$(CM)00:RACKA:PRL_OFFSET")
  field(LNK0, "$(CM)00:RACKA:PRL_OFFSET_NOM PP")
  field(DOL1, "1")
# Optional link to notify users to check phases
  field(LNK1, "$(CKPLNKA)")
}

# PRL forward/reverse offset
# Measured at hardware init
record(longin, "$(CM)00:RACKA:PRL_OFFSET_INIT") {
  field(DESC, "PRL fwd-rev phase offset meas at init")
  field(SCAN, "Passive")
  field(VAL,  "0")
  field(EGU,  "degrees")
  field(LOPR, "-180")
  field(HOPR, "180")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKA:PRL_OFFSET_INITDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCA=prca) reg=mbox_PRL_OFFSET_INIT")
  info(feed:info:input, "$(CM)00:RACKA:PRL_OFFSET_INIT")
  info(feed:info:output, "$(CM)00:RACKA:PRL_OFFSET_INIT")
}

# Read-only, 18-bit angle
# This will have a random offset (2*pi*n/66?) per reboot.
# From S Murthy, convert to degrees by scaling by360*0.5**19
# See rfs_prl.template for bit transmitted to RFS
record(ai, "$(CM)00:RACKA:REG_PRL_ZERO") {
  field(DESC, "Corrected phase between LO and PRL")
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(EGU,  "degrees")
  field(ASLO, "0.000686645507812")
  field(INP , "@name=$(PRCA=prca) reg=phase_zero_mon")
  field(TSE , "-2")
  field(FLNK, "$(CM)00:RACKA:PRL_ZERO")
}

# REG_PRL_ZERO was named before understanding the
# conversion to degrees. Adding PRL_ZERO on top
# because it is easier than renaming in existing systems
record(ai, "$(CM)00:RACKA:PRL_ZERO") {
  field(DESC, "Corrected phase between LO and PRL")
  field(EGU,  "degrees")
  field(INP , "$(CM)00:RACKA:REG_PRL_ZERO MSI")
  field(TSEL, "$(CM)00:RACKA:REG_PRL_ZERO.TIME")
  field(FLNK, "$(CM)00:RACKA:PRL_ZERO_OFFSET_DIFF")
}

record(calc, "$(CM)00:RACKA:PRL_ZERO_OFFSET_DIFF") {
  field(DESC, "PRL zero - offset")
  field(CALC, "A-B")
  field(INPA, "$(CM)00:RACKA:PRL_ZERO")
  field(INPB, "$(CM)00:RACKA:PRL_OFFSET_RAW")
  field(PREC, "1")
  field(EGU,  "degrees")
}

record(bo, "$(CM)00:RACKA:PRL_ENABLE") {
  field(DESC, "Enable PRL tracking")
  field(ZNAM, "Disable")
  field(ONAM, "Enable")
  field(VAL,  "0")
  field(PINI, "YES")
  field(FLNK, "$(CM)00:RACKA:PRL_ENABLE_")
  info(autosaveFields, "VAL")
}

record(calcout, "$(CM)00:RACKA:PRL_ENABLE_") {
  field(DESC, "Enable PRL tracking")
  field(SCAN, "Passive")
  field(CALC, "C?A|B:A&(!B)")
  field(INPA, "$(CM)00:RACKA:REG_PRL_CTL_RBV")
# Bit 8 aka 0x100 aka 256 is phase tracking enable
  field(INPB, "256")
  field(INPC, "$(CM)00:RACKA:PRL_ENABLE")
  field(OOPT, "Every Time")
  field(OUT, "$(CM)00:RACKA:REG_PRL_CTL PP")
}

# Set REG_PRL_CTL OMSL closed_loop to only retrieve value from PRL_ENABLE_?

# Set bit 8 (0x100) to enable phase reference tracking
# When zero, the phase_zero value above is forced to zero.
record(longout, "$(CM)00:RACKA:REG_PRL_CTL") {
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(PRCA=prca) reg=prc_dsp_prl_cfg wait=false")
  info(feed:info:output, "$(CM)00:RACKA:REG_PRL_CTL")
}

record(longin, "$(CM)00:RACKA:REG_PRL_CTL_RBV") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(PRCA=prca) reg=prc_dsp_prl_cfg")
  field(TSE , "-2")
  field(FLNK, "$(CM)00:RACKA:PRL_ENABLE_RBV_")
  info(feed:info:input, "$(CM)00:RACKA:REG_PRL_CTL_RBV")
}

record(calc, "$(CM)00:RACKA:PRL_ENABLE_RBV_") {
  field(DESC, "PRL tracking enabled")
  field(SCAN, "Passive")
  field(CALC, "(A&B)?1:0")
  field(INPA, "$(CM)00:RACKA:REG_PRL_CTL_RBV")
# Bit 8 aka 0x100 aka 256 is phase tracking enable
  field(INPB, "256")
  field(FLNK, "$(CM)00:RACKA:PRL_ENABLE_RBV")
}

record(bi, "$(CM)00:RACKA:PRL_ENABLE_RBV") {
  field(DESC, "PRL tracking enabled")
  field(INP,  "$(CM)00:RACKA:PRL_ENABLE_RBV_")
  field(ZNAM, "Not enabled")
  field(ZSV,  "MAJOR")
  field(ONAM, "Enabled")
}

# Gain registers 16-bit signed
# (not expected to be set by humans)

record(longout, "$(CM)00:RACKA:REG_PRLFWD_GAIN_I") {
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(PRCA=prca) reg=prc_dsp_phs_avg_i_kx_0 wait=false")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:output, "$(CM)00:RACKA:REG_PRLFWD_GAIN_I")
}

record(longin, "$(CM)00:RACKA:REG_PRLFWD_GAIN_I_RBV") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(PRCA=prca) reg=prc_dsp_phs_avg_i_kx_0")
  field(TSE , "-2")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:input, "$(CM)00:RACKA:REG_PRLFWD_GAIN_I_RBV")
}

record(longout, "$(CM)00:RACKA:REG_PRLFWD_GAIN_Q") {
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(PRCA=prca) reg=prc_dsp_phs_avg_i_kx_1 wait=false")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:output, "$(CM)00:RACKA:REG_PRLFWD_GAIN_Q")
}

record(longin, "$(CM)00:RACKA:REG_PRLFWD_GAIN_Q_RBV") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(PRCA=prca) reg=prc_dsp_phs_avg_i_kx_1")
  field(TSE , "-2")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:input, "$(CM)00:RACKA:REG_PRLFWD_GAIN_Q_RBV")
}

record(longout, "$(CM)00:RACKA:REG_PRLREV_GAIN_I") {
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(PRCA=prca) reg=prc_dsp_phs_avg_i_kx_0 wait=false")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:output, "$(CM)00:RACKA:REG_PRLREV_GAIN_I")
}

record(longin, "$(CM)00:RACKA:REG_PRLREV_GAIN_I_RBV") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(PRCA=prca) reg=prc_dsp_phs_avg_i_kx_0")
  field(TSE , "-2")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:input, "$(CM)00:RACKA:REG_PRLREV_GAIN_I_RBV")
}

record(longout, "$(CM)00:RACKA:REG_PRLREV_GAIN_Q") {
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(PRCA=prca) reg=prc_dsp_phs_avg_i_kx_1 wait=false")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:output, "$(CM)00:RACKA:REG_PRLREV_GAIN_Q")
}

record(longin, "$(CM)00:RACKA:REG_PRLREV_GAIN_Q_RBV") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(PRCA=prca) reg=prc_dsp_phs_avg_i_kx_1")
  field(TSE , "-2")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:input, "$(CM)00:RACKA:REG_PRLREV_GAIN_Q_RBV")
}

# Forward and Reverse PRL signal strength
#  Needed by the math; could be acquired and snapshotted as part of
#  that process
# Add units, precision, hopr/lopr?

record(ai, "$(CM)00:RACKA:PRLFWD_MAGN") {
  field(DESC, "PRL fwd signal strength")
  field(SCAN, "Passive")
  field(VAL,  "0")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKA:PRLFWD_MAGNDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCA=prca) reg=mbox_PRLFWD_MAGN")
  info(feed:info:input, "$(CM)00:RACKA:PRLFWD_MAGN")
  info(feed:info:output, "$(CM)00:RACKA:PRLFWD_MAGN")
}

record(ai, "$(CM)00:RACKA:PRLREV_MAGN") {
  field(DESC, "PRL rev signal strength")
  field(SCAN, "Passive")
  field(VAL,  "0")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKA:PRLREV_MAGNDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCA=prca) reg=mbox_PRLREV_MAGN")
  info(feed:info:input, "$(CM)00:RACKA:PRLREV_MAGN")
  info(feed:info:output, "$(CM)00:RACKA:PRLREV_MAGN")
}

#  PRL tracking bandwidth. From LD:
#    SI units of Hz, max valid value around 300 kHz. Set by hand.  I hope
#    a single value around 500 Hz will be adequate everywhere.
#    See LCLSII-2.7-FR-0648, MO/LO and PRL Requirements for LLRF.
record(ao, "$(CM)00:RACKA:PRL_TRACK_BW") {
  field(DESC, "PRL tracking bandwidth")
  field(SCAN, "Passive")
  field(VAL,  "10000")
  field(EGU,  "Hz")
  field(LOPR, "0")
  field(HOPR, "300000")
  field(DRVL, "0")
  field(DRVH, "300000")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKA:PRL_TRACK_BWDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCA=prca) reg=mbox_PRL_TRACK_BW")
  info(feed:info:input, "$(CM)00:RACKA:PRL_TRACK_BW")
  info(feed:info:output, "$(CM)00:RACKA:PRL_TRACK_BW")
}

# Tentatively reserved for rack checkout
record(mbbi, "$(CM)00:RACKA:PRL_CHKSTAT") {
  field(VAL,  "0")
  field(ZRVL, "0")
  field(ZRST, "Good")
  field(ONVL, "1")
  field(ONST, "Warning")
  field(ONSV, "MINOR")
  field(TWVL, "2")
  field(TWST, "Error")
  field(TWSV, "MAJOR")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKA:PRL_CHKSTATDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCA=prca) reg=mbox_PRL_CHKSTAT")
  info(feed:info:input, "$(CM)00:RACKA:PRL_CHKSTAT")
  info(feed:info:output, "$(CM)00:RACKA:PRL_CHKSTAT")
}

# From L Doolittle 2 Nov 2022
# prl_fault_mon is only interesting/useful when read from the PRC.
# prl_fault_from_prc_icc is that same info transferred to the RFS.
# Each of them is a read-and-clear 1-bit register.  Read it as often
# as you want, and it will report if any faults happened since the
# last time you checked.  The threshold is fixed, at a level where
# we don't think it will trip in normal operations.

# From git commit
# Generates a PRL fault bit if sum_filt value from phs_avg.v
# >0.01 or <-0.01 degrees

# PRL fault bit, only valid on PRC
# See rfs_prl.template for bit transmitted to RFS
record(mbbi, "$(CM)00:PRCA:PRLFLT") {
    field(DTYP, "FEED Register Read")
    field(INP , "@name=$(PRCA=prca) reg=prl_fault_mon")
    field(SCAN, "1 second")
    field(ZRVL, "0")
    field(ONVL, "1")
    field(ONSV, "MAJOR")
    field(ZRST, "Ok")
    field(ONST, "Fault")
    field(TSE , "-2")
}
#==================================
#----------------------------------
# prc_prl.template:
#
# Phase Reference Line support
# for a single PRC chassis
#
# PREF - rack or chassis name
#        LCLS2 example: ACCL:L1B:0200:RACKA
# NAME - FEED device name
#        LCLS2 example: prca
#----------------------------------
#==================================

record(longin, "$(CM)00:RACKB:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(PRCB=prcb)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
  field(FLNK, "$(CM)00:RACKB:PRL_ENABLE")
}

# Indicates which PRL inputs are used
# EPICS owns this; lcls2_llrf phs_avg.py
# reads this PV
record(mbbi, "$(CM)00:RACKB:PRL_MODE") {
  field(VAL,  "$(PRLMODE)")
  field(ZRVL, "0")
  field(ZRST, "Fwd/Rev")
  field(ONVL, "1")
  field(ONST, "Fwd Only")
  field(TWVL, "2")
  field(TWST, "Rev Only")
  field(THVL, "3")
  field(THST, "None")
  field(PINI, "YES")
  field(FLNK, "$(CM)00:RACKB:PRL_MODE_STAT")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKB:PRL_MODEDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCB=prcb) reg=mbox_PRL_MODE")
  info(feed:info:input, "$(CM)00:RACKB:PRL_MODE")
  info(feed:info:output, "$(CM)00:RACKB:PRL_MODE")
}

# Expected mode setting for this location
# Value uses same macro as PRL_MODE
# Used to raise alarm if it does not match
# the actual mode
# ('CON' from 'config' - LCLS convention)
record(mbbi, "$(CM)00:RACKB:PRL_MODE_CON") {
  field(VAL,  "$(PRLMODE)")
  field(ZRVL, "0")
  field(ZRST, "Fwd/Rev")
  field(ONVL, "1")
  field(ONST, "Fwd Only")
  field(TWVL, "2")
  field(TWST, "Rev Only")
  field(THVL, "3")
  field(THST, "None")
  field(PINI, "YES")
  field(FLNK, "$(CM)00:RACKB:PRL_MODE_STAT")
}

# Default is for a mismatch to be MINOR_ALARM
record(calc, "$(CM)00:RACKB:PRL_MODE_STAT") {
  field(DESC, "PRL mode matches expected")
  field(CALC, "A=B?1:0")
  field(INPA, "$(CM)00:RACKB:PRL_MODE")
  field(INPB, "$(CM)00:RACKB:PRL_MODE_CON")
  field(LOLO, "0")
  field(HIHI, "1")
  field(LLSV, "1")
}

# Nominal PRL forward/reverse offset
# Set by expert, once or rarely
# Also called unique phase offset
record(longin, "$(CM)00:RACKB:PRL_OFFSET_NOM") {
  field(DESC, "Nominal PRL fwd-rev phase offset")
  field(SCAN, "Passive")
  field(VAL,  "0")
  field(EGU,  "degrees")
  field(LOPR, "-180")
  field(HOPR, "180")
  field(PINI, "YES")
  field(FLNK, "$(CM)00:RACKB:PRL_OFFSET_NOM_TS1_")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKB:PRL_OFFSET_NOMDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCB=prcb) reg=mbox_PRL_OFFSET_NOM")
  info(feed:info:input, "$(CM)00:RACKB:PRL_OFFSET_NOM")
  info(feed:info:output, "$(CM)00:RACKB:PRL_OFFSET_NOM")
}

# Records to update timestamp only when nominal
# offset is changed (and not over-written at IOC startup)
record(calcout, "$(CM)00:RACKB:PRL_OFFSET_NOM_TS1_") {
  field(DESC, "PRL nominal offset change")
  field(CALC, "A")
  field(INPA, "$(CM)00:RACKB:PRL_OFFSET_NOM")
  field(OOPT, "On Change")
  field(OUT, "$(CM)00:RACKB:PRL_OFFSET_NOM_TS2_ PP")
  info(autosaveFields, "A VAL")
}

record(stringin, "$(CM)00:RACKB:PRL_OFFSET_NOM_TS2_") {
  field(DESC, "PRL nominal offset change")
  field(DTYP, "Soft Timestamp")
  field(INP,  "@%m/%d/%Y %H:%M:%S")
  field(FLNK, "$(CM)00:RACKB:PRL_OFFSET_NOM_TS3_")
}

record(stringout, "$(CM)00:RACKB:PRL_OFFSET_NOM_TS3_") {
  field(DESC, "PRL nominal offset change")
  field(OMSL, "closed_loop")
  field(DOL,  "$(CM)00:RACKB:PRL_OFFSET_NOM_TS2_")
  field(OUT,  "$(CM)00:RACKB:PRL_OFFSET_NOM_TS PP")
  field(FLNK, "$(CM)00:RACKB:PRL_OFFSET_RAW")
}

record(stringout, "$(CM)00:RACKB:PRL_OFFSET_NOM_TS") {
  field(DESC, "PRL nominal offset last changed")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# In unscaled units (0-1 range)
# Default value from phs_avg.py
record(ai, "$(CM)00:RACKB:PRL_AMPL_THRESH") {
  field(DESC, "Minimum ampl to calc offset")
  field(VAL,  ".05")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(aSub, "$(CM)00:RACKB:PRL_OFFSET_CALC") {
  field(SNAM, "Calculate PRL Offset")
# If subroutine returns -1, set to INVALID
# Downstream records inherit this severity
  field(BRSV, "INVALID")
# PRLREV updates last in the set, so
# should have synchronous data
  field(INPA, "$(CM)00:RACKB:PRLFWD:AMEAN MSI NPP")
  field(INPB, "$(CM)00:RACKB:PRLREV:AMEAN MSI NPP")
  field(INPC, "$(CM)00:RACKB:PRLFWD:PMEAN MSI NPP")
  field(INPD, "$(CM)00:RACKB:PRLREV:PMEAN MSI CP")
  field(FTE,  "SHORT")
  field(INPE, "$(CM)00:RACKB:PRL_MODE MSI")
  field(INPF, "$(CM)00:RACKB:PRL_OFFSET_NOM MSI")
  field(INPG, "$(CM)00:RACKB:PRLFWD:SCALE MSI")
  field(INPH, "$(CM)00:RACKB:PRLREV:SCALE MSI")
  field(INPI, "$(CM)00:RACKB:PRL_AMPL_THRESH")
  field(NOVA, "1") # Phase offset [degrees]
  field(OUTA, "$(CM)00:RACKB:PRL_OFFSET_RAW PP MS")
  field(FTVB, "SHORT")
  field(OUTB, "$(CM)00:RACKB:PRL_OFFSET_STAT PP MS")
  field(FTVC, "STRING")
  field(NOVC, "1")
  field(OUTC, "$(CM)00:RACKB:PRL_OFFSET_MSG PP MS")
  field(FTVD, "STRING")
  field(NOVD, "1")
  field(OUTD, "$(CM)00:RACKB:PRL_OFFSET_TS PP MS")
  field(TSEL, "$(CM)00:RACKB:PRLREV:AWF.TIME CA")
}

record(mbbi, "$(CM)00:RACKB:PRL_OFFSET_STAT") {
  field(VAL,  "0")
  field(ZRVL, "0")
  field(ZRST, "Good")
  field(ONVL, "1")
  field(ONST, "Warning")
  field(ONSV, "MINOR")
  field(TWVL, "2")
  field(TWST, "Error")
  field(TWSV, "MAJOR")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

record(stringin, "$(CM)00:RACKB:PRL_OFFSET_MSG") {
  field(VAL,  "")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# Timestamp last successfully calculated OFFSET
record(stringin, "$(CM)00:RACKB:PRL_OFFSET_TS") {
  field(VAL,  "")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# PRL forward/reverse offset
# Calculated from waveforms
# LD prefers integer for PRL tracking alarms/status
# Non-integer for users/archiving
record(ai, "$(CM)00:RACKB:PRL_OFFSET_RAW") {
  field(DESC, "PRL fwd-rev phase offset")
  field(SCAN, "Passive")
  field(VAL,  "0")
  field(EGU,  "degrees")
  field(LOPR, "-180")
  field(HOPR, "180")
  field(FLNK, "$(CM)00:RACKB:PRL_OFFSET")
  field(TSEL, "$(CM)00:RACKB:PRLREV:AWF.TIME CA")
  info(autosaveFields, "VAL")
}

record(longin, "$(CM)00:RACKB:PRL_OFFSET") {
  field(DESC, "PRL fwd-rev phase offset")
  field(SCAN, "Passive")
  field(VAL,  "0")
  field(EGU,  "degrees")
  field(LOPR, "-180")
  field(HOPR, "180")
  field(INP,  "$(CM)00:RACKB:PRL_OFFSET_RAW MS")
  field(FLNK, "$(CM)00:RACKB:PRL_OFFSET_DIFF")
  field(TSEL, "$(CM)00:RACKB:PRLREV:AWF.TIME CA")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKB:PRL_OFFSETDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCB=prcb) reg=mbox_PRL_OFFSET")
  info(feed:info:input, "$(CM)00:RACKB:PRL_OFFSET")
  info(feed:info:output, "$(CM)00:RACKB:PRL_OFFSET")
}

record(calc, "$(CM)00:RACKB:PRL_OFFSET_DIFF") {
  field(DESC, "PRL offset - nominal")
  field(CALC, "A-B")
  field(INPA, "$(CM)00:RACKB:PRL_OFFSET")
  field(INPB, "$(CM)00:RACKB:PRL_OFFSET_NOM")
  field(LLSV, "MAJOR")
  field(LSV,  "MINOR")
  field(HSV,  "MINOR")
  field(HHSV, "MAJOR")
  field(LOLO, "-10")
  field(LOW,  "-9")
  field(HIGH, "9")
  field(HIHI, "10")
  field(PREC, "0")
  field(EGU,  "degrees")
  field(FLNK, "$(CM)00:RACKB:PRL_OFFSET_DIAGSUB")
}

record(aSub, "$(CM)00:RACKB:PRL_OFFSET_DIAGSUB") {
  field(DESC, "Graphical diagnostic")
  field(SNAM, "PRL Offset Diag")
# Inputs
  field(FTA, "DOUBLE") # PRL offset current
  field(INPA, "$(CM)00:RACKB:PRL_OFFSET")
  field(FTB, "DOUBLE") # PRL offset nominal
  field(INPB, "$(CM)00:RACKB:PRL_OFFSET_NOM")
  field(FTC, "DOUBLE") # PRL offset last rack checkout
  field(INPC, "$(CM)00:RACKB:PRL_OFFSET_INIT")
  field(FTD, "DOUBLE") # Fwd PRL phase
  field(INPD, "$(CM)00:RACKB:PRLFWD:PMEAN")
  field(FTE, "DOUBLE") # Rev PRL phase
  field(INPE, "$(CM)00:RACKB:PRLREV:PMEAN")
# Outputs
  field(NOVA, "2") # PRL offset current X
  field(NOVB, "2") # PRL offset current Y
  field(NOVC, "2") # PRL offset nominal X
  field(NOVD, "2") # PRL offset nominal Y
  field(NOVE, "2") # PRL offset last rack checkout X
  field(NOVF, "2") # PRL offset last rack checkout Y
  field(NOVG, "2") # Fwd PRL phase X
  field(NOVH, "2") # Fwd PRL phase Y
  field(NOVI, "2") # Rev PRL phase X
  field(NOVJ, "2") # Rev PRL phase Y
  field(NOVK, "2") # Zero, two elements
  field(NOVL, "2") # Graph x axis
  field(NOVM, "2") # Graph y axis
  field(NOVN, "200") # Unit circle x
  field(NOVO, "200") # Unit circle y
}

record(aSub, "$(CM)00:RACKB:PRL_OFFSET_DIAGSUB") {
  field(DESC, "Graphical diagnostic")
  field(SCAN, "1 second")
  field(SNAM, "PRL Offset Diag")
# Inputs
  field(FTA, "DOUBLE") # PRL offset current
  field(INPA, "$(CM)00:RACKB:PRL_OFFSET")
  field(FTB, "DOUBLE") # PRL offset nominal
  field(INPB, "$(CM)00:RACKB:PRL_OFFSET_NOM")
  field(FTC, "DOUBLE") # PRL offset last rack checkout
  field(INPC, "$(CM)00:RACKB:PRL_OFFSET_INIT")
  field(FTD, "DOUBLE") # Fwd PRL phase
  field(INPD, "$(CM)00:RACKB:PRLFWD:PMEAN")
  field(FTE, "DOUBLE") # Rev PRL phase
  field(INPE, "$(CM)00:RACKB:PRLREV:PMEAN")
  field(FTF, "DOUBLE") # Tracking zero phase
  field(INPF, "$(CM)00:RACKB:PRL_ZERO")
  field(FTG, "DOUBLE") # Zero phase - Offset (LO?)
  field(INPG, "$(CM)00:RACKB:PRL_ZERO_OFFSET_DIFF")
# Outputs
  field(NOVA, "2") # PRL offset current X
  field(NOVB, "2") # PRL offset current Y
  field(NOVC, "2") # PRL offset nominal X
  field(NOVD, "2") # PRL offset nominal Y
  field(NOVE, "2") # PRL offset last rack checkout X
  field(NOVF, "2") # PRL offset last rack checkout Y
  field(NOVG, "2") # Fwd PRL phase X
  field(NOVH, "2") # Fwd PRL phase Y
  field(NOVI, "2") # Rev PRL phase X
  field(NOVJ, "2") # Rev PRL phase Y
  field(NOVK, "2") # Zero, two elements
  field(NOVL, "2") # Graph x axis
  field(NOVM, "2") # Graph y axis
  field(NOVN, "200") # Unit circle x
  field(NOVO, "200") # Unit circle y
  field(NOVP, "2") # Zero phase X
  field(NOVQ, "2") # Zero phase Y
  field(NOVR, "2") # Zero phase - offset X
  field(NOVS, "2") # Zero phase - offset Y
}

record(bo, "$(CM)00:RACKB:PRL_OFFSET_RESET") {
  field(DESC, "Set current to nominal")
  field(SCAN, "Passive")
  field(ZNAM, "Reset")
  field(ONAM, "Reset")
  field(FLNK, "$(CM)00:RACKB:PRL_OFFSET_RESET_")
}

record(seq, "$(CM)00:RACKB:PRL_OFFSET_RESET_") {
  field(DESC, "Set current to nominal")
  field(SCAN, "Passive")
  field(SELM, "All")
  field(DOL0, "$(CM)00:RACKB:PRL_OFFSET")
  field(LNK0, "$(CM)00:RACKB:PRL_OFFSET_NOM PP")
  field(DOL1, "1")
# Optional link to notify users to check phases
  field(LNK1, "$(CKPLNKB)")
}

# PRL forward/reverse offset
# Measured at hardware init
record(longin, "$(CM)00:RACKB:PRL_OFFSET_INIT") {
  field(DESC, "PRL fwd-rev phase offset meas at init")
  field(SCAN, "Passive")
  field(VAL,  "0")
  field(EGU,  "degrees")
  field(LOPR, "-180")
  field(HOPR, "180")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKB:PRL_OFFSET_INITDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCB=prcb) reg=mbox_PRL_OFFSET_INIT")
  info(feed:info:input, "$(CM)00:RACKB:PRL_OFFSET_INIT")
  info(feed:info:output, "$(CM)00:RACKB:PRL_OFFSET_INIT")
}

# Read-only, 18-bit angle
# This will have a random offset (2*pi*n/66?) per reboot.
# From S Murthy, convert to degrees by scaling by360*0.5**19
# See rfs_prl.template for bit transmitted to RFS
record(ai, "$(CM)00:RACKB:REG_PRL_ZERO") {
  field(DESC, "Corrected phase between LO and PRL")
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(EGU,  "degrees")
  field(ASLO, "0.000686645507812")
  field(INP , "@name=$(PRCB=prcb) reg=phase_zero_mon")
  field(TSE , "-2")
  field(FLNK, "$(CM)00:RACKB:PRL_ZERO")
}

# REG_PRL_ZERO was named before understanding the
# conversion to degrees. Adding PRL_ZERO on top
# because it is easier than renaming in existing systems
record(ai, "$(CM)00:RACKB:PRL_ZERO") {
  field(DESC, "Corrected phase between LO and PRL")
  field(EGU,  "degrees")
  field(INP , "$(CM)00:RACKB:REG_PRL_ZERO MSI")
  field(TSEL, "$(CM)00:RACKB:REG_PRL_ZERO.TIME")
  field(FLNK, "$(CM)00:RACKB:PRL_ZERO_OFFSET_DIFF")
}

record(calc, "$(CM)00:RACKB:PRL_ZERO_OFFSET_DIFF") {
  field(DESC, "PRL zero - offset")
  field(CALC, "A-B")
  field(INPA, "$(CM)00:RACKB:PRL_ZERO")
  field(INPB, "$(CM)00:RACKB:PRL_OFFSET_RAW")
  field(PREC, "1")
  field(EGU,  "degrees")
}

record(bo, "$(CM)00:RACKB:PRL_ENABLE") {
  field(DESC, "Enable PRL tracking")
  field(ZNAM, "Disable")
  field(ONAM, "Enable")
  field(VAL,  "0")
  field(PINI, "YES")
  field(FLNK, "$(CM)00:RACKB:PRL_ENABLE_")
  info(autosaveFields, "VAL")
}

record(calcout, "$(CM)00:RACKB:PRL_ENABLE_") {
  field(DESC, "Enable PRL tracking")
  field(SCAN, "Passive")
  field(CALC, "C?A|B:A&(!B)")
  field(INPA, "$(CM)00:RACKB:REG_PRL_CTL_RBV")
# Bit 8 aka 0x100 aka 256 is phase tracking enable
  field(INPB, "256")
  field(INPC, "$(CM)00:RACKB:PRL_ENABLE")
  field(OOPT, "Every Time")
  field(OUT, "$(CM)00:RACKB:REG_PRL_CTL PP")
}

# Set REG_PRL_CTL OMSL closed_loop to only retrieve value from PRL_ENABLE_?

# Set bit 8 (0x100) to enable phase reference tracking
# When zero, the phase_zero value above is forced to zero.
record(longout, "$(CM)00:RACKB:REG_PRL_CTL") {
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(PRCB=prcb) reg=prc_dsp_prl_cfg wait=false")
  info(feed:info:output, "$(CM)00:RACKB:REG_PRL_CTL")
}

record(longin, "$(CM)00:RACKB:REG_PRL_CTL_RBV") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(PRCB=prcb) reg=prc_dsp_prl_cfg")
  field(TSE , "-2")
  field(FLNK, "$(CM)00:RACKB:PRL_ENABLE_RBV_")
  info(feed:info:input, "$(CM)00:RACKB:REG_PRL_CTL_RBV")
}

record(calc, "$(CM)00:RACKB:PRL_ENABLE_RBV_") {
  field(DESC, "PRL tracking enabled")
  field(SCAN, "Passive")
  field(CALC, "(A&B)?1:0")
  field(INPA, "$(CM)00:RACKB:REG_PRL_CTL_RBV")
# Bit 8 aka 0x100 aka 256 is phase tracking enable
  field(INPB, "256")
  field(FLNK, "$(CM)00:RACKB:PRL_ENABLE_RBV")
}

record(bi, "$(CM)00:RACKB:PRL_ENABLE_RBV") {
  field(DESC, "PRL tracking enabled")
  field(INP,  "$(CM)00:RACKB:PRL_ENABLE_RBV_")
  field(ZNAM, "Not enabled")
  field(ZSV,  "MAJOR")
  field(ONAM, "Enabled")
}

# Gain registers 16-bit signed
# (not expected to be set by humans)

record(longout, "$(CM)00:RACKB:REG_PRLFWD_GAIN_I") {
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(PRCB=prcb) reg=prc_dsp_phs_avg_i_kx_0 wait=false")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:output, "$(CM)00:RACKB:REG_PRLFWD_GAIN_I")
}

record(longin, "$(CM)00:RACKB:REG_PRLFWD_GAIN_I_RBV") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(PRCB=prcb) reg=prc_dsp_phs_avg_i_kx_0")
  field(TSE , "-2")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:input, "$(CM)00:RACKB:REG_PRLFWD_GAIN_I_RBV")
}

record(longout, "$(CM)00:RACKB:REG_PRLFWD_GAIN_Q") {
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(PRCB=prcb) reg=prc_dsp_phs_avg_i_kx_1 wait=false")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:output, "$(CM)00:RACKB:REG_PRLFWD_GAIN_Q")
}

record(longin, "$(CM)00:RACKB:REG_PRLFWD_GAIN_Q_RBV") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(PRCB=prcb) reg=prc_dsp_phs_avg_i_kx_1")
  field(TSE , "-2")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:input, "$(CM)00:RACKB:REG_PRLFWD_GAIN_Q_RBV")
}

record(longout, "$(CM)00:RACKB:REG_PRLREV_GAIN_I") {
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(PRCB=prcb) reg=prc_dsp_phs_avg_i_kx_0 wait=false")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:output, "$(CM)00:RACKB:REG_PRLREV_GAIN_I")
}

record(longin, "$(CM)00:RACKB:REG_PRLREV_GAIN_I_RBV") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(PRCB=prcb) reg=prc_dsp_phs_avg_i_kx_0")
  field(TSE , "-2")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:input, "$(CM)00:RACKB:REG_PRLREV_GAIN_I_RBV")
}

record(longout, "$(CM)00:RACKB:REG_PRLREV_GAIN_Q") {
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(PRCB=prcb) reg=prc_dsp_phs_avg_i_kx_1 wait=false")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:output, "$(CM)00:RACKB:REG_PRLREV_GAIN_Q")
}

record(longin, "$(CM)00:RACKB:REG_PRLREV_GAIN_Q_RBV") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(PRCB=prcb) reg=prc_dsp_phs_avg_i_kx_1")
  field(TSE , "-2")
  field(LOPR, "-32767")
  field(HOPR, "32767")
  info(feed:info:input, "$(CM)00:RACKB:REG_PRLREV_GAIN_Q_RBV")
}

# Forward and Reverse PRL signal strength
#  Needed by the math; could be acquired and snapshotted as part of
#  that process
# Add units, precision, hopr/lopr?

record(ai, "$(CM)00:RACKB:PRLFWD_MAGN") {
  field(DESC, "PRL fwd signal strength")
  field(SCAN, "Passive")
  field(VAL,  "0")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKB:PRLFWD_MAGNDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCB=prcb) reg=mbox_PRLFWD_MAGN")
  info(feed:info:input, "$(CM)00:RACKB:PRLFWD_MAGN")
  info(feed:info:output, "$(CM)00:RACKB:PRLFWD_MAGN")
}

record(ai, "$(CM)00:RACKB:PRLREV_MAGN") {
  field(DESC, "PRL rev signal strength")
  field(SCAN, "Passive")
  field(VAL,  "0")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKB:PRLREV_MAGNDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCB=prcb) reg=mbox_PRLREV_MAGN")
  info(feed:info:input, "$(CM)00:RACKB:PRLREV_MAGN")
  info(feed:info:output, "$(CM)00:RACKB:PRLREV_MAGN")
}

#  PRL tracking bandwidth. From LD:
#    SI units of Hz, max valid value around 300 kHz. Set by hand.  I hope
#    a single value around 500 Hz will be adequate everywhere.
#    See LCLSII-2.7-FR-0648, MO/LO and PRL Requirements for LLRF.
record(ao, "$(CM)00:RACKB:PRL_TRACK_BW") {
  field(DESC, "PRL tracking bandwidth")
  field(SCAN, "Passive")
  field(VAL,  "10000")
  field(EGU,  "Hz")
  field(LOPR, "0")
  field(HOPR, "300000")
  field(DRVL, "0")
  field(DRVH, "300000")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKB:PRL_TRACK_BWDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCB=prcb) reg=mbox_PRL_TRACK_BW")
  info(feed:info:input, "$(CM)00:RACKB:PRL_TRACK_BW")
  info(feed:info:output, "$(CM)00:RACKB:PRL_TRACK_BW")
}

# Tentatively reserved for rack checkout
record(mbbi, "$(CM)00:RACKB:PRL_CHKSTAT") {
  field(VAL,  "0")
  field(ZRVL, "0")
  field(ZRST, "Good")
  field(ONVL, "1")
  field(ONST, "Warning")
  field(ONSV, "MINOR")
  field(TWVL, "2")
  field(TWST, "Error")
  field(TWSV, "MAJOR")
  field(PINI, "YES")
  info(autosaveFields, "VAL")
}

# Dummy PV to allow python API to access actual PV
record(longin, "$(CM)00:RACKB:PRL_CHKSTATDMY") {
  field(DTYP, "FEED Register Read")
  field(INP , "@name=$(PRCB=prcb) reg=mbox_PRL_CHKSTAT")
  info(feed:info:input, "$(CM)00:RACKB:PRL_CHKSTAT")
  info(feed:info:output, "$(CM)00:RACKB:PRL_CHKSTAT")
}

# From L Doolittle 2 Nov 2022
# prl_fault_mon is only interesting/useful when read from the PRC.
# prl_fault_from_prc_icc is that same info transferred to the RFS.
# Each of them is a read-and-clear 1-bit register.  Read it as often
# as you want, and it will report if any faults happened since the
# last time you checked.  The threshold is fixed, at a level where
# we don't think it will trip in normal operations.

# From git commit
# Generates a PRL fault bit if sum_filt value from phs_avg.v
# >0.01 or <-0.01 degrees

# PRL fault bit, only valid on PRC
# See rfs_prl.template for bit transmitted to RFS
record(mbbi, "$(CM)00:PRCB:PRLFLT") {
    field(DTYP, "FEED Register Read")
    field(INP , "@name=$(PRCB=prcb) reg=prl_fault_mon")
    field(SCAN, "1 second")
    field(ZRVL, "0")
    field(ONVL, "1")
    field(ONSV, "MAJOR")
    field(ZRST, "Ok")
    field(ONST, "Fault")
    field(TSE , "-2")
}
#==================================
#----------------------------------
# rfs_prl.template:
#
# Phase Reference Line support
# for a single RFS chassis
#
# PREF - chassis name
#        LCLS2 example: ACCL:L1B:0200:RFS1A
# NAME - FEED device name
#        LCLS2 example: rfs1a
#----------------------------------
#==================================

record(longin, "$(CM)00:RFS1A:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(RFS1A=rfs1a)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
# Placeholder for any future
# registers that need to be set
# upon software-hardware reconnect
}

# Phase zero from PRC
record(ai, "$(CM)00:RFS1A:PRL_ZERO_PRC") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(EGU,  "degrees")
  field(ASLO, "0.000686645507812")
  field(INP , "@name=$(RFS1A=rfs1a) reg=phase_zero_rfs_mon")
  field(TSE , "-2")
}

# PRL fault bit transmitted from PRC to RFS
# See prc_prl.template for bit read directly from PRC
record(mbbi, "$(CM)00:RFS1A:PRLFLT_PRC") {
    field(DTYP, "FEED Register Read")
    field(INP , "@name=$(RFS1A=rfs1a) reg=prl_fault_from_prc_icc")
    field(SCAN, "1 second")
    field(ZRVL, "0")
    field(ONVL, "1")
    field(ONSV, "MAJOR")
    field(ZRST, "Ok")
    field(ONST, "Fault")
    field(TSE , "-2")
}
#==================================
#----------------------------------
# rfs_prl.template:
#
# Phase Reference Line support
# for a single RFS chassis
#
# PREF - chassis name
#        LCLS2 example: ACCL:L1B:0200:RFS1A
# NAME - FEED device name
#        LCLS2 example: rfs1a
#----------------------------------
#==================================

record(longin, "$(CM)00:RFS2A:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(RFS2A=rfs2a)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
# Placeholder for any future
# registers that need to be set
# upon software-hardware reconnect
}

# Phase zero from PRC
record(ai, "$(CM)00:RFS2A:PRL_ZERO_PRC") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(EGU,  "degrees")
  field(ASLO, "0.000686645507812")
  field(INP , "@name=$(RFS2A=rfs2a) reg=phase_zero_rfs_mon")
  field(TSE , "-2")
}

# PRL fault bit transmitted from PRC to RFS
# See prc_prl.template for bit read directly from PRC
record(mbbi, "$(CM)00:RFS2A:PRLFLT_PRC") {
    field(DTYP, "FEED Register Read")
    field(INP , "@name=$(RFS2A=rfs2a) reg=prl_fault_from_prc_icc")
    field(SCAN, "1 second")
    field(ZRVL, "0")
    field(ONVL, "1")
    field(ONSV, "MAJOR")
    field(ZRST, "Ok")
    field(ONST, "Fault")
    field(TSE , "-2")
}
#==================================
#----------------------------------
# rfs_prl.template:
#
# Phase Reference Line support
# for a single RFS chassis
#
# PREF - chassis name
#        LCLS2 example: ACCL:L1B:0200:RFS1A
# NAME - FEED device name
#        LCLS2 example: rfs1a
#----------------------------------
#==================================

record(longin, "$(CM)00:RFS1B:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(RFS1A=rfs1b)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
# Placeholder for any future
# registers that need to be set
# upon software-hardware reconnect
}

# Phase zero from PRC
record(ai, "$(CM)00:RFS1B:PRL_ZERO_PRC") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(EGU,  "degrees")
  field(ASLO, "0.000686645507812")
  field(INP , "@name=$(RFS1A=rfs1b) reg=phase_zero_rfs_mon")
  field(TSE , "-2")
}

# PRL fault bit transmitted from PRC to RFS
# See prc_prl.template for bit read directly from PRC
record(mbbi, "$(CM)00:RFS1B:PRLFLT_PRC") {
    field(DTYP, "FEED Register Read")
    field(INP , "@name=$(RFS1A=rfs1b) reg=prl_fault_from_prc_icc")
    field(SCAN, "1 second")
    field(ZRVL, "0")
    field(ONVL, "1")
    field(ONSV, "MAJOR")
    field(ZRST, "Ok")
    field(ONST, "Fault")
    field(TSE , "-2")
}
#==================================
#----------------------------------
# rfs_prl.template:
#
# Phase Reference Line support
# for a single RFS chassis
#
# PREF - chassis name
#        LCLS2 example: ACCL:L1B:0200:RFS1A
# NAME - FEED device name
#        LCLS2 example: rfs1a
#----------------------------------
#==================================

record(longin, "$(CM)00:RFS2B:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(RFS2A=rfs2b)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
# Placeholder for any future
# registers that need to be set
# upon software-hardware reconnect
}

# Phase zero from PRC
record(ai, "$(CM)00:RFS2B:PRL_ZERO_PRC") {
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(EGU,  "degrees")
  field(ASLO, "0.000686645507812")
  field(INP , "@name=$(RFS2A=rfs2b) reg=phase_zero_rfs_mon")
  field(TSE , "-2")
}

# PRL fault bit transmitted from PRC to RFS
# See prc_prl.template for bit read directly from PRC
record(mbbi, "$(CM)00:RFS2B:PRLFLT_PRC") {
    field(DTYP, "FEED Register Read")
    field(INP , "@name=$(RFS2A=rfs2b) reg=prl_fault_from_prc_icc")
    field(SCAN, "1 second")
    field(ZRVL, "0")
    field(ONVL, "1")
    field(ONSV, "MAJOR")
    field(ZRST, "Ok")
    field(ONST, "Fault")
    field(TSE , "-2")
}
#==================================
#----------------------------------
# rfs_prl_cav.template:
#
# Phase Reference Line support
# for RFS for a single cavity
#
# PREF - chassis name
#        LCLS2 example: ACCL:L1B:0210
# NAME - FEED device name
#        LCLS2 example: rfs1a
# BIT  - RFS zone, 0 or 1
#----------------------------------
#==================================

record(longin, "$(CM)10:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(RFS1A=rfs1a)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
  field(FLNK, "$(CM)10:PRL_ENABLE")
}

# Enable RFS use of PRL offset sent from PRC
record(mbbo, "$(CM)10:PRL_ENABLE") {
  field(DESC, "Enable RFS PRL tracking")
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(RFS1A=rfs1a) reg=shell_0_dsp_prl_nco_prl_cfg_rfs wait=false")
  field(ZRST, "Disable")
  field(ZRVL, "0")
  field(ONST, "Enable")
  field(ONVL, "1")
  field(VAL,  "0")
  field(PINI, "YES")
  info(feed:info:output, "$(CM)10:PRL_ENABLE")
  info(autosaveFields, "VAL")
}

record(mbbi, "$(CM)10:PRL_ENABLE_RBV") {
  field(DESC, "PRL tracking enabled")
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(RFS1A=rfs1a) reg=shell_0_dsp_prl_nco_prl_cfg_rfs")
  field(ZRST, "Not enabled")
  field(ZRVL, "0")
  field(ZRSV, "MINOR")
  field(ONST, "Enabled")
  field(ONVL, "1")
}
#==================================
#----------------------------------
# rfs_prl_cav.template:
#
# Phase Reference Line support
# for RFS for a single cavity
#
# PREF - chassis name
#        LCLS2 example: ACCL:L1B:0210
# NAME - FEED device name
#        LCLS2 example: rfs1a
# BIT  - RFS zone, 0 or 1
#----------------------------------
#==================================

record(longin, "$(CM)20:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(RFS1A=rfs1a)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
  field(FLNK, "$(CM)20:PRL_ENABLE")
}

# Enable RFS use of PRL offset sent from PRC
record(mbbo, "$(CM)20:PRL_ENABLE") {
  field(DESC, "Enable RFS PRL tracking")
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(RFS1A=rfs1a) reg=shell_1_dsp_prl_nco_prl_cfg_rfs wait=false")
  field(ZRST, "Disable")
  field(ZRVL, "0")
  field(ONST, "Enable")
  field(ONVL, "1")
  field(VAL,  "0")
  field(PINI, "YES")
  info(feed:info:output, "$(CM)20:PRL_ENABLE")
  info(autosaveFields, "VAL")
}

record(mbbi, "$(CM)20:PRL_ENABLE_RBV") {
  field(DESC, "PRL tracking enabled")
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(RFS1A=rfs1a) reg=shell_1_dsp_prl_nco_prl_cfg_rfs")
  field(ZRST, "Not enabled")
  field(ZRVL, "0")
  field(ZRSV, "MINOR")
  field(ONST, "Enabled")
  field(ONVL, "1")
}
#==================================
#----------------------------------
# rfs_prl_cav.template:
#
# Phase Reference Line support
# for RFS for a single cavity
#
# PREF - chassis name
#        LCLS2 example: ACCL:L1B:0210
# NAME - FEED device name
#        LCLS2 example: rfs1a
# BIT  - RFS zone, 0 or 1
#----------------------------------
#==================================

record(longin, "$(CM)30:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(RFS2A=rfs2a)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
  field(FLNK, "$(CM)30:PRL_ENABLE")
}

# Enable RFS use of PRL offset sent from PRC
record(mbbo, "$(CM)30:PRL_ENABLE") {
  field(DESC, "Enable RFS PRL tracking")
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(RFS2A=rfs2a) reg=shell_0_dsp_prl_nco_prl_cfg_rfs wait=false")
  field(ZRST, "Disable")
  field(ZRVL, "0")
  field(ONST, "Enable")
  field(ONVL, "1")
  field(VAL,  "0")
  field(PINI, "YES")
  info(feed:info:output, "$(CM)30:PRL_ENABLE")
  info(autosaveFields, "VAL")
}

record(mbbi, "$(CM)30:PRL_ENABLE_RBV") {
  field(DESC, "PRL tracking enabled")
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(RFS2A=rfs2a) reg=shell_0_dsp_prl_nco_prl_cfg_rfs")
  field(ZRST, "Not enabled")
  field(ZRVL, "0")
  field(ZRSV, "MINOR")
  field(ONST, "Enabled")
  field(ONVL, "1")
}
#==================================
#----------------------------------
# rfs_prl_cav.template:
#
# Phase Reference Line support
# for RFS for a single cavity
#
# PREF - chassis name
#        LCLS2 example: ACCL:L1B:0210
# NAME - FEED device name
#        LCLS2 example: rfs1a
# BIT  - RFS zone, 0 or 1
#----------------------------------
#==================================

record(longin, "$(CM)40:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(RFS2A=rfs2a)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
  field(FLNK, "$(CM)40:PRL_ENABLE")
}

# Enable RFS use of PRL offset sent from PRC
record(mbbo, "$(CM)40:PRL_ENABLE") {
  field(DESC, "Enable RFS PRL tracking")
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(RFS2A=rfs2a) reg=shell_1_dsp_prl_nco_prl_cfg_rfs wait=false")
  field(ZRST, "Disable")
  field(ZRVL, "0")
  field(ONST, "Enable")
  field(ONVL, "1")
  field(VAL,  "0")
  field(PINI, "YES")
  info(feed:info:output, "$(CM)40:PRL_ENABLE")
  info(autosaveFields, "VAL")
}

record(mbbi, "$(CM)40:PRL_ENABLE_RBV") {
  field(DESC, "PRL tracking enabled")
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(RFS2A=rfs2a) reg=shell_1_dsp_prl_nco_prl_cfg_rfs")
  field(ZRST, "Not enabled")
  field(ZRVL, "0")
  field(ZRSV, "MINOR")
  field(ONST, "Enabled")
  field(ONVL, "1")
}
#==================================
#----------------------------------
# rfs_prl_cav.template:
#
# Phase Reference Line support
# for RFS for a single cavity
#
# PREF - chassis name
#        LCLS2 example: ACCL:L1B:0210
# NAME - FEED device name
#        LCLS2 example: rfs1a
# BIT  - RFS zone, 0 or 1
#----------------------------------
#==================================

record(longin, "$(CM)50:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(RFS1B=rfs1b)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
  field(FLNK, "$(CM)50:PRL_ENABLE")
}

# Enable RFS use of PRL offset sent from PRC
record(mbbo, "$(CM)50:PRL_ENABLE") {
  field(DESC, "Enable RFS PRL tracking")
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(RFS1B=rfs1b) reg=shell_0_dsp_prl_nco_prl_cfg_rfs wait=false")
  field(ZRST, "Disable")
  field(ZRVL, "0")
  field(ONST, "Enable")
  field(ONVL, "1")
  field(VAL,  "0")
  field(PINI, "YES")
  info(feed:info:output, "$(CM)50:PRL_ENABLE")
  info(autosaveFields, "VAL")
}

record(mbbi, "$(CM)50:PRL_ENABLE_RBV") {
  field(DESC, "PRL tracking enabled")
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(RFS1B=rfs1b) reg=shell_0_dsp_prl_nco_prl_cfg_rfs")
  field(ZRST, "Not enabled")
  field(ZRVL, "0")
  field(ZRSV, "MINOR")
  field(ONST, "Enabled")
  field(ONVL, "1")
}
#==================================
#----------------------------------
# rfs_prl_cav.template:
#
# Phase Reference Line support
# for RFS for a single cavity
#
# PREF - chassis name
#        LCLS2 example: ACCL:L1B:0210
# NAME - FEED device name
#        LCLS2 example: rfs1a
# BIT  - RFS zone, 0 or 1
#----------------------------------
#==================================

record(longin, "$(CM)60:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(RFS1B=rfs1b)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
  field(FLNK, "$(CM)60:PRL_ENABLE")
}

# Enable RFS use of PRL offset sent from PRC
record(mbbo, "$(CM)60:PRL_ENABLE") {
  field(DESC, "Enable RFS PRL tracking")
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(RFS1B=rfs1b) reg=shell_1_dsp_prl_nco_prl_cfg_rfs wait=false")
  field(ZRST, "Disable")
  field(ZRVL, "0")
  field(ONST, "Enable")
  field(ONVL, "1")
  field(VAL,  "0")
  field(PINI, "YES")
  info(feed:info:output, "$(CM)60:PRL_ENABLE")
  info(autosaveFields, "VAL")
}

record(mbbi, "$(CM)60:PRL_ENABLE_RBV") {
  field(DESC, "PRL tracking enabled")
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(RFS1B=rfs1b) reg=shell_1_dsp_prl_nco_prl_cfg_rfs")
  field(ZRST, "Not enabled")
  field(ZRVL, "0")
  field(ZRSV, "MINOR")
  field(ONST, "Enabled")
  field(ONVL, "1")
}
#==================================
#----------------------------------
# rfs_prl_cav.template:
#
# Phase Reference Line support
# for RFS for a single cavity
#
# PREF - chassis name
#        LCLS2 example: ACCL:L1B:0210
# NAME - FEED device name
#        LCLS2 example: rfs1a
# BIT  - RFS zone, 0 or 1
#----------------------------------
#==================================

record(longin, "$(CM)70:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(RFS2B=rfs2b)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
  field(FLNK, "$(CM)70:PRL_ENABLE")
}

# Enable RFS use of PRL offset sent from PRC
record(mbbo, "$(CM)70:PRL_ENABLE") {
  field(DESC, "Enable RFS PRL tracking")
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(RFS2B=rfs2b) reg=shell_0_dsp_prl_nco_prl_cfg_rfs wait=false")
  field(ZRST, "Disable")
  field(ZRVL, "0")
  field(ONST, "Enable")
  field(ONVL, "1")
  field(VAL,  "0")
  field(PINI, "YES")
  info(feed:info:output, "$(CM)70:PRL_ENABLE")
  info(autosaveFields, "VAL")
}

record(mbbi, "$(CM)70:PRL_ENABLE_RBV") {
  field(DESC, "PRL tracking enabled")
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(RFS2B=rfs2b) reg=shell_0_dsp_prl_nco_prl_cfg_rfs")
  field(ZRST, "Not enabled")
  field(ZRVL, "0")
  field(ZRSV, "MINOR")
  field(ONST, "Enabled")
  field(ONVL, "1")
}
#==================================
#----------------------------------
# rfs_prl_cav.template:
#
# Phase Reference Line support
# for RFS for a single cavity
#
# PREF - chassis name
#        LCLS2 example: ACCL:L1B:0210
# NAME - FEED device name
#        LCLS2 example: rfs1a
# BIT  - RFS zone, 0 or 1
#----------------------------------
#==================================

record(longin, "$(CM)80:PRL_INIT1_") {
  field(DTYP, "FEED On Connect")
  field(INP , "@name=$(RFS2B=rfs2b)")
  field(SCAN, "I/O Intr")
  field(PHAS, "0")
  field(FLNK, "$(CM)80:PRL_ENABLE")
}

# Enable RFS use of PRL offset sent from PRC
record(mbbo, "$(CM)80:PRL_ENABLE") {
  field(DESC, "Enable RFS PRL tracking")
  field(DTYP, "FEED Register Write")
  field(OMSL, "supervisory")
  field(OUT , "@name=$(RFS2B=rfs2b) reg=shell_1_dsp_prl_nco_prl_cfg_rfs wait=false")
  field(ZRST, "Disable")
  field(ZRVL, "0")
  field(ONST, "Enable")
  field(ONVL, "1")
  field(VAL,  "0")
  field(PINI, "YES")
  info(feed:info:output, "$(CM)80:PRL_ENABLE")
  info(autosaveFields, "VAL")
}

record(mbbi, "$(CM)80:PRL_ENABLE_RBV") {
  field(DESC, "PRL tracking enabled")
  field(DTYP, "FEED Register Read")
  field(SCAN, "1 second")
  field(INP , "@name=$(RFS2B=rfs2b) reg=shell_1_dsp_prl_nco_prl_cfg_rfs")
  field(ZRST, "Not enabled")
  field(ZRVL, "0")
  field(ZRSV, "MINOR")
  field(ONST, "Enabled")
  field(ONVL, "1")
}
