4270b202030ca807ccf690ccc393f22a9afafc99
[goodfet] / client / GoodFETMCPCAN.py
1 #!/usr/bin/env python
2 # GoodFET MCP2515 CAN Bus Client
3
4 # (C) 2012 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7 #
8
9 # The MCP2515 is a CAN Bus to SPI adapter from Microchip Technology,
10 # as used in the Goodthopter series of boards.  It requires a separate
11 # chip for voltage level conversion, such as the MCP2551.
12
13 import sys, time, string, cStringIO, struct, glob, os;
14
15 from GoodFETSPI import GoodFETSPI;
16
17 class GoodFETMCPCAN(GoodFETSPI):
18     """This class uses the regular SPI app to implement a CAN Bus
19     adapter on the Goodthopter10 hardware."""
20     MCPMODES=["Normal","Sleep","Loopback","Listen-only","Configuration",
21               "UNKNOWN","UNKNOWN","PowerUp"];
22     def MCPsetup(self):
23         """Sets up the ports."""
24         self.SPIsetup();
25         self.MCPreset(); #Reset the chip.
26         self.MCPreqstatLoopback();
27     def MCPreset(self):
28         """Reset the MCP2515 chip."""
29         self.SPItrans([0xC0]);
30     def MCPcanstat(self):
31         """Get the CAN Status."""
32         return self.peek8(0x0E);
33     def MCPreqstatNormal(self):
34         """Set the CAN state."""
35         state=0x0;
36         self.MCPbitmodify(0x0F,0xE0,(state<<5));
37     def MCPreqstatSleep(self):
38         """Set the CAN state."""
39         state=0x1;
40         self.MCPbitmodify(0x0F,0xE0,(state<<5));
41     def MCPreqstatLoopback(self):
42         """Set the CAN state."""
43         state=0x2;
44         self.MCPbitmodify(0x0F,0xE0,(state<<5));
45     def MCPreqstatListenOnly(self):
46         """Set the CAN state."""
47         state=0x3;
48         self.MCPbitmodify(0x0F,0xE0,(state<<5));
49     def MCPreqstatConfiguration(self):
50         """Set the CAN state."""
51         state=0x4;
52         self.MCPbitmodify(0x0F,0xE0,(state<<5));
53     
54     def MCPcanstatstr(self):
55         """Read the present status as a string."""
56         status=self.MCPcanstat();
57         opmod=(status&0xE0)>>5;
58         return self.MCPMODES[opmod];
59     def MCPrxstatus(self):
60         """Reads the RX Status by the SPI verb of the same name."""
61         data=self.SPItrans([0xB0,0x00]);
62         return ord(data[1]);
63
64     def MCPreadstatus(self):
65         """Reads the Read Status by the SPI verb of the same name."""
66         #See page 67 of the datasheet for the flag names.
67         data=self.SPItrans([0xA0,0x00]);
68         return ord(data[1]);
69
70     def MCPrts(self,TXB0=False,TXB1=False,TXB2=False):
71         """Requests to send one of the transmit buffers."""
72         flags=0;
73         if TXB0: flags=flags|1;
74         if TXB1: flags=flags|2;
75         if TXB2: flags=flags|4;
76         
77         if flags==0:
78             print "Warning: Requesting to send no buffer.";
79         
80         self.SPItrans([0x80|flags]);
81     def readrxbuffer(self,packbuf=0):
82         """Reads the RX buffer.  Might have old data."""
83         data=self.SPItrans([0x90|(packbuf<<2),
84                        0x00,0x00, #SID
85                        0x00,0x00, #EID
86                        0x00,      #DLC
87                        0x00, 0x00, 0x00, 0x00,
88                        0x00, 0x00, 0x00, 0x00
89                        ]);
90         return data[1:len(data)];
91     def rxpacket(self):
92         """Reads the next incoming packet from either buffer.
93         Returns None immediately if no packet is waiting."""
94         status=self.MCPrxstatus()&0xC0;
95         if status&0x40:
96             #Buffer 0 has higher priority.
97             return self.rxbuffer(0);
98         elif status&0x80:
99             #Buffer 1 has lower priority.
100             return self.rxbuffer(1);
101         else:
102             return None;
103     def packet2str(self,packet):
104         """Converts a packet from the internal format to a string."""
105         toprint="";
106         for bar in packet:
107             toprint=toprint+("%02x "%ord(bar))
108         return toprint;
109     def peek8(self,adr):
110         """Read a byte from the given address.  Untested."""
111         data=self.SPItrans([0x03,adr&0xFF,00]);
112         return ord(data[2]);
113     def MCPbitmodify(self,adr,mask,data):
114         """Writes a byte with a mask.  Doesn't work for many registers."""
115         data=self.SPItrans([0x05,adr&0xFF,mask&0xFF,data&0xFF]);
116         return ord(data[2]);
117     def poke8(self,adr,val):
118         """Poke a value into RAM.  Untested"""
119         self.SPItrans([0x02,adr&0xFF,val&0xFF]);
120         return val;