#!../../bin/linuxRT-x86_64/masterSource

## You may have to change iocSim to something else
## everywhere it appears in this file

< envPaths

# ===========================================
#            ENVIRONMENT VARIABLES
# ===========================================

# *********************************************
# **** Environment variables for YCPSWASYN ****

# CPSW Port name
epicsEnvSet("CPSW_PORT","SIM1")

# Yaml File
epicsEnvSet("YAML_FILE", "yaml/MoFreqLocker_project.yaml/000TopLevel.yaml")

# FPGA IP address
#epicsEnvSet("FPGA_IP", "192.168.1.16")
epicsEnvSet("FPGA_IP", "192.168.1.21")

# Use Automatic generation of records from the YAML definition
# 0 = No, 1 = Yes
epicsEnvSet("AUTO_GEN", 1)

# Automatically generated record prefix
epicsEnvSet("PREFIX","SIM16")

# Dictionary file for manual (empty string if none)
epicsEnvSet("DICT_FILE", "")

epicsEnvSet("AREA","SYS2")

# *********************************************
# **** Environment variables for IOC Admin ****

epicsEnvSet(IOC_NAME,"SIOC:$(AREA):MS01")

# ***************************************************
# **** Environment variables for Agilent 53220A  ****

epicsEnvSet("STREAM_PROTOCOL_PATH","${TOP}/db")


cd ${TOP}

# ===========================================
#               DBD LOADING
# ===========================================
## Register all support components
dbLoadDatabase("dbd/masterSource.dbd",0,0)
masterSource_registerRecordDeviceDriver(pdbbase)

# ===========================================
#              DRIVER SETUP
# ===========================================

# ************************************
# **** Driver setup for YCPSWAsyn ****
## Configure asyn port driver
# YCPSWASYNConfig(
#    Port Name,                 # the name given to this port driver
#    Yaml Doc,                  # Path to the YAML file
#    Root Path                  # OPTIONAL: Root path to start the generation. If empty, the Yaml root will be used
#    IP Address,                # OPTIONAL: Target FPGA IP Address. If not given it is taken from the YAML file
#    Record name Prefix,        # Record name prefix
#    Record name Length Max,    # Record name maximum length (must be greater than lenght of prefix + 4)
#    Use DB Autogeneration,     # Set to 1 for autogeneration of records from the YAML definition. Set to 0 to disable it
#    Load dictionary,           # Dictionary file path with registers to load. An empty string will disable this function
#YCPSWASYNConfig("${CPSW_PORT}", "${YAML_FILE}", "", "${FPGA_IP}", "${PREFIX}", 50, "${AUTO_GEN}", "${DICT_FILE}")

cpswLoadYamlFile("${YAML_FILE}", "NetIODev", "yaml/MoFreqLocker_project.yaml", "${FPGA_IP}")

# ************************************
# **** Driver setup for YCPSWAsyn ****
# YCPSWASYNConfig(
#    PORT_NAME,             # the name given to this port driver
#    ROOT_PATH,             # root path to start the generation, if empty, the root will be used
#    PREFIX,                # record name prefix
#    AUTO_GEN_MODE,         # 0: disable auto-generation, 1: enable autogeneration using mapped names
#                           # 2: enable autogeneration using hashed names
#    DICIT_FILE             # dictionary file path with registers to load, an empty string will disable this function
# )
YCPSWASYNConfig("${CPSW_PORT}","", "${PREFIX}", "${AUTO_GEN}", "${DICT_FILE}")

# ********************************************
# **** Agilent 53220A (Frequency counter) ****
drvAsynIPPortConfigure ("agilFC","freq-b34-rf01:5024",0,0,0)

# ===========================================
#               ASYN MASKS
# ===========================================

# **********************************
# **** Asyn Masks for YCPSWAsyn ****

#asynSetTraceMask(${PORT},, -1, 9)

# ===========================================
#               DB LOADING
# ===========================================

# ***************************
# **** Load YCPSWAsyn db ****

#Save/Load configuration related records
#dbLoadRecords("db/saveLoadConfig.db", "P=${AMC_CARRIER_PREFIX}, PORT=${CPSW_PORT}, SAVE_FILE=/tmp/configDump.yaml, LOAD_FILE=yaml/defaults_bergoz.yaml")

# Automatic initialization
#dbLoadRecords("db/monitorFPGAReboot.db", "P=${AMC_CARRIER_PREFIX}, KEY=-66686157")

# Agilent 53220A
dbLoadRecords("db/Agilent53220A.db","DEVICE=FREQ:SYS2:00:,PORT=agilFC,SCAN=1 second")
dbLoadRecords("db/asynRecord.db" "P=FREQ:SYS2:00:,R=asyn,PORT=agilFC,ADDR=-1,OMAX=0,IMAX=0")


# 
dbLoadRecords("db/subRec.db", "NAME=FREQ:SYS2:00:MODACVOLT,DESC=DAC Voltage Reading,INAM=,SNAM=getDACVolt,INPA=SIM16:M:MFL:DAC_OUTPUT3_REMOTE:Rd,EGU=Volt,SCAN=1 second")
dbLoadRecords("db/subRecSet.db", "NAME=FREQ:SYS2:00:MODAC,DESC=set value for DAC control,INAM=,SNAM=setSigned18,OUTPV=SIM16:M:MFL:DAC_OUTPUT3_REMOTE:St")
dbLoadRecords("db/alias.db", "ALIAS=FREQ:SYS2:00:SIMVER,REC=SIM16:M:MFL:Version:Rd")
dbLoadRecords("db/alias.db", "ALIAS=FREQ:SYS2:00:MODAC_RBV,REC=SIM16:M:MFL:DAC_OUTPUT3_REMOTE:Rd")

# **********************************************************************
# **** Load iocAdmin databases to support IOC Health and monitoring ****
dbLoadRecords("db/iocAdminSoft.db","IOC=${IOC_NAME}")
dbLoadRecords("db/iocAdminScanMon.db","IOC=${IOC_NAME}")

# The following database is a result of a python parser
# which looks at RELEASE_SITE and RELEASE to discover
# versions of software your IOC is referencing
# The python parser is part of iocAdmin
dbLoadRecords("db/iocRelease.db","IOC=${IOC_NAME}")

# ===========================================
#           SETUP AUTOSAVE/RESTORE
# ===========================================

# ============================================================
# If all PVs don't connect continue anyway
# ============================================================
save_restoreSet_IncompleteSetsOk(1)

# ============================================================
# created save/restore backup files with date string
# useful for recovery.
# ============================================================
save_restoreSet_DatedBackupFiles(1)

# ============================================================
# Where to find the list of PVs to save
# ============================================================
#set_requestfile_path("${IOC_DATA}/${IOC}/autosave-req")
set_requestfile_path("${IOC_DATA}/vioc-b084-fl01", "autosave-req")

# ============================================================
# Where to write the save files that will be used to restore
# ============================================================
#set_savefile_path("${IOC_DATA}/${IOC}/autosave")
set_savefile_path("${IOC_DATA}/vioc-b084-fl01", "autosave")

# ============================================================
# Prefix that is use to update save/restore status database
# records
# ============================================================
save_restoreSet_status_prefix("${IOC}:")

## Restore datasets
set_pass0_restoreFile("info_positions.sav")
set_pass1_restoreFile("info_positions.sav"
set_pass0_restoreFile("info_settings.sav")
set_pass1_restoreFile("info_settings.sav")



# ===========================================
#          CHANNEL ACESS SECURITY
# ===========================================
# This is required if you use caPutLog.
# Set access security filea
# Load common LCLS Access Configuration File
< ${ACF_INIT}


# ===========================================
#               IOC INIT
# ===========================================
iocInit()

# Turn on caPutLogging:
# Log values only on change to the iocLogServer:
caPutLogInit("${EPICS_CA_PUT_LOG_ADDR}")
caPutLogShow(2)


# Start autosave routines to save our data
# optional, needed if the IOC takes a very long time to boot.
epicsThreadSleep( 1.0)

cd("/data/${IOC}/autosave-req")
iocshCmd("makeAutosaveFiles")

# Start the save_restore task
# save changes on change, but no faster
# than every 5 seconds.
# Note: the last arg cannot be set to 0
create_monitor_set("info_positions.req", 5 )
create_monitor_set("info_settings.req" , 30 )

cd ${TOP}
