Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import serial
- import serial.tools.list_ports
- #Register Mapping
- cSUR1addr = 0
- cSUR2addr = 1
- cSUR3addr = 2
- cDACARaddr = 3
- cDACDRaddr = 4
- cX0Raddr = 5
- cX1Raddr = 6
- cX2Raddr = 7
- cX3Raddr = 8
- cSR1addr = 9
- cMISCCRaddr = 10
- cSR2addr = 11
- cREVRaddr = 14
- commstr = [None]*10
- class USBcount50:
- #Serialport instance
- porthandles = []
- global commstr
- devcnt = 0
- def __init__(self):
- ports = list(serial.tools.list_ports.comports())
- for p in ports:
- #USB_DEVICE(0x10C4, 0xF004),Elan Digital Systems USBcount50
- if(p.vid == 0x10C4 and p.pid == 0xF004):
- #if(p.vid == 0x10C4 and p.pid == 0xEA60):
- #print("Device found")
- self.porthandles.append(serial.Serial(p.device, baudrate=921600,timeout=0.05))
- self.devcnt = len(self.porthandles)
- print("Device count : " + str(self.devcnt))
- #from DLL v1.0.0.2 (USBcount50Drvr.cpp)
- def Close(self, channel):
- if(self.porthandles[channel] != None):
- self.porthandles[channel].close()
- def AbortNow(self, channel):
- print("Device Respond Error!")
- return
- def ReadReg(self, channel, reg):
- got = None
- if(self.porthandles[channel] != None):
- commstr[0] = 0x60 | reg
- #got = self.porthandles[channel].write(commstr[0:1])
- try:
- got = self.porthandles[channel].write(commstr[0:1])
- except:
- self.AbortNow(channel)
- return 0
- commstr[0] = self.porthandles[channel].read(1)
- if(commstr[0] == b''):
- self.AbortNow(channel)
- return None
- """try:
- commstr[0] = self.porthandles[channel].read(1)
- except:
- self.AbortNow(channel)
- return 0"""
- if (got != 1):
- self.AbortNow(channel)
- return 0
- else:
- return int.from_bytes(commstr[0], byteorder='big')
- else:
- return 0
- def WriteReg(self, channel, reg, data):
- got = None
- if(self.porthandles[channel] != None):
- commstr[0] = 0x20 | reg
- commstr[1] = data
- try:
- got = self.porthandles[channel].write(commstr[0:2])
- #print(got)
- except:
- self.AbortNow(channel)
- if (got != 2):
- self.AbortNow(channel)
- def Delay(self, c):
- c //= 2; #takes about 2ms to do a single read over the USB
- if (c <= 0):
- c=1;
- for i in range(c):
- self.ReadReg(0,cREVRaddr)
- def ValidateByte(self, c):
- if (c < 0):
- return 0
- elif (c > 255):
- return 255
- else:
- return c
- def SetTrigMaster(self, channel, master):
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cSUR1addr);
- if (master != 0):
- readbyte = (readbyte | 0x40);
- else:
- readbyte = (readbyte & 0xBF);
- self.WriteReg(channel, 0, readbyte);
- def SetClockMaster(self, channel, master):
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cSUR1addr);
- if (master != 0):
- readbyte = (readbyte | 0x80);
- else:
- readbyte = (readbyte & 0x7F);
- self.WriteReg(channel, 0, readbyte);
- def GetTriggeredStatus(self, channel): #the latched status
- if(self.porthandles[channel] != None):
- return(self.ReadReg(channel, cSR1addr) & 2) >> 1
- else:
- return 0
- def SetLEDMode(self, channel, mode): #0,1,2,3 as modes(off, slow blink, blink, all)
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cMISCCRaddr)
- self.WriteReg(channel, cMISCCRaddr, (readbyte & 0x9F) | ((mode & 3) * 0x20))
- def GetCount(self, channel):
- temp = 0
- if(self.porthandles[channel] != None):
- temp |= self.ReadReg (channel, cX3Raddr)
- temp <<= 8
- temp |= self.ReadReg (channel, cX2Raddr)
- temp <<= 8
- temp |= self.ReadReg (channel, cX1Raddr)
- temp <<= 8
- temp |= self.ReadReg (channel, cX0Raddr)
- return temp
- def SetPLL(self, channel, m, n, u, dly):
- if(self.porthandles[channel] != None):
- if ((m>0) and (n>0) and (u>0)):
- data = None
- temp = None
- bit = None
- pullbits = None
- fout = ratio = None
- OBMUX = FBSEL = FBDLY = XDLYSEL = None
- cMISCCR_pllsclkHI = (1<<0)
- cMISCCR_pllsshiftHI = (1<<1)
- cMISCCR_pllsdinHI = (1<<2)
- cMISCCR_pllsupdateHI = (1<<3)
- cMISCCR_pllmodeHI = (1<<4)
- cMISCCR_pllsclkLO = (0<<0)
- cMISCCR_pllsshiftLO = (0<<1)
- cMISCCR_pllsdinLO = (0<<2)
- cMISCCR_pllsupdateLO = (0<<3)
- cMISCCR_pllmodeLO = (0<<4)
- #readbyte = self.ReadReg(channel, cSUR1addr)
- #self.WriteReg(channel, cSUR1addr, (UCHAR)(readbyte & 0xFE)) #stop run
- #dont set mode to hi until AFTER all the config bits are shifted in to the dynamic
- #s/r reg, else can select a bunch of rubbish bits and stop the clock to the UART !!!!!
- data = self.ReadReg(channel, cMISCCRaddr)
- data &= 0xE0 #kill all config bits
- self.WriteReg(channel, cMISCCRaddr, (data | cMISCCR_pllmodeLO | cMISCCR_pllsshiftLO)) #set dynamic mode
- self.WriteReg(channel, cMISCCRaddr, (data | cMISCCR_pllmodeLO | cMISCCR_pllsshiftHI)) #ready to shift
- """ from PAPLUSPLLdynamicAN.pdf app note
- 26 XDLYSEL Mask-programmable delay select MUX
- 25-22 FBDLY[3:0] Delay line tap select MUX
- 21-20 FBSEL[1:0] Feedback source MUX
- 19-17 OBMUX[2:0] "B" output MUX
- 16-15 OAMUX[1:0] "A" output MUX
- 14-13 OADIV[1:0] "A" output divider (/v)
- 12-11 OBDIV[1:0] "B" output divider (/u)
- 10-5 FBDIV[5:0] Feedback signal divider (/m)
- 4-0 FINDIV[4:0] Input clock driver (/n)
- Configuration Bits from log file:
- FINDIV n
- FBDIV m
- OBDIV u
- OADIV 00b
- OAMUX 00b
- OBMUX 100b
- FBSEL 01b
- FBDLY 0000b
- XDLYSEL 1b
- """
- OBMUX = 2
- FBSEL = 1
- FBDLY = 0
- #if (channel > 1):
- FBDLY = dly
- XDLYSEL = 1
- fout = 12.5e6*m/(n*u)
- ratio = 12.5e6 / fout
- #sniff out sub divisions of the base 12.5MHz input and dont use the VCO at all !
- if ((ratio >= 0.999999999) and (ratio <= 1.000000001)):
- OBMUX = 1
- u = 1
- if ((ratio >= 1.999999999) and (ratio <= 2.000000001)):
- OBMUX = 1
- u = 2
- if ((ratio >= 2.999999999) and (ratio <= 3.000000001)):
- OBMUX = 1
- u = 3
- if ((ratio >= 3.999999999) and (ratio <= 4.000000001)):
- OBMUX = 1
- u = 4
- n-=1
- m-=1
- u-=1
- pllbits = n | (m << 5) | (u << 11)
- pllbits |= (OBMUX << 17) | (FBSEL << 20) | (FBDLY << 22) | (XDLYSEL << 26)
- for bit in range(0,27):
- temp = (data | cMISCCR_pllmodeLO | cMISCCR_pllsshiftHI | (cMISCCR_pllsdinHI if (pllbits & 1) else cMISCCR_pllsdinLO))
- self.WriteReg(channel, cMISCCRaddr, temp) #data bit
- self.WriteReg(channel, cMISCCRaddr, (temp | cMISCCR_pllsclkHI))#clock it
- self.WriteReg(channel, cMISCCRaddr, (temp | cMISCCR_pllsclkLO))
- pllbits >>= 1;
- self.WriteReg(channel, cMISCCRaddr, (data | cMISCCR_pllmodeLO | cMISCCR_pllsshiftLO | cMISCCR_pllsupdateLO)) #disable shift
- self.WriteReg(channel, cMISCCRaddr, (data | cMISCCR_pllmodeLO | cMISCCR_pllsshiftHI | cMISCCR_pllsupdateHI)) #make it update
- self.WriteReg(channel, cMISCCRaddr, (data | cMISCCR_pllmodeHI | cMISCCR_pllsshiftLO | cMISCCR_pllsupdateLO)) #disable shift & update done, leave in dynamic mode
- #self._WriteReg(channel, cSUR1addr, readbyte); //start (or not!) run
- def SetAmplitude(self, channel, voltage):
- if(self.porthandles[channel] != None):
- if (voltage < 1.5):
- voltage = 1.5
- elif (voltage > 5):
- voltage = 5
- daccode = int((voltage-1.5)*255/(5-1.5))
- self.WriteReg (channel, cDACARaddr, 2)
- self.WriteReg (channel, cDACDRaddr, self.ValidateByte(daccode))
- self.Delay(12) #let dac & amp settle (time const is about 12ms)
- def SetRun(self, channel, State):#0=stop 1=run
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cSUR1addr)
- self.WriteReg (channel, cSUR1addr, ((readbyte & 0xFE) | (1 * (State!=0))))
- def SetArm(self, channel, State):#0=disarm 1=arm
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cSUR1addr)
- self.WriteReg (channel, cSUR1addr, ((readbyte & 0xFD) | (2 * (State!=0))))
- def SetTrigger(self, channel, State): #0=disable 1=trigger
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cSUR1addr)
- self.WriteReg (channel, cSUR1addr, ((readbyte & 0xDF) | (32 * (State!=0))))
- def SetExtClk(self, channel, State): #0=int 1=ext
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cSUR1addr)
- self.WriteReg (channel, cSUR1addr, ((readbyte & 0xFB) | (4 * (State!=0))))
- def SetExtGate(self, channel, State): #0=int 1=ext
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cSUR1addr)
- self.WriteReg (channel, cSUR1addr, ((readbyte & 0xF7) | (8 * (State!=0))))
- def SetExtGateMode(self, channel, Mode): #0=nn, 1=pn, 2=np, 3=pp
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cSUR3addr)
- self.WriteReg (channel, cSUR3addr, ((readbyte & 0xFC) | (1 * (Mode))))
- def SelIntClk(self, channel, Freq): #0=100M,1=10M,2=1M
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cSUR3addr)
- self.WriteReg (channel, cSUR3addr, ((readbyte & 0xE3) | (4 * (Freq))))
- def SelIntGate(self, channel, Gate): #0=100ms,1=1s,2=10s
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cSUR3addr)
- self.WriteReg (channel, cSUR3addr, ((readbyte & 0x1F) | (32 * (Gate))))
- def GetIntGateDone(self, channel):
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cSR2addr)
- return 1 if (readbyte & 2) else 0
- else:
- return 1
- def GetExtGateBits(self, channel):
- if(self.porthandles[channel] != None):
- readbyte = self.ReadReg(channel, cSR2addr)
- return (readbyte >> 2);
- else:
- return 0
- def InitCount(self, channel, master):#1 to 4
- self.WriteReg (channel, cSUR1addr, 0)
- self.WriteReg (channel, cSUR2addr, 0)
- self.WriteReg (channel, cSUR3addr, 0)
- #every port that gets opened, strobe the masters J1_6 pin to sync the new device & the existing ones
- self.WriteReg (0, 1, 0x20)
- self.WriteReg (0, 1, 0x24)
- self.WriteReg (0, 1, 0x4)
- self.WriteReg (0, 1, 0x24)
- self.SetClockMaster (channel,master)
- self.SetLEDMode (channel, 1)
- #from tool application (USBcount_Main.frm) not fully compatible
- def SetRunMode(self, Mode):
- if(Mode == 0):#stop
- for p in range(self.devcnt):
- self.SetArm(p, 0)
- self.SetRun(p, 0)
- self.SetTrigger(p, 0)
- elif(Mode == 1):#run
- for p in range(self.devcnt):
- self.SetRun(p, 0)
- self.SetArm(p, 0)
- self.SetTrigger(p, 0)
- for p in range(self.devcnt):
- self.SetRun(p, 1)
- self.SetArm(p, 1)
- elif(Mode == 2):#one shot
- for p in range(self.devcnt):
- self.SetTrigger(p, 0)
- self.SetArm(p, 0)
- self.SetRun(p, 0)
- for p in range(self.devcnt):
- self.SetRun(p, 1)
- self.SetArm(p, 1)
- def SetMeasurementType(self,channel, Mode, idx, edge): #Freq = 0 , Period = 1
- if(Mode == 1):#period mode
- self.SetExtClk(channel, 0) #use internal freq
- self.SetExtGate(channel, 1)#external gating
- self.SelIntClk(channel, idx)#0=100M,1=10M,2=1M
- self.SetExtGateMode(channel, edge)#0=nn, 1=pn, 2=np, 3=pp
- elif(Mode == 0):#freq mode
- self.SetExtClk(channel, 1) #use external freq
- self.SetExtGate(channel, 0)#internal gating
- self.SelIntGate(channel, idx)#0=100ms,1=1s,2=10s
- if __name__ == "__main__":
- USBcount50 = USBcount50()
- #USBcount50.SetAmplitude(0,2.0)
- USBcount50.SetMeasurementType(0,1,0,3)#Ch0,Freqmode,100ms gatetime,always0
- USBcount50.SetClockMaster(0, 1)
- USBcount50.SetTrigMaster(0, 1)
- USBcount50.SetRunMode(1)
- while 1:
- USBcount50.SetTrigger(0, 0)
- USBcount50.SetTrigger(0, 1)
- while 1:
- num = USBcount50.GetExtGateBits(0)
- #print(num)
- if(num & 4 ):
- break
- print((100000000/USBcount50.GetCount(0)))
- USBcount50.SetArm(0,0)
- USBcount50.SetArm(0,1)
- #USBcount50.SetRunMode(0)
- #USBcount50.SetLEDMode(0,3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement