Beginnings of an MCP2515 driver for communicating with a CAN bus.
authortravisutk <travisutk@12e2690d-a6be-4b82-a7b7-67c4a43b65c8>
Wed, 15 Aug 2012 21:19:35 +0000 (21:19 +0000)
committertravisutk <travisutk@12e2690d-a6be-4b82-a7b7-67c4a43b65c8>
Wed, 15 Aug 2012 21:19:35 +0000 (21:19 +0000)
git-svn-id: https://svn.code.sf.net/p/goodfet/code/trunk@1226 12e2690d-a6be-4b82-a7b7-67c4a43b65c8

client/GoodFETMCPCAN.py [new file with mode: 0644]

diff --git a/client/GoodFETMCPCAN.py b/client/GoodFETMCPCAN.py
new file mode 100644 (file)
index 0000000..e942b42
--- /dev/null
@@ -0,0 +1,62 @@
+#!/usr/bin/env python
+# GoodFET MCP2515 CAN Bus Client
+# 
+# (C) 2012 Travis Goodspeed <travis at radiantmachines.com>
+#
+# This code is being rewritten and refactored.  You've been warned!
+#
+
+# The MCP2515 is a CAN Bus to SPI adapter from Microchip Technology,
+# as used in the Goodthopter series of boards.  It requires a separate
+# chip for voltage level conversion, such as the MCP2551.
+
+import sys, time, string, cStringIO, struct, glob, os;
+
+from GoodFETSPI import GoodFETSPI;
+
+class GoodFETMCPCAN(GoodFETSPI):
+    def MCPsetup(self):
+        """Sets up the ports."""
+        self.SPIsetup();
+        self.MCPreset(); #Reset the chip.
+        
+    def MCPreset(self):
+        """Reset the MCP2515 chip."""
+        self.SPItrans([0xC0]);
+    def MCPrxstatus(self):
+        """Reads the RX Status by the SPI verb of the same name."""
+        data=self.SPItrans([0xB0,0x00]);
+        return ord(data[1]);
+    def MCPreadstatus(self):
+        """Reads the RX Status by the SPI verb of the same name."""
+        data=self.SPItrans([0xA0,0x00]);
+        return ord(data[1]);
+    def peek8(self,adr):
+        """Read a byte from the given address.  Untested."""
+        data=self.SPItrans([0x03,adr&0xFF,00]);
+        return ord(data[2]);
+    
+    def poke8(self,adr,val):
+        """Poke a value into RAM.  Untested"""
+        self.SPItrans([0x02,adr&0xFF,val&0xFF]);
+        return val;
+    def rand16(self):
+        """Read a random 16-bit word."""
+        
+        data=self.EZSPtrans([0x49]);
+        if data==None:
+            print "Insufficient random data.";
+            return 0;
+        return ord(data[6])+(ord(data[7])<<8);
+
+    def info(self):
+        """Read the info bytes."""
+        print "Ember EM26 Z-Stack SPI Module.";
+        version=self.EM260spiversion();
+        status=self.EM260spistatus();
+        print "Version: %i" % (version); 
+        print "Status:  %s" % (["dead","alive"][status]);
+        print ""
+        self.setVersion();
+        print "Node ID: %04x" % (self.getNodeID());
+        print "Connected to %2i neighbors." % self.neighborCount();