Index: /rtos_arduino/trunk/arduino_lib/libraries/SD/src/utility/Sd2Card.cpp
===================================================================
--- /rtos_arduino/trunk/arduino_lib/libraries/SD/src/utility/Sd2Card.cpp	(revision 193)
+++ /rtos_arduino/trunk/arduino_lib/libraries/SD/src/utility/Sd2Card.cpp	(revision 194)
@@ -21,4 +21,15 @@
 #include <Arduino.h>
 #include "Sd2Card.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 */
+
 //------------------------------------------------------------------------------
 #ifndef SOFTWARE_SPI
@@ -200,4 +211,5 @@
  */
 uint8_t Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
+  ENTER_CRITICAL;
   if (!eraseSingleBlockEnable()) {
     error(SD_CARD_ERROR_ERASE_SINGLE_BLOCK);
@@ -219,8 +231,10 @@
   }
   chipSelectHigh();
+  LEAVE_CRITICAL;
   return true;
 
  fail:
   chipSelectHigh();
+  LEAVE_CRITICAL;    
   return false;
 }
@@ -253,5 +267,6 @@
   uint16_t t0 = (uint16_t)millis();
   uint32_t arg;
-
+    
+  ENTER_CRITICAL;
   // set pin modes
   pinMode(chipSelectPin_, OUTPUT);
@@ -292,5 +307,4 @@
 #endif
 #endif
-
   chipSelectLow();
 
@@ -335,5 +349,5 @@
   }
   chipSelectHigh();
-
+  LEAVE_CRITICAL;
 #ifndef SOFTWARE_SPI
   return setSckRate(sckRateID);
@@ -344,4 +358,5 @@
  fail:
   chipSelectHigh();
+  LEAVE_CRITICAL;
   return false;
 }
@@ -361,6 +376,8 @@
  */
 void Sd2Card::partialBlockRead(uint8_t value) {
+  ENTER_CRITICAL;    
   readEnd();
   partialBlockRead_ = value;
+  LEAVE_CRITICAL;    
 }
 //------------------------------------------------------------------------------
@@ -392,4 +409,5 @@
   uint16_t n;
   if (count == 0) return true;
+  ENTER_CRITICAL;
   if ((count + offset) > 512) {
     goto fail;
@@ -450,7 +468,9 @@
     readEnd();
   }
+  LEAVE_CRITICAL;    
   return true;
 
  fail:
+  LEAVE_CRITICAL;    
   chipSelectHigh();
   return false;
@@ -483,4 +503,5 @@
 uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) {
   uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
+  ENTER_CRITICAL;
   if (cardCommand(cmd, 0)) {
     error(SD_CARD_ERROR_READ_REG);
@@ -493,8 +514,10 @@
   spiRec();  // get second crc byte
   chipSelectHigh();
+  LEAVE_CRITICAL;
   return true;
 
  fail:
   chipSelectHigh();
+  LEAVE_CRITICAL;    
   return false;
 }
@@ -582,4 +605,5 @@
  */
 uint8_t Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
+  ENTER_CRITICAL;
 #if SD_PROTECT_BLOCK_ZERO
   // don't allow write to first block
@@ -609,8 +633,10 @@
   }
   chipSelectHigh();
+  LEAVE_CRITICAL;    
   return true;
 
  fail:
   chipSelectHigh();
+  LEAVE_CRITICAL;    
   return false;
 }
@@ -618,11 +644,17 @@
 /** Write one data block in a multiple block write sequence */
 uint8_t Sd2Card::writeData(const uint8_t* src) {
+  uint8_t ret;  
+  ENTER_CRITICAL;
   // wait for previous write to finish
   if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
     error(SD_CARD_ERROR_WRITE_MULTIPLE);
     chipSelectHigh();
-    return false;
-  }
-  return writeData(WRITE_MULTIPLE_TOKEN, src);
+    ret = false;  
+  }
+  else {
+      ret = writeData(WRITE_MULTIPLE_TOKEN, src);
+  }
+  LEAVE_CRITICAL;
+  return ret;
 }
 //------------------------------------------------------------------------------
@@ -678,4 +710,5 @@
  */
 uint8_t Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
+  ENTER_CRITICAL;
 #if SD_PROTECT_BLOCK_ZERO
   // don't allow write to first block
@@ -696,8 +729,10 @@
     goto fail;
   }
+  LEAVE_CRITICAL;
   return true;
 
  fail:
   chipSelectHigh();
+  LEAVE_CRITICAL;    
   return false;
 }
@@ -709,8 +744,10 @@
  */
 uint8_t Sd2Card::writeStop(void) {
+  ENTER_CRITICAL;    
   if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
   spiSend(STOP_TRAN_TOKEN);
   if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
   chipSelectHigh();
+  LEAVE_CRITICAL;    
   return true;
 
@@ -718,4 +755,5 @@
   error(SD_CARD_ERROR_STOP_TRAN);
   chipSelectHigh();
-  return false;
-}
+  LEAVE_CRITICAL;    
+  return false;
+}
