Skip to content

libraries/sd: Fixed SDFileSystem when no card present #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions libraries/fs/sd/SDFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
#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) {
FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0) {
_cs = 1;
}

Expand Down Expand Up @@ -200,8 +200,12 @@ int SDFileSystem::initialise_card_v2() {
}

int SDFileSystem::disk_initialize() {
int i = initialise_card();
debug_if(SD_DBG, "init card = %d\n", i);
_is_initialized = initialise_card();
if (_is_initialized == 0) {
debug("Fail to initialize card\n");
return 1;
}
debug_if(SD_DBG, "init card = %d\n", _is_initialized);
_sectors = _sd_sectors();

// Set block length to 512 (CMD16)
Expand All @@ -215,6 +219,10 @@ int SDFileSystem::disk_initialize() {
}

int SDFileSystem::disk_write(const uint8_t *buffer, uint64_t block_number) {
if (!_is_initialized) {
return -1;
}

// set write address for single block (CMD24)
if (_cmd(24, block_number * cdv) != 0) {
return 1;
Expand All @@ -226,6 +234,10 @@ int SDFileSystem::disk_write(const uint8_t *buffer, uint64_t block_number) {
}

int SDFileSystem::disk_read(uint8_t *buffer, uint64_t block_number) {
if (!_is_initialized) {
return -1;
}

// set read address for single block (CMD17)
if (_cmd(17, block_number * cdv) != 0) {
return 1;
Expand All @@ -236,7 +248,15 @@ int SDFileSystem::disk_read(uint8_t *buffer, uint64_t block_number) {
return 0;
}

int SDFileSystem::disk_status() { return 0; }
int SDFileSystem::disk_status() {
// FATFileSystem::disk_status() returns 0 when initialized
if (_is_initialized) {
return 0;
} else {
return 1;
}
}

int SDFileSystem::disk_sync() { return 0; }
uint64_t SDFileSystem::disk_sectors() { return _sectors; }

Expand Down
1 change: 1 addition & 0 deletions libraries/fs/sd/SDFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class SDFileSystem : public FATFileSystem {
SPI _spi;
DigitalOut _cs;
int cdv;
int _is_initialized;
};

#endif