## PAD RTEMS startup script for klystron 21-1 TST PAD in RF Hut

# Set some abbreviations - looks redundant with ssrlApps/m68k-rtems/uC5282/bin/st.sys
ld    = cexpModuleLoad
unld  = cexpModuleUnload

printf("%s\n", getenv("PATH"))

# append /afs/slac/package/rtems/target/ssrlApps/m68k-rtems/uC5282/bin/rtems-gdb-stub.obj to path
buf    = malloc(2000)
(opath = getenv("PATH")) || (opath = "")
strcpy(buf,opath)
strcat(buf,":/boot/package/rtems/target/ssrlApps/m68k-rtems/uC5282/bin:")
setenv("PATH",buf,1)
free(buf)
buf    = 0
printf("%s\n", getenv("PATH"))
# load gdb stub
ld("rtems-gdb-stub.obj")

# load our app
# 3/16/7 Use pad-gen to get scaled (down) averages
ld("bin/pad-gen.obj") 

# nfsMount the filesystem for output data
#This configuration works while the PAD is on IFZ 134.79.21[6-9] subnet
# nfsMount("[uid.gid@]hostip", "path on host", "local mountpoint")
nfsMount("8412.2211.@134.79.127.51","/vol/vol2/g.lcls.dev/epics/ioc/data/LLRF/PAD/TST","/data")

## Set common environment variables. eg. EPICS_TS_NTP_INET and PORTs
cexpsh("../../../config/epicsEnvSetDev")
# PAD needs to be able to see VME. only.
epicsEnvSet("EPICS_CA_ADDR_LIST","134.79.219.124")
epicsEnvSet("EPICS_CA_AUTO_ADDR_LIST","NO")

## Set app-specific environment variable for the PAD
epicsEnvSet ("EPICS_CA_MAX_ARRAY_BYTES", "600000")

## Register all support components
dbLoadDatabase("dbd/pad.dbd")
pad_registerRecordDeviceDriver(pdbbase) 

## Load record instances - use TST macros to avoid creating ambiguous records
dbLoadTemplate("db/padTST.gen102_template")

## Set to 2 so that we see errors
devLongoutPadFlag=2
devLonginPadFlag=2
devWaveformPadFlag=2
devMbboPadFlag=2
devBoPadFlag=2
drvPadFlag=2
drvPadCfgFlag=2
drvPadScalFlag=2
drvPadIntrFlag=2


iocInit()

# lower flags from 4 to 2 if too verbose for processing

# force scalar waveforms to read (once)
dbpf("LAB:205:1:TST_0_SCL_WF.PROC","1")
dbpf("LAB:205:1:TST_1_SCL_WF.PROC","1")
dbpf("LAB:205:1:TST_2_SCL_WF.PROC","1")
dbpf("LAB:205:1:TST_3_SCL_WF.PROC","1")

# if testing by hand, stop some stuff from processing
#dbpf("LAB:205:1:TST_0.SCAN", "Passive")
#dbpf("LAB:205:1:TST_1.SCAN", "Passive")
#dbpf("LAB:205:1:TST_2.SCAN", "Passive")
#dbpf("LAB:205:1:TST_3.SCAN", "Passive")
#dbpf("LAB:205:1:TST_4.SCAN", "Passive")
#dbpf("LAB:205:1:TST_5.SCAN", "Passive")
#dbpf("LAB:205:1:TST_6.SCAN", "Passive")
#dbpf("LAB:205:1:TST_7.SCAN", "Passive")

#override what's set for the offset to the start of the data here
#padSetSampleOffset(channel, sample, n) usage: sample is the sample number (0 for most, can be 1 if BPH)
#                                     n is the number of 16 bit words to read 
#                                     after first 3 clock cycless. 
dbpf("LAB:205:1:TST_0_OFST","20")
dbpf("LAB:205:1:TST_1_OFST","20")
dbpf("LAB:205:1:TST_2_OFST","20")
dbpf("LAB:205:1:TST_3_OFST","20")
dbpf("LAB:205:1:TST_4_OFST","0")
dbpf("LAB:205:1:TST_5_OFST","0")
dbpf("LAB:205:1:TST_6_OFST","0")
dbpf("LAB:205:1:TST_7_OFST","0")

#override what's set for the size of the data here
#padSetSampleSize(channel, sample, n) usage: sample is the sample number (0 for most, can be 1 if BPH)
#                                   n must be a multiple of 4 since 
#                                   data in quadruples of (Q1,I2,Q3,I4) read 
#                                   (see comments in drvPad.c for more info)
dbpf("LAB:205:1:TST_0_SIZE","80")
dbpf("LAB:205:1:TST_1_SIZE","80")
dbpf("LAB:205:1:TST_2_SIZE","80")
dbpf("LAB:205:1:TST_3_SIZE","80")
dbpf("LAB:205:1:TST_4_SIZE","65536")
dbpf("LAB:205:1:TST_5_SIZE","65536")
dbpf("LAB:205:1:TST_6_SIZE","65536")
dbpf("LAB:205:1:TST_7_SIZE","65536")

padSetDataFileStem("dayle") 	# The stem to start all data files with
                                # Format is "<stem><secPastEpoch>.dat"

# change directory to local mountpoint so that files don't need path to dir in their name
cd("/data") # for lcls-dev
#cd("/mntPt") # for klyron

rtems_gdb_start(0,0)

## Start any sequence programs. Note: RTEMS Cexp syntax
# Skip state machine for nonlin test
seq(&sncpad,"PRIM=LAB,LOCA=205,UNIT=1,PAD=TST") 
seq(&trgDetector, "PRIM=LAB,LOCA=205,UNIT=1,PAD=TST")

# for non-lin test
#padSetOperationMode(1)
#dbpf("LAB:205:1:TST_D_CALIB","1")
#padDisableIntr()
#padNonlin(1000)

