
##########################################################################
##########################################################################
##
##     __   __  _______  _______ 
##    |  |_|  ||    _  ||    ___|    LCLS
##    |       ||   |_| ||  |  __     -------------------------------------
##    |       ||    ___||  | [  |    Master
##    | ||_|| ||   |    |  |__| |    Pattern
##    |_|   |_||___|    |_______|    Generator
##
##
##########################################################################
##########################################################################

## MPG IOC RTEMS startup script

# For iocAdmin
setenv("ENGINEER","Kukhee Kim")
setenv("LOCATION","LI20 FACET")

# Load object, including RTEMS
chdir("../../")
ld("bin/RTEMS-beatnik/mpg.obj")



##########################################################################
# Configure udpComm
##########################################################################
# int lanIpSetup(char *ip_addr, char *netmask, int port, uint8_t *en_addr);
# 
# ip_addr:  string with IP address (dot notation) to assign to 2nd/dedicated interface
# netmask:  string with netmask (dot notation) for 2nd interface
#    port:  set to zero
# en_addr:  pointer to 6 bytes with ethernet addr for interface; may be NULL to
#           indicate that address from EEPROM is to be used.
# 
# RETURNS:  Zero on success, nonzero on failure.
#
#    NOTE:  On the 6100, there is no way to specify which ethernet port (port 1 or 2) to
#           use.  It always uses the port that is not used by the BSD stack already.
#           Since the first port is already in use, udpComm uses port 2.

# We are not going to use MPS and 2nd NIC..
#lanIpSetup("192.168.0.128", "255.255.255.0", 0, 0)



##########################################################################
# Set Environment Variables
##########################################################################

# Configure IOC path and EPICS CA
epicsEnvSet("IOCPATH", getcwd())
epicsEnvSet("EPICS_CA_MAX_ARRAY_BYTES", "80000000")



##########################################################################
# Load DBs and perform all registration and envoke all registrars
##########################################################################

dbLoadDatabase("dbd/mpg.dbd")
mpg_registerRecordDeviceDriver(pdbbase) 

## Start the IOC log client
iocLogInit()

# Allows gdb to attach to this target
#rtems_gdb_start(0,0)

# Load record instances
dbLoadRecords("db/evgLI20.db")
dbLoadRecords("db/evgPatternLI20.db")
dbLoadRecords("db/iocAdminRTEMS.db","IOC=IOC:SYS1:EV01")
dbLoadRecords("db/iocRelease.db","IOC=IOC:SYS1:EV01")
#dbLoadRecords("db/eventCodeSequence.db","IOC=IOC:SYS1:EV01")
#dbLoadRecords("db/bykikControl.db","IOC=IOC:SYS1:EV01")

#dbRestore("/data/IOC-IN20-EV01.sav",0,-1)

## Load database for autosave/restore status pv's
#dbLoadRecords("db/save_restoreStatus.db","P=IOC:SYS1:EV01:")


## autosave/restore settings
#save_restoreSet_NFSHost(getenv("NFS_FILE_SYSTEM"), iocData, "/data")
save_restoreSet_status_prefix( "IOC:SYS1:EV01:")
save_restoreSet_IncompleteSetsOk(1)
save_restoreSet_DatedBackupFiles(1)

set_requestfile_path("/data", "autosave-req")
set_requestfile_path("./")
set_savefile_path("/data","autosave")

## ==============================================
## New method for autosave and restore
#  Decide what is saved by specifying it in the
#  EPICS databases
## =============================================
set_pass0_restoreFile("IOC-SYS1-EV01.sav")
set_pass1_restoreFile("IOC-SYS1-EV01.sav")

## =============================================




##########################################################################
# Configure MPG
##########################################################################

mpgEventModulo36Disable()

bspExtVerbosity=0

EgConfigure(0,0x280000,0)

# From Till Straumann (for RTEMS 4.9.1 upgrade):
# This should set the VME chip into a mode where it 
# surrenders the VME bus after every transaction.
# This means that the master has to rearbitrate for the bus for every cycle 
# (which slows things down).
#
# The faster setting I had enabled would let the master hold on to the bus 
# until some other master requests it.
*(long*)(vmeTsi148RegBase + 0x234) &= ~ 0x18

# Set Cexp priority one lower than pnetTask.
Cexp_priority_orig = 100
rtems_task_set_priority(0, 101, &Cexp_priority_orig)



##########################################################################
# List Modules
##########################################################################

# Everything is loaded.  Call lsmod() for debug purposes.

lsmod()



##########################################################################
# Aliases
##########################################################################

# RTEMS/EPICS
#rr = exit
threads = epicsThreadShowAll

# Sequences
ecsr = ecsReport



##########################################################################
# iocInit
##########################################################################

iocInit() # after h/w initialisations



##########################################################################
# Additional Setup
##########################################################################

bspExtVerbosity=1

dbpf("EVG:SYS1:EV01:CTRL.DESC","Production")
dbpf("IOC:SYS1:EV01:EDEFNAME.DESC","Production")


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

create_monitor_set("IOC-SYS1-EV01.req", 10, "")



# eDef reservation sequence
seq(&edefMask,"SYS_PNBN=SYS1,SYS=SYS1,IOC=IOC:SYS1:EV01")
seq(&edefReserve,"SYS=SYS1,IOC=IOC:SYS1:EV01")



