X-Git-Url: http://git.rot13.org/?p=goodfet;a=blobdiff_plain;f=client%2FGoodFETMCPCAN.py;h=b75c034ec380425987369b69a41fe8c69dae09fa;hp=1b8a5fdc24d9ed4f1d67d83146c03fd16d77fc8f;hb=343e4e97f563ce3a61396f7a24593c6c0940f361;hpb=906e2500df02bdb4b0471a5011e713177e959b49 diff --git a/client/GoodFETMCPCAN.py b/client/GoodFETMCPCAN.py index 1b8a5fd..b75c034 100644 --- a/client/GoodFETMCPCAN.py +++ b/client/GoodFETMCPCAN.py @@ -28,6 +28,15 @@ class GoodFETMCPCAN(GoodFETSPI): # mode. self.MCPreqstatConfiguration(); + # If we don't enable promiscous mode, we'll miss a lot of + # packets. It can be manually disabled later. + self.poke8(0x60,0xFF); #TODO Does this have any unpleasant side effects? + + #Set the default rate of 125kHz. + self.MCPsetrate(125); + + def MCPsetrate(self,rate=125): + """Sets the data rate in kHz.""" # Now we need to set the timing registers. See chapter 5 of # the MCP2515 datasheet to get some clue as to how this # arithmetic of this works, as my comments here will likely be @@ -48,19 +57,56 @@ class GoodFETMCPCAN(GoodFETSPI): # CNF1 with a prescaler of 4 and a SJW of 1 TQ. SJW of 4 # might be more stable. - self.poke8(0x2a,0x04); + #self.poke8(0x2a,0x04); # CNF2 with a BLTMODE of 1, SAM of 0, PS1 of 7TQ, and PRSEG of 2TQ - self.poke8(0x29, - 0x80 | # BTLMODE=1 - (6<<3) | # 6+1=7TQ for PHSEG1 - (1) # 1+1=2TQ for PRSEG - ); + #self.poke8(0x29, + # 0x80 | # BTLMODE=1 + # (6<<3) | # 6+1=7TQ for PHSEG1 + # (1) # 1+1=2TQ for PRSEG + # ); #CNF3 with a PS2 length of 6TQ. - self.poke8(0x28, - 5 #5+1=6TQ - ); + #self.poke8(0x28, + # 5 #5+1=6TQ + # ); + + + #These are the new examples. + + if rate==125: + #125 kHz, 16 TQ, not quite as worked out above. + CNF1=0x04; + CNF2=0xB8; + CNF3=0x05; + elif rate==500: + #500 kHz, 20 TQ + CNF1=0x00; + CNF2=0xBA; + CNF3=0x07; + pass; + elif rate==100: + #100 kHz, 20 TQ + CNF1=0x04; + CNF2=0xBA; + CNF3=0x07; + pass; + elif rate==1000: + #1,000 kHz, 10 TQ + CNF1=0x00; + CNF2=0xA0; + CNF3=0x02; + print "WARNING: Because of chip errata, this probably won't work." + else: + print "Given unsupported rate of %i kHz."; + print "Defaulting to 125kHz."; + CNF1=0x04; + CNF2=0xB8; + CNF3=0x05; + self.poke8(0x2a,CNF1); + self.poke8(0x29,CNF2); + self.poke8(0x28,CNF3); + def MCPreset(self): """Reset the MCP2515 chip.""" self.SPItrans([0xC0]);