SDFileSystem with mutex for multiple SPI devices on the same SPI bus

Dependencies:   FATFileSystem

Dependents:   CC3000Nucleo401REProject

Fork of SDFileSystem by Mbed

Revision:
4:f0bd1dfd6f62
Parent:
3:7b35d1709458
diff -r 7b35d1709458 -r f0bd1dfd6f62 SDFileSystem.cpp
--- a/SDFileSystem.cpp	Mon Mar 17 14:34:01 2014 +0000
+++ b/SDFileSystem.cpp	Thu Oct 16 13:40:12 2014 +0000
@@ -120,8 +120,8 @@
 
 #define SD_DBG             0
 
-SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) :
-    FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs) {
+SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, Mutex & mutex, const char* name) :
+    FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs), _mutex(mutex) {
     _cs = 1;
 }
 
@@ -142,10 +142,12 @@
 int SDFileSystem::initialise_card() {
     // Set to 100kHz for initialisation, and clock card with cs = 1
     _spi.frequency(100000);
+    _mutex.lock();
     _cs = 1;
     for (int i = 0; i < 16; i++) {
         _spi.write(0xFF);
     }
+    _mutex.unlock();
     
     // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
     if (_cmd(0, 0) != R1_IDLE_STATE) {
@@ -240,6 +242,7 @@
 
 // PRIVATE FUNCTIONS
 int SDFileSystem::_cmd(int cmd, int arg) {
+    _mutex.lock();
     _cs = 0;
     
     // send a command
@@ -256,14 +259,17 @@
         if (!(response & 0x80)) {
             _cs = 1;
             _spi.write(0xFF);
+            _mutex.unlock();
             return response;
         }
     }
     _cs = 1;
     _spi.write(0xFF);
+    _mutex.unlock();
     return -1; // timeout
 }
 int SDFileSystem::_cmdx(int cmd, int arg) {
+    _mutex.lock();
     _cs = 0;
     
     // send a command
@@ -283,11 +289,13 @@
     }
     _cs = 1;
     _spi.write(0xFF);
+    _mutex.unlock();
     return -1; // timeout
 }
 
 
 int SDFileSystem::_cmd58() {
+    _mutex.lock();
     _cs = 0;
     int arg = 0;
     
@@ -309,15 +317,18 @@
             ocr |= _spi.write(0xFF) << 0;
             _cs = 1;
             _spi.write(0xFF);
+            _mutex.unlock();
             return response;
         }
     }
     _cs = 1;
     _spi.write(0xFF);
+    _mutex.unlock();
     return -1; // timeout
 }
 
 int SDFileSystem::_cmd8() {
+    _mutex.lock();
     _cs = 0;
     
     // send a command
@@ -338,15 +349,18 @@
             }
             _cs = 1;
             _spi.write(0xFF);
+            _mutex.unlock();
             return response[0];
         }
     }
     _cs = 1;
     _spi.write(0xFF);
+    _mutex.unlock();
     return -1; // timeout
 }
 
 int SDFileSystem::_read(uint8_t *buffer, uint32_t length) {
+    _mutex.lock();
     _cs = 0;
     
     // read until start byte (0xFF)
@@ -361,10 +375,12 @@
     
     _cs = 1;
     _spi.write(0xFF);
+    _mutex.unlock();
     return 0;
 }
 
 int SDFileSystem::_write(const uint8_t*buffer, uint32_t length) {
+    _mutex.lock();
     _cs = 0;
     
     // indicate start of block
@@ -383,6 +399,7 @@
     if ((_spi.write(0xFF) & 0x1F) != 0x05) {
         _cs = 1;
         _spi.write(0xFF);
+        _mutex.unlock();
         return 1;
     }
     
@@ -391,6 +408,7 @@
     
     _cs = 1;
     _spi.write(0xFF);
+    _mutex.unlock();
     return 0;
 }