0cbc0904c2e461199a5ac3091c1ecad3477b504e
[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     
21     def MCPsetup(self):
22         """Sets up the ports."""
23         self.SPIsetup();
24         self.MCPreset(); #Reset the chip.
25         
26     def MCPreset(self):
27         """Reset the MCP2515 chip."""
28         self.SPItrans([0xC0]);
29
30     def MCPrxstatus(self):
31         """Reads the RX Status by the SPI verb of the same name."""
32         data=self.SPItrans([0xB0,0x00]);
33         return ord(data[1]);
34
35     def MCPreadstatus(self):
36         """Reads the Read Status by the SPI verb of the same name."""
37         #See page 67 of the datasheet for the flag names.
38         data=self.SPItrans([0xA0,0x00]);
39         return ord(data[1]);
40
41     def MCPrts(self,TXB0=False,TXB1=False,TXB2=False):
42         """Requests to send one of the transmit buffers."""
43         flags=0;
44         if TXB0: flags=flags|1;
45         if TXB1: flags=flags|2;
46         if TXB2: flags=flags|4;
47         
48         if flags==0:
49             print "Warning: Requesting to send no buffer.";
50         
51         self.SPItrans([0x80|flags]);
52     def readrxbuffer(self,packbuf=0):
53         """Reads the RX buffer.  Might have old data."""
54         data=self.SPItrans([0x90|(packbuf<<2),
55                        0x00,0x00, #SID
56                        0x00,0x00, #EID
57                        0x00,      #DLC
58                        0x00, 0x00, 0x00, 0x00,
59                        0x00, 0x00, 0x00, 0x00
60                        ]);
61         return data[1:len(data)];
62     def rxpacket(self):
63         """Reads the next incoming packet from either buffer.
64         Returns None immediately if no packet is waiting."""
65         status=self.MCPrxstatus()&0xC0;
66         if status&0x40:
67             #Buffer 0 has higher priority.
68             return self.rxbuffer(0);
69         elif status&0x80:
70             #Buffer 1 has lower priority.
71             return self.rxbuffer(1);
72         else:
73             return None;
74     def packet2str(self,packet):
75         """Converts a packet from the internal format to a string."""
76         toprint="";
77         for bar in packet:
78             toprint=toprint+("%02x "%ord(bar))
79         return toprint;
80     def peek8(self,adr):
81         """Read a byte from the given address.  Untested."""
82         data=self.SPItrans([0x03,adr&0xFF,00]);
83         return ord(data[2]);
84     def MCPbitmodify(self,adr,mask,data):
85         """Writes a byte with a mask.  Doesn't work for many registers."""
86         data=self.SPItrans([0x05,adr&0xFF,mask&0xFF,data&0xFF]);
87         return ord(data[2]);
88     def poke8(self,adr,val):
89         """Poke a value into RAM.  Untested"""
90         self.SPItrans([0x02,adr&0xFF,val&0xFF]);
91         return val;