#!../../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

# Define macros below
#epicsEnvSet("EPICS_CA_ADDR_LIST", "134.79.47.255")
# 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("ST_CMD","st.cmd")
epicsEnvSet("IOC_NAME","IOCTUNERDEV")

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

## 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 MOTR0
# 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/ioctunerdev.db")

dbLoadRecords("db/asynRecord.db", "P=B122-TUNER,R=:CouplerAsynControl,PORT=COUPLER0,ADDR=0,IMAX=100,OMAX=100")
dbLoadRecords("db/asynRecord.db", "P=B122-TUNER,R=:AnalogInAsynControl,PORT=AIN0,ADDR=0,IMAX=100,OMAX=100")
dbLoadRecords("db/asynRecord.db", "P=B122-TUNER,R=:Cell1MotorAsynControl,PORT=MOTR0,ADDR=0,IMAX=100,OMAX=100")
dbLoadRecords("db/asynRecord.db", "P=B122-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=IOCTUNERDEV")
dbLoadRecords("db/iocAdminScanMon.db","IOC=IOCTUNERDEV")

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

# =====================================================================
# Load database for autosave status
# =====================================================================
dbLoadRecords("db/save_restoreStatus.db", "P=IOCTUNERDEV:")

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

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

# Restore PVs 
< iocBoot/common/init_restore_soft.cmd

# dbLoadRecords("db/EK1100_0x120000.template", "DEVICE=B122-TUNER:Coupler,PORT=COUPLER0,SCAN=1 second")
# dbLoadRecords("db/EL3702_0x30000.template", "DEVICE=B122-TUNER:Cell1Posn,CH=1,LINR=LINEAR,EGUF=10,EGUL=-10,EGU=V,PORT=AIN0,SCAN=I/O Intr")

# dbLoadRecords("db/EL7041_0x190000.template", "DEVICE=B122-TUNER:Cell1,PORT=MOTR0,SCAN=1 second")
# dbLoadRecords("db/EL7041_0x190000.template", "DEVICE=B122-TUNER:Cell5,PORT=MOTR1,SCAN=1 second")

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

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


# Report records to spear RDB.
#dbRestore("spear1","ioctunerdev",-1)
# =====================================================================
# 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)

# 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")')



