
##########################################################################
##########################################################################
##
##     __   __  _______  _______ 
##    |  |_|  ||    _  ||    ___|    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/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")
dbLoadRecords("db/VMTG.db","prefix=IOC:IN20:EV01")

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

ipacAddXy9660("0x1000,0")
initIP440("IP440",0,1,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)



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

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