#Generic MPS Link Node startup script
wdStart(20)  

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...
nfsMountsShow()

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

#rtems_gdb_start(0,0) can't call this, if you don't load the obj.

## 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.
## There are two rotaries: 
##   "ID2" is the higher byte;
##   "ID1" is the lower byte.
## They are in HEXADECIMAL
## 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()

## At this point Link node ID is known. Override if necess

## BLM LN 30
#mpsSetLinkNodeID(30) # "30" is configured to be BLM link node type 
#sprintf(substr,"db/mps%s%s.template",mpsGetLinkNodeLOCA(),mpsGetLinkNodeUNIT())
#printf("%s\n",substr)
#dbLoadTemplate(substr)
#sprintf(substr,"db/mps%s%s.blm_ln30_template",mpsGetLinkNodeLOCA(),mpsGetLinkNodeUNIT())
#printf("%s\n",substr)
#dbLoadTemplate(substr)

# BLM LN 35
mpsSetLinkNodeID(35) # "35" is configured to be BLM link node type 
sprintf(substr,"db/mps%s%s.template",mpsGetLinkNodeLOCA(),mpsGetLinkNodeUNIT())
printf("%s\n",substr)
dbLoadTemplate(substr)
sprintf(substr,"db/mps%s%s.blm_template",mpsGetLinkNodeLOCA(),mpsGetLinkNodeUNIT())
printf("%s\n",substr)
dbLoadTemplate(substr)

sprintf(substr,"IOC=EIOC:%s:%s,ENGINEER=Dayle Kotturi",mpsGetLinkNodeLOCA(),mpsGetLinkNodeUNIT())
dbLoadRecords("db/iocAdmin.db",substr) 

drvMpsFlag = 0
drvMpsLinkNodeTypeFlag = 0
drvMpsGetADCFlag = 0
drvMpsSetDebounceFlag = 0
drvMpsSetDebounceTimeRegFlag = 0
drvMpsGetADCFlag = 0
drvMpsSetBLMHVFlag = 0
drvMpsGetBLMHVFlag = 0
drvMpsSetDeadManFlag = 0
drvMpsSetDeadManRegFlag = 0
drvMpsGetDeadManRegFlag = 0
drvMpsGetDebounceFlag = 0
drvMpsGetLatchedRegFlag = 0
drvMpsGetFlag = 0
drvMpsParseXMLFlag = 0
drvMpsParseChannelsConfigFlag = 0
drvMpsParseNodeConfigFlag = 0
drvMpsGetADCDiagnosticFlag = 0
drvMpsGetQADCBeamDoseFlag = 0
drvMpsGetQADCTestDoseFlag = 0
drvMpsRegistersFlag = 0

devBiMpsFlag = 0
devBoMpsFlag = 0
devAiMpsFlag = 0
devAoMpsFlag = 0
devLonginMpsFlag = 0
devLongoutMpsFlag = 0
devStringinMpsFlag = 0
devWaveformMpsFlag = 0

# autosave/restore settings

# From http://www.aps.anl.gov/bcda/synApps/autosave/autoSaveRestore_R4-4.html,
# you must load save_restoreStatus.db with same prefix to be able to use this.
# save_restore.c, it uses this prefix to find status, hearbeat, statusStr, recentlyStr, rebootStatus, ...  
#sprintf(substr,"EIOC:%s:%s",mpsGetLinkNodeLOCA(),mpsGetLinkNodeUNIT())
#printf("%s\n",substr)
#save_restoreSet_status_prefix(substr) 

save_restoreSet_IncompleteSetsOk(1)
save_restoreSet_DatedBackupFiles(1)

nfsMountsShow()
# 7/7/9 in development, /data = $IOC_DATA/lclsdev-12 

# MPS Link Node layout
sprintf(substr,"/data/lab-blm/autosave-req")
set_requestfile_path(substr)
set_savefile_path("/data/lab-blm","autosave")

## ==============================================
## New method for autosave and restore
## Decide what is saved by specifying it in the
## EPICS databases
## =============================================
#set_pass0_restoreFile("info_positions.sav")
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")

# IF MOUNT POINT GOES BACK, USE: for all 6 cases
#mpsReadDatabaseVersion("/boot/epics/iocTop/MachineProtection/mpsConfiguration/database/current")
# WHILE /boot is /afs/slac...
mpsReadDatabaseVersion("/boot/g/lcls/epics/iocTop/MachineProtection/mpsConfiguration/database/current")

#drvMpsParseDebounceConfigFlag=4
# 12/19/8: out. This .csv file no longer exported from sql. get mappings
# from LinkNodeChannel.csv 
#sprintf(substr,"/boot/g/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/g/lcls/epics/iocTop/MachineProtection/mpsConfiguration/database/%s/csv/LinkNode.csv",mpsGetDatabaseVersion())
printf("%s\n",substr)
mpsParseNodeConfig(substr)

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

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

iocInit()

## =====================================================
## 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()
chdir("/data/lab-blm/autosave-req")
iocshCmd("makeAutosaveFiles()")

## Explaining the difference: even though there is only field line added
## to the record, it is used in two ways. First, on pass0, the positions
## are saved at the rate of 5 sec. Why you would want to do this more
## than once escapes me. Second, you save the the settings every 30 sec
## to a file (if they change more freq, the values are kept in RAM !?

## Eg. create_monitor_set("info_positions.req", 5, "U=USEG:UND1:150:")
# 2/2/2009 deliberately leaving off last argument...
create_monitor_set("info_settings.req", 30, "U=")

free(substr)

# Autosave is unreliable every time...so set the minimum
dbpf("BLM:UND1:MP12:FID_WIDTH","1000000")
dbpf("BLM:UND1:MP12:TDEL","75")
dbpf("BLM:UND1:MP12:TPGDEL","50")
dbpf("BLM:UND1:MP12:TPGWID","2000")
dbpf("BLM:UND1:MP12:GTGDEL","50")
dbpf("BLM:UND1:MP12:GTGWID","10000")
dbpf("BLM:UND1:MP12:SYNCOUTDEL","50")
dbpf("BLM:UND1:MP12:SYNCOUTWID","0")
dbpf("BLM:UND1:MP12:GPIOCTL","0")
dbpf("BLM:UND1:MP12:GTADCDEL","550")
dbpf("BLM:UND1:MP12:BTTDEL","900025")
dbpf("BLM:UND1:MP12:TTBDEL","125")
dbpf("BLM:UND1:MP12:EGTGDEL","50")
dbpf("BLM:UND1:MP12:EGTWID","2524000")
dbpf("BLM:UND1:MP12:QADC_CSR1_CTL","33548")
dbpf("BLM:UND1:MP12:QADC_CSR2_CTL","11826")
dbpf("BLM:UND1:MP12:QADC_CSR3_CTL","2071")

# Start the Sync Read Requests (at 1 Hz) now. Doing it in mpsInitialise was too soon
startSyncReads()

# Use this to monitor
#drvMpsSyncReadRequestFlag=4
