SDFileSystem with mutex for multiple SPI devices on the same SPI bus
Dependents: CC3000Nucleo401REProject
Fork of SDFileSystem by
Revision 4:f0bd1dfd6f62, committed 2014-10-16
- Comitter:
- vpcola
- Date:
- Thu Oct 16 13:40:12 2014 +0000
- Parent:
- 3:7b35d1709458
- Commit message:
- Added mutex for multiple devices on the same SPI bus
Changed in this revision
| SDFileSystem.cpp | Show annotated file Show diff for this revision Revisions of this file |
| SDFileSystem.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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;
}
--- a/SDFileSystem.h Mon Mar 17 14:34:01 2014 +0000
+++ b/SDFileSystem.h Thu Oct 16 13:40:12 2014 +0000
@@ -23,6 +23,7 @@
#define MBED_SDFILESYSTEM_H
#include "mbed.h"
+#include "rtos.h"
#include "FATFileSystem.h"
#include <stdint.h>
@@ -52,7 +53,7 @@
* @param cs DigitalOut pin used as SD Card chip select
* @param name The name used to access the virtual filesystem
*/
- SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name);
+ SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, Mutex & mutex, const char* name);
virtual int disk_initialize();
virtual int disk_status();
@@ -78,6 +79,7 @@
SPI _spi;
DigitalOut _cs;
+ Mutex & _mutex;
int cdv;
};
