Skip to content

Commit 7d08059

Browse files
committed
implemented card (un)locking
1 parent 8821f41 commit 7d08059

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

src/utility/Sd2Card.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,24 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
331331
goto fail;
332332
}
333333
}
334+
335+
uint8_t status_ext;
336+
status_ = cardCommand(CMD13, 0);
337+
status_ext = spiRec();
338+
if ( status_ ) {
339+
error(SD_CARD_ERROR_SEND_STATUS);
340+
goto fail;
341+
} else {
342+
if (status_ext) {
343+
if ( status_ext | 0X01 ) {
344+
error(SD_CARD_ERROR_LOCKED);
345+
} else {
346+
error(SD_CARD_ERROR_SEND_STATUS);
347+
}
348+
goto fail;
349+
}
350+
}
351+
334352
// if SD2 read OCR register to check for SDHC card
335353
if (type() == SD_CARD_TYPE_SD2) {
336354
if (cardCommand(CMD58, 0)) {
@@ -763,6 +781,93 @@ uint8_t Sd2Card::writeStop(void) {
763781
return false;
764782
}
765783
//------------------------------------------------------------------------------
784+
/** lock or unlock SD card by password
785+
*
786+
* \param[in] pwd si the pointer to passowrd buffer
787+
* \return The value one, true, is returned for success and
788+
* the value zero, false, is returned for failure.
789+
*
790+
*/
791+
uint8_t Sd2Card::lockUnlockCard(uint8_t flags, int pass_len, uint8_t* pwd) {
792+
793+
794+
// select card
795+
chipSelectLow();
796+
797+
// set block length
798+
if ( cardCommand(CMD16, 18) ) {
799+
error(SD_CARD_ERROR_CMD16);
800+
goto fail;
801+
}
802+
803+
804+
// send the command
805+
if (cardCommand(CMD42, 0)) {
806+
error(SD_CARD_ERROR_CMD42);
807+
goto fail;
808+
}
809+
810+
spiSend( 0xFE ); // Start Token
811+
spiSend( flags ); // 4-zeros ERASE LOCK_UNLOCK CLR_PWD SET_PWD
812+
spiSend( pass_len ); // password length
813+
814+
// send password
815+
for ( uint16_t i=0; i<pass_len; i++ ) {
816+
spiSend( pwd[i] );
817+
}
818+
819+
/*
820+
//send fake CRC
821+
spiSend(0xFF);
822+
spiSend(0xFF);
823+
*/
824+
825+
// wait for response
826+
do {
827+
status_ = spiRec();
828+
} while ( status_ == 0xFF );
829+
830+
/*
831+
for (uint8_t i = 0; ((status_ = spiRec()) & 0x10) && i != 0xFF; i++) {
832+
Serial.println( status_, HEX );
833+
}
834+
*/
835+
836+
// wait for not busy
837+
// for (uint16_t i = 0; ( ~spiRec() ) && i != 0xFFFF; i++);
838+
uint8_t st;
839+
do {
840+
st = spiRec();
841+
} while ( st != 0xFF );
842+
/*
843+
if ( status_ != 0x05 ) {
844+
error(0x88);
845+
goto fail;
846+
}
847+
*/
848+
status_ = cardCommand(CMD13, 0);
849+
uint8_t status_ext;
850+
status_ext = spiRec();
851+
if ( status_ ) {
852+
goto fail;
853+
error(0x89);
854+
} else {
855+
if ( status_ext ) {
856+
status_ = status_ext;
857+
error(0x8A);
858+
goto fail;
859+
}
860+
}
861+
862+
chipSelectHigh();
863+
return true;
864+
865+
fail:
866+
chipSelectHigh();
867+
return false;
868+
869+
}
870+
//------------------------------------------------------------------------------
766871
/** Check if the SD card is busy
767872
768873
\return The value one, true, is returned when is busy and

src/utility/Sd2Card.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X14;
160160
uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X15;
161161
/** incorrect rate selected */
162162
uint8_t const SD_CARD_ERROR_SCK_RATE = 0X16;
163+
/** error reading status */
164+
uint8_t const SD_CARD_ERROR_SEND_STATUS = 0X17;
165+
/** card is locked */
166+
uint8_t const SD_CARD_ERROR_LOCKED = 0X18;
167+
/** error lock/unlock card */
168+
uint8_t const SD_CARD_ERROR_CMD42 = 0X19;
169+
/** error lock/unlock card */
170+
uint8_t const SD_CARD_ERROR_CMD16 = 0X1A;
163171
//------------------------------------------------------------------------------
164172
// card types
165173
/** Standard capacity V1 SD card */
@@ -242,6 +250,7 @@ class Sd2Card {
242250
uint8_t writeStop(void);
243251
uint8_t isBusy(void);
244252
uint8_t cardCommand(uint8_t cmd, uint32_t arg);
253+
uint8_t lockUnlockCard(uint8_t flags, int pass_len, uint8_t* pwd);
245254
private:
246255
uint32_t block_;
247256
uint8_t chipSelectPin_;

src/utility/SdInfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ uint8_t const CMD9 = 0X09;
4242
uint8_t const CMD10 = 0X0A;
4343
/** SEND_STATUS - read the card status register */
4444
uint8_t const CMD13 = 0X0D;
45+
/** SET_BLOCKLEN = Set Block length */
46+
uint8_t const CMD16 = 0X10;
4547
/** READ_BLOCK - read a single data block from the card */
4648
uint8_t const CMD17 = 0X11;
4749
/** WRITE_BLOCK - write a single data block to the card */
@@ -55,6 +57,8 @@ uint8_t const CMD32 = 0X20;
5557
uint8_t const CMD33 = 0X21;
5658
/** ERASE - erase all previously selected blocks */
5759
uint8_t const CMD38 = 0X26;
60+
/** SD_LOCK_UNLOCK - Sends lock/unlock command with password */
61+
uint8_t const CMD42 = 0X2A;
5862
/** APP_CMD - escape for application specific command */
5963
uint8_t const CMD55 = 0X37;
6064
/** READ_OCR - read the OCR register of a card */

0 commit comments

Comments
 (0)