##########################################################
#
# st-rtems.cmd file for Fast Feedback Controller IOC
# this file is for the development ioc ioc-b34-fb01
# this ioc loads the FB03 database so as not to interfere with
# the soft IOCs on development, ioc-devl-fb01, and ioc-devl-fb02
# author: D. Fairley, L. Piccoli
# 
# modified:
#   0ct. 7, 2010 D. Fairley created, set up for autosave
#
##########################################################

# Where am I?
pwd()

setenv("FCOMMCGRP", "mc-lcls-fcom",1)
setenv("NETMASK1", "255.255.252.0",0)

#ld("watchdog.obj")
#wdStart(20)

ld("rtems-gdb-stub.obj")
rtems_gdb_start(0,0)

ld("lanIpBasic_mve.obj") 

chdir("../..")
ld("bin/RTEMS-beatnik/feedback.obj")
#ld("bin/RTEMS-beatnik/ffTest.obj")
#ld("bin/RTEMS-beatnik/Exception.obj")

fcomUtilSetIPADDR1("-fnet")

lanIpSetup(getenv("IPADDR1"),getenv("NETMASK1"),0,0) #check def of 1st parm
lanIpDebug=0
padProtoDebug=0

lsmod()

nfsMountsShow()

epicsEnvSet("IOCSH_PS1","epics@ioc-b34-fb01>")

# bspExtMemProbe only durint init. clear this to avoid the lecture.
bspExtVerbosity=0

## Register all support components
dbLoadDatabase("dbd/feedback.dbd")
feedback_registerRecordDeviceDriver(pdbbase)

## Load iocAdmin record instances
dbLoadRecords("db/iocAdminRTEMS.db","IOC=IOC:SYS0:FB03")
dbLoadRecords("db/iocRelease.db","IOC=IOC:SYS0:FB03")
# load evr database
dbLoadRecords("db/IOC-SYS0-FB03.template")
# Load application
dbLoadRecords("db/fbckFB03.template")

## Setup autosave/restore will write to ioc-sys0-fb03 directory
set_requestfile_path("/data/autosave-req")
set_savefile_path("/data/autosave")

# this creates autosave status PVs named IOC:SYS0:FB03:xxx 
# to report autosave status
save_restoreSet_status_prefix("IOC:SYS0:FB03")
dbLoadRecords("db/save_restoreStatus.db", "P=IOC:SYS0:FB03")

# save restore setup: incomplete restore ok, backup file overwrites
save_restoreSet_IncompleteSetsOk(1)
save_restoreSet_DatedBackupFiles(0)


# evr hardware initialization
drvMrfErFlag=0
ErConfigure(0, 0, 0, 0, 1)
evrInitialize()

# =================================================================
# User defined environment vars
# 
# =================================================================
epicsEnvSet("LOCA_NAME","FB03",1)
epicsEnvSet("IOC_NAME","FB03",1)
epicsEnvSet("LOCA2_NAME", "FB03", 1)

## iocAdmin
epicsEnvSet("ENGINEER",D Fairley")
epicsEnvSet("LOCATION","ioc-b34-fb01")
epicsEnvSet("STARTUP","/usr/local/lcls/epics/iocCommon/ioc-b34-fb01")
epicsEnvSet("ST_CMD","startup.cmd")

fcomInit(fcomUtilGethostbyname(getenv("FCOMMCGRP"),0),1000)

iocshCmd("dbgrep 'FBCK:*:NAME' > /data/config/loops3.txt")

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

set_pass0_restoreFile("info_config_TR01.sav")
set_pass1_restoreFile("info_config_TR01.sav")
#set_pass0_restoreFile("info_config_TR02.sav")
#set_pass1_restoreFile("info_config_TR02.sav")
#set_pass0_restoreFile("info_config_TR03.sav")
#set_pass1_restoreFile("info_config_TR03.sav")
#set_pass0_restoreFile("info_config_TR04.sav")
#set_pass1_restoreFile("info_config_TR04.sav")
#set_pass0_restoreFile("info_config_TR05.sav")
#set_pass1_restoreFile("info_config_TR05.sav")
#set_pass0_restoreFile("info_config_LG01.sav")
#set_pass1_restoreFile("info_config_LG01.sav")
#set_pass0_restoreFile("info_config_GN01.sav")
#set_pass1_restoreFile("info_config_GN01.sav")
#set_pass0_restoreFile("info_config_GN02.sav")
#set_pass1_restoreFile("info_config_GN02.sav")

# Load configuration and invokes iocInit() internally
ffStart();

# initialize logging
iocLogInit()

## Start saving datasets
cd("/data/autosave-req")
makeAutosaveFileFromDbInfo("info_mon_FB03.req", "autosaveMonFB03")
create_monitor_set("info_mon_FB03.req",60,"")

makeAutosaveFileFromDbInfo("info_config_TR01.req", "autosaveConfigTR01")
create_triggered_set("info_config_TR01.req","FBCK:FB03:TR01:SAVE","")

#makeAutosaveFileFromDbInfo("info_config_TR02.req", "autosaveConfigTR02")
#create_triggered_set("info_config_TR02.req","FBCK:FB03:TR02:SAVE","")

#makeAutosaveFileFromDbInfo("info_config_TR03.req", "autosaveConfigTR03")
#create_triggered_set("info_config_TR03.req","FBCK:FB03:TR03:SAVE","")

#makeAutosaveFileFromDbInfo("info_config_TR04.req", "autosaveConfigTR04")
#create_triggered_set("info_config_TR04.req","FBCK:FB03:TR04:SAVE","")

#makeAutosaveFileFromDbInfo("info_config_TR05.req", "autosaveConfigTR05")
#create_triggered_set("info_config_TR05.req","FBCK:FB03:TR05:SAVE","")

#makeAutosaveFileFromDbInfo("info_config_LG01.req", "autosaveConfigLG01")
#create_triggered_set("info_config_LG01.req","FBCK:FB03:LG01:SAVE","")

#makeAutosaveFileFromDbInfo("info_config_GN01.req", "autosaveConfigGN01")
#create_triggered_set("info_config_GN01.req","FBCK:FB03:GN01:SAVE","")

##makeAutosaveFileFromDbInfo("info_config_GN02.req", "autosaveConfigGN02")
##create_triggered_set("info_config_GN02.req","FBCK:FB03:GN02:SAVE","")

