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.
27 # We're going to set some registers, so we must be in config
29 self.MCPreqstatConfiguration();
31 # If we don't enable promiscous mode, we'll miss a lot of
32 # packets. It can be manually disabled later.
33 self.poke8(0x60,0xFF); #TODO Does this have any unpleasant side effects?
35 # Now we need to set the timing registers. See chapter 5 of
36 # the MCP2515 datasheet to get some clue as to how this
37 # arithmetic of this works, as my comments here will likely be
38 # rambling, incoherent, and unchanged after I get the infernal
41 # First, we must chose a Time Quanta (QT) which is used to
42 # define the durations of these segments. Section 5.3
43 # suggests setting BRP<5:0> to 0x04 to get a TQ=500ns, as a 20
44 # MHz crystal gives a clock period of 50ns. This way, for 125
45 # kHz communication, the bit time must be 16 TQ.
47 # A bit consists of four parts:
50 # 3: PhaseSeg1 (PS1) 7 TQ
51 # 4: PhaseSeg2 (PS2) 6 TQ
53 # CNF1 with a prescaler of 4 and a SJW of 1 TQ. SJW of 4
54 # might be more stable.
55 self.poke8(0x2a,0x04);
57 # CNF2 with a BLTMODE of 1, SAM of 0, PS1 of 7TQ, and PRSEG of 2TQ
60 (6<<3) | # 6+1=7TQ for PHSEG1
61 (1) # 1+1=2TQ for PRSEG
64 #CNF3 with a PS2 length of 6TQ.
69 """Reset the MCP2515 chip."""
70 self.SPItrans([0xC0]);
72 """Get the CAN Status."""
73 return self.peek8(0x0E);
74 def MCPreqstatNormal(self):
75 """Set the CAN state."""
77 self.MCPbitmodify(0x0F,0xE0,(state<<5));
78 def MCPreqstatSleep(self):
79 """Set the CAN state."""
81 self.MCPbitmodify(0x0F,0xE0,(state<<5));
82 def MCPreqstatLoopback(self):
83 """Set the CAN state."""
85 self.MCPbitmodify(0x0F,0xE0,(state<<5));
86 def MCPreqstatListenOnly(self):
87 """Set the CAN state."""
89 self.MCPbitmodify(0x0F,0xE0,(state<<5));
90 def MCPreqstatConfiguration(self):
91 """Set the CAN state."""
93 self.MCPbitmodify(0x0F,0xE0,(state<<5));
95 def MCPcanstatstr(self):
96 """Read the present status as a string."""
97 status=self.MCPcanstat();
98 opmod=(status&0xE0)>>5;
99 return self.MCPMODES[opmod];
100 def MCPrxstatus(self):
101 """Reads the RX Status by the SPI verb of the same name."""
102 data=self.SPItrans([0xB0,0x00]);
105 def MCPreadstatus(self):
106 """Reads the Read Status by the SPI verb of the same name."""
107 #See page 67 of the datasheet for the flag names.
108 data=self.SPItrans([0xA0,0x00]);
111 def MCPrts(self,TXB0=False,TXB1=False,TXB2=False):
112 """Requests to send one of the transmit buffers."""
114 if TXB0: flags=flags|1;
115 if TXB1: flags=flags|2;
116 if TXB2: flags=flags|4;
119 print "Warning: Requesting to send no buffer.";
121 self.SPItrans([0x80|flags]);
122 def readrxbuffer(self,packbuf=0):
123 """Reads the RX buffer. Might have old data."""
124 data=self.SPItrans([0x90|(packbuf<<2),
128 0x00, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x00, 0x00
131 return data[1:len(data)];
132 def writetxbuffer(self,packet,packbuf=0):
133 """Writes the transmit buffer."""
134 self.SPItrans([0x40|(packbuf<<1)]+packet);
137 """Reads the next incoming packet from either buffer.
138 Returns None immediately if no packet is waiting."""
139 status=self.MCPrxstatus()&0xC0;
141 #Buffer 0 has higher priority.
142 return self.readrxbuffer(0);
144 #Buffer 1 has lower priority.
145 return self.readrxbuffer(1);
148 def txpacket(self,packet):
149 """Transmits a packet through one of the outbound buffers.
150 As usual, the packet should begin with SIDH.
151 For now, only TXB0 is supported."""
152 self.writetxbuffer(packet,0);
153 self.MCPrts(TXB0=True);
154 def packet2str(self,packet):
155 """Converts a packet from the internal format to a string."""
158 toprint=toprint+("%02x "%ord(bar))
161 """Read a byte from the given address. Untested."""
162 data=self.SPItrans([0x03,adr&0xFF,00]);
164 def MCPbitmodify(self,adr,mask,data):
165 """Writes a byte with a mask. Doesn't work for many registers."""
166 data=self.SPItrans([0x05,adr&0xFF,mask&0xFF,data&0xFF]);
168 def poke8(self,adr,val):
169 """Poke a value into RAM. Untested"""
170 self.SPItrans([0x02,adr&0xFF,val&0xFF]);