From 6b1c14ee497138857fb715661971eef635f5525c Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Mon, 31 Oct 2022 09:06:32 +0100 Subject: [PATCH 1/4] define default pins for ESP32 --- src/utility/Sd2PinMap.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utility/Sd2PinMap.h b/src/utility/Sd2PinMap.h index 12d8926..bd5cbe2 100644 --- a/src/utility/Sd2PinMap.h +++ b/src/utility/Sd2PinMap.h @@ -523,6 +523,8 @@ void fastDigitalWrite(uint8_t pin, uint8_t value) { #endif // Arduino ARC +#elif defined(ESP32) +// default SPI ports #else #error Architecture or board not supported. #endif From 8821f419eb82945c129c8c81a0b469c8f375d0fe Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Mon, 31 Oct 2022 09:07:10 +0100 Subject: [PATCH 2/4] make cardCommand public --- src/utility/Sd2Card.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/Sd2Card.h b/src/utility/Sd2Card.h index 5d91ebf..3f65d81 100644 --- a/src/utility/Sd2Card.h +++ b/src/utility/Sd2Card.h @@ -241,6 +241,7 @@ class Sd2Card { uint8_t writeStart(uint32_t blockNumber, uint32_t eraseCount); uint8_t writeStop(void); uint8_t isBusy(void); + uint8_t cardCommand(uint8_t cmd, uint32_t arg); private: uint32_t block_; uint8_t chipSelectPin_; @@ -255,7 +256,6 @@ class Sd2Card { cardCommand(CMD55, 0); return cardCommand(cmd, arg); } - uint8_t cardCommand(uint8_t cmd, uint32_t arg); void error(uint8_t code) { errorCode_ = code; } From 7d080598849032c2cb18c625fd4f4b61681730b1 Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Mon, 31 Oct 2022 11:04:43 +0100 Subject: [PATCH 3/4] implemented card (un)locking --- src/utility/Sd2Card.cpp | 105 ++++++++++++++++++++++++++++++++++++++++ src/utility/Sd2Card.h | 9 ++++ src/utility/SdInfo.h | 4 ++ 3 files changed, 118 insertions(+) diff --git a/src/utility/Sd2Card.cpp b/src/utility/Sd2Card.cpp index 7cfbe73..720417b 100644 --- a/src/utility/Sd2Card.cpp +++ b/src/utility/Sd2Card.cpp @@ -331,6 +331,24 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { goto fail; } } + + uint8_t status_ext; + status_ = cardCommand(CMD13, 0); + status_ext = spiRec(); + if ( status_ ) { + error(SD_CARD_ERROR_SEND_STATUS); + goto fail; + } else { + if (status_ext) { + if ( status_ext | 0X01 ) { + error(SD_CARD_ERROR_LOCKED); + } else { + error(SD_CARD_ERROR_SEND_STATUS); + } + goto fail; + } + } + // if SD2 read OCR register to check for SDHC card if (type() == SD_CARD_TYPE_SD2) { if (cardCommand(CMD58, 0)) { @@ -763,6 +781,93 @@ uint8_t Sd2Card::writeStop(void) { return false; } //------------------------------------------------------------------------------ +/** lock or unlock SD card by password + * + * \param[in] pwd si the pointer to passowrd buffer + * \return The value one, true, is returned for success and + * the value zero, false, is returned for failure. + * + */ +uint8_t Sd2Card::lockUnlockCard(uint8_t flags, int pass_len, uint8_t* pwd) { + + + // select card + chipSelectLow(); + + // set block length + if ( cardCommand(CMD16, 18) ) { + error(SD_CARD_ERROR_CMD16); + goto fail; + } + + + // send the command + if (cardCommand(CMD42, 0)) { + error(SD_CARD_ERROR_CMD42); + goto fail; + } + + spiSend( 0xFE ); // Start Token + spiSend( flags ); // 4-zeros ERASE LOCK_UNLOCK CLR_PWD SET_PWD + spiSend( pass_len ); // password length + + // send password + for ( uint16_t i=0; i Date: Fri, 3 May 2024 20:57:24 +0200 Subject: [PATCH 4/4] cleanup --- src/utility/Sd2Card.cpp | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/src/utility/Sd2Card.cpp b/src/utility/Sd2Card.cpp index 720417b..dd07c0c 100644 --- a/src/utility/Sd2Card.cpp +++ b/src/utility/Sd2Card.cpp @@ -783,7 +783,7 @@ uint8_t Sd2Card::writeStop(void) { //------------------------------------------------------------------------------ /** lock or unlock SD card by password * - * \param[in] pwd si the pointer to passowrd buffer + * \param[in] pwd si the pointer to password buffer * \return The value one, true, is returned for success and * the value zero, false, is returned for failure. * @@ -816,35 +816,16 @@ uint8_t Sd2Card::lockUnlockCard(uint8_t flags, int pass_len, uint8_t* pwd) { spiSend( pwd[i] ); } -/* - //send fake CRC - spiSend(0xFF); - spiSend(0xFF); -*/ - // wait for response do { status_ = spiRec(); } while ( status_ == 0xFF ); -/* - for (uint8_t i = 0; ((status_ = spiRec()) & 0x10) && i != 0xFF; i++) { - Serial.println( status_, HEX ); - } -*/ - - // wait for not busy -// for (uint16_t i = 0; ( ~spiRec() ) && i != 0xFFFF; i++); uint8_t st; do { st = spiRec(); } while ( st != 0xFF ); -/* - if ( status_ != 0x05 ) { - error(0x88); - goto fail; - } -*/ + status_ = cardCommand(CMD13, 0); uint8_t status_ext; status_ext = spiRec();