Skip to content

Commit 056eb38

Browse files
committed
add missing CMD16, refactor code
1 parent 012482f commit 056eb38

File tree

3 files changed

+66
-65
lines changed

3 files changed

+66
-65
lines changed

src/SD.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,14 @@ namespace SDLib {
373373

374374
// protect a card with a password or remove it
375375
bool SDClass::lockCard(bool setLock, const char pwd) {
376-
return card.lockCard(setLock, pwd);
376+
return card.lockCard(setLock, pwd);
377377
}
378378

379379
// !!CAUTION!! call this to completly erase a card,
380380
// this will unlock a card but also delete the complete file system,
381381
// reformatting will be required!
382382
bool SDClass::forceEraseCard(){
383-
return card.forceEraseCard();
383+
return card.forceEraseCard();
384384
}
385385

386386
// this little helper is used to traverse paths

src/utility/Sd2Card.cpp

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -780,79 +780,78 @@ uint8_t Sd2Card::isBusy(void) {
780780
\param[in] setLock true if the card should be locked, false if it should be unlocked
781781
\param[in] pwd password to lock/unlock the card
782782
783-
\return true if the locking was successful
783+
\return true if the locking was successful
784784
*/
785785
bool Sd2Card::lockCard(bool setLock, const char *pwd){
786-
if(!isBusy()){
787-
int16_t i;
788-
uint8_t pwd_len = sizeof(pwd);
789-
bool locked = false;
790-
791-
if(cardCommand(CMD42, 0) != 0){
792-
return false;
793-
}
794-
795-
spiSend(DATA_START_BLOCK);
796-
if(setLock) {
797-
spiSend(SET_CARD_PWD & 0x07);
798-
}
799-
else{
800-
spiSend(CLEAR_CARD_PWD & 0x07);
801-
}
802-
803-
spiSend(pwd_len);
804-
for(i=0; i<512; i++){
805-
if(i<pwd_len){
806-
spiSend(pwd[i]);
807-
}
808-
else{
809-
spiSend(0xFF);
810-
}
811-
}
812-
813-
locked = getCardStatus();
814-
815-
spiSend(0xFF);
816-
spiSend(0xFF);
817-
818-
i = 0xFFFF;
819-
while(isBusy() && (--i));
820-
821-
return locked;
822-
}
786+
if(!isBusy()){
787+
int16_t i;
788+
uint8_t pwd_len = sizeof(pwd);
789+
bool locked = false;
790+
791+
if(cardCommand(CMD42, 0) != 0){
792+
return false;
793+
}
794+
795+
spiSend(DATA_START_BLOCK);
796+
if(setLock) {
797+
spiSend(SET_CARD_PWD & 0x07);
798+
}
799+
else{
800+
spiSend(CLEAR_CARD_PWD & 0x07);
801+
}
802+
803+
spiSend(pwd_len);
804+
for(i=0; i<512; i++){
805+
if(i<pwd_len){
806+
spiSend(pwd[i]);
807+
}
808+
else{
809+
spiSend(0xFF);
810+
}
811+
}
812+
813+
locked = getCardStatus();
814+
815+
spiSend(0xFF);
816+
spiSend(0xFF);
817+
818+
i = 0xFFFF;
819+
while(isBusy() && (--i));
820+
821+
return locked;
822+
}
823823
}
824824

825825
/** Completly erase a card
826-
!! Use with caution !! this removes everything from a card, even the file system.
827-
Use this to remove a lock with an unknown password from a card.
828-
829-
\return true if the erase was successful
826+
!! Use with caution !! this removes everything from a card, even the file system.
827+
Use this to remove a lock with an unknown password from a card.
828+
829+
\return true if the erase was successful
830830
*/
831831
bool Sd2Card::forceEraseCard(void) {
832-
// set block size to 512 bytes
833-
cardCommand(CMD16, 1);
834-
835-
// enable
836-
if(cardCommand(CMD42, 0) != 0){
837-
return false;
838-
}
839-
840-
spiSend(DATA_START_BLOCK);
841-
spiSend(FORCE_ERASE);
842-
843-
return true;
832+
// set block size to 512 bytes
833+
cardCommand(CMD16, 1);
834+
835+
// enable
836+
if(cardCommand(CMD42, 0) != 0){
837+
return false;
838+
}
839+
840+
spiSend(DATA_START_BLOCK);
841+
spiSend(FORCE_ERASE);
842+
843+
return true;
844844
}
845845

846846
/** Check status of card lock
847-
848-
\return true if card is locked
847+
\return true if card is locked
849848
*/
850849
bool Sd2Card::getCardStatus(void) {
851-
bool cardOK = true;
852-
853-
if(cardCommand(CMD13, 0) || spiRec()) {
854-
cardOK = false;
855-
}
856-
857-
return cardOK;
850+
bool cardOK = true;
851+
852+
if(cardCommand(CMD13, 0) || spiRec()) {
853+
cardOK = false;
854+
}
855+
856+
return cardOK;
858857
}

src/utility/SdInfo.h

Lines changed: 2 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 - change r/w block size */
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 */

0 commit comments

Comments
 (0)