EM260spiversion() is stabilized.
[goodfet] / client / GoodFETEM260.py
1 #!/usr/bin/env python
2 # GoodFET EM260 Radio Client
3
4 # (C) 2010 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8 # The EM260 is almost textbook SPI, except that the response cannot be
9 # read until after the nHOST_INT pin of the EM260 drops low and a dummy
10 # byte is read.  That is, the sequence will look like the following:
11
12 # Transmit Host->Slave Data
13 # while(nHOST_INT); //Sleep until ready.
14 # Recv Dummy Byte
15 # Recv Slave->Host Data
16
17 # The delay is mandatory.
18
19 import sys, time, string, cStringIO, struct, glob, serial, os;
20
21 from GoodFETSPI import GoodFETSPI;
22
23 class GoodFETEM260(GoodFETSPI):
24     EM260APP=0x01;
25     seq=0;
26     def EM260trans(self,data):
27         """Exchange data by EM260 SPI. (Slightly nonstandard.)"""
28         self.data=data;
29         self.writecmd(0x01,0x82,len(data),data);
30         
31         try:
32             reply=ord(self.data[0]);
33             if(reply==0x00):
34                 print "Warning: EM260 rebooted.";
35                 return self.EM260trans(data);
36             if(reply==0x02):
37                 print "Error: Aborted Transaction.";
38                 #return self.EM260trans(data);
39             if(reply==0x03):
40                 print "Error: Missing Frame Terminator.";
41                 #return self.data;
42         except:
43             print "Error in EM260trans.";
44         return self.data;
45     
46     
47     def peek8(self,adr):
48         """Read a byte from the given address."""
49         data=self.EM260trans([0xfe,0x01,self.seq,0x00,
50                             0x49,
51                             0xA7]);
52         s="";
53         for foo in data:
54             s=s+"%02x " % ord(foo);
55         print s;
56         
57         return ord(data[0]);
58     def info(self):
59         """Read the info bytes."""
60         version=self.EM260spiversion();
61         print "Version %2i" % (version &0x7F); 
62     def EM260spiversion(self):
63         """Read the SPI version number from EM260."""
64         data=self.EM260trans([0x0A,0xA7]);        
65         version=ord(data[0]);
66         
67         if version==0x00:
68             return self.EM260spiversion();
69         if version==0x02:
70             return self.EM260spiversion();
71         if not version&0x80:
72             print "Version misread.";
73             return 0;
74         return version;
75     
76     def EM260spistatus(self):
77         """Read the info bytes."""
78         data=self.EM260trans([0x0B,0xA7]);        
79         s="";
80         for foo in data:
81             s=s+"%02x " % ord(foo);
82         print s;
83