changeset 207:b903d0499d23 main

Improved M1 CAN handling for requests e.g. match message behavior.
author Bob Cook <bob@bobcookdev.com>
date Thu, 17 Apr 2014 21:50:53 -0700
parents d9828055da57
children a99efff0a236
files main/packages/avr/can/m1.cpp
diffstat 1 files changed, 24 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/main/packages/avr/can/m1.cpp	Thu Apr 17 21:49:16 2014 -0700
+++ b/main/packages/avr/can/m1.cpp	Thu Apr 17 21:50:53 2014 -0700
@@ -7,7 +7,7 @@
 //    
 //  This file implements CAN support for the mega(16,32,64)m1 chip.
 //
-//  Copyright (C) 2012 Bob Cook
+//  Copyright (C) 2012-2014 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
@@ -221,22 +221,7 @@
 
 #if ( PRJ_CPU_FREQ == 16000000 )
 
-#if defined( PRJ_M1CAN_CANBUS_20_KHZ )
-
-    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 + 1
-           ;
-
-    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 )
+#if defined( PRJ_M1CAN_CANBUS_125_KHZ )
 
     CANBT1 = ( 0 << BRP5 ) | ( 0 << BRP4 ) | ( 0 << BRP3 )      // BRP = 7 + 1
            | ( 1 << BRP2 ) | ( 1 << BRP1 ) | ( 1 << BRP0 )
@@ -367,6 +352,7 @@
 
         if ( ! g_tx_buffer.is_empty() )
         {
+            spinwait_delay_ms( 1 );
             send_from_tx_buffer();
         }
         else
@@ -374,7 +360,8 @@
             CANCDMOB &= ~( ( 1 << CONMOB1 ) | ( 1 << CONMOB0 ) );
         }
     }
-    else if ( CANSTMOB & ( 1 << RXOK ) )
+    
+    if ( CANSTMOB & ( 1 << RXOK ) )
     {
         read_into_rx_buffer();
         CANSTMOB &= ~( 1 << RXOK ); // clear interrupt reason
@@ -468,7 +455,25 @@
     
 bool m1can_request( const uint32_t& can_id )
 {
-    //--    Got space in the buffer?
+    //--    Disable the CAN interrupt to avoid specific race conditions.
+
+    m1_guard_can_int_disable can_int_disabled;    
+
+    //--    We might be able to send this request immediately, if the TX buffer is empty
+    //      and there is not a transmit already in progress.
+
+    if ( g_tx_buffer.is_empty() )
+    {
+        CANPAGE = 0;
+
+        if ( ( CANCDMOB & ( ( 1 << CONMOB1 ) | ( 1 << CONMOB0 ) ) ) == 0 )
+        {
+            send_request( can_id );
+            return true;
+        }
+    }
+
+    //--    Deferred transmit. Got space in the buffer?
 
     if ( g_tx_buffer.is_full() )
     {
@@ -485,13 +490,6 @@
     
     g_tx_buffer.push( msg );
 
-    //--    And send it now, if the TX process is idle.
-    
-    if ( ( CANCDMOB & ( ( 1 << CONMOB1 ) | ( 1 << CONMOB0 ) ) ) == 0 )
-    {
-        send_from_tx_buffer();
-    }
-
     return true;
 }