Skip to content

Commit 72253b7

Browse files
author
Cruz Monrreal
authored
Merge pull request #8573 from deepikabhavnani/flag_Crc
Compile time config flag MBED_CONF_SD_CRC_ENABLED for CRC in SD
2 parents dee3506 + e7bdf3c commit 72253b7

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,15 @@
247247
// Only HC block size is supported. Making this a static constant reduces code size.
248248
const uint32_t SDBlockDevice::_block_size = BLOCK_SIZE_HC;
249249

250+
#if MBED_CONF_SD_CRC_ENABLED
250251
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
251252
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
252-
_crc_on(crc_on), _init_ref_count(0), _crc16(0, 0, false, false)
253+
_init_ref_count(0), _crc_on(crc_on), _crc16(0, 0, false, false)
254+
#else
255+
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
256+
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
257+
_init_ref_count(0)
258+
#endif
253259
{
254260
_cs = 1;
255261
_card_type = SDCARD_NONE;
@@ -293,10 +299,12 @@ int SDBlockDevice::_initialise_card()
293299
return status;
294300
}
295301

302+
#if MBED_CONF_SD_CRC_ENABLED
296303
if (_crc_on) {
297304
// Enable CRC
298305
status = _cmd(CMD59_CRC_ON_OFF, _crc_on);
299306
}
307+
#endif
300308

301309
// Read OCR - CMD58 Response contains OCR register
302310
if (BD_ERROR_OK != (status = _cmd(CMD58_READ_OCR, 0x0, 0x0, &response))) {
@@ -350,10 +358,15 @@ int SDBlockDevice::_initialise_card()
350358
debug_if(SD_DBG, "Card Initialized: Version 1.x Card\n");
351359
}
352360

361+
#if MBED_CONF_SD_CRC_ENABLED
353362
if (!_crc_on) {
354363
// Disable CRC
355364
status = _cmd(CMD59_CRC_ON_OFF, _crc_on);
356365
}
366+
#else
367+
status = _cmd(CMD59_CRC_ON_OFF, 0);
368+
#endif
369+
357370
return status;
358371
}
359372

@@ -649,7 +662,6 @@ uint8_t SDBlockDevice::_cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg)
649662
{
650663
uint8_t response;
651664
char cmdPacket[PACKET_SIZE];
652-
uint32_t crc;
653665

654666
// Prepare the command packet
655667
cmdPacket[0] = SPI_CMD(cmd);
@@ -658,10 +670,14 @@ uint8_t SDBlockDevice::_cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg)
658670
cmdPacket[3] = (arg >> 8);
659671
cmdPacket[4] = (arg >> 0);
660672

673+
#if MBED_CONF_SD_CRC_ENABLED
674+
uint32_t crc;
661675
if (_crc_on) {
662676
_crc7.compute((void *)cmdPacket, 5, &crc);
663677
cmdPacket[5] = (char)(crc | 0x01);
664-
} else {
678+
} else
679+
#endif
680+
{
665681
// CMD0 is executed in SD mode, hence should have correct CRC
666682
// CMD8 CRC verification is always enabled
667683
switch (cmd) {
@@ -874,6 +890,7 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)
874890
crc = (_spi.write(SPI_FILL_CHAR) << 8);
875891
crc |= _spi.write(SPI_FILL_CHAR);
876892

893+
#if MBED_CONF_SD_CRC_ENABLED
877894
if (_crc_on) {
878895
uint32_t crc_result;
879896
// Compute and verify checksum
@@ -885,6 +902,7 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)
885902
return SD_BLOCK_DEVICE_ERROR_CRC;
886903
}
887904
}
905+
#endif
888906

889907
_deselect();
890908
return 0;
@@ -907,6 +925,7 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length)
907925
crc = (_spi.write(SPI_FILL_CHAR) << 8);
908926
crc |= _spi.write(SPI_FILL_CHAR);
909927

928+
#if MBED_CONF_SD_CRC_ENABLED
910929
if (_crc_on) {
911930
uint32_t crc_result;
912931
// Compute and verify checksum
@@ -917,6 +936,7 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length)
917936
return SD_BLOCK_DEVICE_ERROR_CRC;
918937
}
919938
}
939+
#endif
920940

921941
return 0;
922942
}
@@ -933,10 +953,12 @@ uint8_t SDBlockDevice::_write(const uint8_t *buffer, uint8_t token, uint32_t len
933953
// write the data
934954
_spi.write((char *)buffer, length, NULL, 0);
935955

956+
#if MBED_CONF_SD_CRC_ENABLED
936957
if (_crc_on) {
937958
// Compute CRC
938959
_crc16.compute((void *)buffer, length, &crc);
939960
}
961+
#endif
940962

941963
// write the checksum CRC16
942964
_spi.write(crc >> 8);

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,13 @@ class SDBlockDevice : public BlockDevice {
218218
uint32_t _erase_size;
219219
bool _is_initialized;
220220
bool _dbg;
221-
bool _crc_on;
222221
uint32_t _init_ref_count;
223222

223+
#if MBED_CONF_SD_CRC_ENABLED
224+
bool _crc_on;
224225
mbed::MbedCRC<POLY_7BIT_SD, 7> _crc7;
225226
mbed::MbedCRC<POLY_16BIT_CCITT, 16> _crc16;
227+
#endif
226228
};
227229

228230
#endif /* DEVICE_SPI */

components/storage/blockdevice/COMPONENT_SD/config/mbed_lib.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"FSFAT_SDCARD_INSTALLED": 1,
99
"CMD_TIMEOUT": 10000,
1010
"CMD0_IDLE_STATE_RETRIES": 5,
11-
"SD_INIT_FREQUENCY": 100000
11+
"INIT_FREQUENCY": 100000,
12+
"CRC_ENABLED": 1
1213
},
1314
"target_overrides": {
1415
"DISCO_F051R8": {

0 commit comments

Comments
 (0)