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

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

< envPaths

# ====================================================================
# Setup some additional environment variables
# ====================================================================
# Setup environment variables 

# tag log messages with IOC name
# How to escape the "vioc-b34-ev41" as the PERL program
# will try to repplace it.
# So, uncomment the following and remove the backslash
epicsEnvSet("EPICS_IOC_LOG_CLIENT_INET","${IOC}")

epicsEnvSet(TOP,"${IOC_APP}")

# ============================================
# Set MACROS for EVRs
# ============================================
# FAC = SYS0 ==> LCLS1
# FAC = SYS1 ==> FACET

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

epicsEnvSet(EVR_DEV1,"EVR:B34:EV41")
epicsEnvSet(UNIT,"EV41")
epicsEnvSet(FAC,"SYS0")

epicsEnvSet(EVR_DEV2,"EVR:B34:EV42")
epicsEnvSet(UNIT,"EV42")
epicsEnvSet(FAC,"SYS0")

# ========================================================
# Support Large Arrays/Waveforms; Number in Bytes
# Please calculate the size of the largest waveform
# that you support in your IOC.  Do not just copy numbers
# from other apps.  This will only lead to an exhaustion
# of resources and problems with your IOC.
# The default maximum size for a channel access array is
# 16K bytes.
# ========================================================
epicsEnvSet("EPICS_CA_MAX_ARRAY_BYTES", "800000")

# END: Additional environment variables
# ====================================================================

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

###########################################################
# Initialize all hardware first                           #
###########################################################
# ======================================================
# Init PMC EVR: To support Timing System Event Receiver
# ======================================================
# In this case this EVR is running in a PC under linux

# =====================================================================
# Setup for EVR:
# =====================================================================
# Add ErConfigure for each EVR before iocInit.
#
#    VME:      ErConfigure(<instance>,<address>,<vector>,<level>,0)
#    PMC:      ErConfigure(<instance>,    0    ,    0   ,   0   ,1)
#    Embedded: ErConfigure(<instance>,    0    ,<vector>,<level>,2)
#
#    where instance = EVR instance, starting from 0, incrementing by 1
#                     for each subsequent card.  Only 1 EVR instance
#                    is allowed for Embedded EVRs.
#    and   address  = VME card address, starting from 0x300000,
#                     incrementing by 0x100000 for each subsequent card
#                     (0 for PMC and Embedded)
#    and   vector   = VME or Embedded interrupt vector.
#                    For VME, start from 0x60 and increment by 0x02 for
#                    each subsequent card.
#                     (0 for PMC)
#    and   level    = VME or Embedded interrupt level.
#                    For VME, set to 4.  Can be the same for all EVRs.
#                     (0 for PMC)
#    and   0        = VME
#       or 1        = PMC
#       or 2       = Embedded
#
# ======================================================================
# Debug interest level for EVR Driver
# ErDebug=100

# The first instance of EVR hardware:
# Here we have an EVR230 (PMC Card for PCs with Carrier)
ErConfigure(0, 0, 0, 0, 1)

# The second instance of EVR hardware:
# Here we have an EVR300 (PCI Express Card for PCs)
ErConfigure(1, 0, 0, 0, 4)

# Add evrInitialize (after ErConfigure) if a fiducial routine will be
# registered before iocInit driver initialization:
#evrInitialize()
# ======= EVR Setup Complete ============================================



########################################################################
# BEGIN: Load the record databases
#######################################################################
# =====================================================================
# Load EVENT databases to support use of the timing system
# =====================================================================

dbLoadRecords("db/Pattern.db","IOC=${IOC_NAME},SYS="${FAC})

# PMC EVR230
dbLoadRecords("db/EvrPmc.db","EVR=${EVR_DEV1},CRD=0,SYS=${FAC}")
dbLoadRecords("db/PMC-trig.db","IOC=${IOC_NAME},LOCA=${LOCA},UNIT=${UNIT},SYS=${FAC}")

# PCIe EVR300
dbLoadRecords("db/EvrPci.db","EVR=${EVR_DEV2},CRD=1,SYS=SYS0")
dbLoadRecords("db/PCI-trig.db","IOC=${IOC_NAME},LOCA=${LOCA},UNIT=${UNIT},SYS=${FAC}")

# BSA Data Source Simulator Databases
dbLoadRecords("db/bsaSimulator.db","DEVICE=LEPTON")

# BSA Database for each data source from above
dbLoadRecords("db/Bsa.db","DEVICE=LEPTON, ATRB=COUNTER")
dbLoadRecords("db/Bsa.db","DEVICE=LEPTON, ATRB=SINE")

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

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

# =====================================================================
#Load Additional databases:
# =====================================================================
## Load record instances
#dbLoadRecords("db/dbExample.db","user=ernesto,no=1,scan=1 second")

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

# =====================================================================
## Begin: 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)
save_restoreSet_DatedBackupFiles(0)

# ============================================================
# Where to find the list of PVs to save
# ============================================================
set_requestfile_path("/data/${IOC}/autosave-req")

# ============================================================
# Where to write the save files that will be used to restore
# ============================================================
set_savefile_path("/data/${IOC}/autosave")

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

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

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

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



# =============================================================
# Start EPICS IOC Process (i.e. all threads will start running)
# =============================================================

## Run this to trace the stages of iocInit
#traceIocInit()

iocInit()

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

## Start any sequence programs
#seq sncExample,"user=gwbrownHost"

## ===========================================================
## 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 ${IOC_BOOT}
# 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")')
