# LLRF RTEMS startup script for VME crate for B34 RF01 IOC
# BSA capable

# IOC-B34-RF01 startup script for ASTA LLRF production

setenv("LOCN","B044-152")	                # Rack B044-152 Level
setenv("IOC_MACRO","IOC=IOC:B34:RF01")

setenv("LOCAs","B34",1)
setenv("UNITs","RF01")

# for save-restore
setenv("AUTOSAVE_PREFIX","IOC:B34:RF01:")
setenv("AUTOSAVE_MACRO","P=IOC:B34:RF01:")

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

# execute generic part
. "../st.vmegeneric.cmd"

# Load ASTA VME IOC
ld("bin/RTEMS-beatnik/llrf6RF10App.obj")

lsmod()

epicsEnvSet("EPICS_CA_MAX_ARRAY_BYTES","1000000")

# Register all support components
dbLoadDatabase("dbd/llrf6RF10App.dbd")
llrf6RF10App_registerRecordDeviceDriver(pdbbase)

# hack around the EPICS memory tester
free(malloc(1024*1024*32))

# 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

###########################
# initialize all hardware #
###########################

bspExtVerbosity=0

# Init PMC EVR
ErConfigure(0, 0, 0, 0, 1)
# ErConfigure( 0,0x300000,0x60,4,0)      # VME EVR:B34:RF01

evrInitialize()
bspExtVerbosity = 1

# =============================================================
# Setup/Initialize Charging ADC:
#
# =============================================================
# First arg is index you want to assign to this ADC
# Second is Base Address
# Third is Address Space (24 or 32 only)
# Fourth is interrupt vector
# Fifth is interrupt level
# Sixth is number of samples per cycle, 1 trigger per sample

caenV965Config( 0, 0x00A00000, 24, 90, 3, 0)

# Load EVR and Pattern databases
dbLoadRecords("db/IOC-B34-RF01evr.db","EVR=EVR:B34:RF01,CRD=0")	# EVR CRD 0
# dbLoadRecords("db/IOC-B34-RF01evr.db","EVR=EVR:B34:RF02,CRD=1")	# EVR CRD 1
dbLoadRecords("db/lclsPattern.db","IOC=IOC:B34:RF01",0)

# bspExtMemProbe only durint init. clear this to avoid the lecture.
bspExtVerbosity = 0

########################################################################
# BEGIN: Load the record databases
#######################################################################
# Load iocAdmin databases to support IOC Health and monitoring
# =====================================================================

# Load standard databases
. "iocBoot/st.vmedb.cmd"

# Load BSA database
dbLoadRecords("db/IOC-B34-RF01bsa.db",0)

# Load trigger database
dbLoadRecords("db/IOC-B34-RF01trig.db")

dbLoadRecords("db/IOC-B34-RF01adc.db")			# Caen QADC for Faraday Cup measurements

# END: Loading the record databases
# =====================================================================
# Setup autosave/restore
# =====================================================================

. "iocBoot/init_restore.cmd"

# Capture load addresses of all modules (essential for debugging if iocInit crashes...)

lsmod()

# =====================================================================
# Start the EPICS IOC
# =====================================================================

iocInit()

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

# Generate the autosave PV list (Takes a long time)
#chdir("/data/autosave-req")
#iocshCmd("makeAutosaveFiles()")

# Start autosave process:

. "iocBoot/start_restore.cmd"

epicsEnvShow()

nvramConfigShow()

bootConfigShow()

# dbl()

# Start rtems spy utility:
#iocshCmd("spy(2)")
