@@ -147,6 +147,7 @@ SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs,
147147#define SDCARD_V2HC 3
148148
149149int SDFileSystem::initialise_card () {
150+ _dbg = SD_DBG;
150151 // Set to SCK for initialisation, and clock card with cs = 1
151152 _spi.lock ();
152153 _spi.frequency (_init_sck);
@@ -158,7 +159,7 @@ int SDFileSystem::initialise_card() {
158159
159160 // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
160161 if (_cmd (0 , 0 ) != R1_IDLE_STATE) {
161- debug ( " No disk, or could not put SD card in to SPI idle state\n " );
162+ debug_if (_dbg, " No disk, or could not put SD card in to SPI idle state\n " );
162163 return SDCARD_FAIL;
163164 }
164165
@@ -169,7 +170,7 @@ int SDFileSystem::initialise_card() {
169170 } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
170171 return initialise_card_v1 ();
171172 } else {
172- debug ( " Not in idle state after sending CMD8 (not an SD card?)\n " );
173+ debug_if (_dbg, " Not in idle state after sending CMD8 (not an SD card?)\n " );
173174 return SDCARD_FAIL;
174175 }
175176}
@@ -179,12 +180,12 @@ int SDFileSystem::initialise_card_v1() {
179180 _cmd (55 , 0 );
180181 if (_cmd (41 , 0 ) == 0 ) {
181182 cdv = 512 ;
182- debug_if (SD_DBG , " \n\r Init: SEDCARD_V1\n\r " );
183+ debug_if (_dbg , " \n\r Init: SEDCARD_V1\n\r " );
183184 return SDCARD_V1;
184185 }
185186 }
186187
187- debug ( " Timeout waiting for v1.x card\n " );
188+ debug_if (_dbg, " Timeout waiting for v1.x card\n " );
188189 return SDCARD_FAIL;
189190}
190191
@@ -195,30 +196,30 @@ int SDFileSystem::initialise_card_v2() {
195196 _cmd (55 , 0 );
196197 if (_cmd (41 , 0x40000000 ) == 0 ) {
197198 _cmd58 ();
198- debug_if (SD_DBG , " \n\r Init: SDCARD_V2\n\r " );
199+ debug_if (_dbg , " \n\r Init: SDCARD_V2\n\r " );
199200 cdv = 1 ;
200201 return SDCARD_V2;
201202 }
202203 }
203204
204- debug ( " Timeout waiting for v2.x card\n " );
205+ debug_if (_dbg, " Timeout waiting for v2.x card\n " );
205206 return SDCARD_FAIL;
206207}
207208
208209int SDFileSystem::disk_initialize () {
209210 lock ();
210211 _is_initialized = initialise_card ();
211212 if (_is_initialized == 0 ) {
212- debug ( " Fail to initialize card\n " );
213+ debug_if (_dbg, " Fail to initialize card\n " );
213214 unlock ();
214215 return 1 ;
215216 }
216- debug_if (SD_DBG , " init card = %d\n " , _is_initialized);
217+ debug_if (_dbg , " init card = %d\n " , _is_initialized);
217218 _sectors = _sd_sectors ();
218219
219220 // Set block length to 512 (CMD16)
220221 if (_cmd (16 , 512 ) != 0 ) {
221- debug ( " Set 512-byte block timed out\n " );
222+ debug_if (_dbg, " Set 512-byte block timed out\n " );
222223 unlock ();
223224 return 1 ;
224225 }
@@ -291,6 +292,10 @@ uint32_t SDFileSystem::disk_sectors() {
291292 return sectors;
292293}
293294
295+ void SDFileSystem::debug (bool dbg){
296+ _dbg = dbg;
297+ }
298+
294299
295300// PRIVATE FUNCTIONS
296301int SDFileSystem::_cmd (int cmd, int arg) {
@@ -487,13 +492,13 @@ uint32_t SDFileSystem::_sd_sectors() {
487492
488493 // CMD9, Response R2 (R1 byte + 16-byte block read)
489494 if (_cmdx (9 , 0 ) != 0 ) {
490- debug ( " Didn't get a response from the disk\n " );
495+ debug_if (_dbg, " Didn't get a response from the disk\n " );
491496 return 0 ;
492497 }
493498
494499 uint8_t csd[16 ];
495500 if (_read (csd, 16 ) != 0 ) {
496- debug ( " Couldn't read csd response from disk\n " );
501+ debug_if (_dbg, " Couldn't read csd response from disk\n " );
497502 return 0 ;
498503 }
499504
@@ -516,18 +521,18 @@ uint32_t SDFileSystem::_sd_sectors() {
516521 blocknr = (c_size + 1 ) * mult;
517522 capacity = blocknr * block_len;
518523 blocks = capacity / 512 ;
519- debug_if (SD_DBG , " \n\r SDCard\n\r c_size: %d \n\r capacity: %ld \n\r sectors: %lld\n\r " , c_size, capacity, blocks);
524+ debug_if (_dbg , " \n\r SDCard\n\r c_size: %d \n\r capacity: %ld \n\r sectors: %lld\n\r " , c_size, capacity, blocks);
520525 break ;
521526
522527 case 1 :
523528 cdv = 1 ;
524529 hc_c_size = ext_bits (csd, 63 , 48 );
525530 blocks = (hc_c_size+1 )*1024 ;
526- debug_if (SD_DBG , " \n\r SDHC Card \n\r hc_c_size: %d\n\r capacity: %lld \n\r sectors: %lld\n\r " , hc_c_size, blocks*512 , blocks);
531+ debug_if (_dbg , " \n\r SDHC Card \n\r hc_c_size: %d\n\r capacity: %lld \n\r sectors: %lld\n\r " , hc_c_size, blocks*512 , blocks);
527532 break ;
528533
529534 default :
530- debug ( " CSD struct unsupported\r\n " );
535+ debug_if (_dbg, " CSD struct unsupported\r\n " );
531536 return 0 ;
532537 };
533538 return blocks;
0 commit comments