141
141
#include " SDBlockDevice.h"
142
142
#include " platform/mbed_debug.h"
143
143
#include " platform/mbed_wait_api.h"
144
+ #ifndef __STDC_FORMAT_MACROS
145
+ #define __STDC_FORMAT_MACROS
146
+ #endif
147
+ #include < inttypes.h>
144
148
#include < errno.h>
145
149
146
150
#ifndef MBED_CONF_SD_CMD_TIMEOUT
240
244
#define SPI_READ_ERROR_ECC_C (0x1 << 2 ) /* !< Card ECC failed */
241
245
#define SPI_READ_ERROR_OFR (0x1 << 3 ) /* !< Out of Range */
242
246
247
+ // Only HC block size is supported. Making this a static constant reduces code size.
248
+ const uint32_t SDBlockDevice::_block_size = BLOCK_SIZE_HC;
249
+
243
250
SDBlockDevice::SDBlockDevice (PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
244
251
: _sectors(0 ), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0 ),
245
252
_crc_on(crc_on), _init_ref_count(0 ), _crc16(0 , 0 , false , false )
@@ -253,8 +260,6 @@ SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName c
253
260
_init_sck = MBED_CONF_SD_INIT_FREQUENCY;
254
261
_transfer_sck = hz;
255
262
256
- // Only HC block size is supported.
257
- _block_size = BLOCK_SIZE_HC;
258
263
_erase_size = BLOCK_SIZE_HC;
259
264
}
260
265
@@ -386,7 +391,7 @@ int SDBlockDevice::init()
386
391
387
392
// Set block length to 512 (CMD16)
388
393
if (_cmd (CMD16_SET_BLOCKLEN, _block_size) != 0 ) {
389
- debug_if (SD_DBG, " Set %d -byte block timed out\n " , _block_size);
394
+ debug_if (SD_DBG, " Set %" PRIu32 " -byte block timed out\n " , _block_size);
390
395
unlock ();
391
396
return BD_ERROR_DEVICE_ERROR;
392
397
}
@@ -444,7 +449,7 @@ int SDBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
444
449
uint8_t response;
445
450
446
451
// Get block count
447
- bd_addr_t blockCnt = size / _block_size;
452
+ size_t blockCnt = size / _block_size;
448
453
449
454
// SDSC Card (CCS=0) uses byte unit address
450
455
// SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit)
@@ -514,7 +519,7 @@ int SDBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
514
519
515
520
uint8_t *buffer = static_cast <uint8_t *>(b);
516
521
int status = BD_ERROR_OK;
517
- bd_addr_t blockCnt = size / _block_size;
522
+ size_t blockCnt = size / _block_size;
518
523
519
524
// SDSC Card (CCS=0) uses byte unit address
520
525
// SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit)
@@ -738,24 +743,24 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
738
743
// Process the response R1 : Exit on CRC/Illegal command error/No response
739
744
if (R1_NO_RESPONSE == response) {
740
745
_deselect ();
741
- debug_if (SD_DBG, " No response CMD:%d response: 0x%x \n " , cmd, response);
746
+ debug_if (SD_DBG, " No response CMD:%d response: 0x%" PRIx32 " \n " , cmd, response);
742
747
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE; // No device
743
748
}
744
749
if (response & R1_COM_CRC_ERROR) {
745
750
_deselect ();
746
- debug_if (SD_DBG, " CRC error CMD:%d response 0x%x \n " , cmd, response);
751
+ debug_if (SD_DBG, " CRC error CMD:%d response 0x%" PRIx32 " \n " , cmd, response);
747
752
return SD_BLOCK_DEVICE_ERROR_CRC; // CRC error
748
753
}
749
754
if (response & R1_ILLEGAL_COMMAND) {
750
755
_deselect ();
751
- debug_if (SD_DBG, " Illegal command CMD:%d response 0x%x \n " , cmd, response);
756
+ debug_if (SD_DBG, " Illegal command CMD:%d response 0x%" PRIx32 " \n " , cmd, response);
752
757
if (CMD8_SEND_IF_COND == cmd) { // Illegal command is for Ver1 or not SD Card
753
758
_card_type = CARD_UNKNOWN;
754
759
}
755
760
return SD_BLOCK_DEVICE_ERROR_UNSUPPORTED; // Command not supported
756
761
}
757
762
758
- debug_if (_dbg, " CMD:%d \t arg:0x%x \t Response:0x%x \n " , cmd, arg, response);
763
+ debug_if (_dbg, " CMD:%d \t arg:0x%" PRIx32 " \t Response:0x%" PRIx32 " \n " , cmd, arg, response);
759
764
// Set status for other errors
760
765
if ((response & R1_ERASE_RESET) || (response & R1_ERASE_SEQUENCE_ERROR)) {
761
766
status = SD_BLOCK_DEVICE_ERROR_ERASE; // Erase error
@@ -775,7 +780,7 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
775
780
response |= (_spi.write (SPI_FILL_CHAR) << 16 );
776
781
response |= (_spi.write (SPI_FILL_CHAR) << 8 );
777
782
response |= _spi.write (SPI_FILL_CHAR);
778
- debug_if (_dbg, " R3/R7: 0x%x \n " , response);
783
+ debug_if (_dbg, " R3/R7: 0x%" PRIx32 " \n " , response);
779
784
break ;
780
785
781
786
case CMD12_STOP_TRANSMISSION: // Response R1b
@@ -785,7 +790,7 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
785
790
786
791
case ACMD13_SD_STATUS: // Response R2
787
792
response = _spi.write (SPI_FILL_CHAR);
788
- debug_if (_dbg, " R2: 0x%x \n " , response);
793
+ debug_if (_dbg, " R2: 0x%" PRIx32 " \n " , response);
789
794
break ;
790
795
791
796
default : // Response R1
@@ -822,7 +827,7 @@ int SDBlockDevice::_cmd8()
822
827
if ((BD_ERROR_OK == status) && (SDCARD_V2 == _card_type)) {
823
828
// If check pattern is not matched, CMD8 communication is not valid
824
829
if ((response & 0xFFF ) != arg) {
825
- debug_if (SD_DBG, " CMD8 Pattern mismatch 0x%x : 0x%x \n " , arg, response);
830
+ debug_if (SD_DBG, " CMD8 Pattern mismatch 0x%" PRIx32 " : 0x%" PRIx32 " \n " , arg, response);
826
831
_card_type = CARD_UNKNOWN;
827
832
status = SD_BLOCK_DEVICE_ERROR_UNUSABLE;
828
833
}
@@ -874,8 +879,8 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)
874
879
// Compute and verify checksum
875
880
_crc16.compute ((void *)buffer, length, &crc_result);
876
881
if ((uint16_t )crc_result != crc) {
877
- debug_if (SD_DBG, " _read_bytes: Invalid CRC received 0x%x result of computation 0x%x \n " ,
878
- crc, crc_result);
882
+ debug_if (SD_DBG, " _read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx16 " \n " ,
883
+ crc, ( uint16_t ) crc_result);
879
884
_deselect ();
880
885
return SD_BLOCK_DEVICE_ERROR_CRC;
881
886
}
@@ -908,8 +913,8 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length)
908
913
// Compute and verify checksum
909
914
_crc16.compute ((void *)buffer, length, &crc_result);
910
915
if ((uint16_t )crc_result != crc) {
911
- debug_if (SD_DBG, " _read_bytes: Invalid CRC received 0x%x result of computation 0x%x \n " ,
912
- crc, crc_result);
916
+ debug_if (SD_DBG, " _read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx16 " \n " ,
917
+ crc, ( uint16_t ) crc_result);
913
918
return SD_BLOCK_DEVICE_ERROR_CRC;
914
919
}
915
920
}
@@ -992,11 +997,11 @@ bd_size_t SDBlockDevice::_sd_sectors()
992
997
block_len = 1 << read_bl_len; // BLOCK_LEN = 2^READ_BL_LEN
993
998
mult = 1 << (c_size_mult + 2 ); // MULT = 2^C_SIZE_MULT+2 (C_SIZE_MULT < 8)
994
999
blocknr = (c_size + 1 ) * mult; // BLOCKNR = (C_SIZE+1) * MULT
995
- capacity = blocknr * block_len; // memory capacity = BLOCKNR * BLOCK_LEN
1000
+ capacity = ( bd_size_t ) blocknr * block_len; // memory capacity = BLOCKNR * BLOCK_LEN
996
1001
blocks = capacity / _block_size;
997
- debug_if (SD_DBG, " Standard Capacity: c_size: %d \n " , c_size);
998
- debug_if (SD_DBG, " Sectors: 0x%x : %llu \n " , blocks, blocks);
999
- debug_if (SD_DBG, " Capacity: 0x%x : %llu MB\n " , capacity, (capacity / (1024U * 1024U )));
1002
+ debug_if (SD_DBG, " Standard Capacity: c_size: %" PRIu32 " \n " , c_size);
1003
+ debug_if (SD_DBG, " Sectors: 0x%" PRIx64 " : %" PRIu64 " \n " , blocks, blocks);
1004
+ debug_if (SD_DBG, " Capacity: 0x%" PRIx64 " : %" PRIu64 " MB\n " , capacity, (capacity / (1024U * 1024U )));
1000
1005
1001
1006
// ERASE_BLK_EN = 1: Erase in multiple of 512 bytes supported
1002
1007
if (ext_bits (csd, 46 , 46 )) {
@@ -1010,9 +1015,9 @@ bd_size_t SDBlockDevice::_sd_sectors()
1010
1015
case 1 :
1011
1016
hc_c_size = ext_bits (csd, 69 , 48 ); // device size : C_SIZE : [69:48]
1012
1017
blocks = (hc_c_size + 1 ) << 10 ; // block count = C_SIZE+1) * 1K byte (512B is block size)
1013
- debug_if (SD_DBG, " SDHC/SDXC Card: hc_c_size: %d \n " , hc_c_size);
1014
- debug_if (SD_DBG, " Sectors: 0x%x : %llu \n " , blocks, blocks);
1015
- debug_if (SD_DBG, " Capacity: %llu MB\n " , (blocks / (2048U )));
1018
+ debug_if (SD_DBG, " SDHC/SDXC Card: hc_c_size: %" PRIu32 " \n " , hc_c_size);
1019
+ debug_if (SD_DBG, " Sectors: 0x%" PRIx64 " x : %" PRIu64 " \n " , blocks, blocks);
1020
+ debug_if (SD_DBG, " Capacity: %" PRIu64 " MB\n " , (blocks / (2048U )));
1016
1021
// ERASE_BLK_EN is fixed to 1, which means host can erase one or multiple of 512 bytes.
1017
1022
_erase_size = BLOCK_SIZE_HC;
1018
1023
break ;
0 commit comments