-
Notifications
You must be signed in to change notification settings - Fork 3
/
sd.c
1309 lines (1074 loc) · 39.5 KB
/
sd.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
/* SD/SPI driver for LPC2109
*
* Vanya A. Sergeev - <vsergeev@gmail.com> - copyright 2010
* please inform author of possible use, licensing is still being decided
*
*/
#include "sd.h"
/* A few public ariables keeping track of the SD's capacity
* and block length. */
int sd_high_capacity;
int sd_block_len;
int sd_mmc;
uint8_t sd_csd[SD_CSD_LENGTH];
uint8_t sd_cid[SD_CID_LENGTH];
#ifdef SD_DEBUG
#include "debug.h"
#endif
#ifdef SD_DEBUG
void sd_debug_print_boolean(uint8_t data) {
if (data == 1)
debug_printf("true");
else if (data == 0)
debug_printf("false");
}
void sd_debug_print_data_block(uint8_t *data) {
int i;
for (i = 0; i < sd_block_len; i++) {
debug_printf("%02X ", data[i]);
if (((i+1) % 16) == 0) {
debug_printf("\n");
} else if (((i+1) % 8) == 0) {
debug_printf(" ");
}
}
debug_printf("\n");
}
void sd_debug_print_csd(void) {
int csd_version, i;
uint8_t temp;
debug_printf("\n");
debug_printf(" ********************************\n");
debug_printf(" * CSD\n");
debug_printf(" *\n");
debug_printf(" * Raw CSD Bytes: ");
for (i = 0; i < 16; i++)
debug_printf("%02X ", sd_csd[i]);
debug_printf("\n");
debug_printf(" *\n");
csd_version = (sd_csd[0] & 0xC0) >> 6;
if (csd_version == 0)
debug_printf(" * VERSION: 1.0\n");
else if (csd_version == 1)
debug_printf(" * VERSION: 2.0\n");
else
debug_printf(" * VERSION: Unknown\n");
debug_printf(" * TAAC: %X\n", sd_csd[1]);
debug_printf(" * NSAC: %X\n", sd_csd[2]);
debug_printf(" * TRAN_SPEED: %X\n", sd_csd[3]);
// CCC goes here
debug_printf(" * READ_BL_LEN: %X\n", (sd_csd[5] & 0x0F));
debug_printf(" * READ_BL_PARTIAL: ");
sd_debug_print_boolean((sd_csd[6] & 0x80)>>7);
debug_printf("\n");
debug_printf(" * WRITE_BLK_MISALIGN: ");
sd_debug_print_boolean((sd_csd[6] & 0x40)>>6);
debug_printf("\n");
debug_printf(" * READ_BLK_MISALIGN: ");
sd_debug_print_boolean((sd_csd[6] & 0x20)>>5);
debug_printf("\n");
debug_printf(" * DSR_IMP: ");
sd_debug_print_boolean((sd_csd[6] & 0x10)>>4);
debug_printf("\n");
if (csd_version != 1) {
temp = (sd_csd[6] & 0x3) << 2;
temp |= ((sd_csd[7] & 0xC0) >> 6);
debug_printf(" * C_SIZE: %0X ", temp);
temp = (sd_csd[7] & 0x3F) << 2;
temp |= ((sd_csd[8] & 0xC0) >> 6);
debug_printf("%0X\n", temp);
debug_printf(" * VDD_R_CURR_MIN: %X\n", ((sd_csd[8] & 0x38) >> 3));
debug_printf(" * VDD_R_CURR_MAX: %X\n", (sd_csd[8] & 0x7));
debug_printf(" * VDD_W_CURR_MIN: %X\n", (sd_csd[9] & 0xE0) >> 5);
debug_printf(" * VDD_W_CURR_MAX: %X\n", ((sd_csd[9] & 0x1C) >> 2));
temp = (sd_csd[9] & 0x3) << 1;
temp |= ((sd_csd[10] & 0x80) >> 7);
debug_printf(" * C_SIZE_MULT: %X\n", temp);
} else {
debug_printf(" * C_SIZE: %0X %0X %0X\n", (sd_csd[7] & 0x3F), sd_csd[8], sd_csd[9]);
}
debug_printf(" * ERASE_BLK_EN: ");
sd_debug_print_boolean((sd_csd[10] & 0x40) >> 6);
debug_printf("\n");
temp = (sd_csd[10] & 0x3F) << 1;
temp |= (sd_csd[11] & 0x80) >> 7;
debug_printf(" * SECTOR_SIZE: %0X\n", temp);
debug_printf(" * WP_GRP_SIZE: %X\n", (sd_csd[11] & 0x7F));
debug_printf(" * WP_GRP_ENABLE: ");
sd_debug_print_boolean((sd_csd[12] & 0x80) >> 7);
debug_printf("\n");
debug_printf(" * R2W_FACTOR: %X\n", (sd_csd[12] & 0x1C) >> 2);
temp = (sd_csd[12] & 0x3) << 2;
temp |= ((sd_csd[13] & 0xC0) >> 6);
debug_printf(" * WRITE_BL_LEN: %X\n", temp);
debug_printf(" * WRITE_BL_PARTIAL: ");
sd_debug_print_boolean((sd_csd[13] & 0x20) >> 5);
debug_printf("\n");
debug_printf(" * FILE_FORMAT_GRP: ");
sd_debug_print_boolean((sd_csd[14] & 0x80) >> 7);
debug_printf("\n");
debug_printf(" * COPY: ");
sd_debug_print_boolean((sd_csd[14] & 0x40) >> 6);
debug_printf("\n");
debug_printf(" * PERM_WRITE_PROTECT: ");
sd_debug_print_boolean((sd_csd[14] & 0x20) >> 5);
debug_printf("\n");
debug_printf(" * TMP_WRITE_PROTECT: ");
sd_debug_print_boolean((sd_csd[14] & 0x10) >> 4);
debug_printf("\n");
debug_printf(" * FILE_FORMAT: %X\n", (sd_csd[14] & 0x0C) >> 2);
debug_printf(" *\n");
debug_printf(" ********************************\n\n");
}
void sd_debug_print_cid(void) {
int i;
debug_printf("\n");
debug_printf(" ********************************\n");
debug_printf(" * CID\n");
debug_printf(" *\n");
debug_printf(" * Raw CID Bytes: ");
for (i = 0; i < 16; i++)
debug_printf("%X ", sd_cid[i]);
debug_printf("\n");
debug_printf(" *\n");
debug_printf(" * Manufacturer ID (MID): %X\n", sd_cid[0]);
debug_printf(" * OEM/Application ID (OID): %c%c\n", sd_cid[1], sd_cid[2]);
debug_printf(" * Product name (PNM): %c%c%c%c%c%c\n", sd_cid[3], sd_cid[4], sd_cid[5], sd_cid[6], sd_cid[7]);
debug_printf(" * Product revision (PRV): %X\n", sd_cid[8]);
debug_printf(" * Product serial number (PSV): %X%X%X%X\n", sd_cid[9], sd_cid[10], sd_cid[11], sd_cid[12]);
debug_printf(" * Manufacturing date (MDT): %X %X\n", (sd_cid[13] & 0x0F), sd_cid[14]);
debug_printf(" *\n");
debug_printf(" ********************************\n\n");
}
#endif
void sd_debug_print(char *message, uint8_t *data, int dataLen) {
#ifdef SD_DEBUG
int i;
debug_printf(message);
for (i = 0; i < dataLen; i++)
debug_printf("%02X", data[i]);
debug_printf("\n");
#endif
}
/******************************************************************************
*** Low-level SPI interface functions ***
******************************************************************************/
void sd_cd_wp_init(void) {
/* Set card detect and write protect pins at inputs */
FIO0DIR &= ~((1<<18)|(1<<19));
}
uint8_t sd_card_detect(void) {
/* Return the status of the card detect switch */
if (FIO0PIN & (1<<18))
return 1;
return 0;
}
uint8_t sd_write_protect(void) {
/* Return the status of the write protect switch */
if (FIO0PIN & (1<<19))
return 1;
return 0;
}
void sd_spi_init(void) {
int i;
/* Enable SCK0, MISO0, and MOSI0 for SPI0 bus use,
* but declare SSEL0 as a GPIO for now. */
PINSEL0 |= ((1<<8)|(1<<10)|(1<<12));
PINSEL0 &= ~((1<<9)|(1<<11)|(1<<13)|(1<<15)|(1<<14));
/* Set the SPI Clock speed to 400KHz for now (initialization)
* PCLK = 60MHz, SPI Rate = 400KHz = 60/150, S0SPCCR = 150 */
S0SPCCR = 150;
/* Set the SPI Control Register
* 8 bits data, CPOL = 0, CPHA = 0, Master = 1,
* LSBF = 0 (MSB first), SPIE = 0 */
S0SPCR = (1<<5);
/* Set the SD chip select pin as an output */
SD_CS_IODIR |= SD_CS_PIN;
/* Hold SSEL0 high so the SD card can initialize itself with
* the next 80 clocks on SCK0. */
sd_spi_deselect();
/* Clock out at least 74 cycles with no data, so the SD card can
* initialize itself. */
for (i = 0; i < 15; i++)
sd_spi_delay_clocks();
}
void sd_spi_send(uint8_t data) {
volatile uint8_t dummy;
/* Put the data in the shift register */
S0SPDR = data;
/* Wait until the transfer complete flag clears */
while ((S0SPSR & (1<<7)) != (1<<7))
;
/* Read S0SPDR to clear the status register */
dummy = S0SPDR;
}
uint8_t sd_spi_receive(void) {
sd_spi_select();
/* Send a dummy byte */
S0SPDR = 0xFF;
/* Wait until the transfer complete flag clears */
while ((S0SPSR & (1<<7)) != (1<<7))
;
sd_spi_deselect();
/* Read the data clocked in */
return S0SPDR;
}
void sd_spi_delay_clocks(void) {
/* Ensure that CS is high */
sd_spi_select();
/* Send a dummy byte */
sd_spi_send(0xFF);
}
/******************************************************************************
*** SD CRC and Command Functions ***
******************************************************************************/
uint16_t sd_crc16_bits(uint8_t data, uint16_t seed) {
int i, feedback;
/* Feedback
* --------------------------------------------------------------------------------------X--Input
* | | | |
* [0]->[1]->[2]->[3]->[4]->X->[5]->[6]->[7]->[8]->[9]->[10]->[11]->X->[12]->[13]->[14]->[15]
*
* 0...15 = seed
* X = XOR
*
*/
for (i = 0; i < 8; i++) {
/* Feedback from xor of input and seed MSB bits */
feedback = ((data>>7) ^ (seed>>15)) & 0x1;
/* If we have no feedback, we have nothing to XOR
* and we shift the seed normally to the left */
if (feedback == 0) {
seed <<= 1;
} else {
/* Otherwise, XOR the feedback bits onto the seed */
seed ^= (0x10|0x800);
/* Shift the seed */
seed <<= 1;
/* Append a one to the bottom of the seed */
seed |= 0x01;
}
/* Shift the data to the left */
data <<= 1;
}
return seed;
}
uint16_t sd_crc16_data(const uint8_t *data, int dataLen) {
int i;
uint16_t seed = 0;
for (i = 0; i < dataLen; i++) {
seed = sd_crc16_bits(data[i], seed);
}
return seed;
}
uint8_t sd_crc7_bits(uint8_t data, uint8_t seed) {
int i, feedback;
/* Feedback
* ---------------------------------X--Input
* | | |
* [0]->[1]->[2]->X->[3]->[4]->[5]->[6]
*
* 0...6 = seed
* X = XOR
*
*/
for (i = 0; i < 8; i++) {
/* Feedback from xor of input and seed MSB bits */
feedback = (((data&0x80)>>7) ^ ((seed&0x40)>>6)) & 0x1;
/* If we have no feedback, we have nothing to XOR
* and we shift the seed normally to the left */
if (feedback == 0) {
seed <<= 1;
} else {
/* Otherwise, XOR the feedback bit onto the seed */
seed ^= 0x04;
/* Shift the seed */
seed <<= 1;
/* Append a one to the bottom of the seed */
seed |= 0x01;
}
/* Shift the data to the left */
data <<= 1;
}
return (seed & 0x7F);
}
uint8_t sd_crc7_packet(const uint8_t *data, int dataLen) {
int i;
uint8_t seed = 0;
for (i = 0; i < dataLen; i++)
seed = sd_crc7_bits(data[i], seed);
/* Append the end 1 bit */
seed <<= 1;
seed |= 0x1;
return seed;
}
void sd_spi_command_send(uint8_t command, uint32_t argument) {
uint8_t packet[6];
int i;
sd_spi_select();
/* Begin to transmit the 6-byte packet:
* 1 byte command, 4 bytes argument, 1 byte CRC */
/* Encode 01cc cccc, where c is the 6-bit command */
packet[0] = 0x40 | (command&0x3F);
/* Encode most significant to least significant argument bytes */
packet[1] = (uint8_t)((argument>>24)&0xFF);
packet[2] = (uint8_t)((argument>>16)&0xFF);
packet[3] = (uint8_t)((argument>>8)&0xFF);
packet[4] = (uint8_t)(argument&0xFF);
/* Calculate the CRC7 */
packet[5] = sd_crc7_packet(packet, 5);
for (i = 0; i < 6; i++)
sd_spi_send(packet[i]);
sd_spi_deselect();
}
void sd_spi_command_response(uint8_t *response, int responseLength) {
int i;
/* Read the response */
/* Wait until we start getting some data..*/
for (i = 0; i < SD_SPI_CMD_READ_ATTEMPTS; i++) {
response[0] = sd_spi_receive();
if (response[0] != 0xFF)
break;
}
for (i = 0; i < (responseLength-1); i++) {
response[i+1] = sd_spi_receive();
}
}
void sd_spi_command(uint8_t command, uint32_t argument, int responseLength, uint8_t *response) {
sd_spi_command_send(command, argument);
sd_spi_command_response(response, responseLength);
sd_spi_delay_clocks();
}
/******************************************************************************
*** Higher-level command interface functions ***
******************************************************************************/
int sd_read_csd(void) {
uint8_t response[1];
uint16_t crc16;
int i;
/* Send the CSD command and receive the R1 response. */
sd_spi_command_send(SD_CMD9, 0x00);
sd_spi_command_response(response, SD_CMD9_RL);
/* We have received R1, next up is the CSD and CRC
* data: */
/* Find the data block start byte */
for (i = 0; i < SD_SPI_DATA_READ_ATTEMPTS; i++) {
sd_csd[0] = sd_spi_receive();
if (sd_csd[0] == SD_SPI_DATA_BLOCK_START)
break;
}
/* Receive the 16-byte CSD */
for (i = 0; i < SD_CSD_LENGTH; i++)
sd_csd[i] = sd_spi_receive();
/* Receive the CRC16 of the CSD */
crc16 = (sd_spi_receive() << 8);
crc16 |= sd_spi_receive();
sd_spi_delay_clocks();
/* Check the R1 response for errors */
if (response[0] != 0x00) {
sd_debug_print("* SD -- Failure: CMD9. Error receiving CSD. Response: ", response, SD_CMD9_RL);
return SD_ERROR_GET_CSD;
}
/* Verify the CSD's data CRC */
if (sd_crc16_data(sd_csd, 16) != crc16) {
sd_debug_print("* SD -- Failure: CMD9. CRC16 invalid on CSD data.", 0, 0);
return SD_ERROR_GET_CSD_CRC;
}
sd_debug_print("* SD -- Success: CMD9. Retrieved card CSD.", 0, 0);
return 0;
}
int sd_read_cid(void) {
uint8_t response[1];
uint16_t crc16;
int i;
/* Send the CID command and receive the R1 response. */
sd_spi_command_send(SD_CMD10, 0x00);
sd_spi_command_response(response, SD_CMD10_RL);
/* We have received R1, next up is the CID and CRC
* data: */
/* Find the data block start byte */
for (i = 0; i < SD_SPI_DATA_READ_ATTEMPTS; i++) {
sd_cid[0] = sd_spi_receive();
if (sd_cid[0] == SD_SPI_DATA_BLOCK_START)
break;
}
/* Receive the 16-byte CID */
for (i = 0; i < SD_CID_LENGTH; i++)
sd_cid[i] = sd_spi_receive();
/* Receive the CRC16 of the CID */
crc16 = (sd_spi_receive() << 8);
crc16 |= sd_spi_receive();
sd_spi_delay_clocks();
/* Check the R1 response for errors */
if (response[0] != 0x00) {
sd_debug_print("* SD -- Failure: CMD10. Error receiving CID. Response: ", response, SD_CMD9_RL);
return SD_ERROR_GET_CID;
}
/* Verify the CID's data CRC */
if (sd_crc16_data(sd_cid, 16) != crc16) {
sd_debug_print("* SD -- Failure: CMD10. CRC16 invalid on CID data.", 0, 0);
return SD_ERROR_GET_CID_CRC;
}
sd_debug_print("* SD -- Success: CMD10. Retrieved card CID.", 0, 0);
return 0;
}
int sd_erase_blocks(uint32_t address_start, uint32_t address_end) {
uint8_t response[5];
/* Note: SD uses CMD32 and CMD33 to define the erase region,
* whereas MMC uses CMD35 and CMD36 to define the erase region. */
/* Send the erase address start command with our start address */
if (!sd_mmc) sd_spi_command(SD_CMD32, address_start, SD_CMD32_RL, response);
else sd_spi_command(SD_CMD35, address_start, SD_CMD35_RL, response);
if (response[0] != 0x00) {
if (response[0] & 0x40) {
if (!sd_mmc) sd_debug_print("* SD -- Failure: CMD32. Erase start address misaligned. Response: ", response, SD_CMD32_RL);
else sd_debug_print("* SD -- Failure: CMD35. Erase start address misaligned. Response: ", response, SD_CMD35_RL);
return SD_ERROR_START_ADDR_MISALIGNED;
}
if (response[0] & 0x80) {
if (!sd_mmc) sd_debug_print("* SD -- Failure: CMD32. Erase start address out of bounds. Response: ", response, SD_CMD32_RL);
else sd_debug_print("* SD -- Failure: CMD35. Erase start address out of bounds. Response: ", response, SD_CMD35_RL);
return SD_ERROR_START_ADDR_OUTBOUNDS;
}
if (!sd_mmc) sd_debug_print("* SD -- Failure: CMD32. Unknown error setting erase start address. Response: ", response, SD_CMD32_RL);
else sd_debug_print("* SD -- Failure: CMD35. Unknown error setting erase start address. Response: ", response, SD_CMD35_RL);
return SD_ERROR_START_ADDR_UNKNOWN;
}
if (!sd_mmc) sd_debug_print("* SD -- Success: CMD32. Erase start address set.", 0, 0);
else sd_debug_print("* SD -- Success: CMD35. Erase start address set.", 0, 0);
/* Send the erase address end command with our end address */
if (!sd_mmc)
sd_spi_command(SD_CMD33, address_end, SD_CMD33_RL, response);
else
sd_spi_command(SD_CMD36, address_end, SD_CMD36_RL, response);
if (response[0] != 0x00) {
if (response[0] & 0x40) {
if (!sd_mmc) sd_debug_print("* SD -- Failure: CMD33. Erase end address misaligned. Response: ", response, SD_CMD33_RL);
else sd_debug_print("* SD -- Failure: CMD36. Erase end address misaligned. Response: ", response, SD_CMD36_RL);
return SD_ERROR_END_ADDR_MISALIGNED;
}
if (response[0] & 0x80) {
if (!sd_mmc) sd_debug_print("* SD -- Failure: CMD33. Erase end address out of bounds. Response: ", response, SD_CMD33_RL);
else sd_debug_print("* SD -- Failure: CMD36. Erase end address out of bounds. Response: ", response, SD_CMD36_RL);
return SD_ERROR_END_ADDR_OUTBOUNDS;
}
if (!sd_mmc) sd_debug_print("* SD -- Failure: CMD33. Unknown error setting erase end address. Response: ", response, SD_CMD33_RL);
else sd_debug_print("* SD -- Failure: CMD36. Unknown error setting erase end address. Response: ", response, SD_CMD36_RL);
return SD_ERROR_END_ADDR_UNKNOWN;
}
if (!sd_mmc) sd_debug_print("* SD -- Success: CMD33. Erase end address set.", 0, 0);
else sd_debug_print("* SD -- Success: CMD36. Erase end address set.", 0, 0);
/* Send the erase command */
sd_spi_command_send(SD_CMD38, 0x00);
sd_spi_command_response(response, SD_CMD38_RL);
if (response[0] != 0x00) {
sd_debug_print("* SD -- Failure: CMD38. Error erasing selected blocks. Response: ", response, SD_CMD38_RL);
return SD_ERROR_ERASE;
}
/* Wait for the busy signal to clear */
while (1) {
if (sd_spi_receive() != SD_SPI_BUSY)
break;
}
sd_spi_delay_clocks();
sd_debug_print("* SD -- Success: CMD38. Selected blocks erased.", 0, 0);
return 0;
}
int sd_stop_block_transmission(void) {
uint8_t data;
sd_spi_command_send(SD_CMD12, 0x00);
/* Wait for the command to take into effect */
sd_spi_delay_clocks();
/* Check our R1 response
if (response[0] != 0x00) {
sd_debug_print("* SD -- Failure: CMD12. Error stopping multiple block transmission. Response: ", response, SD_CMD12_RL);
return SD_ERROR_STOP_MULTIPLE_BLOCKS;
} */
/* Now wait for the 0x00 busy signal to clear */
for (;;) {
data = sd_spi_receive();
if (data == 0xFF)
break;
}
sd_debug_print("* SD -- Success: CMD12. Multiple block transmission stopped.", 0, 0);
return 0;
}
int sd_write_block(uint32_t address, const uint8_t *data) {
uint8_t response[5];
uint16_t crc16;
int i, retVal;
/* Error out if the address is not aligned by block length */
if ((address % sd_block_len) != 0) {
sd_debug_print("* SD -- Failure: CMD24. Address not aligned by block length.", 0, 0);
return SD_ERROR_WRITE_ADDR_MISALIGNED;
}
/* If this is a high capacity card, the data is addressed in
* blocks (512 bytes). Adjust the address accordingly. */
if (sd_high_capacity) {
address /= sd_block_len;
}
/* Send the write single block command and receive the R1 response */
sd_spi_command(SD_CMD24, address, SD_CMD24_RL, response);
if (response[0] != 0x00) {
if (response[0] & 0x40) {
sd_debug_print("* SD -- Failure: CMD24. Write data address misaligned. Response: ", response, SD_CMD24_RL);
return SD_ERROR_WRITE_ADDR_MISALIGNED;
}
if (response[0] & 0x80) {
sd_debug_print("* SD -- Failure: CMD24. Write data address out of bounds. Response: ", response, SD_CMD24_RL);
return SD_ERROR_WRITE_ADDR_OUTBOUNDS;
}
sd_debug_print("* SD -- Failure: CMD24. Unknown error with single block write. Response: ", response, SD_CMD24_RL);
return SD_ERROR_WRITE_UNKNOWN;
}
/* CRC16 the data we're sending */
crc16 = sd_crc16_data(data, sd_block_len);
/* Send the data block start token and the data block */
sd_spi_send(SD_SPI_DATA_BLOCK_START);
/* Send every byte of the data block */
for (i = 0; i < sd_block_len; i++)
sd_spi_send(data[i]);
/* Send the CRC16 of the data block */
sd_spi_send((uint8_t)(crc16 >> 8));
sd_spi_send((uint8_t)(crc16 & 0xFF));
/* Wait for the data response token */
for (i = 0; i < SD_SPI_DATA_READ_ATTEMPTS; i++) {
response[0] = sd_spi_receive();
if (response[0] != 0xFF)
break;
}
/* Check the response token */
switch ((response[0] & 0x0E) >> 1) {
case SD_SPI_WRITE_ACCEPTED:
retVal = 0;
break;
case SD_SPI_WRITE_ERROR_CRC:
sd_debug_print("* SD -- Failure: CMD24. CRC error occured during single block write. Data response token: ", response, 1);
retVal = SD_ERROR_WRITE_BLOCK_CRC;
break;
case SD_SPI_WRITE_ERROR_WRITE:
sd_debug_print("* SD -- Failure: CMD24. Write error occured during single block write. Data response token: ", response, 1);
retVal = SD_ERROR_WRITE_BLOCK;
break;
default:
sd_debug_print("* SD -- Failure: CMD24. Unknown error occured during single block write. Data response token: ", response, 1);
retVal = SD_ERROR_WRITE_UNKNOWN;
break;
}
/* Wait for the busy signal to clear */
while (1) {
if (sd_spi_receive() != SD_SPI_BUSY)
break;
}
sd_spi_delay_clocks();
if (retVal == 0)
sd_debug_print("* SD -- Success: CMD24. Single data block written.", 0, 0);
return retVal;
}
int sd_write_blocks(uint32_t address, const uint8_t *data, int dataLen) {
uint8_t response[5];
uint16_t crc16;
int i, dataIndex, retVal;
/* Error out if the address is not aligned by block length */
if ((address % sd_block_len) != 0) {
sd_debug_print("* SD -- Failure: CMD25. Address not aligned by block length.", 0, 0);
return SD_ERROR_WRITE_ADDR_MISALIGNED;
}
/* Make sure the data length is in multiples of the block length. */
if ((dataLen % sd_block_len) != 0) {
sd_debug_print("* SD -- Failure: CMD25. Data length not in block multiples.", 0, 0);
return SD_ERROR_WRITE_DATALEN_MULTIPLE;
}
/* If this is a high capacity card, the data is addressed in
* blocks (512 bytes). Adjust the address accordingly. */
if (sd_high_capacity) {
address /= sd_block_len;
}
/* Send the write multiple blocks command and receive the R1 response */
sd_spi_command(SD_CMD25, address, SD_CMD25_RL, response);
if (response[0] != 0x00) {
if (response[0] & 0x40) {
sd_debug_print("* SD -- Failure: CMD25. Write data address misaligned. Response: ", response, SD_CMD25_RL);
return SD_ERROR_WRITE_ADDR_MISALIGNED;
}
if (response[0] & 0x80) {
sd_debug_print("* SD -- Failure: CMD25. Write data address out of bounds. Response: ", response, SD_CMD25_RL);
return SD_ERROR_WRITE_ADDR_OUTBOUNDS;
}
sd_debug_print("* SD -- Failure: CMD25. Unknown error with single block write. Response: ", response, SD_CMD25_RL);
return SD_ERROR_WRITE_UNKNOWN;
}
for (retVal = 0, dataIndex = 0; dataIndex < dataLen; ) {
/* CRC16 the data we're sending */
crc16 = sd_crc16_data(data+dataIndex, sd_block_len);
/* Send the data block start token and the data block */
sd_spi_send(SD_SPI_MULTIPLE_DATA_BLOCK_START);
/* Send every byte of the data block */
for (i = 0; i < sd_block_len; i++)
sd_spi_send(data[dataIndex++]);
/* Send the CRC16 of the data block */
sd_spi_send((uint8_t)(crc16 >> 8));
sd_spi_send((uint8_t)(crc16 & 0xFF));
/* Wait for the data response token */
for (i = 0; i < SD_SPI_DATA_READ_ATTEMPTS; i++) {
response[0] = sd_spi_receive();
if (response[0] != 0xFF)
break;
}
/* Check the response token */
switch ((response[0] & 0x0E) >> 1) {
case SD_SPI_WRITE_ACCEPTED:
retVal = 0;
break;
case SD_SPI_WRITE_ERROR_CRC:
sd_debug_print("* SD -- Failure: CMD25. CRC error occured during multiple block write. Data response token: ", response, 1);
retVal = SD_ERROR_WRITE_BLOCK_CRC;
break;
case SD_SPI_WRITE_ERROR_WRITE:
sd_debug_print("* SD -- Failure: CMD25. Write error occured during multiple block write. Data response token: ", response, 1);
retVal = SD_ERROR_WRITE_BLOCK;
break;
default:
sd_debug_print("* SD -- Failure: CMD25. Unknown error occured during multiple block write. Data response token: ", response, 1);
retVal = SD_ERROR_WRITE_UNKNOWN;
break;
}
/* Wait for the busy signal to clear */
while (1) {
if (sd_spi_receive() != SD_SPI_BUSY)
break;
}
sd_spi_delay_clocks();
/* Check if we had any errors with this block */
if (retVal < 0)
break;
}
/* Send Stop Transmission token */
sd_spi_send(SD_SPI_MULTIPLE_DATA_BLOCK_END);
sd_spi_delay_clocks();
/* Wait for the busy signal to clear */
while (1) {
if (sd_spi_receive() != SD_SPI_BUSY)
break;
}
sd_debug_print("* SD -- Success: CMD25. Multiple blocks written.", 0, 0);
sd_spi_delay_clocks();
return retVal;
}
int sd_read_blocks(uint32_t address, uint8_t *data, int dataLen) {
uint8_t response[5];
uint16_t crc16;
int i, dataIndex, retVal;
/* Align the address with the nearest block length down */
//address -= (address % sd_block_len);
/* Error out if the address is not aligned by block length */
if ((address % sd_block_len) != 0) {
sd_debug_print("* SD -- Failure: CMD18. Address not aligned by block length.", 0, 0);
return SD_ERROR_READ_ADDR_MISALIGNED;
}
/* Make sure the data length is in multiples of the block length. */
if ((dataLen % sd_block_len) != 0) {
sd_debug_print("* SD -- Failure: CMD18. Data length not in block multiples.", 0, 0);
return SD_ERROR_READ_DATALEN_MULTIPLE;
}
/* If this is a high capacity card, the data is addressed in
* blocks (512 bytes). Adjust the address accordingly. */
if (sd_high_capacity) {
address /= sd_block_len;
}
/* Send the read multiple blocks command and receive the R1 response */
sd_spi_command_send(SD_CMD18, address);
sd_spi_command_response(response, SD_CMD18_RL);
if (response[0] != 0x00) {
if (response[0] & 0x40) {
sd_debug_print("* SD -- Failure: CMD18. Read data address misaligned. Response: ", response, SD_CMD18_RL);
return SD_ERROR_READ_ADDR_MISALIGNED;
}
if (response[0] & 0x80) {
sd_debug_print("* SD -- Failure: CMD18. Read data address out of bounds. Response: ", response, SD_CMD18_RL);
return SD_ERROR_READ_ADDR_OUTBOUNDS;
}
sd_debug_print("* SD -- Failure: CMD18. Unknown error with multiple block read. Response: ", response, SD_CMD18_RL);
return SD_ERROR_READ_UNKNOWN;
}
for (retVal = 0, dataIndex = 0; dataIndex < dataLen; ) {
/* Find the data block start byte */
for (i = 0; i < SD_SPI_DATA_READ_ATTEMPTS; i++) {
data[dataIndex] = sd_spi_receive();
/* Check if we get the start of a block or a data read error */
if (data[dataIndex] == SD_SPI_DATA_BLOCK_START ||
(data[dataIndex] & SD_SPI_DATA_ERROR_TOKEN_MASK) == 0x00)
break;
}
if ((data[dataIndex] & SD_SPI_DATA_ERROR_TOKEN_MASK) == 0x00) {
if (sd_mmc && (data[0] & 0x10)) {
sd_debug_print("* SD -- Failure: CMD18. Read data address misaligned. Error token: ", data, 1);
return SD_ERROR_READ_ADDR_MISALIGNED;
}
if (data[dataIndex] & 0x08) {
sd_debug_print("* SD -- Failure: CMD18. Read data address out of range. Error token: ", data, 1);
retVal = SD_ERROR_READ_ADDR_OUTBOUNDS;
break;
}
if (data[dataIndex] & 0x04) {
sd_debug_print("* SD -- Failure: CMD18. Card ECC failure during read. Error token: ", data, 1);
retVal = SD_ERROR_READ_CARD_ECC;
break;
}
if (data[dataIndex] & 0x02) {
sd_debug_print("* SD -- Failure: CMD18. Card CC failure during read. Error token: ", data, 1);
retVal = SD_ERROR_READ_CARD_CC;
break;
}
sd_debug_print("* SD -- Failure: CMD18. Unknown error with multiple block read. Error token: ", data, 1);
return SD_ERROR_READ_UNKNOWN;
}
/* Read a block length of data */
for (i = 0; i < sd_block_len; i++, dataIndex++) {
data[dataIndex] = sd_spi_receive();
}
/* Read in the CRC16 */
crc16 = (sd_spi_receive() << 8);
crc16 |= sd_spi_receive();
/* Verify the data block's CRC */
if (sd_crc16_data(data+(dataIndex-sd_block_len), sd_block_len) != crc16) {
sd_debug_print("* SD -- Failure: CMD18. CRC16 invalid on read data block.", 0, 0);
retVal = SD_ERROR_READ_MULTIPLE_CRC;
break;
}
sd_debug_print("block ok", 0, 0);
}
/* Check if we had any block read errors */
if (retVal < 0) {
sd_stop_block_transmission();
sd_spi_delay_clocks();
return retVal;
}
/* Stop any further block transmissions */
retVal = sd_stop_block_transmission();
sd_spi_delay_clocks();
/* Check if stopping block transmission went through smoothly */
if (retVal < 0)
return retVal;
sd_debug_print("* SD -- Success: CMD18. Retrieved multiple data blocks.", 0, 0);
return 0;
}
int sd_read_block(uint32_t address, uint8_t *data) {
uint8_t response[5];
uint16_t crc16;
int i;
/* Align the address with the nearest block length down */
//address -= (address % sd_block_len);
/* Error out if the address is not aligned by block length */
if ((address % sd_block_len) != 0) {
sd_debug_print("* SD -- Failure: CMD17. Address not aligned by block length.", 0, 0);
return SD_ERROR_READ_ADDR_MISALIGNED;
}
/* If this is a high capacity card, the data is addressed in
* blocks (512 bytes). Adjust the address accordingly. */
if (sd_high_capacity) {
address /= sd_block_len;
}
/* Make sure we can hold up to one block
if (dataLen < sd_block_len) {
sd_debug_print("* SD -- Failure: CMD17. Insufficient data length to support block size.", 0, 0);
return SD_ERROR_READ_DATALEN;
} */
/* Send the read single block command and receive the R1 response */
sd_spi_command_send(SD_CMD17, address);
sd_spi_command_response(response, SD_CMD17_RL);
if (response[0] != 0x00) {
if (response[0] & 0x40) {
sd_debug_print("* SD -- Failure: CMD17. Read data address misaligned. Response: ", response, SD_CMD17_RL);
return SD_ERROR_READ_ADDR_MISALIGNED;
}
if (response[0] & 0x80) {
sd_debug_print("* SD -- Failure: CMD17. Read data address out of bounds. Response: ", response, SD_CMD17_RL);
return SD_ERROR_READ_ADDR_OUTBOUNDS;
}
sd_debug_print("* SD -- Failure: CMD17. Unknown error with single block read. Response: ", response, SD_CMD17_RL);
return SD_ERROR_READ_UNKNOWN;
}
/* Find the data block start byte */
for (i = 0; i < SD_SPI_DATA_READ_ATTEMPTS; i++) {
data[0] = sd_spi_receive();
/* Check if we get the start of a block or a data read error */
if (data[0] == SD_SPI_DATA_BLOCK_START ||
(data[0] & SD_SPI_DATA_ERROR_TOKEN_MASK) == 0x00)
break;
}
if ((data[0] & SD_SPI_DATA_ERROR_TOKEN_MASK) == 0x00) {
if (sd_mmc && (data[0] & 0x10)) {
sd_debug_print("* SD -- Failure: CMD17. Read data address misaligned. Error token: ", data, 1);
return SD_ERROR_READ_ADDR_MISALIGNED;
}
if (data[0] & 0x08) {
sd_debug_print("* SD -- Failure: CMD17. Read data address out of range. Error token: ", data, 1);
return SD_ERROR_READ_ADDR_OUTBOUNDS;
}
if (data[0] & 0x04) {
sd_debug_print("* SD -- Failure: CMD17. Card ECC failure during read. Error token: ", data, 1);
return SD_ERROR_READ_CARD_ECC;
}
if (data[0] & 0x02) {
sd_debug_print("* SD -- Failure: CMD17. Card CC failure during read. Error token: ", data, 1);
return SD_ERROR_READ_CARD_CC;
}
sd_debug_print("* SD -- Failure: CMD17. Unknown error with single block read. Error token: ", data, 1);
return SD_ERROR_READ_UNKNOWN;
}
/* Read a block length of data */
for (i = 0; i < sd_block_len; i++) {
data[i] = sd_spi_receive();
}