20180516a
- JSON Server - version un0_e
import spidev
import RPi.GPIO as GPIO
import time
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import json
import time
matplotlib.use('Agg')
BigJS0N = {}
BigJS0N["firmware_md5"]="fa6a7560ade6d6b1149b6e78e0de051f"
BigJS0N["firmware_version"]="e_un0"
BigJS0N["data"]=[]
BigJS0N["registers"]={}
BigJS0N["parameters"]={}
def CreateDACCurve(Deb,Fin,CurveType):
n = 200/5
DACValues = []
for k in range(n+1):
if CurveType:
val = int(Deb+1.0*k*(Fin-Deb)/n)
else:
val = int((Fin-Deb)*k**3/n**3+Deb)
DACValues.append(val)
DACValues[-1] = 0
DACValues[-2] = 0
return DACValues,len(DACValues)
def SetDACCurve(spi,DACValues):
if len(DACValues) < 43:
for i in range(len(DACValues)):
if (DACValues[i] >= 0) and (DACValues[i] < 1020):
WriteFPGA(spi,16+i,DACValues[i]/4)
else:
WriteFPGA(spi,16+i,0)
return 0
def WriteFPGA(spi,adress,value):
spi.xfer([0xAA] )
spi.xfer([adress] )
spi.xfer([value] )
BigJS0N["registers"][int(adress)]=value
def StartUp():
GPIO.setmode(GPIO.BCM)
PRESET = 23
IO4 = 26
CS_FLASH = 7
GPIO.setup(CS_FLASH,GPIO.OUT)
GPIO.output(CS_FLASH,GPIO.LOW)
GPIO.setup(PRESET,GPIO.OUT)
GPIO.setup(IO4,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print "Reset GPIO 23 - Low 1s"
GPIO.output(PRESET,GPIO.LOW)
time.sleep(3)
print "Reset GPIO 23 - High 0.2s"
GPIO.output(PRESET,GPIO.HIGH)
time.sleep(0.2)
spi = spidev.SpiDev()
spi.open(0,0)
spi.mode = 0b01
print "spi.cshigh is " + str(spi.cshigh)
print "spi mode is " + str(spi.mode)
spi.max_speed_hz = 2000000
print "spi maxspeed is "+str(spi.max_speed_hz)+"hz"
return spi
def TestSPI(spi,ncycles):
i = 0
while i < ncycles:
WriteFPGA(spi,0xEB,0x01)
time.sleep(0.5)
WriteFPGA(spi,0xEB,0x00)
time.sleep(0.5)
i = i+1
def LoopSPI(spi):
while 1:
WriteFPGA(spi,0xEB,0x01)
WriteFPGA(spi,0xEB,0x00)
def LoopAcq(spi):
while 1:
WriteFPGA(spi,0xEB,0x00)
WriteFPGA(spi,0xEF,0x01)
WriteFPGA(spi,0xEA,0x01)
time.sleep(0.001)
def ClearMem(spi):
WriteFPGA(spi,0xEF,0x01)
def ConfigSPI(spi):
setPon(200,spi)
setPulsesDelay(100,spi)
setPoff(2000,spi)
WriteFPGA(spi,0xEC,0x33)
setDeltaAcq(7000,spi)
WriteFPGA(spi,0xEB,0x00)
WriteFPGA(spi,0xED,0x03)
WriteFPGA(spi,0xEE,0xA0)
print "Config FPGA done!"
def setDACConstant(mV,spi):
if mV > 1000:
mV = 1000
elif mV < 0:
mV = 0
hmV = mV/4
print "Gain:", mV," mV -- ",hex(hmV)
WriteFPGA(spi,0xEC,hmV)
def setPon(POn,spi):
if POn > 2500:
POn = 2500
elif POn < 0:
POn = 0
HPon = POn* 128 / 1000
BigJS0N["parameters"]["Pon"] = int(POn)
print "Pulse width:", POn," ns -- ",hex(HPon)
WriteFPGA(spi,0xE0,HPon)
def setPulsesDelay(DeltaPP,spi):
if DeltaPP > 2500:
DeltaPP = 2500
elif DeltaPP < 0:
DeltaPP = 0
HPP =DeltaPP * 128 / 1000
BigJS0N["parameters"]["PulsesDelay"] = int(DeltaPP)
print "Pulses delay:", DeltaPP," ns -- ",hex(HPP)
WriteFPGA(spi,0xD0,HPP)
def setPoff(sEEPoff,spi):
POff = sEEPoff * 128 / 1000
POffMSB, POffLSB = 0x00FF&POff/256,0x00FF&POff
print "Poff:", sEEPoff," ns -- ",hex(POffMSB),hex(POffLSB)
BigJS0N["parameters"]["Poff"] = int(sEEPoff)
WriteFPGA(spi,0xE1,POffMSB)
WriteFPGA(spi,0xE2,POffLSB)
def setDeltaAcq(DeltaAcq,spi):
if DeltaAcq > 255*255:
DeltaAcq = 254*254
elif DeltaAcq < 0:
DeltaAcq = 0
hDA = DeltaAcq * 128 / 1000
hDAMSB, hDALSB = hDA/255 , 0x00FF&hDA
print "Delay between:",DeltaAcq,"ns -- ", hex(hDAMSB),hex(hDALSB)
BigJS0N["parameters"]["DeltaAcq"] = int(DeltaAcq)
WriteFPGA(spi,0xE3,hDAMSB)
WriteFPGA(spi,0xE4,hDALSB)
def SetLengthAcq(LAcqI,spi):
LAcq = LAcqI * 128 / 1000
BigJS0N["parameters"]["LengthAcq"] = int(LAcqI)
LAcqMSB, LAcqLSB = 0x00FF&LAcq/256 , 0x00FF&LAcq
print "Acquisition length: ", LAcq, " ns -- ",hex(LAcqMSB),hex(LAcqLSB)
WriteFPGA(spi,0xE5,LAcqMSB)
WriteFPGA(spi,0xE6,LAcqLSB)
def setPeriodAcq(lEPeriod,spi):
lEPNs = lEPeriod*128/1000
EPNsMSB, EPNs, EPNsLSB = 0x00FF&lEPNs/(256*256),0x00FF&lEPNs/256,0x0000FF&lEPNs
print "Period between two acquisitions:", lEPeriod,"us --", hex(EPNsMSB),hex(EPNs),hex(EPNsLSB)
BigJS0N["parameters"]["PeriodAcq"] = int(lEPeriod)
WriteFPGA(spi,0xE7,EPNsMSB)
WriteFPGA(spi,0xE8,EPNs)
WriteFPGA(spi,0xE9,EPNsLSB)
def setPulseTrain(Pon,Pdelay,Poff,DelayAcq,Acq,spi):
setPon(Pon,spi)
setPulsesDelay(Pdelay+Pon,spi)
setPoff(Poff+Pdelay+Pon,spi)
setDeltaAcq(DelayAcq+Poff+Pdelay+Pon,spi)
SetLengthAcq(Acq+DelayAcq+Poff+Pdelay+Pon,spi)
spi = StartUp()
print("------")
ConfigSPI(spi)
print("------")
t1 = 200
t2 = 95
t3 = 2000
t4 = 7000
t5 = 130000
setPulseTrain(t1,t2,t3,t4,t5,spi)
N = 0
/usr/local/lib/python2.7/dist-packages/ipykernel_launcher.py:56: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
Reset GPIO 23 - Low 1s
Reset GPIO 23 - High 0.2s
spi.cshigh is False
spi mode is 1
spi maxspeed is 2000000hz
------
Pulse width: 200 ns -- 0x19
Pulses delay: 100 ns -- 0xc
Poff: 2000 ns -- 0x1 0x0
Delay between: 7000 ns -- 0x3 0x80
Config FPGA done!
------
Pulse width: 200 ns -- 0x19
Pulses delay: 295 ns -- 0x25
Poff: 2295 ns -- 0x1 0x25
Delay between: 9295 ns -- 0x4 0xa5
Acquisition length: 17829 ns -- 0x45 0xa5
BigJS0N
{'data': [],
'firmware_md5': 'fa6a7560ade6d6b1149b6e78e0de051f',
'firmware_version': 'e_un0',
'parameters': {'DeltaAcq': 9295,
'LengthAcq': 139295,
'Poff': 2295,
'Pon': 200,
'PulsesDelay': 295},
'registers': {208: 37,
224: 25,
225: 1,
226: 37,
227: 4,
228: 165,
229: 69,
230: 165,
235: 0,
236: 51,
237: 3,
238: 160}}
Curve = CreateDACCurve(0,1000,True)[0]
print Curve,len(Curve)
SetDACCurve(spi,Curve)
[0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 525, 550, 575, 600, 625, 650, 675, 700, 725, 750, 775, 800, 825, 850, 875, 900, 925, 950, 0, 0] 41
0
N=0
spi.max_speed_hz = 2000000
BigJS0N["N"] = N
BigJS0N["V"]="25"
f = 0x00
t1 = 70
t2 = 100
t3 = 2000
t4 = 300-t1-t2+10
t5 = 200000
Curve = CreateDACCurve(200,600,True)[0]
SetDACCurve(spi,Curve)
BigJS0N["experiment"]={}
BigJS0N["experiment"]["id"] = "20180516a"
BigJS0N["experiment"]["description"]="Classical experiment with calibration piezo"
setPulseTrain(t1,t2,t3,t4,t5,spi)
LAcq = t5/1000
WriteFPGA(spi,0xED,f)
WriteFPGA(spi,0xEB,0x00)
WriteFPGA(spi,0xEC,0x03)
if BigJS0N["registers"][235]:
NLines = BigJS0N["registers"][236]
else:
NLines = 1
BigJS0N["registers"][236] = 1
print "Lines number: "+str(NLines)
WriteFPGA(spi,0xEF,0x01)
WriteFPGA(spi,0xEA,0x01)
Fech = int(64/((1+f)))
Nacq = LAcq * Fech * NLines
print Fech, "-> "+str(Nacq) + ' samples'
A = []
for i in range(2*Nacq+1):
A.append ( spi.xfer([0x00] )[0] )
a = np.asarray(A).astype(int)
BigJS0N["data"] = A
BigJS0N["experiment"]["target"] = "calibration rig"
BigJS0N["experiment"]["position"] = "0"
print N
with open(BigJS0N["experiment"]["id"]+'-'+str(N)+".json", 'w') as outfile:
json.dump(BigJS0N, outfile)
N = N+1
print "Done"
Pulse width: 70 ns -- 0x8
Pulses delay: 170 ns -- 0x15
Poff: 2170 ns -- 0x1 0x15
Delay between: 2310 ns -- 0x1 0x27
Acquisition length: 25895 ns -- 0x65 0x27
Lines number: 1
64 -> 12800 samples
6
Done
BigJS0N["parameters"].keys()
['Pon', 'PulsesDelay', 'Poff', 'LengthAcq', 'DeltaAcq']
Curve
[200,
210,
220,
230,
240,
250,
260,
270,
280,
290,
300,
310,
320,
330,
340,
350,
360,
370,
380,
390,
400,
410,
420,
430,
440,
450,
460,
470,
480,
490,
500,
510,
520,
530,
540,
550,
560,
570,
580,
0,
0]