From: travisutk Date: Wed, 15 Aug 2012 21:19:35 +0000 (+0000) Subject: Beginnings of an MCP2515 driver for communicating with a CAN bus. X-Git-Url: http://git.rot13.org/?p=goodfet;a=commitdiff_plain;h=6de9415b927cc697ec315e9792733427fc031945;ds=sidebyside Beginnings of an MCP2515 driver for communicating with a CAN bus. git-svn-id: https://svn.code.sf.net/p/goodfet/code/trunk@1226 12e2690d-a6be-4b82-a7b7-67c4a43b65c8 --- diff --git a/client/GoodFETMCPCAN.py b/client/GoodFETMCPCAN.py new file mode 100644 index 0000000..e942b42 --- /dev/null +++ b/client/GoodFETMCPCAN.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# GoodFET MCP2515 CAN Bus Client +# +# (C) 2012 Travis Goodspeed +# +# 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();