2 # GoodFET MCP2515 CAN Bus Client
4 # (C) 2012 Travis Goodspeed <travis at radiantmachines.com>
6 # This code is being rewritten and refactored. You've been warned!
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.
13 import sys, time, string, cStringIO, struct, glob, os;
15 from GoodFETSPI import GoodFETSPI;
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"];
23 """Sets up the ports."""
25 self.MCPreset(); #Reset the chip.
26 self.MCPreqstatLoopback();
28 """Reset the MCP2515 chip."""
29 self.SPItrans([0xC0]);
31 """Get the CAN Status."""
32 return self.peek8(0x0E);
33 def MCPreqstatNormal(self):
34 """Set the CAN state."""
36 self.MCPbitmodify(0x0F,0xE0,(state<<5));
37 def MCPreqstatSleep(self):
38 """Set the CAN state."""
40 self.MCPbitmodify(0x0F,0xE0,(state<<5));
41 def MCPreqstatLoopback(self):
42 """Set the CAN state."""
44 self.MCPbitmodify(0x0F,0xE0,(state<<5));
45 def MCPreqstatListenOnly(self):
46 """Set the CAN state."""
48 self.MCPbitmodify(0x0F,0xE0,(state<<5));
49 def MCPreqstatConfiguration(self):
50 """Set the CAN state."""
52 self.MCPbitmodify(0x0F,0xE0,(state<<5));
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]);
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]);
70 def MCPrts(self,TXB0=False,TXB1=False,TXB2=False):
71 """Requests to send one of the transmit buffers."""
73 if TXB0: flags=flags|1;
74 if TXB1: flags=flags|2;
75 if TXB2: flags=flags|4;
78 print "Warning: Requesting to send no buffer.";
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),
87 0x00, 0x00, 0x00, 0x00,
88 0x00, 0x00, 0x00, 0x00
90 return data[1:len(data)];
92 """Reads the next incoming packet from either buffer.
93 Returns None immediately if no packet is waiting."""
94 status=self.MCPrxstatus()&0xC0;
96 #Buffer 0 has higher priority.
97 return self.rxbuffer(0);
99 #Buffer 1 has lower priority.
100 return self.rxbuffer(1);
103 def packet2str(self,packet):
104 """Converts a packet from the internal format to a string."""
107 toprint=toprint+("%02x "%ord(bar))
110 """Read a byte from the given address. Untested."""
111 data=self.SPItrans([0x03,adr&0xFF,00]);
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]);
117 def poke8(self,adr,val):
118 """Poke a value into RAM. Untested"""
119 self.SPItrans([0x02,adr&0xFF,val&0xFF]);