Clock Tamer SPI

28 views
Skip to first unread message

Zack

unread,
Jan 14, 2011, 2:29:07 AM1/14/11
to ClockTamer
Hey Clock Tamers,

I really enjoy the product and the fact that you made everything open.
The only downside I see is that the documentation on SPI was a little
poor. I'd like to submit a script that I used to interface the
ClockTamer. It could serve as an example for how to talk over SPI. It
is available below.

Thanks again for publishing your schematics and source code.

Examples of use are:
./clock_tamer.py --reset
./clock_tamer --set_clock 52000000
./clock_tamer --cmd "HWI;VER"

#!/usr/bin/env python

from gnuradio import usrp
from time import sleep
import optparse, sys

class ClockTamer:
def __init__(self, usrp, which):
self.__usrp = usrp
self.__side = which
self.NSS = 0x0100
self.SCK = 0x0200
self.MOSI = 0x0400
self.MISO = 0x0800
self.NRST = 0x1000
self.__usrp._write_oe(self.__side, self.NSS | self.SCK |
self.MOSI | self.NRST, self.NSS | self.SCK | self.MOSI | self.MISO |
self.NRST )
self.set_hi(self.NSS)
self.set_hi(self.NRST)

# Cycle the reset
self.set_lo(self.NRST)
self.set_hi(self.NRST)
sleep(0.5)

def set_lo( self, pin ):
self.__usrp.write_io(self.__side, 0x0000, pin )
def set_hi( self, pin ):
self.__usrp.write_io(self.__side, 0xFFFF, pin )
def get_pin( self, pin ):
return self.__usrp.read_io(self.__side) & pin

def clean( self, text ):
return "".join(i for i in text if ord(i)<128 and ord(i)>31)

def write( self, text ):
result_string = ""
for c in text:
b = ord(c)
char_buffer = 0x00

# Prep the first bit
value = (b >> 7) & 1
if ( value > 0 ):
self.set_hi(self.MOSI)
else:
self.set_lo(self.MOSI)

# Start sending
self.set_hi(self.SCK)
self.set_lo(self.NSS)

for i in xrange(7,-1,-1):
self.set_hi(self.SCK)
value = (b >> i) & 1
if ( value > 0 ):
self.set_hi(self.MOSI)
else:
self.set_lo(self.MOSI)
self.set_lo(self.SCK)
bit = self.get_pin(self.MISO)
if ( bit > 0 ):
char_buffer = (char_buffer << 1) | 0x01
else:
char_buffer = char_buffer << 1
self.set_hi(self.SCK)
self.set_hi(self.NSS)
result_string += chr(char_buffer)
return result_string

class Usage(Exception):
def __init__(self, msg):
self.msg = msg

def main():
try:
try:
usage = "usage: clock_tamer.py [--reset][--set_clock N][--
cmd \"\"]"
parser = optparse.OptionParser(usage=usage)
parser.set_defaults(reset=False)
parser.set_defaults(clock=0)
parser.set_defaults(cmd="")
parser.add_option("-r","--reset", dest="reset",
action="store_true",
help="Only reset the Clock Tamer")
parser.add_option("--set_clock", dest="clock",
help="Set the output clock",type="int")
parser.add_option("--cmd", dest="cmd",
help="Commands to send to Clock
Tamer",type="string")
(options, args) = parser.parse_args()

if not options.reset and options.clock < 1 and options.cmd
== "":
parser.error("Need input arguments")

except optparse.OptionError, msg:
raise Usage(msg)

clock = ClockTamer(usrp.source_c(0), 0)
if not options.reset:
if not options.cmd == "":
cmds = options.cmd.split(";")
for cmd in cmds:
print "Wrote: "+cmd
clock.write(cmd+"\r")
result = clock.write("".center(48))
print "Response: "+clock.clean(result)
else:
cmd = "SET,,OUT,"+str(options.clock)
print "Wrote: "+cmd
clock.write(cmd+"\r")
result = clock.write("".center(48))
print "Response: "+clock.clean(result)
else:
print "ClockTamer cycled"

except Usage, err:
print >>sys.stderr, err.msg
return 2

# except Exception, err:
# sys.stderr.write( str(err) + '\n' )
# return 1

if __name__ == "__main__":
sys.exit(main())

Alexander Chemeris

unread,
Jan 14, 2011, 7:13:33 AM1/14/11
to clock...@googlegroups.com
Hi Zack,

Nice work!
Could you please post it as an attachment instead of inline? Then I'll
look through the code more closely and if you agree to publish it
under BSD license, I'll check it into mainline after some testing.

PS If anyone want to contribute to documentation - please do! I could
give you access to the wiki or you could post in comments to the wiki
or just mail here and I'll move it to the wiki.

--
Regards,
Alexander Chemeris.
http://www.fairwaves.ru

Zachary Moratto

unread,
Jan 15, 2011, 3:57:53 PM1/15/11
to clock...@googlegroups.com
Trying again to do an attachment. There doesn't seem to be a way to do this from the Google Groups page.
clock_tamer.py
Reply all
Reply to author
Forward
0 new messages