Skip to content

[SDFileSystem] Enable / Disable serial debug. #3093

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 2 commits into from
Oct 26, 2016
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
33 changes: 19 additions & 14 deletions features/unsupported/fs/sd/SDFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs,
#define SDCARD_V2HC 3

int SDFileSystem::initialise_card() {
_dbg = SD_DBG;
// Set to SCK for initialisation, and clock card with cs = 1
_spi.lock();
_spi.frequency(_init_sck);
Expand All @@ -158,7 +159,7 @@ int SDFileSystem::initialise_card() {

// send CMD0, should return with all zeros except IDLE STATE set (bit 0)
if (_cmd(0, 0) != R1_IDLE_STATE) {
debug("No disk, or could not put SD card in to SPI idle state\n");
debug_if(_dbg, "No disk, or could not put SD card in to SPI idle state\n");
return SDCARD_FAIL;
}

Expand All @@ -169,7 +170,7 @@ int SDFileSystem::initialise_card() {
} else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
return initialise_card_v1();
} else {
debug("Not in idle state after sending CMD8 (not an SD card?)\n");
debug_if(_dbg, "Not in idle state after sending CMD8 (not an SD card?)\n");
return SDCARD_FAIL;
}
}
Expand All @@ -179,12 +180,12 @@ int SDFileSystem::initialise_card_v1() {
_cmd(55, 0);
if (_cmd(41, 0) == 0) {
cdv = 512;
debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
debug_if(_dbg, "\n\rInit: SEDCARD_V1\n\r");
return SDCARD_V1;
}
}

debug("Timeout waiting for v1.x card\n");
debug_if(_dbg, "Timeout waiting for v1.x card\n");
return SDCARD_FAIL;
}

Expand All @@ -195,30 +196,30 @@ int SDFileSystem::initialise_card_v2() {
_cmd(55, 0);
if (_cmd(41, 0x40000000) == 0) {
_cmd58();
debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
debug_if(_dbg, "\n\rInit: SDCARD_V2\n\r");
cdv = 1;
return SDCARD_V2;
}
}

debug("Timeout waiting for v2.x card\n");
debug_if(_dbg, "Timeout waiting for v2.x card\n");
return SDCARD_FAIL;
}

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

// Set block length to 512 (CMD16)
if (_cmd(16, 512) != 0) {
debug("Set 512-byte block timed out\n");
debug_if(_dbg, "Set 512-byte block timed out\n");
unlock();
return 1;
}
Expand Down Expand Up @@ -291,6 +292,10 @@ uint32_t SDFileSystem::disk_sectors() {
return sectors;
}

void SDFileSystem::debug(bool dbg){
_dbg = dbg;
}


// PRIVATE FUNCTIONS
int SDFileSystem::_cmd(int cmd, int arg) {
Expand Down Expand Up @@ -487,13 +492,13 @@ uint32_t SDFileSystem::_sd_sectors() {

// CMD9, Response R2 (R1 byte + 16-byte block read)
if (_cmdx(9, 0) != 0) {
debug("Didn't get a response from the disk\n");
debug_if(_dbg, "Didn't get a response from the disk\n");
return 0;
}

uint8_t csd[16];
if (_read(csd, 16) != 0) {
debug("Couldn't read csd response from disk\n");
debug_if(_dbg, "Couldn't read csd response from disk\n");
return 0;
}

Expand All @@ -516,18 +521,18 @@ uint32_t SDFileSystem::_sd_sectors() {
blocknr = (c_size + 1) * mult;
capacity = blocknr * block_len;
blocks = capacity / 512;
debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
debug_if(_dbg, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
break;

case 1:
cdv = 1;
hc_c_size = ext_bits(csd, 63, 48);
blocks = (hc_c_size+1)*1024;
debug_if(SD_DBG, "\n\rSDHC Card \n\rhc_c_size: %d\n\rcapacity: %lld \n\rsectors: %lld\n\r", hc_c_size, blocks*512, blocks);
debug_if(_dbg, "\n\rSDHC Card \n\rhc_c_size: %d\n\rcapacity: %lld \n\rsectors: %lld\n\r", hc_c_size, blocks*512, blocks);
break;

default:
debug("CSD struct unsupported\r\n");
debug_if(_dbg, "CSD struct unsupported\r\n");
return 0;
};
return blocks;
Expand Down
2 changes: 2 additions & 0 deletions features/unsupported/fs/sd/SDFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class SDFileSystem : public FATFileSystem {
virtual int disk_write(const uint8_t* buffer, uint32_t block_number, uint32_t count);
virtual int disk_sync();
virtual uint32_t disk_sectors();
void debug(bool dbg);

protected:

Expand All @@ -84,6 +85,7 @@ class SDFileSystem : public FATFileSystem {
DigitalOut _cs;
int cdv;
int _is_initialized;
bool _dbg;
};

#endif