20180430a - JSON Server - version p

#!/usr/bin/python
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"]="49ed003fae3a312590fe99a2fa90392c"
BigJS0N["firmware_version"]="p"
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: # to correct
        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)
            #print 16+i,len(DACValues)
    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 = 25
    IO4 = 26
    GPIO.setup(PRESET,GPIO.OUT)
    GPIO.setup(IO4,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    print "Reset 25 - Low 1s"
    GPIO.output(PRESET,GPIO.LOW)
    time.sleep(3)
    print "Reset 25 - High 0.2s"
    GPIO.output(PRESET,GPIO.HIGH)
    time.sleep(0.2)

    spi = spidev.SpiDev()
    spi.open(0,1) # CS2 - FPGA, on CE1 = IO4
    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) # 0: single mode 1 continious mode
        time.sleep(0.5)
        WriteFPGA(spi,0xEB,0x00) # 0: single mode 1 continious mode
        time.sleep(0.5)  
        i = i+1

def LoopSPI(spi):
    while 1:
        WriteFPGA(spi,0xEB,0x01) # 0: single mode 1 continious mode
        WriteFPGA(spi,0xEB,0x00) # 0: single mode 1 continious mode

def LoopAcq(spi):
    while 1:
        WriteFPGA(spi,0xEB,0x00) # Doing 1 shot 
        WriteFPGA(spi,0xEF,0x01) # Cleaning memory pointer
        WriteFPGA(spi,0xEA,0x01) # Software Trig : As to be clear by software
        time.sleep(0.001) # sleep 1ms

def ClearMem(spi):
    WriteFPGA(spi,0xEF,0x01) # To access memory

def ConfigSPI(spi):
    # Setup FPGA values by default
    setPon(200,spi)              # Set PulseOn
    setPulsesDelay(100,spi)      # Set Lengh between Pon and Poff: 100ns
    setPoff(2000,spi)            # Setting Poff 2us
    #setDACConstant(20,spi)   # gain at 20mV (2%)
    WriteFPGA(spi,0xEC,0x33)
    setDeltaAcq(7000,spi)    # 7us
    #WriteFPGA(spi,0xEA,0x00) # Software Trig : As to be clear by software
    WriteFPGA(spi,0xEB,0x00) # 0: single mode 1 continious mode
    WriteFPGA(spi,0xED,0x03) # Frequency of ADC acquisition / sEEADC_freq (3 = 16Msps, 1 = 32, 0 = 64, 2 = 21Msps)
    WriteFPGA(spi,0xEE,0xA0) # How many cycles in countinious mode
    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) # Voltage gain control: 0V to 1V

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) # set sEEPon

def setPulsesDelay(DeltaPP,spi):
# Set Lengh between Pon and Poff
    if DeltaPP > 2500:
        DeltaPP = 2500
    elif DeltaPP < 0:
        DeltaPP = 0
    HPP =DeltaPP * 128 / 1000
    #print  hex(HPP)
    BigJS0N["parameters"]["PulsesDelay"] = int(DeltaPP)
    print "Pulses delay:", DeltaPP," ns -- ",hex(HPP)
    WriteFPGA(spi,0xD0,HPP) # set sEEPon

def setPoff(sEEPoff,spi):
    # Sets the damping length.
    POff = sEEPoff * 128 / 1000
    #print sEEPoff,POff
    POffMSB, POffLSB = 0x00FF&POff/256,0x00FF&POff 
    print "Poff:", sEEPoff," ns -- ",hex(POffMSB),hex(POffLSB)
    BigJS0N["parameters"]["Poff"] = int(sEEPoff)
    WriteFPGA(spi,0xE1,POffMSB) # set sEEPon MSB
    WriteFPGA(spi,0xE2,POffLSB) # set sEEPon LSB

    # Setting Poff to Acq delay sEEDelayACQ
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) # set sEEPon MSB
    WriteFPGA(spi,0xE4,hDALSB) # set sEEPon LSB

def SetLengthAcq(LAcqI,spi):
    LAcq = LAcqI * 128 / 1000
    #print LAcq,hex(LAcq),hex(LAcqI)
    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) # set sEEPon MSB
    WriteFPGA(spi,0xE6,LAcqLSB) # set sEEPon LSB

def setPeriodAcq(lEPeriod,spi):
    lEPNs = lEPeriod*128/1000 #ns
    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) # Period of one cycle MSB
    WriteFPGA(spi,0xE8,EPNs) # Period of one cycle 15 to 8
    WriteFPGA(spi,0xE9,EPNsLSB) # Period of one cycle LSB

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)
    #SetLengthAcq(Acq,spi)
/usr/lib/python2.7/dist-packages/matplotlib/__init__.py:1314: UserWarning:  This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

  warnings.warn(_use_error_msg)
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:54: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.


Reset 25 - Low 1s
Reset 25 - 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': '49ed003fae3a312590fe99a2fa90392c',
 'firmware_version': 'p',
 '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] # Beginning, Ending, Linear (if False, expo)
print Curve,len(Curve)
#setDACConstant(0x77,spi)
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
t1 = 150
t2 = 60
t3 = 600
t4 = 7000
t5 = 130000
setPulseTrain(t1,t2,t3,t4,t5,spi)
Pulse width: 150  ns --  0x13
Pulses delay: 210  ns --  0x1a
Poff: 810  ns --  0x0 0x67
Delay between: 7810 ns --  0x3 0xe7
Acquisition length:  17639  ns --  0x44 0xe7
#LoopSPI(spi)
#LoopAcq(spi)
TestSPI(spi,3) # LED2 clignote 3x
N=0
f = 0x00
t1 = 200
t2 = 100
t3 = 2000
t4 = 300-t1-t2+10
t5 = 200000
Curve = CreateDACCurve(100,400,True)[0]
SetDACCurve(spi,Curve)

setPulseTrain(t1,t2,t3,t4,t5,spi)
LAcq = t5/1000 #ns to us
WriteFPGA(spi,0xED,f) # Frequency of ADC acquisition / sEEADC_freq (3 = 16Msps, 1 = 32, 0 = 64, 2 = 21Msps)
WriteFPGA(spi,0xEB,0x00) # Doing one line


for pp in range(60):

    WriteFPGA(spi,0xEF,0x01) # Cleaning memory pointer
    WriteFPGA(spi,0xEA,0x01) # Software Trig : As to be clear by software

    Fech = int(64/((1+f)))
    Nacq = LAcq * Fech
    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["target"] = "pu90"
    BigJS0N["position"] = pp

    N = N+1
    print N
    with open('p_serPU90-'+str(N)+".json", 'w') as outfile:
        json.dump(BigJS0N, outfile)

print "Done"
Pulse width: 200  ns --  0x19
Pulses delay: 300  ns --  0x26
Poff: 2300  ns --  0x1 0x26
Delay between: 2310 ns --  0x1 0x27
Acquisition length:  25895  ns --  0x65 0x27
64 -> 12800 samples
121
64 -> 12800 samples
122
64 -> 12800 samples
123
64 -> 12800 samples
124
64 -> 12800 samples
125
64 -> 12800 samples
126
64 -> 12800 samples
127
64 -> 12800 samples
128
64 -> 12800 samples
129
64 -> 12800 samples
130
64 -> 12800 samples
131
64 -> 12800 samples
132
64 -> 12800 samples
133
64 -> 12800 samples
134
64 -> 12800 samples
135
64 -> 12800 samples
136
64 -> 12800 samples
137
64 -> 12800 samples
138
64 -> 12800 samples
139
64 -> 12800 samples
140
64 -> 12800 samples
141
64 -> 12800 samples
142
64 -> 12800 samples
143
64 -> 12800 samples
144
64 -> 12800 samples
145
64 -> 12800 samples
146
64 -> 12800 samples
147
64 -> 12800 samples
148
64 -> 12800 samples
149
64 -> 12800 samples
150
64 -> 12800 samples
151
64 -> 12800 samples
152
64 -> 12800 samples
153
64 -> 12800 samples
154
64 -> 12800 samples
155
64 -> 12800 samples
156
64 -> 12800 samples
157
64 -> 12800 samples
158
64 -> 12800 samples
159
64 -> 12800 samples
160
64 -> 12800 samples
161
64 -> 12800 samples
162
64 -> 12800 samples
163
64 -> 12800 samples
164
64 -> 12800 samples
165
64 -> 12800 samples
166
64 -> 12800 samples
167
64 -> 12800 samples
168
64 -> 12800 samples
169
64 -> 12800 samples
170
64 -> 12800 samples
171
64 -> 12800 samples
172
64 -> 12800 samples
173
64 -> 12800 samples
174
64 -> 12800 samples
175
64 -> 12800 samples
176
64 -> 12800 samples
177
64 -> 12800 samples
178
64 -> 12800 samples
179
64 -> 12800 samples
180
Done
BigJS0N["parameters"].keys()
['Pon', 'PulsesDelay', 'Poff', 'LengthAcq', 'DeltaAcq']
import json


results matching ""

    No results matching ""