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):
19 """Sets up the ports."""
21 self.MCPreset(); #Reset the chip.
24 """Reset the MCP2515 chip."""
25 self.SPItrans([0xC0]);
26 def MCPrxstatus(self):
27 """Reads the RX Status by the SPI verb of the same name."""
28 data=self.SPItrans([0xB0,0x00]);
30 def MCPreadstatus(self):
31 """Reads the Read Status by the SPI verb of the same name."""
32 #See page 67 of the datasheet for the flag names.
33 data=self.SPItrans([0xA0,0x00]);
35 def MCPrts(self,TXB0=False,TXB1=False,TXB2=False):
36 """Requests to send one of the transmit buffers."""
38 if TXB0: flags=flags|1;
39 if TXB1: flags=flags|2;
40 if TXB2: flags=flags|4;
43 print "Warning: Requesting to send no buffer.";
45 """Request To Send a transmission."""
46 self.SPItrans([0x80|flags]);
48 """Read a byte from the given address. Untested."""
49 data=self.SPItrans([0x03,adr&0xFF,00]);
52 def poke8(self,adr,val):
53 """Poke a value into RAM. Untested"""
54 self.SPItrans([0x02,adr&0xFF,val&0xFF]);