Fork of the SD file system library from simon. Changed constructor allows to inject the SPI handler in order to reuse it.
Fork of SDFileSystem by
Diff: SDFileSystem.cpp
- Revision:
- 4:498b360ea482
- Parent:
- 0:b1ddfc9a9b25
- Child:
- 5:9050a1baede1
--- a/SDFileSystem.cpp Sun Mar 02 12:44:07 2014 +0000 +++ b/SDFileSystem.cpp Sun Mar 02 18:14:50 2014 +0000 @@ -118,8 +118,9 @@ #define SD_COMMAND_TIMEOUT 5000 -SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) : - FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs) { +SDFileSystem::SDFileSystem(SPI* spiHandler, PinName cs, const char* name) : + FATFileSystem(name), _cs(cs) { + _spi = spiHandler; _cs = 1; } @@ -142,12 +143,10 @@ #define SDCARD_V2 2 #define SDCARD_V2HC 3 -int SDFileSystem::initialise_card() { - // Set to 100kHz for initialisation, and clock card with cs = 1 - _spi.frequency(100000); +int SDFileSystem::initialise_card() { _cs = 1; for(int i=0; i<16; i++) { - _spi.write(0xFF); + _spi->write(0xFF); } // send CMD0, should return with all zeros except IDLE STATE set (bit 0) @@ -207,8 +206,7 @@ fprintf(stderr, "Set 512-byte block timed out\n"); return 1; } - - _spi.frequency(1000000); // Set to 1MHz for data transfer + return 0; } @@ -244,46 +242,46 @@ _cs = 0; // send a command - _spi.write(0x40 | cmd); - _spi.write(arg >> 24); - _spi.write(arg >> 16); - _spi.write(arg >> 8); - _spi.write(arg >> 0); - _spi.write(0x95); + _spi->write(0x40 | cmd); + _spi->write(arg >> 24); + _spi->write(arg >> 16); + _spi->write(arg >> 8); + _spi->write(arg >> 0); + _spi->write(0x95); // wait for the repsonse (response[7] == 0) for(int i=0; i<SD_COMMAND_TIMEOUT; i++) { - int response = _spi.write(0xFF); + int response = _spi->write(0xFF); if(!(response & 0x80)) { _cs = 1; - _spi.write(0xFF); + _spi->write(0xFF); return response; } } _cs = 1; - _spi.write(0xFF); + _spi->write(0xFF); return -1; // timeout } int SDFileSystem::_cmdx(int cmd, int arg) { _cs = 0; // send a command - _spi.write(0x40 | cmd); - _spi.write(arg >> 24); - _spi.write(arg >> 16); - _spi.write(arg >> 8); - _spi.write(arg >> 0); - _spi.write(0x95); + _spi->write(0x40 | cmd); + _spi->write(arg >> 24); + _spi->write(arg >> 16); + _spi->write(arg >> 8); + _spi->write(arg >> 0); + _spi->write(0x95); // wait for the repsonse (response[7] == 0) for(int i=0; i<SD_COMMAND_TIMEOUT; i++) { - int response = _spi.write(0xFF); + int response = _spi->write(0xFF); if(!(response & 0x80)) { return response; } } _cs = 1; - _spi.write(0xFF); + _spi->write(0xFF); return -1; // timeout } @@ -293,29 +291,29 @@ int arg = 0; // send a command - _spi.write(0x40 | 58); - _spi.write(arg >> 24); - _spi.write(arg >> 16); - _spi.write(arg >> 8); - _spi.write(arg >> 0); - _spi.write(0x95); + _spi->write(0x40 | 58); + _spi->write(arg >> 24); + _spi->write(arg >> 16); + _spi->write(arg >> 8); + _spi->write(arg >> 0); + _spi->write(0x95); // wait for the repsonse (response[7] == 0) for(int i=0; i<SD_COMMAND_TIMEOUT; i++) { - int response = _spi.write(0xFF); + int response = _spi->write(0xFF); if(!(response & 0x80)) { - int ocr = _spi.write(0xFF) << 24; - ocr |= _spi.write(0xFF) << 16; - ocr |= _spi.write(0xFF) << 8; - ocr |= _spi.write(0xFF) << 0; + int ocr = _spi->write(0xFF) << 24; + ocr |= _spi->write(0xFF) << 16; + ocr |= _spi->write(0xFF) << 8; + ocr |= _spi->write(0xFF) << 0; // printf("OCR = 0x%08X\n", ocr); _cs = 1; - _spi.write(0xFF); + _spi->write(0xFF); return response; } } _cs = 1; - _spi.write(0xFF); + _spi->write(0xFF); return -1; // timeout } @@ -323,28 +321,28 @@ _cs = 0; // send a command - _spi.write(0x40 | 8); // CMD8 - _spi.write(0x00); // reserved - _spi.write(0x00); // reserved - _spi.write(0x01); // 3.3v - _spi.write(0xAA); // check pattern - _spi.write(0x87); // crc + _spi->write(0x40 | 8); // CMD8 + _spi->write(0x00); // reserved + _spi->write(0x00); // reserved + _spi->write(0x01); // 3.3v + _spi->write(0xAA); // check pattern + _spi->write(0x87); // crc // wait for the repsonse (response[7] == 0) for(int i=0; i<SD_COMMAND_TIMEOUT * 1000; i++) { char response[5]; - response[0] = _spi.write(0xFF); + response[0] = _spi->write(0xFF); if(!(response[0] & 0x80)) { for(int j=1; j<5; j++) { - response[i] = _spi.write(0xFF); + response[i] = _spi->write(0xFF); } _cs = 1; - _spi.write(0xFF); + _spi->write(0xFF); return response[0]; } } _cs = 1; - _spi.write(0xFF); + _spi->write(0xFF); return -1; // timeout } @@ -352,17 +350,17 @@ _cs = 0; // read until start byte (0xFF) - while(_spi.write(0xFF) != 0xFE); + while(_spi->write(0xFF) != 0xFE); // read data for(int i=0; i<length; i++) { - buffer[i] = _spi.write(0xFF); + buffer[i] = _spi->write(0xFF); } - _spi.write(0xFF); // checksum - _spi.write(0xFF); + _spi->write(0xFF); // checksum + _spi->write(0xFF); _cs = 1; - _spi.write(0xFF); + _spi->write(0xFF); return 0; } @@ -370,29 +368,29 @@ _cs = 0; // indicate start of block - _spi.write(0xFE); + _spi->write(0xFE); // write the data for(int i=0; i<length; i++) { - _spi.write(buffer[i]); + _spi->write(buffer[i]); } // write the checksum - _spi.write(0xFF); - _spi.write(0xFF); + _spi->write(0xFF); + _spi->write(0xFF); // check the repsonse token - if((_spi.write(0xFF) & 0x1F) != 0x05) { + if((_spi->write(0xFF) & 0x1F) != 0x05) { _cs = 1; - _spi.write(0xFF); + _spi->write(0xFF); return 1; } // wait for write to finish - while(_spi.write(0xFF) == 0); + while(_spi->write(0xFF) == 0); _cs = 1; - _spi.write(0xFF); + _spi->write(0xFF); return 0; }