120
120
#define SD_DBG 0
121
121
122
122
SDFileSystem::SDFileSystem (PinName mosi, PinName miso, PinName sclk, PinName cs, const char * name) :
123
- FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0 ) {
124
- _cs = 1 ;
123
+ FATFileSystem(name), _spi(mosi, miso, sclk, cs, use_gpio_ssel), _is_initialized(0 ) {
124
+ _spi.deselect ();
125
+ // _spi.lock();
125
126
126
127
// Set default to 100kHz for initialisation and 1MHz for data transfer
127
128
_init_sck = 100000 ;
@@ -153,7 +154,7 @@ int SDFileSystem::initialise_card() {
153
154
154
155
_spi.set_dma_usage (DMAUsage::DMA_USAGE_OPPORTUNISTIC);
155
156
156
- _cs = 1 ;
157
+ _spi. deselect () ;
157
158
for (int i = 0 ; i < 16 ; i++) {
158
159
_spi.write (0xFF );
159
160
}
@@ -280,7 +281,7 @@ uint32_t SDFileSystem::disk_sectors() { return _sectors; }
280
281
281
282
// PRIVATE FUNCTIONS
282
283
int SDFileSystem::_cmd (int cmd, int arg) {
283
- _cs = 0 ;
284
+ _spi. select () ;
284
285
285
286
// send a command
286
287
_spi.write (0x40 | cmd);
@@ -294,17 +295,17 @@ int SDFileSystem::_cmd(int cmd, int arg) {
294
295
for (int i = 0 ; i < SD_COMMAND_TIMEOUT; i++) {
295
296
int response = _spi.write (0xFF );
296
297
if (!(response & 0x80 )) {
297
- _cs = 1 ;
298
+ _spi. deselect () ;
298
299
_spi.write (0xFF );
299
300
return response;
300
301
}
301
302
}
302
- _cs = 1 ;
303
+ _spi. deselect () ;
303
304
_spi.write (0xFF );
304
305
return -1 ; // timeout
305
306
}
306
307
int SDFileSystem::_cmdx (int cmd, int arg) {
307
- _cs = 0 ;
308
+ _spi. select () ;
308
309
309
310
// send a command
310
311
_spi.write (0x40 | cmd);
@@ -321,14 +322,14 @@ int SDFileSystem::_cmdx(int cmd, int arg) {
321
322
return response;
322
323
}
323
324
}
324
- _cs = 1 ;
325
+ _spi. deselect () ;
325
326
_spi.write (0xFF );
326
327
return -1 ; // timeout
327
328
}
328
329
329
330
330
331
int SDFileSystem::_cmd58 () {
331
- _cs = 0 ;
332
+ _spi. select () ;
332
333
int arg = 0 ;
333
334
334
335
// send a command
@@ -347,18 +348,18 @@ int SDFileSystem::_cmd58() {
347
348
ocr |= _spi.write (0xFF ) << 16 ;
348
349
ocr |= _spi.write (0xFF ) << 8 ;
349
350
ocr |= _spi.write (0xFF ) << 0 ;
350
- _cs = 1 ;
351
+ _spi. deselect () ;
351
352
_spi.write (0xFF );
352
353
return response;
353
354
}
354
355
}
355
- _cs = 1 ;
356
+ _spi. deselect () ;
356
357
_spi.write (0xFF );
357
358
return -1 ; // timeout
358
359
}
359
360
360
361
int SDFileSystem::_cmd8 () {
361
- _cs = 0 ;
362
+ _spi. select () ;
362
363
363
364
// send a command
364
365
_spi.write (0x40 | 8 ); // CMD8
@@ -376,18 +377,18 @@ int SDFileSystem::_cmd8() {
376
377
for (int j = 1 ; j < 5 ; j++) {
377
378
response[i] = _spi.write (0xFF );
378
379
}
379
- _cs = 1 ;
380
+ _spi. deselect () ;
380
381
_spi.write (0xFF );
381
382
return response[0 ];
382
383
}
383
384
}
384
- _cs = 1 ;
385
+ _spi. deselect () ;
385
386
_spi.write (0xFF );
386
387
return -1 ; // timeout
387
388
}
388
389
389
390
int SDFileSystem::_read (uint8_t *buffer, uint32_t length) {
390
- _cs = 0 ;
391
+ _spi. select () ;
391
392
392
393
// read until start byte (0xFF)
393
394
while (_spi.write (0xFF ) != 0xFE );
@@ -399,13 +400,13 @@ int SDFileSystem::_read(uint8_t *buffer, uint32_t length) {
399
400
_spi.write (0xFF ); // checksum
400
401
_spi.write (0xFF );
401
402
402
- _cs = 1 ;
403
+ _spi. deselect () ;
403
404
_spi.write (0xFF );
404
405
return 0 ;
405
406
}
406
407
407
408
int SDFileSystem::_write (const uint8_t *buffer, uint32_t length) {
408
- _cs = 0 ;
409
+ _spi. select () ;
409
410
410
411
// indicate start of block
411
412
_spi.write (0xFE );
@@ -421,15 +422,15 @@ int SDFileSystem::_write(const uint8_t*buffer, uint32_t length) {
421
422
422
423
// check the response token
423
424
if ((_spi.write (0xFF ) & 0x1F ) != 0x05 ) {
424
- _cs = 1 ;
425
+ _spi. deselect () ;
425
426
_spi.write (0xFF );
426
427
return 1 ;
427
428
}
428
429
429
430
// wait for write to finish
430
431
while (_spi.write (0xFF ) == 0 );
431
432
432
- _cs = 1 ;
433
+ _spi. deselect () ;
433
434
_spi.write (0xFF );
434
435
return 0 ;
435
436
}
0 commit comments