
##########################################################################
##########################################################################
##
##     __   __  _______  _______ 
##    |  |_|  ||    _  ||    ___|    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

# 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/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")



##########################################################################
# 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)



##########################################################################
# 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:IN20:EV01:CTRL.DESC","Development")
dbpf("IOC:IN20:EV01:EDEFNAME.DESC","Development")
# eDef reservation sequence
seq(&edefMask,"SYS=SYS0,IOC=IOC:IN20:EV01")
seq(&edefReserve,"SYS=SYS0,IOC=IOC:IN20:EV01")


#####################################################
# 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")

