Skip to content

Add support for card locking/unlocking #152

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
86 changes: 86 additions & 0 deletions src/utility/Sd2Card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -763,6 +781,74 @@ uint8_t Sd2Card::writeStop(void) {
return false;
}
//------------------------------------------------------------------------------
/** lock or unlock SD card by password
*
* \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.
*
*/
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<pass_len; i++ ) {
spiSend( pwd[i] );
}

// wait for response
do {
status_ = spiRec();
} while ( status_ == 0xFF );

uint8_t st;
do {
st = spiRec();
} while ( st != 0xFF );

status_ = cardCommand(CMD13, 0);
uint8_t status_ext;
status_ext = spiRec();
if ( status_ ) {
goto fail;
error(0x89);
} else {
if ( status_ext ) {
status_ = status_ext;
error(0x8A);
goto fail;
}
}

chipSelectHigh();
return true;

fail:
chipSelectHigh();
return false;

}
//------------------------------------------------------------------------------
/** Check if the SD card is busy

\return The value one, true, is returned when is busy and
Expand Down
11 changes: 10 additions & 1 deletion src/utility/Sd2Card.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X14;
uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X15;
/** incorrect rate selected */
uint8_t const SD_CARD_ERROR_SCK_RATE = 0X16;
/** error reading status */
uint8_t const SD_CARD_ERROR_SEND_STATUS = 0X17;
/** card is locked */
uint8_t const SD_CARD_ERROR_LOCKED = 0X18;
/** error lock/unlock card */
uint8_t const SD_CARD_ERROR_CMD42 = 0X19;
/** error lock/unlock card */
uint8_t const SD_CARD_ERROR_CMD16 = 0X1A;
//------------------------------------------------------------------------------
// card types
/** Standard capacity V1 SD card */
Expand Down Expand Up @@ -241,6 +249,8 @@ 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);
uint8_t lockUnlockCard(uint8_t flags, int pass_len, uint8_t* pwd);
private:
uint32_t block_;
uint8_t chipSelectPin_;
Expand All @@ -255,7 +265,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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/utility/Sd2PinMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/utility/SdInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ uint8_t const CMD9 = 0X09;
uint8_t const CMD10 = 0X0A;
/** SEND_STATUS - read the card status register */
uint8_t const CMD13 = 0X0D;
/** SET_BLOCKLEN = Set Block length */
uint8_t const CMD16 = 0X10;
/** READ_BLOCK - read a single data block from the card */
uint8_t const CMD17 = 0X11;
/** WRITE_BLOCK - write a single data block to the card */
Expand All @@ -55,6 +57,8 @@ uint8_t const CMD32 = 0X20;
uint8_t const CMD33 = 0X21;
/** ERASE - erase all previously selected blocks */
uint8_t const CMD38 = 0X26;
/** SD_LOCK_UNLOCK - Sends lock/unlock command with password */
uint8_t const CMD42 = 0X2A;
/** APP_CMD - escape for application specific command */
uint8_t const CMD55 = 0X37;
/** READ_OCR - read the OCR register of a card */
Expand Down
Loading