## Example RTEMS startup script using the cexp
## shell.

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

chdir("../..")
ld("bin/_ARCH_/_APPNAME_.obj")
bspExtVerbosity=0


## Register all support components
dbLoadDatabase("dbd/_APPNAME_.dbd")
_APPNAME__registerRecordDeviceDriver(pdbbase) 


# ==========================================================================================
# Let's dynamically load and register the RTEMS Spy Utility
# ==========================================================================================
# I hope to use ENV variables from "envPaths" with RTEMS someday!!

# Load RTEMS Spy Utility application object file:
ld("bin/_ARCH_/rtemsUtilsSup.obj")

## Register components for RTEMS Spy Utility
dbLoadDatabase("dbd/rtemsUtilsSup.dbd")
rtemsUtilsSup_registerRecordDeviceDriver(pdbbase)
# =========================================================================================

# =========================================================================================
# Load up shortcuts to paths that can be used by the EPICS IOC Shell
# =========================================================================================
< iocBoot/_IOC_/envPaths
< iocBoot/_IOC_/cdCommands
# ====================================================================
# Setup some additional environment variables
# ====================================================================
epicsEnvSet("ENGINEER","Mary Poppins")
epicsEnvSet("LOCATION","Twilight Zone")

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

# =====================================================================
# Turn Off BSP Verbosity
# =====================================================================
bspExtVerbosity=0



###########################################################
# 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

#ErConfigure(0, 0, 0, 0, 1)
#evrTimeFlag=0

# 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 iocAdmin databases to support IOC Health and monitoring
# =====================================================================
dbLoadRecords("db/iocAdminRTEMS.db","IOC=${IOC}")
dbLoadRecords("db/iocAdminScanMon.db","IOC=${IOC}")

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

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

# ========================================================
# Load EVR Databases for the Timing system
# ========================================================
dbLoadRecords("db/${IOC}-pattern.db")
dbLoadRecords("db/${IOC}-evr.db")
dbLoadRecords("db/${IOC}-trig.db")
dbLoadRecords("db/${IOC}-bsa.db")
# ========================================================

# =====================================================================
#Load Additional databases:
# =====================================================================
## Load record instances
dbLoadRecords("db/dbExample.db","user=_USER_Host")

# 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(0)

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

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

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

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

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

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

iocInit()

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

## Start any sequence programs
#seq(sncExample,"user=_USER_")

## Start autosave process:
create_monitor_set("info_settings.req",60,"")

# Start rtems spy utility:
#iocshCmd("spy(2)")
