Skip to content

Commit 863abd9

Browse files
committed
supplement to SD lock/unlock code
1 parent 2e1e4d3 commit 863abd9

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

libraries/SD/src/SD.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ using namespace fs;
2222

2323
SDFS::SDFS(FSImplPtr impl): FS(impl), _pdrv(0xFF) {}
2424

25-
bool SDFS::begin(uint8_t ssPin, SPIClass &spi, uint32_t frequency, const char * mountpoint)
25+
bool SDFS::begin(uint8_t ssPin, SPIClass &spi, uint32_t frequency, const char * mountpoint, const char* lockcode, int lockcode_len)
2626
{
2727
if(_pdrv != 0xFF) {
2828
return true;
2929
}
3030

3131
spi.begin();
3232

33-
_pdrv = sdcard_init(ssPin, &spi, frequency);
33+
_pdrv = sdcard_init(ssPin, &spi, frequency, lockcode, lockcode_len);
3434
if(_pdrv == 0xFF) {
3535
return false;
3636
}

libraries/SD/src/SD.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SDFS : public FS
2828

2929
public:
3030
SDFS(FSImplPtr impl);
31-
bool begin(uint8_t ssPin=SS, SPIClass &spi=SPI, uint32_t frequency=4000000, const char * mountpoint="/sd");
31+
bool begin(uint8_t ssPin=SS, SPIClass &spi=SPI, uint32_t frequency=4000000, const char * mountpoint="/sd", const char * lockcode=NULL, int lockcode_len=0);
3232
void end();
3333
sdcard_type_t cardType();
3434
uint64_t cardSize();

libraries/SD/src/sd_diskio.cpp

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ typedef struct {
5555
unsigned long sectors;
5656
bool supports_crc;
5757
int status;
58+
59+
const char * lockcode;
60+
int lockcode_len;
5861
} ardu_sdcard_t;
5962

6063
static ardu_sdcard_t* s_cards[FF_VOLUMES] = { NULL };
@@ -416,6 +419,58 @@ unsigned long sdGetSectorsCount(uint8_t pdrv)
416419
return 0;
417420
}
418421

422+
bool sdLockUnlock(uint8_t pdrv, const char* buffer, int len, char mode )
423+
{
424+
ardu_sdcard_t * card = s_cards[pdrv];
425+
unsigned short crc;
426+
char token;
427+
char cmdPacket[16+2];
428+
429+
if(!sdSelectCard(pdrv)) {
430+
return false;
431+
}
432+
433+
cmdPacket[0] = 42 | 0x40;
434+
cmdPacket[1] = 0;
435+
cmdPacket[2] = 0;
436+
cmdPacket[3] = 0;
437+
cmdPacket[4] = 0;
438+
cmdPacket[5] = (CRC7(cmdPacket, 5) << 1) | 0x00; //0x01; //0x28;
439+
cmdPacket[6] = 0xFF;
440+
card->spi->writeBytes((uint8_t*)cmdPacket, 7);
441+
442+
for (int i = 0; i < 9; i++) {
443+
token = card->spi->transfer(0xFF);
444+
if ((token & 0x80)==0x00) {
445+
break;
446+
}
447+
}
448+
449+
if((token&0x80)==0x00) {
450+
// Data Token
451+
card->spi->write(0xFE); // Data Token
452+
453+
// Data Block
454+
memset(cmdPacket, 0xFF, sizeof(cmdPacket));
455+
cmdPacket[0] = mode;
456+
cmdPacket[1] = len;
457+
memcpy(&cmdPacket[2], buffer, len);
458+
card->spi->writeBytes((uint8_t*)cmdPacket, 16+2);
459+
460+
// CRC
461+
crc = CRC16(cmdPacket, 16+2);
462+
card->spi->write16(crc);
463+
464+
// Wait for Data Response
465+
uint32_t start = millis();
466+
do {
467+
token = card->spi->transfer(0xFF);
468+
} while (token == 0xFF && (millis() - start) < 500);
469+
}
470+
sdDeselectCard(pdrv);
471+
472+
return true;
473+
}
419474

420475
namespace
421476
{
@@ -547,6 +602,13 @@ DSTATUS ff_sd_initialize(uint8_t pdrv)
547602
}
548603
}
549604

605+
if ((card->lockcode != NULL) && (card->lockcode_len > 0)) {
606+
if (sdTransaction(pdrv, SET_BLOCKLEN, 16+2, NULL) == 0x00) {
607+
sdLockUnlock(pdrv, card->lockcode, card->lockcode_len, 0x02);
608+
sdTransaction(pdrv, SET_BLOCKLEN, 512, NULL);
609+
}
610+
}
611+
550612
if (card->type != CARD_SDHC) {
551613
if (sdTransaction(pdrv, SET_BLOCKLEN, 512, NULL) != 0x00) {
552614
log_w("SET_BLOCKLEN failed");
@@ -658,7 +720,7 @@ uint8_t sdcard_uninit(uint8_t pdrv)
658720
return err;
659721
}
660722

661-
uint8_t sdcard_init(uint8_t cs, SPIClass * spi, int hz)
723+
uint8_t sdcard_init(uint8_t cs, SPIClass * spi, int hz, const char * lockcode, int lockcode_len)
662724
{
663725

664726
uint8_t pdrv = 0xFF;
@@ -680,6 +742,9 @@ uint8_t sdcard_init(uint8_t cs, SPIClass * spi, int hz)
680742
card->type = CARD_NONE;
681743
card->status = STA_NOINIT;
682744

745+
card->lockcode = lockcode;
746+
card->lockcode_len = lockcode_len;
747+
683748
pinMode(card->ssPin, OUTPUT);
684749
digitalWrite(card->ssPin, HIGH);
685750

libraries/SD/src/sd_diskio.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "SPI.h"
1919
#include "sd_defines.h"
2020

21-
uint8_t sdcard_init(uint8_t cs, SPIClass * spi, int hz);
21+
uint8_t sdcard_init(uint8_t cs, SPIClass * spi, int hz, const char * lockcode, int lockcode_len);
2222
uint8_t sdcard_uninit(uint8_t pdrv);
2323

2424
bool sdcard_mount(uint8_t pdrv, const char* path);
@@ -28,4 +28,6 @@ sdcard_type_t sdcard_type(uint8_t pdrv);
2828
uint32_t sdcard_num_sectors(uint8_t pdrv);
2929
uint32_t sdcard_sector_size(uint8_t pdrv);
3030

31+
bool sdLockUnlock(uint8_t pdrv, const char* buffer, int len, char mode );
32+
3133
#endif /* _SD_DISKIO_H_ */

0 commit comments

Comments
 (0)