@@ -251,43 +251,16 @@ using namespace std::chrono;
251
251
// Only HC block size is supported. Making this a static constant reduces code size.
252
252
const uint32_t SDBlockDevice::_block_size = BLOCK_SIZE_HC;
253
253
254
- SDBlockDevice::SDBlockDevice (PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
255
- : _sectors(0 ), _spi(mosi, miso, sclk, cs), _is_initialized(0 ),
256
254
#if MBED_CONF_SD_CRC_ENABLED
257
- _init_ref_count (0 ), _crc_on(crc_on)
258
- #else
259
- _init_ref_count (0 )
260
- #endif
261
- {
262
- #if !MBED_CONF_SD_CRC_ENABLED
263
- // If this assert fails, this code was compiled without CRC support but you tried to use it.
264
- MBED_ASSERT (!crc_on);
265
- #endif
266
-
267
- _card_type = SDCARD_NONE;
268
-
269
- // Set default to 100kHz for initialisation and 1MHz for data transfer
270
- static_assert (((MBED_CONF_SD_INIT_FREQUENCY >= 100000 ) && (MBED_CONF_SD_INIT_FREQUENCY <= 400000 )),
271
- " Initialization frequency should be between 100KHz to 400KHz" );
272
- _init_sck = MBED_CONF_SD_INIT_FREQUENCY;
273
- _transfer_sck = hz;
274
-
275
- _erase_size = BLOCK_SIZE_HC;
276
- }
277
-
278
- SDBlockDevice::SDBlockDevice (mbed::use_gpio_ssel_t , PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
255
+ SDBlockDevice::SDBlockDevice (PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
279
256
: _sectors(0 ), _spi(mosi, miso, sclk, cs, use_gpio_ssel), _is_initialized(0 ),
280
- #if MBED_CONF_SD_CRC_ENABLED
281
257
_init_ref_count(0 ), _crc_on(crc_on)
282
258
#else
259
+ SDBlockDevice::SDBlockDevice (PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
260
+ : _sectors(0 ), _spi(mosi, miso, sclk, cs, use_gpio_ssel), _is_initialized(0 ),
283
261
_init_ref_count(0 )
284
262
#endif
285
263
{
286
- #if !MBED_CONF_SD_CRC_ENABLED
287
- // If this assert fails, this code was compiled without CRC support but you tried to use it.
288
- MBED_ASSERT (!crc_on);
289
- #endif
290
-
291
264
_card_type = SDCARD_NONE;
292
265
293
266
// Set default to 100kHz for initialisation and 1MHz for data transfer
@@ -299,43 +272,16 @@ SDBlockDevice::SDBlockDevice(mbed::use_gpio_ssel_t, PinName mosi, PinName miso,
299
272
_erase_size = BLOCK_SIZE_HC;
300
273
}
301
274
302
- SDBlockDevice::SDBlockDevice (const spi_pinmap_t &spi_pinmap, uint64_t hz, bool crc_on)
303
- : _sectors(0 ), _spi(spi_pinmap), _is_initialized(0 ),
304
275
#if MBED_CONF_SD_CRC_ENABLED
305
- _init_ref_count (0 ), _crc_on(crc_on)
306
- #else
307
- _init_ref_count (0 )
308
- #endif
309
- {
310
- #if !MBED_CONF_SD_CRC_ENABLED
311
- // If this assert fails, this code was compiled without CRC support but you tried to use it.
312
- MBED_ASSERT (!crc_on);
313
- #endif
314
-
315
- _card_type = SDCARD_NONE;
316
-
317
- // Set default to 100kHz for initialisation and 1MHz for data transfer
318
- static_assert (((MBED_CONF_SD_INIT_FREQUENCY >= 100000 ) && (MBED_CONF_SD_INIT_FREQUENCY <= 400000 )),
319
- " Initialization frequency should be between 100KHz to 400KHz" );
320
- _init_sck = MBED_CONF_SD_INIT_FREQUENCY;
321
- _transfer_sck = hz;
322
-
323
- _erase_size = BLOCK_SIZE_HC;
324
- }
325
-
326
- SDBlockDevice::SDBlockDevice (const spi_pinmap_t &spi_pinmap, mbed::use_gpio_ssel_t , PinName cs, uint64_t hz, bool crc_on)
276
+ SDBlockDevice::SDBlockDevice (const spi_pinmap_t &spi_pinmap, PinName cs, uint64_t hz, bool crc_on)
327
277
: _sectors(0 ), _spi(spi_pinmap, cs), _is_initialized(0 ),
328
- #if MBED_CONF_SD_CRC_ENABLED
329
278
_init_ref_count(0 ), _crc_on(crc_on)
330
279
#else
280
+ SDBlockDevice::SDBlockDevice (const spi_pinmap_t &spi_pinmap, PinName cs, uint64_t hz, bool crc_on)
281
+ : _sectors(0 ), _spi(spi_pinmap, cs), _is_initialized(0 ),
331
282
_init_ref_count(0 )
332
283
#endif
333
284
{
334
- #if !MBED_CONF_SD_CRC_ENABLED
335
- // If this assert fails, this code was compiled without CRC support but you tried to use it.
336
- MBED_ASSERT (!crc_on);
337
- #endif
338
-
339
285
_card_type = SDCARD_NONE;
340
286
341
287
// Set default to 100kHz for initialisation and 1MHz for data transfer
@@ -499,6 +445,12 @@ int SDBlockDevice::init()
499
445
return BD_ERROR_OK;
500
446
}
501
447
448
+ void SDBlockDevice::set_async_spi_mode (bool enabled, DMAUsage dma_usage_hint)
449
+ {
450
+ _async_spi_enabled = enabled;
451
+ _spi.set_dma_usage (dma_usage_hint);
452
+ }
453
+
502
454
int SDBlockDevice::deinit ()
503
455
{
504
456
lock ();
@@ -778,8 +730,13 @@ uint8_t SDBlockDevice::_cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg)
778
730
}
779
731
780
732
// send a command
781
- for (int i = 0 ; i < PACKET_SIZE; i++) {
782
- _spi.write (cmdPacket[i]);
733
+ #if DEVICE_SPI_ASYNCH
734
+ if (_async_spi_enabled) {
735
+ _spi.transfer_and_wait (cmdPacket, PACKET_SIZE, nullptr , 0 );
736
+ } else
737
+ #endif
738
+ {
739
+ _spi.write (cmdPacket, PACKET_SIZE, nullptr , 0 );
783
740
}
784
741
785
742
// The received byte immediataly following CMD12 is a stuff byte,
@@ -966,8 +923,13 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)
966
923
}
967
924
968
925
// read data
969
- for (uint32_t i = 0 ; i < length; i++) {
970
- buffer[i] = _spi.write (SPI_FILL_CHAR);
926
+ #if DEVICE_SPI_ASYNCH
927
+ if (_async_spi_enabled) {
928
+ _spi.transfer_and_wait (nullptr , 0 , buffer, length);
929
+ } else
930
+ #endif
931
+ {
932
+ _spi.write (nullptr , 0 , buffer, length);
971
933
}
972
934
973
935
// Read the CRC16 checksum for the data block
@@ -1004,7 +966,16 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length)
1004
966
}
1005
967
1006
968
// read data
1007
- _spi.write (NULL , 0 , (char *)buffer, length);
969
+ #if DEVICE_SPI_ASYNCH
970
+ if (_async_spi_enabled) {
971
+ if (_spi.transfer_and_wait (nullptr , 0 , buffer, length) != 0 ) {
972
+ return SD_BLOCK_DEVICE_ERROR_WRITE;
973
+ }
974
+ } else
975
+ #endif
976
+ {
977
+ _spi.write (NULL , 0 , (char *) buffer, length);
978
+ }
1008
979
1009
980
// Read the CRC16 checksum for the data block
1010
981
crc = (_spi.write (SPI_FILL_CHAR) << 8 );
@@ -1037,7 +1008,14 @@ uint8_t SDBlockDevice::_write(const uint8_t *buffer, uint8_t token, uint32_t len
1037
1008
_spi.write (token);
1038
1009
1039
1010
// write the data
1040
- _spi.write ((char *)buffer, length, NULL , 0 );
1011
+ #if DEVICE_SPI_ASYNCH
1012
+ if (_async_spi_enabled) {
1013
+ _spi.transfer_and_wait (buffer, length, nullptr , 0 );
1014
+ } else
1015
+ #endif
1016
+ {
1017
+ _spi.write (buffer, length, nullptr , 0 );
1018
+ }
1041
1019
1042
1020
#if MBED_CONF_SD_CRC_ENABLED
1043
1021
if (_crc_on) {
0 commit comments