147
147
#include " mbed_debug.h"
148
148
#include < errno.h>
149
149
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
+
150
155
#define SD_COMMAND_TIMEOUT 5000 /* !< Timeout in ms for response */
151
156
#define SD_CMD0_GO_IDLE_STATE_RETRIES 5 /* !< Number of retries for sending CMDO */
152
157
#define SD_DBG 0 /* !< 1 - Enable debugging */
@@ -656,12 +661,12 @@ uint8_t SDBlockDevice::_cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg) {
656
661
// The received byte immediataly following CMD12 is a stuff byte,
657
662
// it should be discarded before receive the response of the CMD12.
658
663
if (CMD12_STOP_TRANSMISSION == cmd) {
659
- _spi.write (0xFF );
664
+ _spi.write (SPI_FILL_CHAR );
660
665
}
661
666
662
667
// Loop for response: Response is sent back within command response time (NCR), 0 to 8 bytes for SDC
663
668
for (int i = 0 ; i < 0x10 ; i++) {
664
- response = _spi.write (0xFF );
669
+ response = _spi.write (SPI_FILL_CHAR );
665
670
// Got the response
666
671
if (!(response & R1_RESPONSE_RECV)) {
667
672
break ;
@@ -738,10 +743,10 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
738
743
_card_type = SDCARD_V2;
739
744
// Note: No break here, need to read rest of the response
740
745
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 );
745
750
debug_if (_dbg, " R3/R7: 0x%x \n " , response);
746
751
break ;
747
752
@@ -751,7 +756,7 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
751
756
break ;
752
757
753
758
case ACMD13_SD_STATUS: // Response R2
754
- response = _spi.write (0xFF );
759
+ response = _spi.write (SPI_FILL_CHAR );
755
760
debug_if (_dbg, " R2: 0x%x \n " , response);
756
761
break ;
757
762
@@ -821,12 +826,12 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length) {
821
826
822
827
// read data
823
828
for (uint32_t i = 0 ; i < length; i++) {
824
- buffer[i] = _spi.write (0xFF );
829
+ buffer[i] = _spi.write (SPI_FILL_CHAR );
825
830
}
826
831
827
832
// 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 );
830
835
831
836
_deselect ();
832
837
return 0 ;
@@ -848,8 +853,8 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length) {
848
853
_spi.write (NULL , 0 , (char *)buffer, length);
849
854
850
855
// 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 );
853
858
854
859
_deselect ();
855
860
return 0 ;
@@ -880,7 +885,7 @@ uint8_t SDBlockDevice::_write(const uint8_t *buffer, uint8_t token, uint32_t len
880
885
_spi.write (crc);
881
886
882
887
// check the response token
883
- response = _spi.write (0xFF );
888
+ response = _spi.write (SPI_FILL_CHAR );
884
889
_deselect ();
885
890
return (response & SPI_DATA_RESPONSE_MASK);
886
891
}
@@ -963,7 +968,7 @@ bool SDBlockDevice::_wait_token(uint8_t token) {
963
968
_spi_timer.start ();
964
969
965
970
do {
966
- if (token == _spi.write (0xFF )) {
971
+ if (token == _spi.write (SPI_FILL_CHAR )) {
967
972
_spi_timer.stop ();
968
973
return true ;
969
974
}
@@ -980,7 +985,7 @@ bool SDBlockDevice::_wait_ready(uint16_t ms) {
980
985
_spi_timer.reset ();
981
986
_spi_timer.start ();
982
987
do {
983
- response = _spi.write (0xFF );
988
+ response = _spi.write (SPI_FILL_CHAR );
984
989
if (response == 0xFF ) {
985
990
_spi_timer.stop ();
986
991
return true ;
@@ -994,7 +999,7 @@ bool SDBlockDevice::_wait_ready(uint16_t ms) {
994
999
void SDBlockDevice::_spi_wait (uint8_t count)
995
1000
{
996
1001
for (uint8_t i = 0 ; i < count; ++i) {
997
- _spi.write (0xFF );
1002
+ _spi.write (SPI_FILL_CHAR );
998
1003
}
999
1004
}
1000
1005
@@ -1003,6 +1008,7 @@ void SDBlockDevice::_spi_init() {
1003
1008
// Set to SCK for initialization, and clock card with cs = 1
1004
1009
_spi.frequency (_init_sck);
1005
1010
_spi.format (8 , 0 );
1011
+ _spi.set_default_write_value (SPI_FILL_CHAR);
1006
1012
// Initial 74 cycles required for few cards, before selecting SPI mode
1007
1013
_cs = 1 ;
1008
1014
_spi_wait (10 );
0 commit comments