93d1bcb4b14bc818b543be0212a510c89d54bc0c
[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         status=self.EM260spistatus();
62         print "Version: %i" % (version); 
63         print "Status:  %s" % (["dead","alive"][status]);
64     def EM260spiversion(self):
65         """Read the SPI version number from EM260."""
66         data=self.EM260trans([0x0A,0xA7]);        
67         version=ord(data[0]);
68         
69         if version==0x00:
70             return self.EM260spiversion();
71         if version==0x02:
72             return self.EM260spiversion();
73         if not version&0x80:
74             print "Version misread.";
75             return 0;
76         return version&0x7F;
77     
78     def EM260spistatus(self):
79         """Read the status bit."""
80         data=self.EM260trans([0x0B,0xA7]);
81         status=ord(data[0]);
82         
83         if status==0x00:
84             return self.EM260spistatus();
85         if status==0x02:
86             return self.EM260spistatus();
87         if not status&0x80 and status&0x40:
88             print "Status misread.";
89             return 0;
90         return status&1;