+
+
+ ################ SETTING BAUD RATE ################
+
+
+ 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.
+
+
+ #STORE the prior status
+ oldstatus=self.MCPcanstatint();
+ print "Setting rate of %i kHz." % rate;
+ #print "Current state is %s." % self.MCPcanstatstr();
+ self.MCPreqstatConfiguration();
+ # print "Switched to %s state." % self.MCPcanstatstr();
+
+
+ if rate>41 and rate<42:
+ # NOT CHECKED: based on kvaser website.
+ # Sets baud rate for 41.6 kbps
+ CNF1=0x8e;
+ CNF2=0xa3;
+ CNF3=0x05;
+ elif rate>10 and rate<11:
+ # NOT CHECKED: based on kvaser website.
+ # Sets baud rate for 10.4 kbps
+ CNF1=0xbb;
+ CNF2=0xa3;
+ CNF3=0x05;
+ elif 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);
+
+ # and now return to whatever state we were in before
+ self.MCPreqstat(oldstatus);
+ #print "Reverted to %s." % self.MCPcanstatstr();
+
+
+ ################# STATE MANAGEMENT ##################
+
+ def MCPcanstat(self):
+ """Get the CAN Status."""
+ return self.peek8(0x0E);
+ def MCPcanstatstr(self):
+ """Read the present status as a string."""
+ opmod=self.MCPcanstatint();
+ return self.MCPMODES[opmod];
+ def MCPcanstatint(self):
+ """Read present status as an int."""
+ return self.MCPcanstat()>>5;
+
+
+ def MCPreqstat(self, state):
+ """Set the CAN state."""
+ if state==0:
+ self.MCPreqstatNormal();
+ elif state==1:
+ self.MCPreqstatSleep();
+ elif state==2:
+ self.MCPreqstatLoopback();
+ elif state==3:
+ self.MCPreqstatListenOnly();
+ elif state==4:
+ self.MCPreqstatConfiguration();
+ else:
+ print "Invalid state entered; defaulting to Listen-Only"
+ self.MCPreqstatListenOnly();
+ def MCPreqstatNormal(self):
+ """Set the CAN state."""
+ state=0x0;
+ self.MCPbitmodify(0x0F,0xE0,(state<<5));
+ def MCPreqstatSleep(self):
+ """Set the CAN state."""
+ state=0x1;
+ self.MCPbitmodify(0x0F,0xE0,(state<<5));
+ def MCPreqstatLoopback(self):
+ """Set the CAN state."""
+ state=0x2;
+ self.MCPbitmodify(0x0F,0xE0,(state<<5));
+ def MCPreqstatListenOnly(self):
+ """Set the CAN state."""
+ state=0x3;
+ self.MCPbitmodify(0x0F,0xE0,(state<<5));
+ def MCPreqstatConfiguration(self):
+ """Set the CAN state."""
+ state=0x4;
+ self.MCPbitmodify(0x0F,0xE0,(state<<5));
+
+ #################### RX MANAGEMENT #####################
+