X-Git-Url: http://git.rot13.org/?p=goodfet;a=blobdiff_plain;f=client%2FGoodFETMCPCAN.py;h=7182d3706d7e9cf80a5a8d0f296aa78649aeb871;hp=d4dc1590cc4f5fad9de1cd08d9aa8146a1e97b91;hb=4ac8acb7df57251bc53db2102da617269801e793;hpb=9565c64a06b7302cd820214f58ea0970d4f1e07a diff --git a/client/GoodFETMCPCAN.py b/client/GoodFETMCPCAN.py index d4dc159..7182d37 100644 --- a/client/GoodFETMCPCAN.py +++ b/client/GoodFETMCPCAN.py @@ -24,8 +24,104 @@ class GoodFETMCPCAN(GoodFETSPI): self.SPIsetup(); self.MCPreset(); #Reset the chip. - #Loopback mode for testing. + # We're going to set some registers, so we must be in config + # 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? + self.poke8(0x60,0x66); #Wanted FF, but some bits are reserved. + + #Set the default rate. + self.MCPsetrate(); + + #Array of supported rates. + MCPrates=[83.3, 100, 125, + 250, 500, 1000]; + + 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 + # rambling, incoherent, and unchanged after I get the infernal + # thing working. + + # First, we must chose a Time Quanta (QT) which is used to + # define the durations of these segments. Section 5.3 + # suggests setting BRP<5:0> to 0x04 to get a TQ=500ns, as a 20 + # MHz crystal gives a clock period of 50ns. This way, for 125 + # kHz communication, the bit time must be 16 TQ. + + # A bit consists of four parts: + # 1: SyncSeg 1 TQ + # 2: PropSeg 2 TQ + # 3: PhaseSeg1 (PS1) 7 TQ + # 4: PhaseSeg2 (PS2) 6 TQ + + # CNF1 with a prescaler of 4 and a SJW of 1 TQ. SJW of 4 + # might be more stable. + #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 + # ); + + #CNF3 with a PS2 length of 6TQ. + #self.poke8(0x28, + # 5 #5+1=6TQ + # ); + + + print "Setting rate of %i kHz." % rate; + + #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==100: + #100 kHz, 20 TQ + CNF1=0x04; + CNF2=0xBA; + CNF3=0x07; + elif rate>83 and rate<83.5: + #83+1/3 kHz, 8 TQ + # 0.04% error from 83.30 + CNF1=0x0E; + CNF2=0x90; + CNF3=0x02; + elif rate==250: + #256 kHz, 20 TQ + CNF1=0x01; + CNF2=0xBA; + CNF3=0x07; + elif rate==500: + #500 kHz, 20 TQ + CNF1=0x00; + CNF2=0xBA; + CNF3=0x07; + 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." % rate; + 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]); @@ -129,4 +225,8 @@ class GoodFETMCPCAN(GoodFETSPI): def poke8(self,adr,val): """Poke a value into RAM. Untested""" self.SPItrans([0x02,adr&0xFF,val&0xFF]); + newval=self.peek8(adr); + if newval!=val: + print "Failed to poke %02x to %02x. Got %02x." % (adr,val,newval); + print "Are you not in idle mode?"; return val;