From 9565c64a06b7302cd820214f58ea0970d4f1e07a Mon Sep 17 00:00:00 2001 From: travisutk Date: Wed, 15 Aug 2012 22:52:49 +0000 Subject: [PATCH] MCP2515 can now loopback packets! git-svn-id: https://svn.code.sf.net/p/goodfet/code/trunk@1231 12e2690d-a6be-4b82-a7b7-67c4a43b65c8 --- client/GoodFETMCPCAN.py | 18 +++++++++++++++--- client/goodfet.mcpcan | 14 +++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/client/GoodFETMCPCAN.py b/client/GoodFETMCPCAN.py index 4270b20..d4dc159 100644 --- a/client/GoodFETMCPCAN.py +++ b/client/GoodFETMCPCAN.py @@ -23,7 +23,9 @@ class GoodFETMCPCAN(GoodFETSPI): """Sets up the ports.""" self.SPIsetup(); self.MCPreset(); #Reset the chip. - self.MCPreqstatLoopback(); + + #Loopback mode for testing. + self.MCPreqstatConfiguration(); def MCPreset(self): """Reset the MCP2515 chip.""" self.SPItrans([0xC0]); @@ -88,18 +90,28 @@ class GoodFETMCPCAN(GoodFETSPI): 0x00, 0x00, 0x00, 0x00 ]); return data[1:len(data)]; + def writetxbuffer(self,packet,packbuf=0): + """Writes the transmit buffer.""" + self.SPItrans([0x40|(packbuf<<1)]+packet); + def rxpacket(self): """Reads the next incoming packet from either buffer. Returns None immediately if no packet is waiting.""" status=self.MCPrxstatus()&0xC0; if status&0x40: #Buffer 0 has higher priority. - return self.rxbuffer(0); + return self.readrxbuffer(0); elif status&0x80: #Buffer 1 has lower priority. - return self.rxbuffer(1); + return self.readrxbuffer(1); else: return None; + def txpacket(self,packet): + """Transmits a packet through one of the outbound buffers. + As usual, the packet should begin with SIDH. + For now, only TXB0 is supported.""" + self.writetxbuffer(packet,0); + self.MCPrts(TXB0=True); def packet2str(self,packet): """Converts a packet from the internal format to a string.""" toprint=""; diff --git a/client/goodfet.mcpcan b/client/goodfet.mcpcan index 99620a3..9de2cc7 100755 --- a/client/goodfet.mcpcan +++ b/client/goodfet.mcpcan @@ -53,12 +53,24 @@ if(sys.argv[1]=="sniff"): if(sys.argv[1]=="test"): print "MCP2515 Self Test:"; + #Switch to config mode and try to rewrite TEC. + client.MCPreqstatConfiguration(); client.poke8(0x00,0xde); if client.peek8(0x00)!=0xde: print "ERROR: Poke to TEC failed."; else: print "SUCCESS: Register read/write."; - + + #Switch to Loopback mode and try to catch our own packet. + client.MCPreqstatLoopback(); + client.txpacket([0xb1, 0x6b, 0x00, 0x65, + 0xb1, 0x6b, 0x00, 0x65 + ]); + print "Waiting on loopback packet."; + packet=None; + while packet==None: + packet=client.rxpacket(); + print "Success! Got %s" % client.packet2str(packet); if(sys.argv[1]=="peek"): start=0x0000; if(len(sys.argv)>2): -- 2.20.1