Skip to content

Compile time config flag MBED_CONF_SD_CRC_ENABLED for CRC in SD #8573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,15 @@
// Only HC block size is supported. Making this a static constant reduces code size.
const uint32_t SDBlockDevice::_block_size = BLOCK_SIZE_HC;

#if MBED_CONF_SD_CRC_ENABLED
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
_crc_on(crc_on), _init_ref_count(0), _crc16(0, 0, false, false)
_init_ref_count(0), _crc_on(crc_on), _crc16(0, 0, false, false)
#else
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
_init_ref_count(0)
#endif
{
_cs = 1;
_card_type = SDCARD_NONE;
Expand Down Expand Up @@ -293,10 +299,12 @@ int SDBlockDevice::_initialise_card()
return status;
}

#if MBED_CONF_SD_CRC_ENABLED
if (_crc_on) {
// Enable CRC
status = _cmd(CMD59_CRC_ON_OFF, _crc_on);
}
#endif

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

#if MBED_CONF_SD_CRC_ENABLED
if (!_crc_on) {
// Disable CRC
status = _cmd(CMD59_CRC_ON_OFF, _crc_on);
}
#else
status = _cmd(CMD59_CRC_ON_OFF, 0);
#endif

return status;
}

Expand Down Expand Up @@ -649,7 +662,6 @@ uint8_t SDBlockDevice::_cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg)
{
uint8_t response;
char cmdPacket[PACKET_SIZE];
uint32_t crc;

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

#if MBED_CONF_SD_CRC_ENABLED
uint32_t crc;
if (_crc_on) {
_crc7.compute((void *)cmdPacket, 5, &crc);
cmdPacket[5] = (char)(crc | 0x01);
} else {
} else
#endif
{
// CMD0 is executed in SD mode, hence should have correct CRC
// CMD8 CRC verification is always enabled
switch (cmd) {
Expand Down Expand Up @@ -874,6 +890,7 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)
crc = (_spi.write(SPI_FILL_CHAR) << 8);
crc |= _spi.write(SPI_FILL_CHAR);

#if MBED_CONF_SD_CRC_ENABLED
if (_crc_on) {
uint32_t crc_result;
// Compute and verify checksum
Expand All @@ -885,6 +902,7 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)
return SD_BLOCK_DEVICE_ERROR_CRC;
}
}
#endif

_deselect();
return 0;
Expand All @@ -908,6 +926,7 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length)
crc = (_spi.write(SPI_FILL_CHAR) << 8);
crc |= _spi.write(SPI_FILL_CHAR);

#if MBED_CONF_SD_CRC_ENABLED
if (_crc_on) {
uint32_t crc_result;
// Compute and verify checksum
Expand All @@ -918,6 +937,7 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length)
return SD_BLOCK_DEVICE_ERROR_CRC;
}
}
#endif

return 0;
}
Expand All @@ -934,10 +954,12 @@ uint8_t SDBlockDevice::_write(const uint8_t *buffer, uint8_t token, uint32_t len
// write the data
_spi.write((char *)buffer, length, NULL, 0);

#if MBED_CONF_SD_CRC_ENABLED
if (_crc_on) {
// Compute CRC
_crc16.compute((void *)buffer, length, &crc);
}
#endif

// write the checksum CRC16
_spi.write(crc >> 8);
Expand Down
4 changes: 3 additions & 1 deletion components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,13 @@ class SDBlockDevice : public BlockDevice {
uint32_t _erase_size;
bool _is_initialized;
bool _dbg;
bool _crc_on;
uint32_t _init_ref_count;

#if MBED_CONF_SD_CRC_ENABLED
bool _crc_on;
mbed::MbedCRC<POLY_7BIT_SD, 7> _crc7;
mbed::MbedCRC<POLY_16BIT_CCITT, 16> _crc16;
#endif
};

#endif /* DEVICE_SPI */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"FSFAT_SDCARD_INSTALLED": 1,
"CMD_TIMEOUT": 10000,
"CMD0_IDLE_STATE_RETRIES": 5,
"SD_INIT_FREQUENCY": 100000
"INIT_FREQUENCY": 100000,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did the the name change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial name setting was a typo, "SD_" was not needed as the tools append "MBED_CONF_SD_" for all the config options, with old name the macro would be "MBED_CONF_SD_SD_INIT_FREQUENCY"

"CRC_ENABLED": 1
},
"target_overrides": {
"DISCO_F051R8": {
Expand Down