Skip to content

Commit 5abee1e

Browse files
author
Deepika
authored
Merge pull request ARMmbed#43 from ARMmbed/dummy_read
SPI block read fix
2 parents 3b85386 + d94cb26 commit 5abee1e

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

SDBlockDevice.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@
147147
#include "mbed_debug.h"
148148
#include <errno.h>
149149

150+
/* Required version: 5.5.4 and above */
151+
#if (MBED_VERSION < MBED_ENCODE_VERSION(5,5,4))
152+
#error "Incompatible mbed-os version detected! Required 5.5.4 and above"
153+
#endif
154+
150155
#define SD_COMMAND_TIMEOUT 5000 /*!< Timeout in ms for response */
151156
#define SD_CMD0_GO_IDLE_STATE_RETRIES 5 /*!< Number of retries for sending CMDO */
152157
#define SD_DBG 0 /*!< 1 - Enable debugging */
@@ -656,12 +661,12 @@ uint8_t SDBlockDevice::_cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg) {
656661
// The received byte immediataly following CMD12 is a stuff byte,
657662
// it should be discarded before receive the response of the CMD12.
658663
if (CMD12_STOP_TRANSMISSION == cmd) {
659-
_spi.write(0xFF);
664+
_spi.write(SPI_FILL_CHAR);
660665
}
661666

662667
// Loop for response: Response is sent back within command response time (NCR), 0 to 8 bytes for SDC
663668
for (int i = 0; i < 0x10; i++) {
664-
response = _spi.write(0xFF);
669+
response = _spi.write(SPI_FILL_CHAR);
665670
// Got the response
666671
if (!(response & R1_RESPONSE_RECV)) {
667672
break;
@@ -738,10 +743,10 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
738743
_card_type = SDCARD_V2;
739744
// Note: No break here, need to read rest of the response
740745
case CMD58_READ_OCR: // Response R3
741-
response = (_spi.write(0xFF) << 24);
742-
response |= (_spi.write(0xFF) << 16);
743-
response |= (_spi.write(0xFF) << 8);
744-
response |= _spi.write(0xFF);
746+
response = (_spi.write(SPI_FILL_CHAR) << 24);
747+
response |= (_spi.write(SPI_FILL_CHAR) << 16);
748+
response |= (_spi.write(SPI_FILL_CHAR) << 8);
749+
response |= _spi.write(SPI_FILL_CHAR);
745750
debug_if(_dbg, "R3/R7: 0x%x \n", response);
746751
break;
747752

@@ -751,7 +756,7 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
751756
break;
752757

753758
case ACMD13_SD_STATUS: // Response R2
754-
response = _spi.write(0xFF);
759+
response = _spi.write(SPI_FILL_CHAR);
755760
debug_if(_dbg, "R2: 0x%x \n", response);
756761
break;
757762

@@ -821,12 +826,12 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length) {
821826

822827
// read data
823828
for (uint32_t i = 0; i < length; i++) {
824-
buffer[i] = _spi.write(0xFF);
829+
buffer[i] = _spi.write(SPI_FILL_CHAR);
825830
}
826831

827832
// Read the CRC16 checksum for the data block
828-
crc = (_spi.write(0xFF) << 8);
829-
crc |= _spi.write(0xFF);
833+
crc = (_spi.write(SPI_FILL_CHAR) << 8);
834+
crc |= _spi.write(SPI_FILL_CHAR);
830835

831836
_deselect();
832837
return 0;
@@ -848,8 +853,8 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length) {
848853
_spi.write(NULL, 0, (char*)buffer, length);
849854

850855
// Read the CRC16 checksum for the data block
851-
crc = (_spi.write(0xFF) << 8);
852-
crc |= _spi.write(0xFF);
856+
crc = (_spi.write(SPI_FILL_CHAR) << 8);
857+
crc |= _spi.write(SPI_FILL_CHAR);
853858

854859
_deselect();
855860
return 0;
@@ -880,7 +885,7 @@ uint8_t SDBlockDevice::_write(const uint8_t *buffer, uint8_t token, uint32_t len
880885
_spi.write(crc);
881886

882887
// check the response token
883-
response = _spi.write(0xFF);
888+
response = _spi.write(SPI_FILL_CHAR);
884889
_deselect();
885890
return (response & SPI_DATA_RESPONSE_MASK);
886891
}
@@ -963,7 +968,7 @@ bool SDBlockDevice::_wait_token(uint8_t token) {
963968
_spi_timer.start();
964969

965970
do {
966-
if (token == _spi.write(0xFF)) {
971+
if (token == _spi.write(SPI_FILL_CHAR)) {
967972
_spi_timer.stop();
968973
return true;
969974
}
@@ -980,7 +985,7 @@ bool SDBlockDevice::_wait_ready(uint16_t ms) {
980985
_spi_timer.reset();
981986
_spi_timer.start();
982987
do {
983-
response = _spi.write(0xFF);
988+
response = _spi.write(SPI_FILL_CHAR);
984989
if (response == 0xFF) {
985990
_spi_timer.stop();
986991
return true;
@@ -994,7 +999,7 @@ bool SDBlockDevice::_wait_ready(uint16_t ms) {
994999
void SDBlockDevice::_spi_wait(uint8_t count)
9951000
{
9961001
for (uint8_t i = 0; i < count; ++i) {
997-
_spi.write(0xFF);
1002+
_spi.write(SPI_FILL_CHAR);
9981003
}
9991004
}
10001005

@@ -1003,6 +1008,7 @@ void SDBlockDevice::_spi_init() {
10031008
// Set to SCK for initialization, and clock card with cs = 1
10041009
_spi.frequency(_init_sck);
10051010
_spi.format(8, 0);
1011+
_spi.set_default_write_value(SPI_FILL_CHAR);
10061012
// Initial 74 cycles required for few cards, before selecting SPI mode
10071013
_cs = 1;
10081014
_spi_wait(10);

0 commit comments

Comments
 (0)