[SDFileSystem] Enable / Disable serial debug.

+ Added ``void debug(bool dbg)`` method to allow enabling/disabling
  serial debug at runtime.
+ Replaced calls to ``debug`` with ``debug_if`` to prevent messges
  from being thrown via serial when debug is disabled.

Signed-off-by: Bruno Monteiro Pires <brunomonteiropires@gmail.com>
pull/3093/head
Bruno Monteiro Pires 2016-10-20 14:20:32 -02:00
parent 9fce16dddf
commit 0e8ba855a0
2 changed files with 21 additions and 14 deletions

View File

@ -147,6 +147,7 @@ SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs,
#define SDCARD_V2HC 3 #define SDCARD_V2HC 3
int SDFileSystem::initialise_card() { int SDFileSystem::initialise_card() {
_dbg = SD_DBG;
// Set to SCK for initialisation, and clock card with cs = 1 // Set to SCK for initialisation, and clock card with cs = 1
_spi.lock(); _spi.lock();
_spi.frequency(_init_sck); _spi.frequency(_init_sck);
@ -158,7 +159,7 @@ int SDFileSystem::initialise_card() {
// send CMD0, should return with all zeros except IDLE STATE set (bit 0) // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
if (_cmd(0, 0) != R1_IDLE_STATE) { 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; return SDCARD_FAIL;
} }
@ -169,7 +170,7 @@ int SDFileSystem::initialise_card() {
} else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) { } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
return initialise_card_v1(); return initialise_card_v1();
} else { } 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; return SDCARD_FAIL;
} }
} }
@ -179,12 +180,12 @@ int SDFileSystem::initialise_card_v1() {
_cmd(55, 0); _cmd(55, 0);
if (_cmd(41, 0) == 0) { if (_cmd(41, 0) == 0) {
cdv = 512; cdv = 512;
debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r"); debug_if(_dbg, "\n\rInit: SEDCARD_V1\n\r");
return SDCARD_V1; return SDCARD_V1;
} }
} }
debug("Timeout waiting for v1.x card\n"); debug_if(_dbg, "Timeout waiting for v1.x card\n");
return SDCARD_FAIL; return SDCARD_FAIL;
} }
@ -195,13 +196,13 @@ int SDFileSystem::initialise_card_v2() {
_cmd(55, 0); _cmd(55, 0);
if (_cmd(41, 0x40000000) == 0) { if (_cmd(41, 0x40000000) == 0) {
_cmd58(); _cmd58();
debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r"); debug_if(_dbg, "\n\rInit: SDCARD_V2\n\r");
cdv = 1; cdv = 1;
return SDCARD_V2; return SDCARD_V2;
} }
} }
debug("Timeout waiting for v2.x card\n"); debug_if(_dbg, "Timeout waiting for v2.x card\n");
return SDCARD_FAIL; return SDCARD_FAIL;
} }
@ -209,16 +210,16 @@ int SDFileSystem::disk_initialize() {
lock(); lock();
_is_initialized = initialise_card(); _is_initialized = initialise_card();
if (_is_initialized == 0) { if (_is_initialized == 0) {
debug("Fail to initialize card\n"); debug_if(_dbg, "Fail to initialize card\n");
unlock(); unlock();
return 1; return 1;
} }
debug_if(SD_DBG, "init card = %d\n", _is_initialized); debug_if(_dbg, "init card = %d\n", _is_initialized);
_sectors = _sd_sectors(); _sectors = _sd_sectors();
// Set block length to 512 (CMD16) // Set block length to 512 (CMD16)
if (_cmd(16, 512) != 0) { if (_cmd(16, 512) != 0) {
debug("Set 512-byte block timed out\n"); debug_if(_dbg, "Set 512-byte block timed out\n");
unlock(); unlock();
return 1; return 1;
} }
@ -291,6 +292,10 @@ uint32_t SDFileSystem::disk_sectors() {
return sectors; return sectors;
} }
void SDFileSystem::debug(bool dbg){
_dbg = dbg;
}
// PRIVATE FUNCTIONS // PRIVATE FUNCTIONS
int SDFileSystem::_cmd(int cmd, int arg) { int SDFileSystem::_cmd(int cmd, int arg) {
@ -487,13 +492,13 @@ uint32_t SDFileSystem::_sd_sectors() {
// CMD9, Response R2 (R1 byte + 16-byte block read) // CMD9, Response R2 (R1 byte + 16-byte block read)
if (_cmdx(9, 0) != 0) { 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; return 0;
} }
uint8_t csd[16]; uint8_t csd[16];
if (_read(csd, 16) != 0) { 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; return 0;
} }
@ -516,18 +521,18 @@ uint32_t SDFileSystem::_sd_sectors() {
blocknr = (c_size + 1) * mult; blocknr = (c_size + 1) * mult;
capacity = blocknr * block_len; capacity = blocknr * block_len;
blocks = capacity / 512; 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; break;
case 1: case 1:
cdv = 1; cdv = 1;
hc_c_size = ext_bits(csd, 63, 48); hc_c_size = ext_bits(csd, 63, 48);
blocks = (hc_c_size+1)*1024; 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; break;
default: default:
debug("CSD struct unsupported\r\n"); debug_if(_dbg, "CSD struct unsupported\r\n");
return 0; return 0;
}; };
return blocks; return blocks;

View File

@ -58,6 +58,7 @@ public:
virtual int disk_write(const uint8_t* buffer, uint32_t block_number, uint32_t count); virtual int disk_write(const uint8_t* buffer, uint32_t block_number, uint32_t count);
virtual int disk_sync(); virtual int disk_sync();
virtual uint32_t disk_sectors(); virtual uint32_t disk_sectors();
void debug(bool dbg);
protected: protected:
@ -84,6 +85,7 @@ protected:
DigitalOut _cs; DigitalOut _cs;
int cdv; int cdv;
int _is_initialized; int _is_initialized;
bool _dbg;
}; };
#endif #endif