-
Notifications
You must be signed in to change notification settings - Fork 3
/
sdlocker2.c
1277 lines (1051 loc) · 29.3 KB
/
sdlocker2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* sdlocker2 lock/unlock an SD card, uses ATmega328P
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <util/delay.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include "uart.h"
#ifndef FALSE
#define FALSE 0
#define TRUE !FALSE
#endif
/*
* Calc the value to write to the UART baud rate register, based on desired
* baud rate and MCU operating frequency (F_CPU).
*/
#define BAUDRATE 38400L
#define BAUDREG ((unsigned int)((F_CPU/(BAUDRATE*8UL))-1))
/*
* Define commands for the SD card
*/
#define SD_GO_IDLE (0x40 + 0) /* CMD0 - go to idle state */
#define SD_INIT (0x40 + 1) /* CMD1 - start initialization */
#define SD_SEND_IF_COND (0x40 + 8) /* CMD8 - send interface (conditional), works for SDHC only */
#define SD_SEND_CSD (0x40 + 9) /* CMD9 - send CSD block (16 bytes) */
#define SD_SEND_CID (0x40 + 10) /* CMD10 - send CID block (16 bytes) */
#define SD_SEND_STATUS (0x40 + 13) /* CMD13 - send card status */
#define SD_SET_BLK_LEN (0x40 + 16) /* CMD16 - set length of block in bytes */
#define SD_READ_BLK (0x40 + 17) /* read single block */
#define SD_LOCK_UNLOCK (0x40 + 42) /* CMD42 - lock/unlock card */
#define CMD55 (0x40 + 55) /* multi-byte preface command */
#define SD_READ_OCR (0x40 + 58) /* read OCR */
#define SD_ADV_INIT (0xc0 + 41) /* ACMD41, for SDHC cards - advanced start initialization */
#define SD_PROGRAM_CSD (0x40 + 27) /* CMD27 - get CSD block (15 bytes data + CRC) */
/*
* Define error tokens that can be returned following a data read/write
* request.
*/
#define ERRTKN_CARD_LOCKED (1<<4)
#define ERRTKN_OUT_OF_RANGE (1<<3)
#define ERRTKN_CARD_ECC (1<<2)
#define ERRTKN_CARD_CC (1<<1)
/*
* Define error codes that can be returned by local functions
*/
#define SDCARD_OK 0 /* success */
#define SDCARD_NO_DETECT 1 /* unable to detect SD card */
#define SDCARD_TIMEOUT 2 /* last operation timed out */
#define SDCARD_RWFAIL -1 /* read/write command failed */
/*
* Define options for accessing the SD card's PWD (CMD42)
*/
#define MASK_ERASE 0x08 /* erase the entire card */
#define MASK_LOCK_UNLOCK 0x04 /* lock or unlock the card with password */
#define MASK_CLR_PWD 0x02 /* clear password */
#define MASK_SET_PWD 0x01 /* set password */
/*
* Define card types that could be reported by the SD card during probe
*/
#define SDTYPE_UNKNOWN 0 /* card type not determined */
#define SDTYPE_SD 1 /* SD v1 (1 MB to 2 GB) */
#define SDTYPE_SDHC 2 /* SDHC (4 GB to 32 GB) */
/*
* Define values for the various switch closure states
*/
#define SW_LOCK 1
#define SW_UNLOCK 2
#define SW_NONE 3
#define SW_INFO 4
#define SW_READBLK 5
#define SW_PWD_LOCK 6
#define SW_PWD_UNLOCK 7
#define SW_PWD_CHECK 8
#define SW_LOCK_CHECK 9
#define SW_ERASE 10
/*
* Define the port and DDR used by the SPI.
*/
#define SPI_PORT PORTB
#define SPI_DDR DDRB
/*
* Define bits used by the SPI port.
*/
#define MOSI_BIT 3
#define MISO_BIT 4
#define SCK_BIT 5
/*
* Define the port, DDR, and bit used as chip-select for the
* SD card.
*/
#define SD_CS_PORT PORTB
#define SD_CS_DDR DDRB
#define SD_CS_BIT 2
#define SD_CS_MASK (1<<SD_CS_BIT)
/*
* Define the port and bit used for the lock LED.
*/
#define LOCK_LED_PORT PORTD
#define LOCK_LED_DDR DDRD
#define LOCK_LED_BIT 2
#define LOCK_LED_MASK (1<<LOCK_LED_BIT)
#define LOCK_LED_OFF (LOCK_LED_PORT=LOCK_LED_PORT&~LOCK_LED_MASK)
#define LOCK_LED_ON (LOCK_LED_PORT=LOCK_LED_PORT|LOCK_LED_MASK)
/*
* Define the port and bit used for the unlock LED.
*/
#define UNLOCK_LED_PORT PORTD
#define UNLOCK_LED_DDR DDRD
#define UNLOCK_LED_BIT 3
#define UNLOCK_LED_MASK (1<<UNLOCK_LED_BIT)
#define UNLOCK_LED_OFF (UNLOCK_LED_PORT=UNLOCK_LED_PORT&~UNLOCK_LED_MASK)
#define UNLOCK_LED_ON (UNLOCK_LED_PORT=UNLOCK_LED_PORT|UNLOCK_LED_MASK)
/*
* Define the port and bit used for the switches.
*/
#define SW_PORT PORTC
#define SW_DDR DDRC
#define SW_PIN PINC
#define SW_LOCK_BIT 0
#define SW_UNLOCK_BIT 1
#define SW_PWD_BIT 2
#define SW_LOCK_MASK (1<<SW_LOCK_BIT)
#define SW_UNLOCK_MASK (1<<SW_UNLOCK_BIT)
#define SW_PWD_MASK (1<<SW_PWD_BIT)
#define SW_ALL_MASK (SW_LOCK_MASK | SW_UNLOCK_MASK | SW_PWD_MASK)
/*
* Define LED patterns.
*/
#define PATTERN_NO_DETECT 0xc800c800
#define PATTERN_CANNOT_CHG 0xa5000000
/*
* Define the CRC7 polynomial
*/
#define CRC7_POLY 0x89 /* polynomial used for CSD CRCs */
/*
* Define bit masks for fields in the lock/unlock command (CMD42) data structure
*/
#define SET_PWD_MASK (1<<0)
#define CLR_PWD_MASK (1<<1)
#define LOCK_UNLOCK_MASK (1<<2)
#define ERASE_MASK (1<<3)
/*
* Local variables
*/
uint32_t LEDPattern;
uint8_t sdtype; // flag for SD card type
uint8_t csd[16];
uint8_t cid[16];
uint8_t ocr[4];
uint8_t crctable[256];
uint8_t block[512];
uint8_t cardstatus[2]; // updated by ReadLockStatus
uint8_t pwd[16];
uint8_t pwd_len;
const char GlobalPWDStr[16] PROGMEM =
{'F', 'o', 'u', 'r', 't', 'h', ' ', 'A',
'm', 'e', 'n', 'd', 'm', 'e', 'n', 't'};
#define GLOBAL_PWD_LEN (sizeof(GlobalPWDStr))
/*
* Local functions
*/
static void select(void);
static void deselect(void);
static uint8_t xchg(uint8_t c);
static int8_t SDInit(void);
static void BlinkLED(uint32_t pattern);
static uint8_t ReadSwitch(void);
static void ProcessSwitch(void);
static int8_t ExamineSD(void);
static int8_t ReadOCR(void);
static int8_t ReadCID(void);
static int8_t ReadCSD(void);
static int8_t WriteCSD(void);
static int8_t ReadBlock(uint32_t blocknum, uint8_t *buffer);
static void ShowBlock(void);
static void ShowErrorCode(int8_t status);
static int8_t ReadCardStatus(void);
static void ShowCardStatus(void);
static void ShowLockState(void);
static void LoadGlobalPWD(void);
static int8_t ModifyPWD(uint8_t mask);
static int8_t ForceErase(void);
static int8_t sd_send_command(uint8_t command, uint32_t arg);
static int8_t sd_wait_for_data(void);
static void GenerateCRCTable(void);
static uint8_t AddByteToCRC(uint8_t crc, uint8_t b);
int main(void)
{
/*
* Set up the hardware lines and ports associated with accessing the SD card.
*/
SD_CS_DDR = SD_CS_DDR | SD_CS_MASK; // make CS line an output
deselect(); // always start with SD card deselected
SPI_PORT = SPI_PORT | ((1<<MOSI_BIT) | (1<<SCK_BIT)); // drive outputs to the SPI port
SPI_DDR = SPI_DDR | ((1<<MOSI_BIT) | (1<<SCK_BIT)); // make the proper lines outputs
SPI_PORT = SPI_PORT | (1<<MISO_BIT); // turn on pull-up for DI
SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0);
/*
* Set up the hardware line and port for accessing the LEDs.
*/
LOCK_LED_OFF; // start with output line low
LOCK_LED_DDR = LOCK_LED_DDR | LOCK_LED_MASK; // make the LED line an output
UNLOCK_LED_OFF; // start with output line low
UNLOCK_LED_DDR = UNLOCK_LED_DDR | UNLOCK_LED_MASK; // make the LED line an output
/*
* Set up the switch lines for input.
*/
SW_DDR = SW_DDR & ~(SW_PWD_MASK | SW_LOCK_MASK | SW_UNLOCK_MASK);
SW_PORT = SW_PORT | (SW_PWD_MASK | SW_LOCK_MASK | SW_UNLOCK_MASK); // turn on pullups for switch lines
/*
* Set up the UART, then connect to standard I/O streams.
*/
uart_init();
stdout = &uart_output;
stdin = &uart_input;
stderr = &uart_output;
sei(); // let the UART ISR work
printf_P(PSTR("\r\nSDLocker2.1\r\n"));
printf_P(PSTR("? - SD info\r\n"));
printf_P(PSTR("u - Write Unlock\r\n"));
printf_P(PSTR("l - Write Lock\r\n"));
printf_P(PSTR("p - Password Unlock\r\n"));
printf_P(PSTR("P - Password Lock\r\n"));
printf_P(PSTR("E - Erase\r\n"));
printf_P(PSTR("r - Read\r\n"));
GenerateCRCTable();
while (1)
{
ProcessSwitch();
}
return 0; // should never happen
}
void BlinkLED(uint32_t pattern)
{
uint8_t i;
for (i=0; i<32; i++)
{
if (pattern & 0x80000000)
{
LOCK_LED_ON;
}
else
{
LOCK_LED_OFF;
if (pattern == 0) break; // leave blink loop if no more ON bits
}
_delay_ms(50);
pattern = pattern << 1;
}
}
static void GenerateCRCTable()
{
int i, j;
// generate a table value for all 256 possible byte values
for (i = 0; i < 256; i++)
{
crctable[i] = (i & 0x80) ? i ^ CRC7_POLY : i;
for (j = 1; j < 8; j++)
{
crctable[i] <<= 1;
if (crctable[i] & 0x80)
crctable[i] ^= CRC7_POLY;
}
}
}
static uint8_t AddByteToCRC(uint8_t crc, uint8_t b)
{
return crctable[(crc << 1) ^ b];
}
static void ProcessSwitch(void)
{
uint8_t sw;
static uint8_t prev_sw = 0;
uint8_t r;
sw = ReadSwitch();
if ((sw != prev_sw) && (prev_sw == SW_NONE))
{
/*
* Need to access the card. In all cases, first try to initialize
* the card.
*/
r = SDInit();
if (r != SDCARD_OK)
{
printf_P(PSTR("\n\r\n\rCannot initialize card. Make sure the card is plugged in properly."));
BlinkLED(PATTERN_NO_DETECT);
}
/*
* Now see what we need to do.
*/
if (sw == SW_INFO)
{
LOCK_LED_OFF;
UNLOCK_LED_OFF;
printf_P(PSTR("\r\nCard type %d"), sdtype);
r = ExamineSD();
if (r == SDCARD_OK)
{
printf_P(PSTR("\r\nOCR = "));
for (r = 0; r<4; r++)
{
printf_P(PSTR("%02X "), ocr[r]);
}
printf_P(PSTR("\r\nCSD = "));
for (r=0; r<16; r++)
{
printf_P(PSTR("%02X "), csd[r]);
}
printf_P(PSTR("\r\nCID = "));
for (r=0; r<16; r++)
{
printf_P(PSTR("%02X "), cid[r]);
}
ShowCardStatus();
}
else
{
printf_P(PSTR("\r\nUnable to read CSD."));
}
}
else if (sw == SW_LOCK)
{
LOCK_LED_OFF;
UNLOCK_LED_OFF;
printf_P(PSTR("\r\nSetting temporary lock on SD card..."));
r = ReadCSD();
if (r == SDCARD_OK)
{
csd[14] = csd[14] | 0x10; // set bit 12 of CSD (temp lock)
r = WriteCSD();
if (r == SDCARD_OK)
{
ReadOCR();
r = ReadCSD();
if (r == SDCARD_OK)
{
ShowLockState();
printf_P(PSTR("done."));
}
else
{
printf_P(PSTR("failed; cannot read CSD to confirm."));
}
}
else
{
printf_P(PSTR("failed; response was %d."), r);
BlinkLED(PATTERN_CANNOT_CHG);
}
}
else
{
printf_P(PSTR("failed; unable to read CSD."));
BlinkLED(PATTERN_NO_DETECT);
}
}
else if (sw == SW_UNLOCK)
{
LOCK_LED_OFF;
UNLOCK_LED_OFF;
printf_P(PSTR("\r\nClearing temporary lock on SD card..."));
r = ReadCSD();
if (r == SDCARD_OK)
{
csd[14] = csd[14] & ~0x10; // clear bit 12 of CSD (temp lock)
r = WriteCSD();
if (r == SDCARD_OK)
{
ReadOCR();
r = ReadCSD();
if (r == SDCARD_OK)
{
ShowLockState();
printf_P(PSTR("done."));
}
else
{
printf_P(PSTR("failed; cannot read CSD to confirm."));
}
}
else
{
printf_P(PSTR("failed; response was %d."), r);
BlinkLED(PATTERN_CANNOT_CHG);
}
}
else
{
printf_P(PSTR("failed; unable to read CSD."));
BlinkLED(PATTERN_NO_DETECT);
}
}
else if (sw == SW_READBLK)
{
printf_P(PSTR("\r\nTest read of block 0 on SD card..."));
r = ReadBlock(0, block);
if (r == SDCARD_OK)
{
ShowBlock();
}
}
else if (sw == SW_ERASE)
{
printf_P(PSTR("\r\nTrying to ERASE SD CARD..."));
LOCK_LED_OFF;
UNLOCK_LED_OFF;
ReadCardStatus();
if (cardstatus[1] & 0x01) // if card is locked...
{
r = ForceErase();
printf_P(PSTR("please wait..."));
_delay_ms(1000);
ReadCardStatus();
if (cardstatus[1] & 0x01) // if card is still locked...
{
r = ForceErase(); // erasing failed, try one more time
printf_P(PSTR("please wait..."));
_delay_ms(1000);
ReadCardStatus();
}
if (cardstatus[1] & 0x01) // if card is still locked...
{
printf_P(PSTR("failed! Card is still locked."));
LOCK_LED_ON;
}
else
{
printf_P(PSTR("done."));
UNLOCK_LED_ON;
}
}
else // silly person, card is already unlocked
{
printf_P(PSTR("the card is not locked"));
UNLOCK_LED_ON;
}
}
else if (sw == SW_PWD_UNLOCK)
{
LOCK_LED_OFF;
UNLOCK_LED_OFF;
ReadCardStatus();
if (cardstatus[1] & 0x01) // if card is locked...
{
printf_P(PSTR("\r\nTrying to unlock card..."));
LoadGlobalPWD();
r = ModifyPWD(MASK_CLR_PWD);
ReadCardStatus();
if (cardstatus[1] & 0x01) // if card is still locked...
{
r = ModifyPWD(MASK_CLR_PWD); // the unlock failed, try one more time
ReadCardStatus();
}
if (cardstatus[1] & 0x01) // if card is still locked...
{
printf_P(PSTR("failed! Card is still locked."));
LOCK_LED_ON;
}
else
{
printf_P(PSTR("done."));
UNLOCK_LED_ON;
}
}
else // silly person, card is already unlocked
{
UNLOCK_LED_ON;
}
}
else if (sw == SW_PWD_LOCK)
{
LOCK_LED_OFF;
UNLOCK_LED_OFF;
ReadCardStatus();
if ((cardstatus[1] & 0x01) == 0) // if card is unlocked...
{
printf_P(PSTR("\r\nTrying to lock card..."));
LoadGlobalPWD();
r = ModifyPWD(MASK_SET_PWD);
ReadCardStatus();
r = ModifyPWD(MASK_LOCK_UNLOCK);
ReadCardStatus();
if ((cardstatus[1] & 0x01) == 0) // if card is still unlocked...
{
printf_P(PSTR("failed! Card is still unlocked."));
UNLOCK_LED_ON;
}
else
{
printf_P(PSTR("done."));
LOCK_LED_ON;
}
}
else // silly person, card is already locked
{
LOCK_LED_ON;
}
}
else if (sw == SW_PWD_CHECK)
{
LOCK_LED_OFF;
UNLOCK_LED_OFF;
printf_P(PSTR("\r\nChecking PWD state..."));
ReadCardStatus();
if ((cardstatus[1] & 0x01) == 0) // if card is unlocked...
{
UNLOCK_LED_ON;
}
else
{
LOCK_LED_ON;
}
}
else if (sw == SW_LOCK_CHECK)
{
printf_P(PSTR("\r\nChecking temp-lock state..."));
ReadOCR();
r = ReadCSD();
if (r == SDCARD_OK)
{
ShowLockState();
}
else
{
BlinkLED(PATTERN_NO_DETECT);
}
}
}
prev_sw = sw;
}
static uint8_t ReadSwitch(void)
{
uint8_t r;
static uint8_t prev_sw = SW_ALL_MASK;
static uint16_t sw_hold_counter = 0;
uint8_t sw;
_delay_ms(50);
r = SW_NONE;
if (uart_pending_data())
{
r = getchar();
if (r == 'u') r = SW_UNLOCK;
else if (r == 'l') r = SW_LOCK;
else if (r == '?') r = SW_INFO;
else if (r == 'r') r = SW_READBLK;
else if (r == 'p') r = SW_PWD_UNLOCK;
else if (r == 'P') r = SW_PWD_LOCK;
else if (r == 'E') r = SW_ERASE;
else r = SW_NONE;
}
if (r == SW_NONE)
{
sw = SW_PIN & SW_ALL_MASK;
if (sw != SW_ALL_MASK) // if at least one switch is down...
{
if (((sw & SW_PWD_MASK) == 0) && ((prev_sw & SW_PWD_MASK) == 0)) // if PWD switch is down both scans...
{
sw_hold_counter++;
if(sw_hold_counter > 0xB0) // PWD hold (about 10 sec timeout)
{
sw_hold_counter = 0;
r = SW_ERASE;
}
else if (((sw & SW_LOCK_MASK) == 0) && (prev_sw & SW_LOCK_MASK)) // if LOCK switch was just pressed...
{
sw_hold_counter = 0;
r = SW_PWD_LOCK;
}
else if (((sw & SW_UNLOCK_MASK) == 0) && (prev_sw & SW_UNLOCK_MASK)) // if UNLOCK switch was just pressed...
{
sw_hold_counter = 0;
r = SW_PWD_UNLOCK;
}
}
else if (((sw & SW_PWD_MASK) == 0) && (prev_sw & SW_PWD_MASK)) // if PWD switch was just pressed...
{
sw_hold_counter = 0;
if ((sw & (SW_LOCK_MASK | SW_UNLOCK_MASK)) == (SW_LOCK_MASK | SW_UNLOCK_MASK)) // if other switches are open...
{
r = SW_PWD_CHECK;
}
}
else if ((sw & SW_PWD_MASK) == SW_PWD_MASK) // if PWD switch is now open...
{
sw_hold_counter = 0;
if ((sw & (SW_LOCK_MASK | SW_UNLOCK_MASK)) == SW_UNLOCK_MASK) // if LOCK switch is pressed...
{
if (prev_sw & SW_LOCK_MASK) // but LOCK switch wasn't pressed before...
{
r = SW_LOCK;
}
}
else if ((sw & (SW_LOCK_MASK | SW_UNLOCK_MASK)) == SW_LOCK_MASK) // if UNLOCK switch is pressed...
{
if (prev_sw & SW_UNLOCK_MASK) // but UNLOCK switch wasn't pressed before...
{
r = SW_UNLOCK;
}
}
}
}
else // no switches are down...
{
sw_hold_counter = 0;
if ((prev_sw & SW_PWD_MASK) == 0) // if PWD switch was just released...
{
r = SW_LOCK_CHECK;
}
}
prev_sw = sw; // record for next time
}
return r;
}
static void ShowLockState(void)
{
LOCK_LED_OFF;
UNLOCK_LED_OFF;
if (csd[14] & 0x10) // check lock bit in CSD...
{
LOCK_LED_ON;
}
else
{
UNLOCK_LED_ON;
}
}
/*
* select select (enable) the SD card
*/
static void select(void)
{
SD_CS_PORT = SD_CS_PORT & ~SD_CS_MASK;
}
/*
* deselect deselect (disable) the SD card.
*/
static void deselect(void)
{
SD_CS_PORT = SD_CS_PORT | SD_CS_MASK;
}
/*
* xchg exchange a byte of data with the SD card via host's SPI bus
*/
static unsigned char xchg(unsigned char c)
{
SPDR = c;
while ((SPSR & (1<<SPIF)) == 0) ;
return SPDR;
}
static int8_t SDInit(void)
{
int i;
int8_t response;
sdtype = SDTYPE_UNKNOWN; // assume this fails
/*
* Begin initialization by sending CMD0 and waiting until SD card
* responds with In Idle Mode (0x01). If the response is not 0x01
* within a reasonable amount of time, there is no SD card on the bus.
*/
deselect(); // always make sure
for (i=0; i<10; i++) // send several clocks while card power stabilizes
xchg(0xff);
for (i=0; i<0x10; i++)
{
response = sd_send_command(SD_GO_IDLE, 0); // send CMD0 - go to idle state
if (response == 1) break;
}
if (response != 1)
{
return SDCARD_NO_DETECT;
}
sd_send_command(SD_SET_BLK_LEN, 512); // always set block length (CMD6) to 512 bytes
response = sd_send_command(SD_SEND_IF_COND, 0x1aa); // probe to see if card is SDv2 (SDHC)
if (response == 0x01) // if card is SDHC...
{
for (i=0; i<4; i++) // burn the 4-byte response (OCR)
{
xchg(0xff);
}
for (i=20000; i>0; i--)
{
response = sd_send_command(SD_ADV_INIT, 1UL<<30);
if (response == 0) break;
}
sdtype = SDTYPE_SDHC;
}
else
{
response = sd_send_command(SD_READ_OCR, 0);
if (response == 0x01)
{
for (i=0; i<4; i++) // OCR is 4 bytes
{
xchg(0xff); // burn the 4-byte response (OCR)
}
for (i=20000; i>0; i--)
{
response = sd_send_command(SD_INIT, 0);
if (response == 0) break;
}
sd_send_command(SD_SET_BLK_LEN, 512);
sdtype = SDTYPE_SD;
}
}
xchg(0xff); // send 8 final clocks
/*
* At this point, the SD card has completed initialization. The calling routine
* can now increase the SPI clock rate for the SD card to the maximum allowed by
* the SD card (typically, 20 MHz).
*/
return SDCARD_OK; // if no power routine or turning off the card, call it good
}
static void ShowBlock(void)
{
uint32_t i;
uint8_t str[17];
str[16] = 0;
str[0] = 0; // only need for first newline, overwritten as chars are processed
printf_P(PSTR("\n\rContents of block buffer:"));
for (i=0; i<512; i++)
{
if ((i % 16) == 0)
{
printf_P(PSTR(" %s\n\r%04X: "), str, i);
}
printf_P(PSTR("%02X "), (uint8_t)block[i]);
if (isalpha(block[i]) || isdigit(block[i])) str[i%16] = block[i];
else str[i%16] = '.';
}
printf_P(PSTR(" %s\n\r"), str);
}
static int8_t ExamineSD(void)
{
int8_t response;
response = ReadOCR(); // this fails with Samsung; don't test response until know why
response = ReadCSD();
if (response == SDCARD_OK)
{
// printf_P(PSTR(" ReadCSD is OK "));
response = ReadCID();
}
if (response == SDCARD_OK)
{
// printf_P(PSTR(" ReadCID is OK "));
response = ReadCardStatus();
}
return response;
}
static int8_t ReadOCR(void)
{
uint8_t i;
int8_t response;
for (i=0; i<4; i++) ocr[i] = 0;
if (sdtype == SDTYPE_SDHC)
{
response = sd_send_command(SD_SEND_IF_COND, 0x1aa);
if (response != 0)
{
return SDCARD_RWFAIL;
}
for (i=0; i<4; i++)
{
ocr[i] = xchg(0xff);
}
xchg(0xff); // burn the CRC
}
else
{
response = sd_send_command(SD_READ_OCR, 0);
if (response != 0x00)
{
return SDCARD_RWFAIL;
}
for (i=0; i<4; i++) // OCR is 4 bytes
{
ocr[i] = xchg(0xff);
}
xchg(0xff);
}
return SDCARD_OK;
}
static int8_t ReadCSD(void)
{
uint8_t i;
int8_t response;
for (i=0; i<16; i++) csd[i] = 0;
response = sd_send_command(SD_SEND_CSD, 0);
response = sd_wait_for_data();
if (response != (int8_t)0xfe)
{
printf_P(PSTR("\n\rReadCSD(), sd_wait_for_data returns %02x."), response);
return SDCARD_RWFAIL;
}
for (i=0; i<16; i++)
{
csd[i] = xchg(0xff);
}
xchg(0xff); // burn the CRC
return SDCARD_OK;
}
static int8_t ReadCID(void)
{
uint8_t i;
int8_t response;
for (i=0; i<16; i++) cid[i] = 0;
response = sd_send_command(SD_SEND_CID, 0);
response = sd_wait_for_data();
if (response != (int8_t)0xfe)
{
return SDCARD_RWFAIL;
}
for (i=0; i<16; i++)
{
cid[i] = xchg(0xff);
}
xchg(0xff); // burn the CRC
return SDCARD_OK;
}
static int8_t WriteCSD(void)
{
int8_t response;
uint8_t tcrc;
uint16_t i;
response = sd_send_command(SD_PROGRAM_CSD, 0);
if (response != 0)
{
return SDCARD_RWFAIL;
}
xchg(0xfe); // send data token marking start of data block
tcrc = 0;
for (i=0; i<15; i++) // for all 15 data bytes in CSD...
{
xchg(csd[i]); // send each byte via SPI
tcrc = AddByteToCRC(tcrc, csd[i]); // add byte to CRC
}
xchg((tcrc<<1) + 1); // format the CRC7 value and send it
xchg(0xff); // ignore dummy checksum
xchg(0xff); // ignore dummy checksum
i = 0xffff; // max timeout