From: travisutk Date: Thu, 20 May 2010 00:04:31 +0000 (+0000) Subject: Nordic RF client library, test script. X-Git-Url: http://git.rot13.org/?p=goodfet;a=commitdiff_plain;h=1536c622c446a0928fb2e9e44cdc67e7cceb6e7e Nordic RF client library, test script. git-svn-id: https://svn.code.sf.net/p/goodfet/code/trunk@533 12e2690d-a6be-4b82-a7b7-67c4a43b65c8 --- diff --git a/client/GoodFETNRF.py b/client/GoodFETNRF.py new file mode 100644 index 0000000..0c62cdf --- /dev/null +++ b/client/GoodFETNRF.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# GoodFET Nordic RF Radio Client +# +# (C) 2009 Travis Goodspeed +# +# This code is being rewritten and refactored. You've been warned! + +import sys, time, string, cStringIO, struct, glob, serial, os; + +from GoodFET import GoodFET; + +class GoodFETNRF(GoodFET): + NRFAPP=0x50; + def NRFsetup(self): + """Move the FET into the NRF application.""" + self.writecmd(self.NRFAPP,0x10,0,self.data); #NRF/SETUP + + def NRFtrans8(self,byte): + """Read and write 8 bits by NRF.""" + data=self.NRFtrans([byte]); + return ord(data[0]); + + def NRFtrans(self,data): + """Exchange data by NRF.""" + self.data=data; + self.writecmd(self.NRFAPP,0x00,len(data),data); + return self.data; + + def peek(self,reg,bytes=1): + """Read an NRF Register. For long regs, result is flipped.""" + data=[reg,0,0,0,0,0]; + self.writecmd(self.NRFAPP,0x02,len(data),data); + toret=0; + for i in range(0,bytes): + toret=toret|(ord(self.data[i+1])<<(8*i)); + return toret; + def poke(self,reg,val,bytes=1): + """Write an NRF Register.""" + data=[reg]; + for i in range(0,bytes): + data=data+[(val>>(8*i))&0xFF]; + self.writecmd(self.NRFAPP,0x03,len(data),data); + if self.peek(reg,bytes)!=val: + print "Warning, failed to set register %02x." %reg; + return; + + def status(self): + """Read the status byte.""" + self.poke(0x07,0x78); #Reset status + status=self.peek(0x07); + print "Status=%02x" % status; + + #Radio stuff begins here. + def RF_freq(self,frequency): + """Set the frequency in Hz.""" + + #On the NRF24L01+, register 0x05 is the offset in + #MHz above 2400. + + mhz=frequency/1000000-2400; + print "Setting channel %i." % mhz + self.poke(0x05,mhz); + def RF_getsmac(self): + """Return the source MAC address.""" + + #Register 0A is RX_ADDR_P0, five bytes. + mac=self.peek(0x0A, 5); + return mac; diff --git a/client/goodfet.nrf b/client/goodfet.nrf index c29ca1a..1a5a503 100755 --- a/client/goodfet.nrf +++ b/client/goodfet.nrf @@ -35,11 +35,16 @@ if(sys.argv[1]=="test"): client.poke(0x06,8+10+4+2); client.RF_freq(2480 * 10**6); - #Print register, no idea why. - for r in range(0,30): - print "r[0x%02x]=0x%02x" % (r,client.peek(r)); - print "SMAC=%010x" % client.RF_getsmac(); + #Print registers, just for fun. + print "SMAC was %010x" % client.RF_getsmac(); + + client.poke(0x0A,0xDEADBEEF,5); + print "SMAC set to %010x" % client.RF_getsmac(); + if client.RF_getsmac()!=0xdeadbeef: + print "ERROR: Failed to set MAC address."; if(sys.argv[1]=="regs"): for r in range(0,30): print "r[0x%02x]=0x%02x" % (r,client.peek(r)); + +