#==============================================================
#
#  Abs:  Generic piece of startup script for RTEMS
#
#  Name: init_st.cmd
#
#  Facility:  CAMAC-to-VME Controls
#
#  Auth: 30-Jun-2009, Kristi Luchini  (LUCHINI)
#  Rev:  dd-mmm-yyyy, Reviewer's Name (USERNAME)
#--------------------------------------------------------------
#  Mod:
#        02-Jun-2020, K. Luchini      (LUCHINI):
#          define PIOP_PATH after setting dir to TOP
#        01-Jun-2020, K. Luchini      (LUCHINI):
#          chg < to cexpsh()
#        13-May-2020, K. Luchini      (LUCHINI):
#          add CA security, init log
#          replace IOC_TOP with TOP, defined in cdCommands
#          add SUBSYS for init_log.cmd, note that
#          add IOC_MACR and SIOC_MACRO
#          initLogPrefix() doesn't work in R3.14.12.
#          add iocAdminRTEMS.db,iocRelase.db
#          and devSeqCar.db
#        05-Oct-2010, K. Luchini      (LUCHINI):
#          load cdCommands and build PIOP_PATH
#        04-Oct-2010, K. Luchini      (LUCHINI):
#          set environment variables PIOP_PATH,
#          EPICS_CA_MAX_ARRAY_BYTES and ENGINEER
#          Need to build PIOP_PATH dynamically using
#          TOP environment variable set in envPaths.
#          However, currently envPaths thas a bug when 
#          generated for RTEMS.
#
#==============================================================
#
# Set environment variables
cexpsh("cdCommands")

# Setup environment variables
# Get current iocBoot and topdirectory path so we can return easily
setenv("IOC_BOOT",getcwd())
setenv("IOC"     ,pathSubstitute("%H"))

# iocAdmin environment variables
putenv("ENGINEER=Sonya Hoobler")
setenv("IOC_MACRO" ,getenv("IOC_NAME"))
addenv("IOC_MACRO" ,"IOC=",1)

# for devSeqCar
setenv("SIOC_MACRO",getenv("IOC_MACRO"))
addenv("SIOC_MACRO","S",1)

# for BSA
putenv("EPICS_CA_MAX_ARRAY_BYTES=8000000")

# for iocLogPrefix
setenv("SUBSYS","camac")

# Change to the TOP of ioc application
cd("../..")
setenv("IOC_TOP",getcwd())

setenv("PIOP_PATH" ,getcwd())
addenv("PIOP_PATH" ,"/iocBoot/common/",0)

#==============================================================
# Load Objects
#==============================================================
# Load magnet application and gdb object files
ld("bin/RTEMS-beatnik/CAMAC.obj")

# Load the RTEMS debugger
ld("rtems-gdb-stub.obj")

# instead of compiling with -mlong flag
free(malloc(0x2000000))

#==============================================================
# Load EPICS Database  
#==============================================================
dbLoadDatabase("dbd/CAMAC.dbd")
CAMAC_registerRecordDeviceDriver(pdbbase)

# Load database to support IOC Health and monitoring
dbLoadRecords("db/iocAdminRTEMS.db",getenv("IOC_MACRO"))

# 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",getenv("IOC_MACRO"))

# Load sequence admin
dbLoadRecords("db/devSeqCar.db",getenv("SIOC_MACRO"))

#==============================================================
# Start IOC Log Client
#==============================================================
# Optionally disable logging
# NOTE: must be done before iocInit!
iocLogDisable=0

# Initialize EPICS errlog message queue size.  This needs to be
# done here before any dbLoad call is made.  Note the default is
# 1260 bytes - too many messages are being discarded making it
# hard to debug.
errlogInit(5000)

getenv("LOG_INIT") && cexpsh(getenv("LOG_INIT"))

#==============================================================
#  Load Channe Access Security if configuration file exists
#==============================================================
getenv("ACF_INIT") && cexpsh(getenv("ACF_INIT"))

#==============================================================
#  Start RTEMS debugger
#==============================================================
rtems_gdb_start(200,0)

# End of script






