@@ -331,6 +331,24 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
331
331
goto fail;
332
332
}
333
333
}
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
+
334
352
// if SD2 read OCR register to check for SDHC card
335
353
if (type () == SD_CARD_TYPE_SD2) {
336
354
if (cardCommand (CMD58, 0 )) {
@@ -763,6 +781,93 @@ uint8_t Sd2Card::writeStop(void) {
763
781
return false ;
764
782
}
765
783
// ------------------------------------------------------------------------------
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
+ // ------------------------------------------------------------------------------
766
871
/* * Check if the SD card is busy
767
872
768
873
\return The value one, true, is returned when is busy and
0 commit comments