# Generic MPS Link Node startup script
wdStart(20)

# Debugger stub daemon
ld("rtems-gdb-stub.obj")

chdir("..")

ld("bin/RTEMS-uC5282/mpsLinkNode.obj")

# note: you can substitute getbenv for bsp_getbenv for AUTOBOOT, but not for
# others...
mpsReadBenvFactory(bsp_getbenv("FACTORY"))
mpsReadBenvRevision(bsp_getbenv("REVISION"))
mpsReadBenvSerial(bsp_getbenv("SERIAL"))
mpsReadBenvConsole(bsp_getbenv("CONSOLE"))
mpsReadBenvKernel(bsp_getbenv("KERNEL"))
mpsReadBenvKernelArgs(bsp_getbenv("KERNEL_ARGS"))
mpsReadBenvHWAddr0(bsp_getbenv("HWADDR0"))
mpsReadBenvFirmware(bsp_getbenv("FW_VERSION"))
mpsReadBenvStartPoint(bsp_getbenv("_0"))
mpsReadBenvRAMImage(bsp_getbenv("RAMIMAGE"))
mpsReadBenvCache(bsp_getbenv("CACHE"))
mpsReadBenvAutoboot(bsp_getbenv("AUTOBOOT"))
mpsReadBenvBootpEnable(bsp_getbenv("BP_ENBL"))

## /data now mapped by pre_st.cmd
nfsMountsShow()

## Register all support components
dbLoadDatabase("dbd/mpsLinkNode.dbd")
iocshCmd("mpsLinkNode_registerRecordDeviceDriver(pdbbase)") 

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

## Dynamic loading is not working in 3.14.10!! What is going on!!???
# Load RTEMS Spy Utility application object file:
##ld("/boot/lcls/epics/modules/rtemsutils/devl-epics-3-14-10/bin/RTEMS-uC5282/rtemsUtilsSup.obj")

## Register components for RTEMS Spy Utility
##dbLoadDatabase("/boot/lcls/epics/modules/rtemsutils/devl-epics-3-14-10/dbd/rtemsUtilsSup.dbd")
##iocshCmd("rtemsUtilsSup_registerRecordDeviceDriver(pdbbase)")
# =========================================================================================



#rtems_gdb_start(0,0)

## Load record instances
substr=malloc(500) #freed before iocInit()
## Link node ID is used to determine which database to load.
## Link node ID is set with rotary switch on the circuit board.
## This value is accessible via memory mapped I/O register, but 
## hardware initialization must be done in order to read it, so
## instead of doing hardware init via iocInit, it MUST be done here
mpsInitialise()

sprintf(substr,"db/mps%s%s.template",mpsGetLinkNodeLOCA(),mpsGetLinkNodeUNIT())
dbLoadTemplate(substr)

sprintf(substr,"IOC=EIOC:%s:%s",mpsGetLinkNodeLOCA(),mpsGetLinkNodeUNIT())
dbLoadRecords("db/iocAdmin.db",substr) 
sprintf(substr,"P=EIOC:%s:%s:",mpsGetLinkNodeLOCA(),mpsGetLinkNodeUNIT())
dbLoadRecords("db/save_restoreStatus.db",substr)

drvMpsFlag = 0
drvMpsGetADCFlag = 0
drvMpsSetDebounceFlag = 0
drvMpsGetDebounceFlag = 0
drvMpsGetLatchedFlag = 0
drvMpsGetBlmHVFlag = 0
drvMpsParseXMLFlag = 0
devAiMpsFlag = 0
devAoMpsFlag = 0
devLonginMpsFlag = 0
devLongoutMpsFlag = 0
devStringinMpsFlag = 0

## ==============================================
## Begin Part 1. New method for autosave and restore
## ==============================================
sprintf(substr,"EIOC:%s:%s:",mpsGetLinkNodeLOCA(),mpsGetLinkNodeUNIT())
printf("Using save_restoreSet_status_prefix %s\n",substr)
save_restoreSet_status_prefix( substr)
save_restoreSet_IncompleteSetsOk(1)
save_restoreSet_DatedBackupFiles(1)
## ==============================================
## From pre_st.cmd, /data gets mapped to /u1/lcls/epics/ioc/data/<hostname>
## ==============================================
set_requestfile_path("/data/autosave-req")
set_requestfile_path("./") ################## may not need this
## ==============================================
# Reusing substr from above for the save
## ==============================================
set_savefile_path("/data","autosave")
## ==============================================
## Decide what is saved by specifying it in the EPICS databases
## =============================================
set_pass0_restoreFile("info_settings.sav")
set_pass1_restoreFile("info_settings.sav")
## =============================================
## Load Access Security File
## caPutLog now depends on this running.
## =============================================
asSetFilename("db/mpsLinkNode.acf")
## ==============================================
## End Part 1. New method for autosave and restore
## ==============================================

drvMpsParseDebounceConfigFlag=4
mpsReadDatabaseVersion("/boot/lcls/epics/iocTop/MachineProtection/mpsConfiguration/database/current")

sprintf(substr,"/boot/lcls/epics/iocTop/MachineProtection/mpsConfiguration/database/%s/csv/LinkNodeDebounce.csv",mpsGetDatabaseVersion())
printf("%s\n",substr)
mpsParseDebounceConfig(substr)

# Need to read link_node_type_enum for this link_node_id to know what type it is
sprintf(substr,"/boot/lcls/epics/iocTop/MachineProtection/mpsConfiguration/database/%s/csv/LinkNode.csv",mpsGetDatabaseVersion())
printf("%s\n",substr)
mpsParseNodeConfig(substr)

sprintf(substr,"/boot/lcls/epics/iocTop/MachineProtection/mpsConfiguration/database/%s/csv/LinkNodeChannel.csv",mpsGetDatabaseVersion())
printf("%s\n",substr)
mpsParseChannelsConfig(substr)

sprintf(substr,"/boot/lcls/epics/iocTop/MachineProtection/mpsConfiguration/database/%s/csv/LinkNodeRack.csv",mpsGetDatabaseVersion())
printf("%s\n",substr)
mpsParseNodeRackConfig(substr)

iocInit()

## ==============================================
## Begin Part 2. New method for autosave and restore
## ==============================================
## Turn on caPutLogging:
## Log values only on change to the iocLogServer:
## =====================================================
caPutLogInit("172.27.8.31:7004")
caPutLogShow(2)
## ===========================================================
## Start autosave routines to save our data
## ===========================================================
## optional, needed if the IOC takes a very long time to boot.
## ===========================================================
epicsThreadSleep( 1.0)
## ===========================================================
## Handle autosave 'commands' contained in loaded databases.
## change directory to ioc startup area to run makeAutosaveFiles()
## ===========================================================
printf("making autosave files in: /data/autosave-req\n")
chdir("/data/autosave-req")
iocshCmd("makeAutosaveFiles()")
## ===========================================================
## Explaining the args: 
## you save the the settings every 30 sec
## to a file (if they change more freq, the values are kept in RAM 
## and so if IOC reboots, you risk restoring data stale by up to 30 sec 
create_monitor_set("info_settings.req", 30, "U=")

#############NEW 2/26/2009
#############epicsThreadSleep(1)
#############fdbrestore( "info_settings.sav") # seems to fault the QADC
#############END NEW 2/26/2009

## ==============================================
## End Part 2. New method for autosave and restore
## ==============================================

free(substr)

## Extra write. Shouldn't be needed. Isn't need for BLM, but right now 
## needed for BYKIK on power cycle to FPGA. 

########### TAKE THIS OUT *(long*)0x8000006C=1 2/26/9 

### Let's do some spying on this CPU:
#iocshCmd("spy(2)")
