Index: /rtos_arduino/trunk/arduino_lib/libraries/NcesCan/mcp_can.cpp
===================================================================
--- /rtos_arduino/trunk/arduino_lib/libraries/NcesCan/mcp_can.cpp	(revision 194)
+++ /rtos_arduino/trunk/arduino_lib/libraries/NcesCan/mcp_can.cpp	(revision 195)
@@ -22,4 +22,14 @@
 */
 #include "mcp_can.h"
+
+#ifdef TOPPERS_WITH_ARDUINO
+#include "rca.h"
+#define ENTER_CRITICAL wai_sem(SPI_SEM);
+#define LEAVE_CRITICAL sig_sem(SPI_SEM);
+#else /* !TOPPERS_WITH_ARDUINO */
+#define WAIT_TIMEOUT
+#define ENTER_CRITICAL
+#define LEAVE_CRITICAL
+#endif /* TOPPERS_WITH_ARDUINO */
 
 #define spi_readwrite SPI.transfer
@@ -553,7 +563,10 @@
 {
     INT8U res;
-
+    
+    ENTER_CRITICAL
     SPI.begin();
     res = mcp2515_init(speedset);
+    LEAVE_CRITICAL
+      
     if (res == MCP2515_OK) return CAN_OK;
     else return CAN_FAILINIT;
@@ -566,5 +579,8 @@
 INT8U MCP_CAN::enableInterrupt(INT8U mask)
 {
+    ENTER_CRITICAL
     mcp2515_modifyRegister(MCP_CANINTE, mask, mask);
+    LEAVE_CRITICAL
+      
     return CAN_OK;
 }
@@ -576,5 +592,8 @@
 INT8U MCP_CAN::disableInterrupt(INT8U mask)
 {
+    ENTER_CRITICAL
     mcp2515_modifyRegister(MCP_CANINTE, mask, !mask);
+    LEAVE_CRITICAL
+      
     return CAN_OK;    
 }
@@ -597,4 +616,6 @@
 {
     INT8U res = MCP2515_OK;
+    
+    ENTER_CRITICAL;
 #if DEBUG_MODE
     Serial.print("Begin to set Mask!!\r\n");
@@ -605,4 +626,5 @@
     Serial.print("Enter setting mode fall\r\n"); 
 #endif
+    LEAVE_CRITICAL;
     return res;
     }
@@ -621,4 +643,5 @@
     Serial.print("Enter normal mode fall\r\n"); 
 #endif
+    LEAVE_CRITICAL;        
     return res;
   }
@@ -626,4 +649,5 @@
     Serial.print("set Mask success!!\r\n");
 #endif
+    LEAVE_CRITICAL;
     return res;
 }
@@ -636,4 +660,6 @@
 {
     INT8U res = MCP2515_OK;
+
+    ENTER_CRITICAL;    
 #if DEBUG_MODE
     Serial.print("Begin to set Filter!!\r\n");
@@ -645,4 +671,5 @@
       Serial.print("Enter setting mode fall\r\n"); 
 #endif
+      LEAVE_CRITICAL;
       return res;
     }
@@ -686,4 +713,5 @@
       delay(10);
 #endif
+      LEAVE_CRITICAL;        
       return res;
     }
@@ -693,5 +721,5 @@
     delay(10);
 #endif
-    
+    LEAVE_CRITICAL;
     return res;
 }
@@ -705,4 +733,5 @@
     CHECK_RXBID(rxbid);
 
+    ENTER_CRITICAL;
 #if (DEBUG_RXANY==1)
                                                                         /* enable both receive-buffers  */
@@ -732,5 +761,6 @@
     }
 #endif
-
+    LEAVE_CRITICAL;
+    
     return CAN_OK;
 }
@@ -748,8 +778,9 @@
     CHECK_TXBID(txbid);
     
+    ENTER_CRITICAL;
     mcp2515_write_canMsg(txbid, id, len, p_buf, ext, rtr);
     mcp2515_modifyRegister(MCP_CANINTF, intf_bit, 0);
     mcp2515_start_transmit( txbid );
-
+    LEAVE_CRITICAL;
     return CAN_OK;
 }
@@ -784,5 +815,6 @@
     INT8U stat_bit = (rxbid == 0)? MCP_STAT_RX0IF : MCP_STAT_RX1IF;
     INT8U intf_bit = (rxbid == 0)? MCP_RX0IF : MCP_RX1IF;
-      
+    
+    ENTER_CRITICAL;      
     stat = mcp2515_readStatus();
 
@@ -796,4 +828,5 @@
         res = CAN_NOMSG;
     }
+    LEAVE_CRITICAL;    
     return res;
 }
@@ -841,18 +874,22 @@
 INT8U MCP_CAN::checkReceive(INT8U rxbid)
 {
-    INT8U res;
+    INT8U res = CAN_NOMSG;
     CHECK_RXBID(rxbid);
+    
+    ENTER_CRITICAL;
     res = mcp2515_readStatus();                                         /* RXnIF in Bit 1 and 0         */
     if (rxbid == 0) {
         if ( (res & MCP_STAT_RX0IF) == MCP_STAT_RX0IF) {
-            return CAN_MSGAVAIL;
+            res = CAN_MSGAVAIL;
         }
     }
     else if (rxbid == 1) {
         if ( (res & MCP_STAT_RX1IF) == MCP_STAT_RX1IF ) {
-            return CAN_MSGAVAIL;
+            res = CAN_MSGAVAIL;
         }        
     }
-    return CAN_NOMSG;
+    LEAVE_CRITICAL;
+    
+    return res;
 }
 
@@ -863,15 +900,17 @@
 INT8U MCP_CAN::checkSend(INT8U txbid)
 {
-    INT8U res;
+    INT8U res = CAN_NOSENDWAIT;
     INT8U txbx_ctrl[MCP_N_TXBUFFERS] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL };
     
     CHECK_TXBID(txbid);
     
+    ENTER_CRITICAL;    
     res = mcp2515_readRegister(txbx_ctrl[txbid]);
 
     if ( (res & MCP_TXB_TXREQ_M) == MCP_TXB_TXREQ_M) {
-        return CAN_SENDWAIT;
-    }
-
+        res = CAN_SENDWAIT;
+    }
+    LEAVE_CRITICAL;
+    
     return CAN_NOSENDWAIT;
 }
@@ -883,14 +922,16 @@
 INT8U MCP_CAN::checkError(void)
 {
+    INT8U res = CAN_OK;
+    
+    ENTER_CRITICAL;        
     INT8U eflg = mcp2515_readRegister(MCP_EFLG);
 
     if ( eflg & MCP_EFLG_ERRORMASK ) 
     {
-        return CAN_CTRLERROR;
-    }
-    else 
-    {
-        return CAN_OK;
-    }
+        res = CAN_CTRLERROR;
+    }
+    LEAVE_CRITICAL;
+
+    return res;
 }
 
