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
79
80
83
84
87
88
105
106
107
110
111
112
113
114
115
116
117
118
119
120
121
124
125
126
127
128
129
130
131
132
133
138
139
140
141
142
143
144
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
204
205
206
207
208
209
210
213
214
224
225
226
229
230
231
235
236
237
240
241
242
243
244
245
246
247
248
249
252
255
256
257
258
261
262
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
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
335
336
337
338
342
343
344
348
349
350
351
352
353
354
355
356
357
358
359
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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
420
421
422
423
427
428
429
433
434
435
436
437
438
439
440
442
443
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
517
518
519
520
521
522
523
524
525
526
531
532
533
534
535
536
537
538
539
540
541
545
546
547
548
549
556
557
558
559
560
561
562
563
564
565
566
567
568
569
574
575
576
577
578
579
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
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
789
790
791
792
793
794
795
796
797
798
799
800
801
805
806
813
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
853
856
858
859
860
861
862
863
864
865
870
871
872
873
874
876
886
887
888
889
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
941
942
943
944
945
946
947
948
949
950
951
952
953
957
958
959
963
964
965
966
967
972
973
974
975
976
977
978
983
984
985
986
987
988
993
994
995
996
997
998
999
1000
1004
1005
1010
1011
1012
1013
1014
1015
1018
1019
1022
1023
1026
1027
1030
/* ... */
/* ... */
#include "stm32_adafruit_sd.h"
#include "stdlib.h"
#include "string.h"
#include "stdio.h"
/* ... */
/* ... */
/* ... */
Includes
/* ... */
typedef struct {
uint8_t r1;
uint8_t r2;
uint8_t r3;
uint8_t r4;
uint8_t r5;
...} SD_CmdAnswer_typedef;
/* ... */
Private typedef
/* ... */
#define SD_DUMMY_BYTE 0xFF
#define SD_MAX_FRAME_LENGTH 17
#define SD_CMD_LENGTH 6
#define SD_MAX_TRY 100
#define SD_CSD_STRUCT_V1 0x2
#define SD_CSD_STRUCT_V2 0x1
6 defines
/* ... */
typedef enum {
SD_ANSWER_R1_EXPECTED,
SD_ANSWER_R1B_EXPECTED,
SD_ANSWER_R2_EXPECTED,
SD_ANSWER_R3_EXPECTED,
SD_ANSWER_R4R5_EXPECTED,
SD_ANSWER_R7_EXPECTED,
...}SD_Answer_type;
/* ... */
#define SD_TOKEN_START_DATA_SINGLE_BLOCK_READ 0xFE
#define SD_TOKEN_START_DATA_MULTIPLE_BLOCK_READ 0xFE
#define SD_TOKEN_START_DATA_SINGLE_BLOCK_WRITE 0xFE
#define SD_TOKEN_START_DATA_MULTIPLE_BLOCK_WRITE 0xFD
#define SD_TOKEN_STOP_DATA_MULTIPLE_BLOCK_WRITE 0xFD
/* ... */
#define SD_CMD_GO_IDLE_STATE 0
#define SD_CMD_SEND_OP_COND 1
#define SD_CMD_SEND_IF_COND 8
#define SD_CMD_SEND_CSD 9
#define SD_CMD_SEND_CID 10
#define SD_CMD_STOP_TRANSMISSION 12
#define SD_CMD_SEND_STATUS 13
#define SD_CMD_SET_BLOCKLEN 16
#define SD_CMD_READ_SINGLE_BLOCK 17
#define SD_CMD_READ_MULT_BLOCK 18
#define SD_CMD_SET_BLOCK_COUNT 23
#define SD_CMD_WRITE_SINGLE_BLOCK 24
#define SD_CMD_WRITE_MULT_BLOCK 25
#define SD_CMD_PROG_CSD 27
#define SD_CMD_SET_WRITE_PROT 28
#define SD_CMD_CLR_WRITE_PROT 29
#define SD_CMD_SEND_WRITE_PROT 30
#define SD_CMD_SD_ERASE_GRP_START 32
#define SD_CMD_SD_ERASE_GRP_END 33
#define SD_CMD_UNTAG_SECTOR 34
#define SD_CMD_ERASE_GRP_START 35
#define SD_CMD_ERASE_GRP_END 36
#define SD_CMD_UNTAG_ERASE_GROUP 37
#define SD_CMD_ERASE 38
#define SD_CMD_SD_APP_OP_COND 41
#define SD_CMD_APP_CMD 55
#define SD_CMD_READ_OCR 58
32 defines
/* ... */
typedef enum
{
SD_R1_NO_ERROR = (0x00),
SD_R1_IN_IDLE_STATE = (0x01),
SD_R1_ERASE_RESET = (0x02),
SD_R1_ILLEGAL_COMMAND = (0x04),
SD_R1_COM_CRC_ERROR = (0x08),
SD_R1_ERASE_SEQUENCE_ERROR= (0x10),
SD_R1_ADDRESS_ERROR = (0x20),
SD_R1_PARAMETER_ERROR = (0x40),
SD_R2_NO_ERROR = 0x00,
SD_R2_CARD_LOCKED = 0x01,
SD_R2_LOCKUNLOCK_ERROR = 0x02,
SD_R2_ERROR = 0x04,
SD_R2_CC_ERROR = 0x08,
SD_R2_CARD_ECC_FAILED = 0x10,
SD_R2_WP_VIOLATION = 0x20,
SD_R2_ERASE_PARAM = 0x40,
SD_R2_OUTOFRANGE = 0x80,
/* ... */
SD_DATA_OK = (0x05),
SD_DATA_CRC_ERROR = (0x0B),
SD_DATA_WRITE_ERROR = (0x0D),
SD_DATA_OTHER_ERROR = (0xFF)
...} SD_Error;
/* ... */
Private define
/* ... */
/* ... */
Private macro
/* ... */
__IO uint8_t SdStatus = SD_NOT_PRESENT;
/* ... */
uint16_t flag_SDHC = 0;
/* ... */
Private variables
static uint8_t SD_GetCIDRegister(SD_CID* Cid);
static uint8_t SD_GetCSDRegister(SD_CSD* Csd);
static uint8_t SD_GetDataResponse(void);
static uint8_t SD_GoIdleState(void);
static SD_CmdAnswer_typedef SD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Answer);
static uint8_t SD_WaitData(uint8_t data);
static uint8_t SD_ReadData(void);
/* ... */
/* ... */
Private function prototypes
/* ... */
/* ... */
uint8_t BSP_SD_Init(void)
{
SD_IO_Init();
SdStatus = SD_PRESENT;
return SD_GoIdleState();
}{ ... }
/* ... */
uint8_t BSP_SD_GetCardInfo(SD_CardInfo *pCardInfo)
{
uint8_t status;
status = SD_GetCSDRegister(&(pCardInfo->Csd));
status|= SD_GetCIDRegister(&(pCardInfo->Cid));
if(flag_SDHC == 1 )
{
pCardInfo->LogBlockSize = 512;
pCardInfo->CardBlockSize = 512;
pCardInfo->CardCapacity = (pCardInfo->Csd.version.v2.DeviceSize + 1) * 1024 * pCardInfo->LogBlockSize;
pCardInfo->LogBlockNbr = (pCardInfo->CardCapacity) / (pCardInfo->LogBlockSize);
}if (flag_SDHC == 1) { ... }
else
{
pCardInfo->CardCapacity = (pCardInfo->Csd.version.v1.DeviceSize + 1) ;
pCardInfo->CardCapacity *= (1 << (pCardInfo->Csd.version.v1.DeviceSizeMul + 2));
pCardInfo->LogBlockSize = 512;
pCardInfo->CardBlockSize = 1 << (pCardInfo->Csd.RdBlockLen);
pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;
pCardInfo->LogBlockNbr = (pCardInfo->CardCapacity) / (pCardInfo->LogBlockSize);
}else { ... }
return status;
}{ ... }
/* ... */
uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout)
{
uint32_t offset = 0;
uint32_t addr;
uint8_t retr = BSP_SD_ERROR;
uint8_t *ptr = NULL;
SD_CmdAnswer_typedef response;
uint16_t BlockSize = 512;
/* ... */
response = SD_SendCmd(SD_CMD_SET_BLOCKLEN, BlockSize, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if ( response.r1 != SD_R1_NO_ERROR)
{
goto error;
}if (response.r1 != SD_R1_NO_ERROR) { ... }
ptr = malloc(sizeof(uint8_t)*BlockSize);
if( ptr == NULL )
{
goto error;
}if (ptr == NULL) { ... }
memset(ptr, SD_DUMMY_BYTE, sizeof(uint8_t)*BlockSize);
addr = (ReadAddr * ((flag_SDHC == 1) ? 1 : BlockSize));
while (NumOfBlocks--)
{
response = SD_SendCmd(SD_CMD_READ_SINGLE_BLOCK, addr, 0xFF, SD_ANSWER_R1_EXPECTED);
if ( response.r1 != SD_R1_NO_ERROR)
{
goto error;
}if (response.r1 != SD_R1_NO_ERROR) { ... }
if (SD_WaitData(SD_TOKEN_START_DATA_SINGLE_BLOCK_READ) == BSP_SD_OK)
{
SD_IO_WriteReadData(ptr, (uint8_t*)pData + offset, BlockSize);
offset += BlockSize;
addr = ((flag_SDHC == 1) ? (addr + 1) : (addr + BlockSize));
SD_IO_WriteByte(SD_DUMMY_BYTE);
SD_IO_WriteByte(SD_DUMMY_BYTE);
}if (SD_WaitData(SD_TOKEN_START_DATA_SINGLE_BLOCK_READ) == BSP_SD_OK) { ... }
else
{
goto error;
}else { ... }
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
}while (NumOfBlocks--) { ... }
retr = BSP_SD_OK;
error :
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if(ptr != NULL) free(ptr);
return retr;
}{ ... }
/* ... */
uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout)
{
uint32_t offset = 0;
uint32_t addr;
uint8_t retr = BSP_SD_ERROR;
uint8_t *ptr = NULL;
SD_CmdAnswer_typedef response;
uint16_t BlockSize = 512;
/* ... */
response = SD_SendCmd(SD_CMD_SET_BLOCKLEN, BlockSize, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if ( response.r1 != SD_R1_NO_ERROR)
{
goto error;
}if (response.r1 != SD_R1_NO_ERROR) { ... }
ptr = malloc(sizeof(uint8_t)*BlockSize);
if (ptr == NULL)
{
goto error;
}if (ptr == NULL) { ... }
addr = (WriteAddr * ((flag_SDHC == 1) ? 1 : BlockSize));
while (NumOfBlocks--)
{
/* ... */
response = SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, addr, 0xFF, SD_ANSWER_R1_EXPECTED);
if (response.r1 != SD_R1_NO_ERROR)
{
goto error;
}if (response.r1 != SD_R1_NO_ERROR) { ... }
SD_IO_WriteByte(SD_DUMMY_BYTE);
SD_IO_WriteByte(SD_DUMMY_BYTE);
SD_IO_WriteByte(SD_TOKEN_START_DATA_SINGLE_BLOCK_WRITE);
SD_IO_WriteReadData((uint8_t*)pData + offset, ptr, BlockSize);
offset += BlockSize;
addr = ((flag_SDHC == 1) ? (addr + 1) : (addr + BlockSize));
SD_IO_WriteByte(SD_DUMMY_BYTE);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if (SD_GetDataResponse() != SD_DATA_OK)
{
goto error;
}if (SD_GetDataResponse() != SD_DATA_OK) { ... }
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
}while (NumOfBlocks--) { ... }
retr = BSP_SD_OK;
error :
if(ptr != NULL) free(ptr);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
return retr;
}{ ... }
/* ... */
uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr)
{
uint8_t retr = BSP_SD_ERROR;
SD_CmdAnswer_typedef response;
uint16_t BlockSize = 512;
response = SD_SendCmd(SD_CMD_SD_ERASE_GRP_START, (StartAddr) * (flag_SDHC == 1 ? 1 : BlockSize), 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE); if (response.r1 == SD_R1_NO_ERROR)
{
response = SD_SendCmd(SD_CMD_SD_ERASE_GRP_END, (EndAddr*512) * (flag_SDHC == 1 ? 1 : BlockSize), 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if (response.r1 == SD_R1_NO_ERROR)
{
response = SD_SendCmd(SD_CMD_ERASE, 0, 0xFF, SD_ANSWER_R1B_EXPECTED);
if (response.r1 == SD_R1_NO_ERROR)
{
retr = BSP_SD_OK;
}if (response.r1 == SD_R1_NO_ERROR) { ... }
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
}if (response.r1 == SD_R1_NO_ERROR) { ... }
}if (response.r1 == SD_R1_NO_ERROR) { ... }
return retr;
}{ ... }
/* ... */
uint8_t BSP_SD_GetCardState(void)
{
SD_CmdAnswer_typedef retr;
retr = SD_SendCmd(SD_CMD_SEND_STATUS, 0, 0xFF, SD_ANSWER_R2_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if(( retr.r1 == SD_R1_NO_ERROR) && ( retr.r2 == SD_R2_NO_ERROR))
{
return BSP_SD_OK;
}if (( retr.r1 == SD_R1_NO_ERROR) && ( retr.r2 == SD_R2_NO_ERROR)) { ... }
return BSP_SD_ERROR;
}{ ... }
/* ... */
uint8_t SD_GetCSDRegister(SD_CSD* Csd)
{
uint16_t counter = 0;
uint8_t CSD_Tab[16];
uint8_t retr = BSP_SD_ERROR;
SD_CmdAnswer_typedef response;
response = SD_SendCmd(SD_CMD_SEND_CSD, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
if(response.r1 == SD_R1_NO_ERROR)
{
if (SD_WaitData(SD_TOKEN_START_DATA_SINGLE_BLOCK_READ) == BSP_SD_OK)
{
for (counter = 0; counter < 16; counter++)
{
CSD_Tab[counter] = SD_IO_WriteByte(SD_DUMMY_BYTE);
}for (counter = 0; counter < 16; counter++) { ... }
SD_IO_WriteByte(SD_DUMMY_BYTE);
SD_IO_WriteByte(SD_DUMMY_BYTE);
/* ... */
Csd->CSDStruct = (CSD_Tab[0] & 0xC0) >> 6;
Csd->Reserved1 = CSD_Tab[0] & 0x3F;
Csd->TAAC = CSD_Tab[1];
Csd->NSAC = CSD_Tab[2];
Csd->MaxBusClkFrec = CSD_Tab[3];
Csd->CardComdClasses = (CSD_Tab[4] << 4) | ((CSD_Tab[5] & 0xF0) >> 4);
Csd->RdBlockLen = CSD_Tab[5] & 0x0F;
Csd->PartBlockRead = (CSD_Tab[6] & 0x80) >> 7;
Csd->WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6;
Csd->RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5;
Csd->DSRImpl = (CSD_Tab[6] & 0x10) >> 4;
/* ... */
if(flag_SDHC == 0)
{
Csd->version.v1.Reserved1 = ((CSD_Tab[6] & 0x0C) >> 2);
Csd->version.v1.DeviceSize = ((CSD_Tab[6] & 0x03) << 10)
| (CSD_Tab[7] << 2)
| ((CSD_Tab[8] & 0xC0) >> 6);
Csd->version.v1.MaxRdCurrentVDDMin = (CSD_Tab[8] & 0x38) >> 3;
Csd->version.v1.MaxRdCurrentVDDMax = (CSD_Tab[8] & 0x07);
Csd->version.v1.MaxWrCurrentVDDMin = (CSD_Tab[9] & 0xE0) >> 5;
Csd->version.v1.MaxWrCurrentVDDMax = (CSD_Tab[9] & 0x1C) >> 2;
Csd->version.v1.DeviceSizeMul = ((CSD_Tab[9] & 0x03) << 1)
|((CSD_Tab[10] & 0x80) >> 7);
}if (flag_SDHC == 0) { ... }
else
{
Csd->version.v2.Reserved1 = ((CSD_Tab[6] & 0x0F) << 2) | ((CSD_Tab[7] & 0xC0) >> 6);
Csd->version.v2.DeviceSize= ((CSD_Tab[7] & 0x3F) << 16) | (CSD_Tab[8] << 8) | CSD_Tab[9];
Csd->version.v2.Reserved2 = ((CSD_Tab[10] & 0x80) >> 8);
}else { ... }
Csd->EraseSingleBlockEnable = (CSD_Tab[10] & 0x40) >> 6;
Csd->EraseSectorSize = ((CSD_Tab[10] & 0x3F) << 1)
|((CSD_Tab[11] & 0x80) >> 7);
Csd->WrProtectGrSize = (CSD_Tab[11] & 0x7F);
Csd->WrProtectGrEnable = (CSD_Tab[12] & 0x80) >> 7;
Csd->Reserved2 = (CSD_Tab[12] & 0x60) >> 5;
Csd->WrSpeedFact = (CSD_Tab[12] & 0x1C) >> 2;
Csd->MaxWrBlockLen = ((CSD_Tab[12] & 0x03) << 2)
|((CSD_Tab[13] & 0xC0) >> 6);
Csd->WriteBlockPartial = (CSD_Tab[13] & 0x20) >> 5;
Csd->Reserved3 = (CSD_Tab[13] & 0x1F);
Csd->FileFormatGrouop = (CSD_Tab[14] & 0x80) >> 7;
Csd->CopyFlag = (CSD_Tab[14] & 0x40) >> 6;
Csd->PermWrProtect = (CSD_Tab[14] & 0x20) >> 5;
Csd->TempWrProtect = (CSD_Tab[14] & 0x10) >> 4;
Csd->FileFormat = (CSD_Tab[14] & 0x0C) >> 2;
Csd->Reserved4 = (CSD_Tab[14] & 0x03);
Csd->crc = (CSD_Tab[15] & 0xFE) >> 1;
Csd->Reserved5 = (CSD_Tab[15] & 0x01);
retr = BSP_SD_OK;
}if (SD_WaitData(SD_TOKEN_START_DATA_SINGLE_BLOCK_READ) == BSP_SD_OK) { ... }
}if (response.r1 == SD_R1_NO_ERROR) { ... }
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
return retr;
}{ ... }
/* ... */
uint8_t SD_GetCIDRegister(SD_CID* Cid)
{
uint32_t counter = 0;
uint8_t retr = BSP_SD_ERROR;
uint8_t CID_Tab[16];
SD_CmdAnswer_typedef response;
response = SD_SendCmd(SD_CMD_SEND_CID, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
if(response.r1 == SD_R1_NO_ERROR)
{
if(SD_WaitData(SD_TOKEN_START_DATA_SINGLE_BLOCK_READ) == BSP_SD_OK)
{
for (counter = 0; counter < 16; counter++)
{
CID_Tab[counter] = SD_IO_WriteByte(SD_DUMMY_BYTE);
}for (counter = 0; counter < 16; counter++) { ... }
SD_IO_WriteByte(SD_DUMMY_BYTE);
SD_IO_WriteByte(SD_DUMMY_BYTE);
Cid->ManufacturerID = CID_Tab[0];
Cid->OEM_AppliID = CID_Tab[1] << 8;
Cid->OEM_AppliID |= CID_Tab[2];
Cid->ProdName1 = CID_Tab[3] << 24;
Cid->ProdName1 |= CID_Tab[4] << 16;
Cid->ProdName1 |= CID_Tab[5] << 8;
Cid->ProdName1 |= CID_Tab[6];
Cid->ProdName2 = CID_Tab[7];
Cid->ProdRev = CID_Tab[8];
Cid->ProdSN = CID_Tab[9] << 24;
Cid->ProdSN |= CID_Tab[10] << 16;
Cid->ProdSN |= CID_Tab[11] << 8;
Cid->ProdSN |= CID_Tab[12];
Cid->Reserved1 |= (CID_Tab[13] & 0xF0) >> 4;
Cid->ManufactDate = (CID_Tab[13] & 0x0F) << 8;
Cid->ManufactDate |= CID_Tab[14];
Cid->CID_CRC = (CID_Tab[15] & 0xFE) >> 1;
Cid->Reserved2 = 1;
retr = BSP_SD_OK;
}if (SD_WaitData(SD_TOKEN_START_DATA_SINGLE_BLOCK_READ) == BSP_SD_OK) { ... }
}if (response.r1 == SD_R1_NO_ERROR) { ... }
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
return retr;
}{ ... }
/* ... */
SD_CmdAnswer_typedef SD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Answer)
{
uint8_t frame[SD_CMD_LENGTH], frameout[SD_CMD_LENGTH];
SD_CmdAnswer_typedef retr = {0xFF, 0xFF , 0xFF, 0xFF, 0xFF};
frame[0] = (Cmd | 0x40);
frame[1] = (uint8_t)(Arg >> 24);
frame[2] = (uint8_t)(Arg >> 16);
frame[3] = (uint8_t)(Arg >> 8);
frame[4] = (uint8_t)(Arg);
frame[5] = (Crc | 0x01);
SD_IO_CSState(0);
SD_IO_WriteReadData(frame, frameout, SD_CMD_LENGTH);
switch(Answer)
{
case SD_ANSWER_R1_EXPECTED :
retr.r1 = SD_ReadData();
break;case SD_ANSWER_R1_EXPECTED :
case SD_ANSWER_R1B_EXPECTED :
retr.r1 = SD_ReadData();
retr.r2 = SD_IO_WriteByte(SD_DUMMY_BYTE);
SD_IO_CSState(1);
HAL_Delay(1);
SD_IO_CSState(0);
while (SD_IO_WriteByte(SD_DUMMY_BYTE) != 0xFF);
break;case SD_ANSWER_R1B_EXPECTED :
case SD_ANSWER_R2_EXPECTED :
retr.r1 = SD_ReadData();
retr.r2 = SD_IO_WriteByte(SD_DUMMY_BYTE);
break;case SD_ANSWER_R2_EXPECTED :
case SD_ANSWER_R3_EXPECTED :
case SD_ANSWER_R7_EXPECTED :
retr.r1 = SD_ReadData();
retr.r2 = SD_IO_WriteByte(SD_DUMMY_BYTE);
retr.r3 = SD_IO_WriteByte(SD_DUMMY_BYTE);
retr.r4 = SD_IO_WriteByte(SD_DUMMY_BYTE);
retr.r5 = SD_IO_WriteByte(SD_DUMMY_BYTE);
break;case SD_ANSWER_R7_EXPECTED :
default :
break;default
}switch (Answer) { ... }
return retr;
}{ ... }
/* ... */
uint8_t SD_GetDataResponse(void)
{
uint8_t dataresponse;
uint8_t rvalue = SD_DATA_OTHER_ERROR;
dataresponse = SD_IO_WriteByte(SD_DUMMY_BYTE);
SD_IO_WriteByte(SD_DUMMY_BYTE);
switch (dataresponse & 0x1F)
{
case SD_DATA_OK:
rvalue = SD_DATA_OK;
SD_IO_CSState(1);
SD_IO_CSState(0);
while (SD_IO_WriteByte(SD_DUMMY_BYTE) != 0xFF);
break;case SD_DATA_OK:
case SD_DATA_CRC_ERROR:
rvalue = SD_DATA_CRC_ERROR;
break;case SD_DATA_CRC_ERROR:
case SD_DATA_WRITE_ERROR:
rvalue = SD_DATA_WRITE_ERROR;
break;case SD_DATA_WRITE_ERROR:
default:
break;default
}switch (dataresponse & 0x1F) { ... }
return rvalue;
}{ ... }
/* ... */
uint8_t SD_GoIdleState(void)
{
SD_CmdAnswer_typedef response;
__IO uint8_t counter = 0;
/* ... */
do{
counter++;
response = SD_SendCmd(SD_CMD_GO_IDLE_STATE, 0, 0x95, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if(counter >= SD_MAX_TRY)
{
return BSP_SD_ERROR;
}if (counter >= SD_MAX_TRY) { ... }
...}
while(response.r1 != SD_R1_IN_IDLE_STATE);
/* ... */
response = SD_SendCmd(SD_CMD_SEND_IF_COND, 0x1AA, 0x87, SD_ANSWER_R7_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if((response.r1 & SD_R1_ILLEGAL_COMMAND) == SD_R1_ILLEGAL_COMMAND)
{
do
{
response = SD_SendCmd(SD_CMD_APP_CMD, 0x00000000, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
response = SD_SendCmd(SD_CMD_SD_APP_OP_COND, 0x00000000, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
...}
while(response.r1 == SD_R1_IN_IDLE_STATE);
flag_SDHC = 0;
}if ((response.r1 & SD_R1_ILLEGAL_COMMAND) == SD_R1_ILLEGAL_COMMAND) { ... }
else if(response.r1 == SD_R1_IN_IDLE_STATE)
{
do {
response = SD_SendCmd(SD_CMD_APP_CMD, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
response = SD_SendCmd(SD_CMD_SD_APP_OP_COND, 0x40000000, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
...}
while(response.r1 == SD_R1_IN_IDLE_STATE);
if((response.r1 & SD_R1_ILLEGAL_COMMAND) == SD_R1_ILLEGAL_COMMAND)
{
do {
response = SD_SendCmd(SD_CMD_APP_CMD, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if(response.r1 != SD_R1_IN_IDLE_STATE)
{
return BSP_SD_ERROR;
}if (response.r1 != SD_R1_IN_IDLE_STATE) { ... }
response = SD_SendCmd(SD_CMD_SD_APP_OP_COND, 0x00000000, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
...}
while(response.r1 == SD_R1_IN_IDLE_STATE);
}if ((response.r1 & SD_R1_ILLEGAL_COMMAND) == SD_R1_ILLEGAL_COMMAND) { ... }
response = SD_SendCmd(SD_CMD_READ_OCR, 0x00000000, 0xFF, SD_ANSWER_R3_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if(response.r1 != SD_R1_NO_ERROR)
{
return BSP_SD_ERROR;
}if (response.r1 != SD_R1_NO_ERROR) { ... }
flag_SDHC = (response.r2 & 0x40) >> 6;
}else if (response.r1 == SD_R1_IN_IDLE_STATE) { ... }
else
{
return BSP_SD_ERROR;
}else { ... }
return BSP_SD_OK;
}{ ... }
/* ... */
uint8_t SD_ReadData(void)
{
uint8_t timeout = 0x08;
uint8_t readvalue;
do {
readvalue = SD_IO_WriteByte(SD_DUMMY_BYTE);
timeout--;
...}while ((readvalue == SD_DUMMY_BYTE) && timeout);
return readvalue;
}{ ... }
/* ... */
uint8_t SD_WaitData(uint8_t data)
{
uint16_t timeout = 0xFFFF;
uint8_t readvalue;
do {
readvalue = SD_IO_WriteByte(SD_DUMMY_BYTE);
timeout--;
...}while ((readvalue != data) && timeout);
if (timeout == 0)
{
return BSP_SD_TIMEOUT;
}if (timeout == 0) { ... }
return BSP_SD_OK;
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */
Private functions