Newer
Older
import time
import uhal
from I2CuHal import I2CCore
import StringIO
import csv
import sys
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class si5345:
#Class to configure the Si5344 clock generator
def __init__(self, i2c, slaveaddr=0x68):
self.i2c = i2c
self.slaveaddr = slaveaddr
#self.configList=
#def writeReg(self, address, data):
def readRegister(self, myaddr, nwords, verbose= False):
### Read a specific register on the Si5344 chip. There is not check on the validity of the address but
### the code sets the correct page before reading.
#First make sure we are on the correct page
currentPg= self.getPage()
requirePg= (myaddr & 0xFF00) >> 8
if verbose:
print "REG", hex(myaddr), "CURR PG" , hex(currentPg[0]), "REQ PG", hex(requirePg)
if currentPg[0] != requirePg:
self.setPage(requirePg)
#Now read from register.
addr=[]
addr.append(myaddr)
mystop=False
self.i2c.write( self.slaveaddr, addr, mystop)
# time.sleep(0.1)
res= self.i2c.read( self.slaveaddr, nwords)
return res
def writeRegister(self, myaddr, data, verbose=False):
### Write a specific register on the Si5344 chip. There is not check on the validity of the address but
### the code sets the correct page before reading.
### myaddr is an int
### data is a list of ints
#First make sure we are on the correct page
myaddr= myaddr & 0xFFFF
currentPg= self.getPage()
requirePg= (myaddr & 0xFF00) >> 8
#print "REG", hex(myaddr), "CURR PG" , currentPg, "REQ PG", hex(requirePg)
if currentPg[0] != requirePg:
self.setPage(requirePg)
#Now write to register.
data.insert(0, myaddr)
if verbose:
print " Writing: "
result="\t "
for iaddr in data:
result+="%#02x "%(iaddr)
print result
self.i2c.write( self.slaveaddr, data)
#time.sleep(0.01)
def setPage(self, page, verbose=False):
#Configure the chip to perform operations on the specified address page.
mystop=True
myaddr= [0x01, page]
self.i2c.write( self.slaveaddr, myaddr, mystop)
#time.sleep(0.01)
if verbose:
print " Si5345 Set Reg Page:", page
def getPage(self, verbose=False):
#Read the current address page
mystop=False
myaddr= [0x01]
self.i2c.write( self.slaveaddr, myaddr, mystop)
rPage= self.i2c.read( self.slaveaddr, 1)
#time.sleep(0.1)
if verbose:
print "\tPage read:", rPage
return rPage
def getDeviceVersion(self):
#Read registers containing chip information
mystop=False
nwords=2
myaddr= [0x02 ]#0xfa
self.setPage(0)
self.i2c.write( self.slaveaddr, myaddr, mystop)
#time.sleep(0.1)
res= self.i2c.read( self.slaveaddr, nwords)
print " Si5345 EEPROM: "
result="\t"
for iaddr in reversed(res):
result+="%#02x "%(iaddr)
print result
return res
def parse_clk(self, filename, verbose= False):
#Parse the configuration file produced by Clockbuilder Pro (Silicon Labs)
deletedcomments=""""""
if verbose:
print "\tParsing file", filename
with open(filename, 'rb') as configfile:
for i, line in enumerate(configfile):
if not line.startswith('#') :
if not line.startswith('Address'):
deletedcomments+=line
else:
if "Delay 300" in line: # Have found the string "Delay" in a line starting with #
if verbose:
print "Found delay 300ms command"
time.sleep(0.3)
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
csvfile = StringIO.StringIO(deletedcomments)
cvr= csv.reader(csvfile, delimiter=',', quotechar='|')
#print "\tN elements parser:", sum(1 for row in cvr)
#return [addr_list,data_list]
# for item in cvr:
# print "rere"
# regAddr= int(item[0], 16)
# regData[0]= int(item[1], 16)
# print "\t ", hex(regAddr), hex(regData[0])
#self.configList= cvr
regSettingList= list(cvr)
if verbose:
print "\t ", len(regSettingList), "elements"
return regSettingList
def writeConfiguration(self, regSettingList, verbose= 0):
print " Si5345 Writing configuration:"
toolbar_width = 38
if (verbose==1):
sys.stdout.write(" [%s]" % (" " * toolbar_width))
sys.stdout.flush()
sys.stdout.write("\b" * (toolbar_width+1)) # return to start of line, after '['
#regSettingList= list(regSettingCsv)
counter=0
for item in regSettingList:
regAddr= int(item[0], 16)
regData=[0]
regData[0]= int(item[1], 16)
if (verbose > 1):
print "\t", counter, "Reg:", hex(regAddr), "Data:", regData
counter += 1
self.writeRegister(regAddr, regData, False)
if (not(counter % 10) and (verbose==1)):
sys.stdout.write("-")
sys.stdout.flush()
sys.stdout.write("\n")
print "\tSi5345 configuration done"
def checkDesignID(self):
regAddr= 0x026B
res= self.readRegister(regAddr, 8)
result= " Si5345 design Id:\n\t"
for iaddr in res:
result+=chr(iaddr)
print result