Skip to content

Commit f4708bb

Browse files
committed
Fix up types in SDBlockDevice
Correct types passed to debug() calls - as part of this, block size and erase size are changed to be 32-bit - using 64-bit variables for these will cause unnecessary code bloat. Many uses of bd_size_t in the BlockDevice API should really be uint32_t or size_t, but that would be a bigger API change.
1 parent 638e071 commit f4708bb

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@
141141
#include "SDBlockDevice.h"
142142
#include "platform/mbed_debug.h"
143143
#include "platform/mbed_wait_api.h"
144+
#ifndef __STDC_FORMAT_MACROS
145+
#define __STDC_FORMAT_MACROS
146+
#endif
147+
#include <inttypes.h>
144148
#include <errno.h>
145149

146150
#ifndef MBED_CONF_SD_CMD_TIMEOUT
@@ -240,6 +244,9 @@
240244
#define SPI_READ_ERROR_ECC_C (0x1 << 2) /*!< Card ECC failed */
241245
#define SPI_READ_ERROR_OFR (0x1 << 3) /*!< Out of Range */
242246

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+
243250
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
244251
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
245252
_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
253260
_init_sck = MBED_CONF_SD_INIT_FREQUENCY;
254261
_transfer_sck = hz;
255262

256-
// Only HC block size is supported.
257-
_block_size = BLOCK_SIZE_HC;
258263
_erase_size = BLOCK_SIZE_HC;
259264
}
260265

@@ -386,7 +391,7 @@ int SDBlockDevice::init()
386391

387392
// Set block length to 512 (CMD16)
388393
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);
390395
unlock();
391396
return BD_ERROR_DEVICE_ERROR;
392397
}
@@ -444,7 +449,7 @@ int SDBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
444449
uint8_t response;
445450

446451
// Get block count
447-
bd_addr_t blockCnt = size / _block_size;
452+
size_t blockCnt = size / _block_size;
448453

449454
// SDSC Card (CCS=0) uses byte unit address
450455
// 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)
514519

515520
uint8_t *buffer = static_cast<uint8_t *>(b);
516521
int status = BD_ERROR_OK;
517-
bd_addr_t blockCnt = size / _block_size;
522+
size_t blockCnt = size / _block_size;
518523

519524
// SDSC Card (CCS=0) uses byte unit address
520525
// 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
738743
// Process the response R1 : Exit on CRC/Illegal command error/No response
739744
if (R1_NO_RESPONSE == response) {
740745
_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);
742747
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE; // No device
743748
}
744749
if (response & R1_COM_CRC_ERROR) {
745750
_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);
747752
return SD_BLOCK_DEVICE_ERROR_CRC; // CRC error
748753
}
749754
if (response & R1_ILLEGAL_COMMAND) {
750755
_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);
752757
if (CMD8_SEND_IF_COND == cmd) { // Illegal command is for Ver1 or not SD Card
753758
_card_type = CARD_UNKNOWN;
754759
}
755760
return SD_BLOCK_DEVICE_ERROR_UNSUPPORTED; // Command not supported
756761
}
757762

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);
759764
// Set status for other errors
760765
if ((response & R1_ERASE_RESET) || (response & R1_ERASE_SEQUENCE_ERROR)) {
761766
status = SD_BLOCK_DEVICE_ERROR_ERASE; // Erase error
@@ -775,7 +780,7 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
775780
response |= (_spi.write(SPI_FILL_CHAR) << 16);
776781
response |= (_spi.write(SPI_FILL_CHAR) << 8);
777782
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);
779784
break;
780785

781786
case CMD12_STOP_TRANSMISSION: // Response R1b
@@ -785,7 +790,7 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
785790

786791
case ACMD13_SD_STATUS: // Response R2
787792
response = _spi.write(SPI_FILL_CHAR);
788-
debug_if(_dbg, "R2: 0x%x \n", response);
793+
debug_if(_dbg, "R2: 0x%" PRIx32 "\n", response);
789794
break;
790795

791796
default: // Response R1
@@ -822,7 +827,7 @@ int SDBlockDevice::_cmd8()
822827
if ((BD_ERROR_OK == status) && (SDCARD_V2 == _card_type)) {
823828
// If check pattern is not matched, CMD8 communication is not valid
824829
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);
826831
_card_type = CARD_UNKNOWN;
827832
status = SD_BLOCK_DEVICE_ERROR_UNUSABLE;
828833
}
@@ -874,8 +879,8 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)
874879
// Compute and verify checksum
875880
_crc16.compute((void *)buffer, length, &crc_result);
876881
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);
879884
_deselect();
880885
return SD_BLOCK_DEVICE_ERROR_CRC;
881886
}
@@ -908,8 +913,8 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length)
908913
// Compute and verify checksum
909914
_crc16.compute((void *)buffer, length, &crc_result);
910915
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);
913918
return SD_BLOCK_DEVICE_ERROR_CRC;
914919
}
915920
}
@@ -992,11 +997,11 @@ bd_size_t SDBlockDevice::_sd_sectors()
992997
block_len = 1 << read_bl_len; // BLOCK_LEN = 2^READ_BL_LEN
993998
mult = 1 << (c_size_mult + 2); // MULT = 2^C_SIZE_MULT+2 (C_SIZE_MULT < 8)
994999
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
9961001
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)));
10001005

10011006
// ERASE_BLK_EN = 1: Erase in multiple of 512 bytes supported
10021007
if (ext_bits(csd, 46, 46)) {
@@ -1010,9 +1015,9 @@ bd_size_t SDBlockDevice::_sd_sectors()
10101015
case 1:
10111016
hc_c_size = ext_bits(csd, 69, 48); // device size : C_SIZE : [69:48]
10121017
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)));
10161021
// ERASE_BLK_EN is fixed to 1, which means host can erase one or multiple of 512 bytes.
10171022
_erase_size = BLOCK_SIZE_HC;
10181023
break;

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ class SDBlockDevice : public BlockDevice {
214214
}
215215

216216
PlatformMutex _mutex;
217-
bd_size_t _block_size;
218-
bd_size_t _erase_size;
217+
static const uint32_t _block_size;
218+
uint32_t _erase_size;
219219
bool _is_initialized;
220220
bool _dbg;
221221
bool _crc_on;

0 commit comments

Comments
 (0)