##########################################################################
##########################################################################
##
##     __   __  _______  _______ 
##    |  |_|  ||    _  ||  _____|        LCLS
##    |       ||   |_| || |_____         ---------------------------------
##    |       ||    ___||_____  |        Machine
##    | ||_|| ||   |     _____| |        Protection
##    |_|   |_||___|    |_______| BSA    System BSA Processor 
##
##
##########################################################################
##########################################################################

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

ld("lanIpBasic_mve.obj") 

ld("../../bin/RTEMS-beatnik/mpsBSAProcessor.obj")

# Grab and free memory to get around EPICS memory bug.
free(malloc(80000000))

##########################################################################
# Configure udpComm
##########################################################################

# mpsLanIpBscConfigWrapper -- setup Tx and Rx rings and buffer
#
# Be sure to configure enough resources for a large application
#
# numberOfSatellites
# ---------------------------
# Default to at least 20
#
# N_SATELLITES = 20
#
#
# rxRingSize
# ---------------------------
# Must be greater than number of 'simultaneously' arriving packets -- the number of
# satellite devices we receive data from plus some overhead.
#
# RX_RING_SIZE = N_SATELLITES + (N_SATELLITES / 4) + 1
# 
# 
# numberOfRBufs
# ---------------------------
# The ring needs bufs.  An application might keep some RX data around.
# Configure enough bufs so that 20 packets per satellite device could be kept.
#
# NRBUFS = RX_RING_SIZE + (RX_RING_SIZE * 20) + 20
#
#
# txRingSize
# ---------------------------
# Set to be a fraction of the number of satellites.
# 
# TX_RING_SIZE = (N_SATELLITES / 4) + 1
#
#so we configure our lanIpBscConfig with 128 SATELLITES as follows:
#mpsLanIpBscConfigWrapper(N_SATELLITES, RX_RING_SIZE, NRBUFS, TX_RING_SIZE)
#
mpsLanIpBscConfigWrapper(128,161,3401,129)
lanIpBscDumpConfig(0)

# 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.2", "255.255.255.0", 0, 0)

# Init PMC EVR
ErConfigure(0, 0x000000, 0x00, 0, 1)
evrInitialize()

# Increase queue sizes
callbackSetQueueSize(16000)
scanOnceSetQueueSize(8000)

cd("../../")

## Register all support components
dbLoadDatabase("dbd/mpsBSAProcessor.dbd",0,0)
mpsBSAProcessor_registerRecordDeviceDriver(pdbbase) 


mpsBSAReceiverCreate("/boot/g/lcls/vol8/epics/iocTop/MachineProtection/mpsBSAProcessor/iocBoot/iocmpsBSAProcessor/linknodeConf.txt")

## Load record instances
dbLoadRecords("db/mpsBSA.db")
dbLoadRecords("db/mpsBSABLMAll.db")
dbLoadRecords("db/mpsBSAPICAll.db")

## Timing record instances
dbLoadRecords("db/mpsTimingEvr.db","EVR=EVR:B34:MP02,SYS=SYS0")
dbLoadRecords("db/mpsTimingEvrPattern.db","IOC=IOC:B34:MP02,SYS=SYS0")

iocInit()

## Start any sequence programs

