changeset 154:7374d1cd677f main

Add new bus speed options for the AVR CAN support.
author Bob Cook <bob@bobcookdev.com>
date Sun, 26 Aug 2012 15:35:05 -0700
parents cf96319a494f
children a5289fe64ef6
files main/packages/avr/can/m1.cpp main/packages/avr/can/m1.h main/packages/avr/can/mcp2515.cpp main/packages/avr/can/mcp2515.h
diffstat 4 files changed, 78 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/main/packages/avr/can/m1.cpp	Tue Jul 17 19:44:21 2012 -0700
+++ b/main/packages/avr/can/m1.cpp	Sun Aug 26 15:35:05 2012 -0700
@@ -7,7 +7,7 @@
 //    
 //  This file implements CAN support for the mega(16,32,64)m1 chip.
 //
-//  Copyright (C) 2011 Bob Cook
+//  Copyright (C) 2012 Bob Cook
 //
 //  Permission is hereby granted, free of charge, to any person obtaining a copy
 //  of this software and associated documentation files (the "Software"), to deal
@@ -223,36 +223,63 @@
 
 #if defined( PRJ_M1CAN_CANBUS_20_KHZ )
 
-    CANBT1 = ( 0 << BRP5 ) | ( 1 << BRP4 ) | ( 0 << BRP3 ) | ( 0 << BRP2 )  // BRP = 19
-           | ( 1 << BRP1 ) | ( 1 << BRP0 )
+    CANBT1 = ( 0 << BRP5 ) | ( 1 << BRP4 ) | ( 0 << BRP3 )      // BRP = 19 + 1
+           | ( 0 << BRP2 ) | ( 1 << BRP1 ) | ( 1 << BRP0 )
            ;
 
     CANBT2 = ( 0 << SJW1 ) | ( 0 << SJW0 )                      // SWJ = 0
-           | ( 0 << PRS2 ) | ( 1 << PRS1 ) | ( 0 << PRS0 )      // PRS = 2
+           | ( 0 << PRS2 ) | ( 1 << PRS1 ) | ( 0 << PRS0 )      // PRS = 2 + 1
            ;
 
-    CANBT3 = ( 1 << PHS22 ) | ( 1 << PHS21 ) | ( 1 << PHS20 )   // PHSEG2 = 7
-           | ( 1 << PHS12 ) | ( 1 << PHS11 ) | ( 1 << PHS10 )   // PHSEG1 = 7
+    CANBT3 = ( 1 << PHS22 ) | ( 1 << PHS21 ) | ( 1 << PHS20 )   // PHSEG2 = 7 + 1
+           | ( 1 << PHS12 ) | ( 1 << PHS11 ) | ( 1 << PHS10 )   // PHSEG1 = 7 + 1
            | ( 0 << SMP )                                       // SMP = 0
            ;
 
 #elif defined( PRJ_M1CAN_CANBUS_125_KHZ )
 
-    CANBT1 = ( 0 << BRP5 ) | ( 0 << BRP4 ) | ( 0 << BRP3 )      // BRP = 8
+    CANBT1 = ( 0 << BRP5 ) | ( 0 << BRP4 ) | ( 0 << BRP3 )      // BRP = 7 + 1
            | ( 1 << BRP2 ) | ( 1 << BRP1 ) | ( 1 << BRP0 )
            ;
-//CANBT1 = 0x0E; // (BRP_VALUE - 1) << 1;
 
     CANBT2 = ( 0 << SJW1 ) | ( 0 << SJW0 )                      // SWJ = 0
-           | ( 1 << PRS2 ) | ( 1 << PRS1 ) | ( 0 << PRS0 )      // PRS = 6
+           | ( 1 << PRS2 ) | ( 1 << PRS1 ) | ( 0 << PRS0 )      // PRS = 6 + 1
            ;
-//CANBT2 = 0x0C; // ((SJW_VALUE - 1) << 5) | ((PROP_SEG - 1) << 1);
 
-    CANBT3 = ( 0 << PHS22 ) | ( 1 << PHS21 ) | ( 1 << PHS20 )   // PHSEG2 = 4
-           | ( 0 << PHS12 ) | ( 1 << PHS11 ) | ( 1 << PHS10 )   // PHSEG1 = 4
+    CANBT3 = ( 0 << PHS22 ) | ( 1 << PHS21 ) | ( 1 << PHS20 )   // PHSEG2 = 3 + 1
+           | ( 0 << PHS12 ) | ( 1 << PHS11 ) | ( 1 << PHS10 )   // PHSEG1 = 3 + 1
            | ( 1 << SMP )                                       // SMP = 1
            ;
-//CANBT3 = 0x37; // ((PHASE_SEG_2 - 1) << 4) | ((PHASE_SEG_1 - 1) << 1) | 1;
+
+#elif defined( PRJ_M1CAN_CANBUS_250_KHZ )
+
+    CANBT1 = ( 0 << BRP5 ) | ( 0 << BRP4 ) | ( 0 << BRP3 )      // BRP = 3 + 1
+           | ( 0 << BRP2 ) | ( 1 << BRP1 ) | ( 1 << BRP0 )
+           ;
+
+    CANBT2 = ( 0 << SJW1 ) | ( 0 << SJW0 )                      // SWJ = 0 + 1
+           | ( 1 << PRS2 ) | ( 1 << PRS1 ) | ( 0 << PRS0 )      // PRS = 6 + 1
+           ;
+
+    CANBT3 = ( 0 << PHS22 ) | ( 1 << PHS21 ) | ( 1 << PHS20 )   // PHSEG2 = 3 + 1
+           | ( 0 << PHS12 ) | ( 1 << PHS11 ) | ( 1 << PHS10 )   // PHSEG1 = 3 + 1
+           | ( 1 << SMP )                                       // SMP = 1
+           ;
+
+#elif defined( PRJ_M1CAN_CANBUS_500_KHZ )
+
+    CANBT1 = ( 0 << BRP5 ) | ( 0 << BRP4 ) | ( 0 << BRP3 )      // BRP = 1 + 1
+           | ( 0 << BRP2 ) | ( 0 << BRP1 ) | ( 1 << BRP0 )
+           ;
+
+    CANBT2 = ( 0 << SJW1 ) | ( 0 << SJW0 )                      // SWJ = 0 + 1
+           | ( 1 << PRS2 ) | ( 1 << PRS1 ) | ( 0 << PRS0 )      // PRS = 6 + 1
+           ;
+
+    CANBT3 = ( 0 << PHS22 ) | ( 1 << PHS21 ) | ( 1 << PHS20 )   // PHSEG2 = 3 + 1
+           | ( 0 << PHS12 ) | ( 1 << PHS11 ) | ( 1 << PHS10 )   // PHSEG1 = 3 + 1
+           | ( 1 << SMP )                                       // SMP = 1
+           ;
 
 #else
 #error Unsupported CAN bus speed selection!
--- a/main/packages/avr/can/m1.h	Tue Jul 17 19:44:21 2012 -0700
+++ b/main/packages/avr/can/m1.h	Sun Aug 26 15:35:05 2012 -0700
@@ -7,7 +7,7 @@
 //    
 //  This file implements CAN support for the mega(16,32,64)m1 chip.
 //
-//  Copyright (C) 2011 Bob Cook
+//  Copyright (C) 2012 Bob Cook
 //
 //  Permission is hereby granted, free of charge, to any person obtaining a copy
 //  of this software and associated documentation files (the "Software"), to deal
@@ -50,6 +50,8 @@
 //
 //      + PRJ_M1CAN_CANBUS_20_KHZ
 //      + PRJ_M1CAN_CANBUS_125_KHZ
+//      + PRJ_M1CAN_CANBUS_250_KHZ
+//      + PRJ_M1CAN_CANBUS_500_KHZ
 //
 //  enable the debug support with:
 //
--- a/main/packages/avr/can/mcp2515.cpp	Tue Jul 17 19:44:21 2012 -0700
+++ b/main/packages/avr/can/mcp2515.cpp	Sun Aug 26 15:35:05 2012 -0700
@@ -7,7 +7,7 @@
 //    
 //  This file implements CAN support using the Microchip MCP2515 via SPI.
 //
-//  Copyright (C) 2007 Bob Cook
+//  Copyright (C) 2012 Bob Cook
 //
 //  Permission is hereby granted, free of charge, to any person obtaining a copy
 //  of this software and associated documentation files (the "Software"), to deal
@@ -606,6 +606,36 @@
     write_register( MCP_REGISTER_CNF3, ( ( 0 << 7 ) | ( 0 << 6 ) | ( 5 ) ) );
 
 
+#elif defined( PRJ_MCP2515_CANBUS_250_KHZ ) && defined( PRJ_MCP2515_FREQUENCY_16_MHZ )
+
+    //--    SWJ = 1 (2 X TQ), BRP = 1
+
+    write_register( MCP_REGISTER_CNF1, ( ( 1 << 6 ) | ( 1 ) ) );
+
+    //--    BLTMODE = 1, SAM = 0, PHSEG1 = 5 (6 x TQ), PRSEG = 2 (3 x TQ)
+
+    write_register( MCP_REGISTER_CNF2, ( ( 1 << 7 ) | ( 0 << 6 ) | ( 5 << 3 ) | ( 2 ) ) );
+        
+    //--    SOF = 0, WAKFIL = 0, PHSEG2 = 5 (6 x TQ)
+
+    write_register( MCP_REGISTER_CNF3, ( ( 0 << 7 ) | ( 0 << 6 ) | ( 5 ) ) );
+
+
+#elif defined( PRJ_MCP2515_CANBUS_500_KHZ ) && defined( PRJ_MCP2515_FREQUENCY_16_MHZ )
+
+    //--    SWJ = 0 (1 X TQ), BRP = 0
+
+    write_register( MCP_REGISTER_CNF1, ( ( 0 << 6 ) | ( 0 ) ) );
+
+    //--    BLTMODE = 1, SAM = 0, PHSEG1 = 5 (6 x TQ), PRSEG = 2 (3 x TQ)
+
+    write_register( MCP_REGISTER_CNF2, ( ( 1 << 7 ) | ( 0 << 6 ) | ( 5 << 3 ) | ( 2 ) ) );
+        
+    //--    SOF = 0, WAKFIL = 0, PHSEG2 = 5 (6 x TQ)
+
+    write_register( MCP_REGISTER_CNF3, ( ( 0 << 7 ) | ( 0 << 6 ) | ( 5 ) ) );
+
+
 #else
 #error Unsupported CAN bus speed selection!
 #endif
--- a/main/packages/avr/can/mcp2515.h	Tue Jul 17 19:44:21 2012 -0700
+++ b/main/packages/avr/can/mcp2515.h	Sun Aug 26 15:35:05 2012 -0700
@@ -7,7 +7,7 @@
 //    
 //  This file implements CAN support using the Microchip MCP2515 via SPI.
 //
-//  Copyright (C) 2007 Bob Cook
+//  Copyright (C) 2012 Bob Cook
 //
 //      This program is free software; you can redistribute it and/or modify it
 //      under the terms of the GNU General Public License as published by the
@@ -60,10 +60,13 @@
 //
 //      + PRJ_MCP2515_CANBUS_20_KHZ
 //      + PRJ_MCP2515_CANBUS_125_KHZ
+//      + PRJ_MCP2515_CANBUS_250_KHZ
+//      + PRJ_MCP2515_CANBUS_500_KHZ
 //
 //  specify the clock frequency for the MCP2515 with one of:
 //
 //      + PRJ_MCP2515_FREQUENCY_4_MHZ
+//      + PRJ_MCP2515_FREQUENCY_8_MHZ
 //      + PRJ_MCP2515_FREQUENCY_16_MHZ
 //
 //  enable the debug support with: