#########################################################
# SNS vxWorks startup file
#########################################################

#*******************************************************
# Project Name: My IOC Application
# Boards --- List the boards in your crate here
# IOC Team Leader: <your name here>
# EPICS System Support: Ernest L. Williams Jr.
# Computing and Network Systems Support: Greg Lawson
# Host Platform: Linux 2.4.X
# Target Platform: PowerPC 603e/604 (vxWorks 5.5.1)
#
# List all hardware in this VME crate with its base address,
# interrupt vector, interrupt level:
#**************************************************


## The following is needed if your board support package doesn't at boot time
## automatically cd to the directory containing its startup script
#cd "/ade/epics/iocTop/R3.14.7/testIOC/new/iocBoot/iocics-ctl-ioc-test01"

# Next we load the cdCommands file which contains needed aliases that
# vxWorks will use to navigate our NFS mounted file system.
# cdCommnads can also be parsed to determine the components and their version
# at runtime.

< cdCommands




# =========================================================================
# Setup Default Route
# =========================================================================
defaultGateway=172.31.72.1
mRouteAdd("0.0.0.0",defaultGateway,0,0,0)




#***********************************************************************
###############  Load all Libraries here ######################
#***********************************************************************


## You may have to change ics-ctl-ioc-test01 to something else
## everywhere it appears in this file

# Let's load our main munch file.  This munch file should
# include EPICS BASE, and vxWorks and your local software
# components.  

# ------------------------------------------------------------------------
# ---------  Load Main Application Library -------------------------------
# ------------------------------------------------------------------------
cd topbin
ld < theOne.munch

## Register all support components
cd top
dbLoadDatabase("dbd/theOne.dbd",0,0)
theOne_registerRecordDeviceDriver(pdbbase)


# The SNS standard way of loading libraries from 
# share will be done outside of the main  "*.munch"  file.

#==========================================================================
# ---------  Load genSubRecord Library -----------------------------------
#==========================================================================
cd gensubrecordbin
ld < genSubRecordLib.munch
cd gensubrecord
dbLoadDatabase("dbd/genSubRecord.dbd",0,0)
genSubRecord_registerRecordDeviceDriver(pdbbase)
#=========================================================================
# ------------------------------------------------------------------------
#=========================================================================

#=========================================================================
# ---------  Load autosave Library --------------------------------
#=========================================================================
cd autosavebin
ld < autosaveLib.munch

cd autosave
dbLoadDatabase("dbd/autosave.dbd",0,0)
autosave_registerRecordDeviceDriver(pdbbase)
#=========================================================================
# ------------------------------------------------------------------------
#=========================================================================

#=========================================================================
# ---------  Load vxStats Library ----------------------------------------
#=========================================================================
cd vxstatsbin
ld < vxStatsLib.munch

cd vxstats
dbLoadDatabase("dbd/vxStats.dbd",0,0)
vxStats_registerRecordDeviceDriver(pdbbase)
#=========================================================================
# ------------------------------------------------------------------------
#=========================================================================

#=========================================================================
# ---------  Load pvlistServer Library -----------------------------------
#=========================================================================
cd pvlistsrvbin
ld < pvlistServerLib.munch

cd pvlistsrv
dbLoadDatabase("dbd/pvlistServer.dbd",0,0)
pvlistServer_registerRecordDeviceDriver(pdbbase)
#=========================================================================
#------------------------------------------------------------------------
#=========================================================================

#=========================================================================
# ---------  Load general time before the utility module------------------
#=========================================================================
cd generaltimebin
ld <generalTimeLib.munch

cd generaltime
dbLoadDatabase("dbd/generalTime.dbd")
generalTime_registerRecordDeviceDriver(pdbbase)
#=========================================================================
#------------------------------------------------------------------------
#=========================================================================

#=========================================================================
# ---------  Load utility Library ----------------------------------------
#=========================================================================
##cd utilitybin
##ld < utilityLib.munch

##cd utility
##dbLoadDatabase("dbd/utility.dbd",0,0)
##utility_registerRecordDeviceDriver(pdbbase)
#=========================================================================
#------------------------------------------------------------------------
#=========================================================================


#==========================================================================
# ---------  Load timing Library --------------------------------------
#==========================================================================
##cd timingbin
##ld < timingLib.munch

##cd timing
##dbLoadDatabase("dbd/timing.dbd",0,0)
##timing_registerRecordDeviceDriver(pdbbase)
#=========================================================================
#------------------------------------------------------------------------
#=========================================================================


#***********************************************************************
###############  Hardware/Software Initialization ######################
#***********************************************************************

# ----------------------------------------------------------------------
# Begin Initialization of Utility Module Support
# ----------------------------------------------------------------------
# Initialize the hardware, and select which interrupts you want enabled.
# The utility module has to at least have the event interrupt enabled.
#This call HAS to be before any v124 initialization or anything that might
#call TSInit().

##snsUtilInit (0, 0x4000)
##snsUtilConfigEvent (0, 0xd0, 5)

# The other two calls are HIGHLY recommended to prevent work queue overflows.
##snsUtilConfigEnviron (0, 0xd2, 5)
##snsUtilConfigExtern  (0, 0xd4, 5)
#==========================================================================
#---------------- Utility module initialization complete ----------------- 
#========================================================================== 
 

#===============================================================================  
# ----------------- Begin Initialization of V124S Module Support ---------------
#===============================================================================
# Initialize the V124S Timing Card
# Timing System driver setup
# Inputs for card configuration are:
    # int     card        Card number
    # int     baseAddress VME base address for this card
    # int     intNumber   Interrupt vector for this card
    # int     intLevel    Interrupt request level for this card
    # double  delay       Base time adjustment (nanoseconds) for this card

##v124sConfig (0,0x9000, 0xCA, 3, 0.0)
#================================================================================
#---------------- V124S module initialization complete -----------------
#================================================================================




#######################################################
#####   Load "Fully instantiated" databases here  #####
#######################################################
cd top
## Load vxStats database
# Load IOC Health monitoring database
# This will report CPU usage, memory usage.
# IOC heartbeat, etc...
# Change IOCNAME to the network name of your IOC
dbLoadDatabase("db/vxStats-ics-ctl-ioc-test01.db")

# Load autosave database for status and diagnostics of
# autosave/restore program
# Replace $(S), $(SS), $(N) with your system, subsys, instance.
cd autosave
dbLoadRecords("db/save_restoreStatus.db", "P=ICS_Ctl:IOC_test01:")
save_restoreSet_status_prefix("ICS_Ctl:IOC_test01:")

## Load generalTime status database
# Change SNS_IOCNAME to the SNS naming convention name of
# your IOC
cd generaltime
dbLoadDatabase("db/generalTime.template",0,"IOC=ICS_Ctl:IOC_test01")

cd top
## Load Utility Module databases
# Change IOCNAME to the network name of your IOC
##dbLoadDatabase("db/utility-ics-ctl-ioc-test01.db")


## Load databases for the V124S Timing Receiver
# Change IOCNAME to the network name of your IOC
##dbLoadDatabase("db/timing-ics-ctl-ioc-test01.db")

# Stress Test databases
dbLoadDatabase("db/sineBPM.db")
dbLoadRecords("db/dbExample.db","user=fermion,no=1,scan=1 second")
dbLoadRecords("db/dbExample.db","user=fermion,no=2,scan=2 second")
dbLoadRecords("db/dbExample.db","user=fermion,no=3,scan=5 second")

# 60 Hz test crafted by Coles Sibley:
##dbLoadRecords("db/timerTst.db")

######################################################
### ---------- Database loading complete ----------###
#######################################################



#*******************************************************
####### Begin autoSaveRestore setup #####################
#*******************************************************
# -------------------------------------
# Configure and Initialize save/restore
# -------------------------------------

### dbrestore setup
#reboot_restoreDebug  = 0

# ok to restore a save set that had missing values (no CA connection to PV)?
##sr_restore_incomplete_sets_ok = 1

# dbrestore saves a copy of the save file it restored.  File name is, e.g.,
# "auto_settings.sav.bu" or "auto_settings.sav020306-083522" if
# reboot_restoreDatedBU is nonzero.
# At boot time, the restore software writes a backup copy of the ".sav" file
# from which it restored PV's.  This file can either be named xxx.bu, and
# be rewritten every reboot, or named xxxYYMMDD-HHMMSS, where "YY..." is a date.
# Dated backups are not overwritten.
# If you want dated backup files, set reboot_restoreDatedBU = 1
# I don't want dated since it is making too many darn files.
# Maybe, this means that we are rebooting way too much!!!
# When we have a stable production RELEASE then we can go back
# to having dated files.

# We are ready for production
#reboot_restoreDatedBU = 1
# We are ready for production
save_restoreSet_DatedBackupFiles(1)


# specify where save files should go
set_savefile_path("/var/autosave");

# specify where request files come from
# current directory
# This works since "startup" is a symbol already
# defined by the cdCommands. So, setting up
# an additional NFS mount point is not necessary
# to tell autoSaveRestore where you are keeping
# the request files.

set_requestfile_path(startup)

# specify what save files should be restored when
set_pass0_restoreFile("ics-ctl-ioc-test01.sav")
set_pass1_restoreFile("ics-ctl-ioc-test01.sav")

#==============================================================================
#---------------  End IOC AutoSave Restore ------------------------------------
#==============================================================================

# -----------------------------------------
# Channel Access Security
# -----------------------------------------
# Remember it is necessary to cd back to top
# before calling iocInit.
# since iocInit will use the relative path name
# that is specified by asSetFilename.
# So, don't forget.

cd top
#asSetFilename("db/Mysecurity.acf")
#asSetSubstitutions("P=ICS_Ctl:IOC_test01")
# ---------------------------------------------------------




# ============================================================
# =================== Start pvlistServer =====================
#=============================================================
# pvlistServer:
# This will provide a dynamic query to grab all of the
# PVs served by this IOC.  So on the client side execute:
# pvlistClient  <iocName>:22222
#=============================================================
pvlistserver("22222")


cd startup
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ------- Start EPICS KERNEL -------------
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
iocInit()



# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ------- Start IOC LogClient ------
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Connect to the iocLogServer on EPICS Boot
# server.
# Check error logs in /ade/epics/iocCommon/errLogs
iocLogInit()


# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ------- Begin autosave Task  ------------
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
### Start up the autosave task and tell it what to do.
# The task is actually named "save_restore".
# (See also, 'initHooks' above, which is the means by which the values that
# will be saved by the task we're starting here are going to be restored.
#
# Shall we test fopen() to make sure we can create a file before actually
# doing it.  If we don't have an NFS mount, we won't be allowed to remove the
# test file.

# Uncomment below if you need to do some debugging
##save_restoreDebug=20
##save_restore_test_fopen = 1

# save pvs every 30 seconds
taskDelay(sysClkRateGet()*5)
create_monitor_set("ics-ctl-ioc-test01.req",30)


## Start any sequence programs
snc_subst="stack=10000,user=fermion"

#seq &sncExample,"user=fermion"
seq &sncExample, snc_subst


