#!../../bin/linuxRT_glibc-x86_64/tuner

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

< envPaths

pwd()

# Define macros below
epicsEnvSet("EPICS_CA_ADDR_LIST", "134.79.47.255 b221-cpu1:5064 b221-cpu1:5071 b221-cpu1:5072 b221-atca1:5064 b221-atca1:5071 b221-atca1:5072")
# Setup the CA server port address
epicsEnvSet("EPICS_CA_SERVER_PORT","5071")


# =====================================================================
# Set some facility specific MACROs for database instantiation below
# This is in effort to make IOCs Applications facility agnostic

# =====================================================================
## For iocAdmin
epicsEnvSet("ENGINEER","Shantha Condamoor")
epicsEnvSet("LOCATION","SPEAR")
epicsEnvSet("STARTUP","/afs/slac/g/spear/epics/app/spear/iocBoot/soft-ioctuner-boo-rf")
epicsEnvSet("ST_CMD","st.cmd")
epicsEnvSet("IOC_NAME","SOFT-IOCTUNER-BOO-RF")

# System Location:
epicsEnvSet("LOCA","B221")

## Location of stream protocol files
epicsEnvSet "STREAM_PROTOCOL_PATH", "../../db"

# Need this path to EPICS BASE so that caRepeater can be started:
# Let's figure out a way to pass this one in via the IOC's
# initial startup.cmd: another job for hookIOC.py :)
# Not needed caRepeater is started up by laci for all IOCs at
# CPU boot up.
#epicsEnvSet(PATH,"${EPICS_BASE}/bin/linuxRT_glibc-x86_64")

# ===========================================

cd ${TOP}
# ====================================================
## Register all support components
dbLoadDatabase("dbd/tuner.dbd",0,0)
tuner_registerRecordDeviceDriver(pdbbase)
# ====================================================

# ======================================================
# Init EtherCAT: To support Real Time fieldbus
# ======================================================

ecAsynInit("/tmp/sock1", 1000000)

# ========================================================================================
# Creates port M0
# connected to ECASYN port MOTOR0
# 1 Axis
# 500 ms moving poll
# 1000 ms idle poll
# For Beckhoff Motor Controller
# ========================================================================================
EtherCATCreateController(M0, MOTR0, 1, 500, 1000)
EtherCATCreateController(M1, MOTR1, 1, 500, 1000)

# All motor parameters hard-coded in substitutions files for safety reasons. 
# Always boot motors with calibrated values.

dbLoadRecords("db/soft-ioctuner-boo-rf.db")

dbLoadRecords("db/asynRecord.db", "P=BOO-RF-TUNER,R=:CouplerAsynControl,PORT=COUPLER0,ADDR=0,IMAX=100,OMAX=100")
dbLoadRecords("db/asynRecord.db", "P=BOO-RF-TUNER,R=:AnalogInAsynControl,PORT=AIN0,ADDR=0,IMAX=100,OMAX=100")
dbLoadRecords("db/asynRecord.db", "P=BOO-RF-TUNER,R=:Cell1MotorAsynControl,PORT=MOTR0,ADDR=0,IMAX=100,OMAX=100")
dbLoadRecords("db/asynRecord.db", "P=BOO-RF-TUNER,R=:Cell5MotorAsynControl,PORT=MOTR1,ADDR=0,IMAX=100,OMAX=100")

# ============================================================================
# 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}")

# =====================================================================
# Load database for autosave status
# =====================================================================
dbLoadRecords("db/save_restoreStatus.db", "P=${IOC_NAME}:")

# END: Loading the record databases
#####################################################################################################

# =====================================================================
## Begin: Setup autosave/restore
# =====================================================================

# Restore PVs 
< iocBoot/common/init_restore_soft.cmd

# Report records to spear RDB.
#dbRestore("spear1","soft-ioctuner-boo-rf",-1)

dbLoadRecords("db/EK1101.template", "DEVICE=BOO-RF-TUNER:Coupler,PORT=COUPLER0,SCAN=1 second")
dbLoadRecords("db/EL3702.template", "DEVICE=BOO-RF-TUNER:Cell1Posn,CH=1,LINR=LINEAR,EGUF=10,EGUL=-10,EGU=V,PORT=AIN0,SCAN=I/O Intr")
dbLoadRecords("db/EL3702.template", "DEVICE=BOO-RF-TUNER:Cell5Posn,CH=2,LINR=LINEAR,EGUF=10,EGUL=-10,EGU=V,PORT=AIN0,SCAN=I/O Intr")

dbLoadRecords("db/EL7041.template", "DEVICE=BOO-RF-TUNER:Cell1,PORT=MOTR0,SCAN=1 second")
dbLoadRecords("db/EL7041.template", "DEVICE=BOO-RF-TUNER:Cell5,PORT=MOTR1,SCAN=1 second")

# =====================================================================
# End: Setup autosave/restore
# =====================================================================

# Mask out asyn error messages related to the EtherCat modules
asynSetTraceMask(COUPLER0, -1, 0)
asynSetTraceMask(AIN0, -1, 0)
asynSetTraceMask(MOTR0, -1, 0)

# =====================================================================
# Channel Access Security:  
# This is required if you use caPutLog.
# Set access security file
# Load common LCLS Access Configuration File
# < ${ACF_INIT}

# =============================================================
# Start EPICS IOC Process (i.e. all threads will start running)
# =============================================================
cd iocBoot/${IOC}
iocInit()

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

## Start any sequence programs

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

# Start autosave 
< ../common/start_restore_soft.cmd

# Turn off save_restore messages for spear
save_restoreSet_Debug(0)

# ===========================================================================

# ===========================================================================
# Setup Real-time priorities after iocInit for driver threads
# ===========================================================================
system("/bin/su root -c `pwd`/rtPrioritySetup.cmd")

# cd ${IOC_BOOT}

# An example of using the CEXP Shell:
# cexpsh("-c",'printf("hello\n")')



