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

## MPG IOC RTEMS startup script

setenv("EPICS_CA_ADDR_LIST", "mccdev")

# For iocAdmin
setenv("ENGINEER","Stephanie Allison")
setenv("LOCATION","B34 Second Floor Timing Test Area")

# Set common environment variables. eg. EPICS_TS_NTP_INET and PORTs
< /boot/g/lcls/epics/iocCommon/All/Dev/pre_st.cmd

ld("lanIpBasic_mve.obj")

# Load object
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.

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/mpgTmskAll.db")
dbLoadRecords("db/evgIN20.db")
dbLoadRecords("db/evgPatternIN20.db")
dbLoadRecords("db/iocAdminRTEMS.db","IOC=IOC:IN20:EV01")
dbLoadRecords("db/iocRelease.db","IOC=IOC:IN20:EV01")
dbLoadRecords("db/eventCodeSequence.db","IOC=IOC:IN20:EV01")
dbLoadRecords("db/bykikControl.db","IOC=IOC:IN20:EV01")
dbLoadRecords("db/pabig.db","IOC=IOC:IN20:EV01, LOC=IN20")
# The 'intidly' can probably always stay at 570us this is
# about 150uS after PNET. Modify (increase) this only if you need 
# more time for MPS processing.
#
# 'tszddly' depends on the relation of the VMTG's TS0 detector
# (and hence the phase of the wall-plug transformer) and PNET's TS0.
# For a different instance of PABIG/EVG you may have to re-tune this
# (from pabig control panel).
dbLoadRecords("db/VMTG.db","prefix=IOC:IN20:EV01,intidly=570,tszddly=5655")

##########################################################################
# Configure IPAC
##########################################################################

ipacAddXy9660("0x1000,0")
initIP440("IP440",0,0,50)


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

#mpsCommunicationInitTS(1,4,15)

bspExtVerbosity=0

EgConfigure(0,0x280000,0)

evrTimeFlag=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)


## autosave/restore settings
#save_restoreSet_NFSHost(getenv("NFS_FILE_SYSTEM"), iocData, "/data")
save_restoreSet_status_prefix( "IOC:IN20: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_pass1_restoreFile("pabig_settings.sav")
set_pass0_restoreFile("vmtg_settings.sav")
set_pass0_restoreFile("evg_settings.sav")
set_pass1_restoreFile("mpgTmsk_setting.sav")

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

## Load Access Security File
asSetFilename("db/evgAccessControl.acf")




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

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

lsmod()



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

# RTEMS/EPICS
#rr = exit
threads = epicsThreadShowAll

# Sequences
ecsr = ecsReport


getenv("ACF_INIT") && cexpsh( getenv("ACF_INIT"), 0 )

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

iocInit() # after h/w initialisations


# =====================================================
# Turn on caPutLogging:
# Log values only on change to the iocLogServer:
caPutLogInit("172.27.8.31:7004")
caPutLogShow(2)
# =====================================================




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

bspExtVerbosity=1

dbpf("EVG:IN20:EV01:CTRL.DESC","Development")
dbpf("IOC:IN20:EV01:EDEFNAME.DESC","Development")
# eDef reservation sequence
seq(&edefMask,"SYS_PNBN=MP00,SYS=SYS0,IOC=IOC:IN20:EV01")
seq(&edefReserve,"SYS=SYS0,IOC=IOC:IN20:EV01")

# <<<<<<< st.cmd.XL15
# PABIG early testing: bypass internal rate
# generation; copy everything from PNET.
dbpf("IOC:IN20:EV01:PNET_MASK_MOD1","-1")
dbpf("IOC:IN20:EV01:PNET_MASK_MOD2","-1")
dbpf("IOC:IN20:EV01:PNET_MASK_MOD3","-1")
dbpf("IOC:IN20:EV01:PNET_MASK_MOD4","-1")

# PABIG early testing: auto-sync to PNET
dbpf("IOC:IN20:EV01:PNET_MODE","AUTO")

# VMTG testing
# enable AuxDin[3]
#dbpf("IOC:IN20:EV01:CSR2_DES","32768")
# free-run ADC, enable (0x8001)
#dbpf("IOC:IN20:EV01:CSR1_DES","32769")
# TSE, TSG, IRE, ARM ENA: 0x1c0c
#dbpf("IOC:IN20:EV01:CSR0_DES","7180")
=======

#####################################################
# Temporal solution to avoid ECS range corruptsions #
# Need to remove it, after switch to autosave       #
#####################################################
dbpf("IOC:IN20:EV01:ECS_MINEC_1", "67")
dbpf("IOC:IN20:EV01:ECS_MINEC_2", "83")
dbpf("IOC:IN20:EV01:ECS_MINEC_3", "167")
dbpf("IOC:IN20:EV01:ECS_MINEC_4", "183")
#
dbpf("IOC:IN20:EV01:ECS_MAXEC_1", "82")
dbpf("IOC:IN20:EV01:ECS_MAXEC_2", "98")
dbpf("IOC:IN20:EV01:ECS_MAXEC_3", "182")
dbpf("IOC:IN20:EV01:ECS_MAXEC_4", "198")

# >>>>>>> 1.14.2.1


## ===========================================================
## 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("pabig_settings.req", 30, "P=IOC:IN20:EV01")
create_monitor_set("vmtg_settings.req", 30, "P=IOC:IN20:EV01")
create_monitor_set("evg_settings.req", 30, "")
create_monitor_set("mpgTmsk_setting.req", 30, "")

