@@ -147,6 +147,7 @@ SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs,
147
147
#define SDCARD_V2HC 3
148
148
149
149
int SDFileSystem::initialise_card () {
150
+ _dbg = SD_DBG;
150
151
// Set to SCK for initialisation, and clock card with cs = 1
151
152
_spi.lock ();
152
153
_spi.frequency (_init_sck);
@@ -158,7 +159,7 @@ int SDFileSystem::initialise_card() {
158
159
159
160
// send CMD0, should return with all zeros except IDLE STATE set (bit 0)
160
161
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 " );
162
163
return SDCARD_FAIL;
163
164
}
164
165
@@ -169,7 +170,7 @@ int SDFileSystem::initialise_card() {
169
170
} else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
170
171
return initialise_card_v1 ();
171
172
} 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 " );
173
174
return SDCARD_FAIL;
174
175
}
175
176
}
@@ -179,12 +180,12 @@ int SDFileSystem::initialise_card_v1() {
179
180
_cmd (55 , 0 );
180
181
if (_cmd (41 , 0 ) == 0 ) {
181
182
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 " );
183
184
return SDCARD_V1;
184
185
}
185
186
}
186
187
187
- debug ( " Timeout waiting for v1.x card\n " );
188
+ debug_if (_dbg, " Timeout waiting for v1.x card\n " );
188
189
return SDCARD_FAIL;
189
190
}
190
191
@@ -195,30 +196,30 @@ int SDFileSystem::initialise_card_v2() {
195
196
_cmd (55 , 0 );
196
197
if (_cmd (41 , 0x40000000 ) == 0 ) {
197
198
_cmd58 ();
198
- debug_if (SD_DBG , " \n\r Init: SDCARD_V2\n\r " );
199
+ debug_if (_dbg , " \n\r Init: SDCARD_V2\n\r " );
199
200
cdv = 1 ;
200
201
return SDCARD_V2;
201
202
}
202
203
}
203
204
204
- debug ( " Timeout waiting for v2.x card\n " );
205
+ debug_if (_dbg, " Timeout waiting for v2.x card\n " );
205
206
return SDCARD_FAIL;
206
207
}
207
208
208
209
int SDFileSystem::disk_initialize () {
209
210
lock ();
210
211
_is_initialized = initialise_card ();
211
212
if (_is_initialized == 0 ) {
212
- debug ( " Fail to initialize card\n " );
213
+ debug_if (_dbg, " Fail to initialize card\n " );
213
214
unlock ();
214
215
return 1 ;
215
216
}
216
- debug_if (SD_DBG , " init card = %d\n " , _is_initialized);
217
+ debug_if (_dbg , " init card = %d\n " , _is_initialized);
217
218
_sectors = _sd_sectors ();
218
219
219
220
// Set block length to 512 (CMD16)
220
221
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 " );
222
223
unlock ();
223
224
return 1 ;
224
225
}
@@ -291,6 +292,10 @@ uint32_t SDFileSystem::disk_sectors() {
291
292
return sectors;
292
293
}
293
294
295
+ void SDFileSystem::debug (bool dbg){
296
+ _dbg = dbg;
297
+ }
298
+
294
299
295
300
// PRIVATE FUNCTIONS
296
301
int SDFileSystem::_cmd (int cmd, int arg) {
@@ -487,13 +492,13 @@ uint32_t SDFileSystem::_sd_sectors() {
487
492
488
493
// CMD9, Response R2 (R1 byte + 16-byte block read)
489
494
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 " );
491
496
return 0 ;
492
497
}
493
498
494
499
uint8_t csd[16 ];
495
500
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 " );
497
502
return 0 ;
498
503
}
499
504
@@ -516,18 +521,18 @@ uint32_t SDFileSystem::_sd_sectors() {
516
521
blocknr = (c_size + 1 ) * mult;
517
522
capacity = blocknr * block_len;
518
523
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);
520
525
break ;
521
526
522
527
case 1 :
523
528
cdv = 1 ;
524
529
hc_c_size = ext_bits (csd, 63 , 48 );
525
530
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);
527
532
break ;
528
533
529
534
default :
530
- debug ( " CSD struct unsupported\r\n " );
535
+ debug_if (_dbg, " CSD struct unsupported\r\n " );
531
536
return 0 ;
532
537
};
533
538
return blocks;
0 commit comments