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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2011-05-13 onelife Initial creation for using EFM32 USART module
* 2011-07-07 onelife Modify initialization function to return error code
* 2011-12-08 onelife Add giant gecko development kit support
* 2011-12-15 onelife Move MicroSD enabling routine to driver
* initialization function (board.c)
* 2011-12-21 onelife Modify code due to SPI write format changed
*/
/***************************************************************************//**
* @addtogroup efm32_dk
* @{
******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "board.h"
#include "drv_usart.h"
#include "drv_sdcard.h"
#if defined(EFM32_USING_SPISD)
#include <dfs_fs.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#ifdef EFM32_SDCARD_DEBUG
#define sdcard_debug(format,args...) rt_kprintf(format, ##args)
#else
#define sdcard_debug(format,args...)
#endif
/* Private constants ---------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static struct rt_device sd_device;
static struct dfs_partition sdPart;
static rt_device_t spi = RT_NULL;
static rt_uint16_t sdType;
static rt_bool_t sdAutoCs = true;
static rt_timer_t sdTimer = RT_NULL;
static volatile rt_bool_t sdInTime = true;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/***************************************************************************//**
* @brief
* Memory device timeout interrupt handler
*
* @details
*
* @note
*
* @param[in] parameter
* Parameter
******************************************************************************/
static void efm_spiSd_timer(void* parameter)
{
sdInTime = false;
}
/***************************************************************************//**
* @brief
* Set/Clear chip select
*
* @details
*
* @note
*
* @param[in] enable
* Chip select pin setting
******************************************************************************/
static void efm_spiSd_cs(rt_uint8_t enable)
{
if (!sdAutoCs)
{
if (enable)
{
GPIO_PinOutClear(SD_CS_PORT, SD_CS_PIN);
}
else
{
GPIO_PinOutSet(SD_CS_PORT, SD_CS_PIN);
}
}
}
/***************************************************************************//**
* @brief
* Set operation speed level
*
* @details
*
* @note
*
* @param[in] level
* Set SD speed level
******************************************************************************/
static void efm_spiSd_speed(rt_uint8_t level)
{
RT_ASSERT(spi != RT_NULL);
struct efm32_usart_device_t *usart;
rt_uint32_t baudrate;
usart = (struct efm32_usart_device_t *)(spi->user_data);
if (level == SD_SPEED_HIGH)
{
baudrate = EFM32_SDCLK_HIGH;
}
else
{
baudrate = EFM32_SDCLK_LOW;
}
USART_BaudrateSyncSet(usart->usart_device, 0, baudrate);
}
/***************************************************************************//**
* @brief
* Read raw data from memory device
*
* @details
*
* @note
*
* @param[in] buffer
* Poniter to the buffer
*
* @param[in] size
* Buffer size in byte
*
* @return
* Number of read bytes
******************************************************************************/
static rt_ssize_t efm_spiSd_read(void *buffer, rt_size_t size)
{
RT_ASSERT(spi != RT_NULL);
rt_uint8_t buf_read[5], ret;
/* Build instruction buffer */
buf_read[0] = 0x00;
*(rt_uint8_t **)(&buf_read[1]) = buffer;
/* Read data */
efm_spiSd_cs(1);
if ((ret = spi->read(spi, EFM32_NO_DATA, buf_read, size)) == 0)
{
sdcard_debug("SPISD: Read failed!\n");
}
efm_spiSd_cs(0);
return ret;
}
/***************************************************************************//**
* @brief
* Send command to memory device
*
* @details
*
* @note
*
* @param[in] cmd
* Command index
*
* @param[in] arg
* Argument
*
* @param[in] trail
* Pointer to the buffer to store trailing data
*
* @return
* Command response
******************************************************************************/
static rt_uint16_t efm_spiSd_cmd(
rt_uint8_t cmd,
rt_uint32_t arg,
rt_uint8_t *trail)
{
RT_ASSERT(spi != RT_NULL);
rt_uint8_t buf_ins[11];
rt_uint8_t buf_res[32]; /* Expect (x+1+4) bytes for CRC, (x+1+19) for CSD/CID */
rt_uint8_t len_trl, i, j;
rt_uint16_t ret;
rt_bool_t skip;
ret = 0xffff;
rt_memset(buf_res, 0xff, sizeof(buf_res));
sdcard_debug("SPISD: Send command %d(%x)\n", cmd, arg);
do
{
/* Build instruction buffer */
buf_ins[0] = 6; /* Instruction length */
buf_ins[1] = 0x40 | cmd; /* Command index */
buf_ins[2] = (arg >> 24) & 0x000000ff; /* Argument: MSB first */
buf_ins[3] = (arg >> 16) & 0x000000ff;
buf_ins[4] = (arg >> 8) & 0x000000ff;
buf_ins[5] = arg & 0x000000ff;
if (cmd == CMD0)
{
buf_ins[6] = 0x95; /* Valid CRC for CMD0(0) */
}
else if (cmd == CMD8)
{
buf_ins[6] = 0x87; /* Valid CRC for CMD8(0x1AA) */
}
else if (cmd == CMD58)
{
buf_ins[6] = 0x01; /* Dummy CRC + Stop */
}
else
{
buf_ins[6] = 0x01; /* Dummy CRC + Stop */
}
*(rt_uint8_t **)(&buf_ins[7]) = buf_res; /* Pointer to RX buffer */
/* Set trail length */
if (cmd == CMD8)
{
len_trl = 4; /* R7 response */
}
else if (cmd == CMD9)
{
len_trl = SD_BLOCK_SIZE_CSD;
}
else if (cmd == CMD10)
{
len_trl = SD_BLOCK_SIZE_CID;
}
else if (cmd == CMD58)
{
len_trl = SD_BLOCK_SIZE_OCR; /* R3 response */
}
else
{
len_trl = 0;
}
/* Send command and get response */
efm_spiSd_cs(1);
if (spi->read(spi, EFM32_NO_DATA, buf_ins, sizeof(buf_res)) == 0)
{
sdcard_debug("SPISD: Send command failed!\n");
break;
}
efm_spiSd_cs(0);
/* Skip a stuff byte when stop reading */
if (cmd == CMD12)
{
skip = true;
}
else
{
skip = false;
}
/* Find valid response: The response is sent back within command response time
(NCR), 0 to 8 bytes for SDC, 1 to 8 bytes for MMC */
for (i = 0; i < sizeof(buf_res); i++)
{
if (buf_res[i] != 0xff)
{
if (skip)
{
skip = false;
sdcard_debug("SPISD: Skip %x (at %d)\n", buf_res[i], i);
continue;
}
if (cmd == ACMD13 & 0x7f)
{
ret = (rt_uint16_t)buf_res[i]; /* R2 response */
}
else
{
ret = (rt_uint8_t)buf_res[i];
}
break;
}
}
sdcard_debug("SPISD: Response %x (at %d)\n", ret, i);
i++;
/* Copy the trailing data */
if ((ret != 0xffff) && len_trl && trail)
{
if (cmd == CMD9 || cmd == CMD10)
{
/* Wait for data block */
for (; i < sizeof(buf_res); i++)
{
if (buf_res[i] == 0xfe)
{
break;
}
}
/* Check if valid */
if (i >= sizeof(buf_res))
{
sdcard_debug("SPISD: Token is not found!\n");
ret = 0xffff;
break;
}
i++;
}
/* Copy the data */
for (j = 0; j < len_trl; j++)
{
trail[j] = buf_res[i + j];
}
}
} while(0);
return ret;
}
/***************************************************************************//**
* @brief
* Read a block of data from memory device. This function is used to handle
* the responses of specified commands (e.g. ACMD13, CMD17 and CMD18)
*
* @details
*
* @note
*
* @param[in] buffer
* Poniter to the buffer
*
* @param[in] size
* Buffer size in byte
*
* @return
* Error code
******************************************************************************/
static rt_err_t efm_spiSd_readBlock(void *buffer, rt_size_t size)
{
RT_ASSERT(spi != RT_NULL);
rt_uint8_t buf_ins[5];
rt_uint8_t buf_res[8]; /* Expect 2 bytes for CRC */
rt_uint8_t i, len_copy;
rt_bool_t start;
start = false;
do
{
/* Build instruction buffer */
buf_ins[0] = 0; /* Instruction length */
*(rt_uint8_t **)(&buf_ins[1]) = buf_res; /* Pointer to RX buffer */
while(1)
{
/* Send read command */
efm_spiSd_cs(1);
if (spi->read(spi, EFM32_NO_DATA, buf_ins, \
sizeof(buf_res)) == 0)
{
sdcard_debug("SPISD: Get read command response failed!\n");
break;
}
efm_spiSd_cs(0);
/* Wait for data */
for (i = 0; i < sizeof(buf_res); i++)
{
if (buf_res[i] != 0xff)
{
start = true;
break;
}
}
if (start)
{
break;
}
};
/* Ckeck if valid */
if (!start || (buf_res[i] != 0xfe))
{
sdcard_debug("SPISD: Token is invalid! (%x)\n", buf_res[i]);
break;
}
/* Copy data to buffer and read the rest */
len_copy = sizeof(buf_res) - i - 1;
rt_memcpy(buffer, &buf_res[i + 1], len_copy);
sdcard_debug("SPISD: Read block start at %d, copy %d bytes\n", i, \
len_copy);
/* Build instruction buffer */
buf_ins[0] = 0; /* Instruction length */
*(rt_uint8_t **)(&buf_ins[1]) = (rt_uint8_t *)buffer + len_copy; /* Pointer to RX buffer */
/* Send read command */
efm_spiSd_cs(1);
if (spi->read(spi, EFM32_NO_DATA, buf_ins, size - len_copy) == 0)
{
sdcard_debug("SPISD: Read data block failed!\n");
break;
}
*(rt_uint8_t **)(&buf_ins[1]) = buf_res; /* Pointer to RX buffer */
if (spi->read(spi, EFM32_NO_DATA, buf_ins, sizeof(buf_res)) == 0)
{
sdcard_debug("SPISD: Read CRC failed!\n");
break;
}
sdcard_debug("SPISD: Read CRC %x %x\n", buf_res[0], buf_res[1]);
efm_spiSd_cs(0);
return RT_EOK;
} while(0);
sdcard_debug("SPISD: Read block failed!\n");
return -RT_ERROR;
}
/***************************************************************************//**
* @brief
* Write a block of data to memory device. This function is used to send data
* and control tokens for block write commands (e.g. CMD24 and CMD25)
*
* @details
*
* @note
*
* @param[in] buffer
* Poniter to the buffer
*
* @param[in] token
* Control token
*
* @return
* Error code
******************************************************************************/
static rt_err_t efm_spiSd_writeBlock(void *buffer, rt_uint8_t token)
{
RT_ASSERT(spi != RT_NULL);
rt_err_t ret;
rt_uint8_t buf_ins[11];
rt_uint8_t buf_res[8]; /* Expect a byte for data response */
rt_uint8_t i;
ret = -RT_ERROR;
sdcard_debug("SPISD: Write block\n");
do
{
/* Initialize timer */
sdInTime = true;
rt_timer_start(sdTimer);
/* Wait for card ready */
do
{
efm_spiSd_read(buf_res, sizeof(buf_res));
} while (sdInTime && (buf_res[sizeof(buf_res) - 1] != 0xff));
if (buf_res[sizeof(buf_res) - 1] != 0xff)
{
sdcard_debug("SPISD: Card is busy before writing! (%x)\n", \
buf_res[sizeof(buf_res) - 1]);
ret = -RT_EBUSY;
break;
}
rt_timer_stop(sdTimer);
/* Send data */
sdcard_debug("SPISD: Send data, token %x\n", token);
if (token != 0xfd)
{
/* Send token and data */
buf_ins[0] = 1; /* Instruction length */
buf_ins[1] = token;
*(rt_uint8_t **)(&buf_ins[2]) = (rt_uint8_t *)buffer; /* Pointer to TX buffer */
efm_spiSd_cs(1);
if (spi->write(spi, EFM32_NO_DATA, buf_ins, SD_SECTOR_SIZE) == 0)
{
sdcard_debug("SPISD: Write data failed!\n");
break;
}
/* Build instruction buffer */
buf_ins[0] = 2; /* Instruction length */
buf_ins[1] = 0xff; /* CRC (Dummy) */
buf_ins[2] = 0xff;
*(rt_uint8_t **)(&buf_ins[3]) = buf_res; /* Pointer to RX buffer */
/* Send CRC and read a byte */
if (spi->read(spi, EFM32_NO_DATA, buf_ins, sizeof(buf_res)) == 0)
{
sdcard_debug("SPISD: Write CRC failed!\n");
break;
}
efm_spiSd_cs(0);
/* Check if accepted */
for (i = 0; i < sizeof(buf_res); i++)
{
if (buf_res[i] != 0xff)
{
buf_res[i] &= 0x1f;
break;
}
}
if (buf_res[i] != 0x05)
{
sdcard_debug("SPISD: Writing is not accepted! (%x at %d)\n", \
buf_res[i], i);
break;
}
}
else
{
/* Send token */
buf_ins[0] = 1; /* Instruction length */
buf_ins[1] = token;
*(rt_uint8_t **)(&buf_ins[2]) = RT_NULL; /* Pointer to TX buffer */
efm_spiSd_cs(1);
if (spi->write(spi, EFM32_NO_DATA, buf_ins, 0) != 0)
{
sdcard_debug("SPISD: Write token failed!\n");
break;
}
/* Initialize timer */
sdInTime = true;
rt_timer_start(sdTimer);
/* Wait for card ready */
do
{
efm_spiSd_read(buf_res, sizeof(buf_res));
} while (sdInTime && (buf_res[sizeof(buf_res) - 1] != 0xff));
if (buf_res[sizeof(buf_res) - 1] != 0xff)
{
sdcard_debug("SPISD: Card is busy after writing! (%x)\n", \
buf_res[sizeof(buf_res) - 1] );
ret = -RT_EBUSY;
break;
}
rt_timer_stop(sdTimer);
}
return RT_EOK;
} while(0);
sdcard_debug("SPISD: Write block failed!\n");
return ret;
}
/***************************************************************************//**
* @brief
* Wrapper function of send command to memory device
*
* @details
*
* @note
*
* @param[in] cmd
* Command index
*
* @param[in] arg
* Argument
*
* @param[in] trail
* Pointer to the buffer to store trailing data
*
* @return
* Command response
******************************************************************************/
rt_uint16_t efm_spiSd_sendCmd(
rt_uint8_t cmd,
rt_uint32_t arg,
rt_uint8_t *trail)
{
rt_uint16_t ret;
/* ACMD<n> is the command sequense of CMD55-CMD<n> */
if (cmd & 0x80)
{
cmd &= 0x7f;
ret = efm_spiSd_cmd(CMD55, 0x00000000, EFM32_NO_POINTER);
if (ret > 0x01)
{
return ret;
}
}
return efm_spiSd_cmd(cmd, arg, trail);
}
/***************************************************************************//**
* @brief
* Initialize memory card device
*
* @details
*
* @note
*
* @param[in] dev
* Pointer to device descriptor
*
* @return
* Error code
******************************************************************************/
static rt_err_t rt_spiSd_init(rt_device_t dev)
{
RT_ASSERT(spi != RT_NULL);
rt_uint8_t type, cmd, tril[4];
rt_uint8_t *buf_res;
type = 0;
buf_res = RT_NULL;
do
{
/* Create and setup timer */
if ((sdTimer = rt_timer_create(
"sd_tmr",
efm_spiSd_timer,
RT_NULL,
SD_WAIT_PERIOD,
RT_TIMER_FLAG_ONE_SHOT)) == RT_NULL)
{
sdcard_debug("SPISD: Create timer failed!\n");
break;
}
/* Open SPI device */
if (spi->open(spi, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
{
break;
}
/* Switch to low speed */
efm_spiSd_speed(SD_SPEED_LOW);
/* 80 dummy clocks */
efm_spiSd_read(RT_NULL, 80);
/* Enter Idle state */
if (efm_spiSd_sendCmd(CMD0, 0x00000000, EFM32_NO_POINTER) != 0x01)
{
break;
}
/* Check if SDv2 */
if (efm_spiSd_sendCmd(CMD8, 0x000001AA, tril) == 0x01)
{
/* SDv2, Vdd: 2.7-3.6V */
if (tril[2] == 0x01 && tril[3] == 0xAA)
{
/* Initialize timer */
sdInTime = true;
rt_timer_start(sdTimer);
/* Wait for leaving idle state (ACMD41 with HCS bit) */
while (efm_spiSd_sendCmd(ACMD41, 0x40000000, EFM32_NO_POINTER) \
&& sdInTime);
/* Check CCS bit (bit 30) in the OCR */
if (sdInTime && efm_spiSd_sendCmd(CMD58, 0x00000000, tril) \
== 0x00)
{
type = (tril[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;
}
}
}
else
{
if (efm_spiSd_sendCmd(ACMD41, 0x00000000, EFM32_NO_POINTER) <= 0x01)
{
/* SDv1 */
type = CT_SD1;
cmd = ACMD41;
}
else
{
/* MMCv3 */
type = CT_MMC;
cmd = CMD1;
}
/* Initialize timer */
sdInTime = true;
rt_timer_start(sdTimer);
/* Wait for leaving idle state */
while (efm_spiSd_sendCmd(cmd, 0x00000000, EFM32_NO_POINTER) && \
sdInTime);
/* Set read/write block length to SD_BLOCK_SIZE */
if (!sdInTime || \
(efm_spiSd_sendCmd(CMD16, SD_SECTOR_SIZE, EFM32_NO_POINTER) \
!= 0x00))
{
type = 0;
break;
}
}
rt_timer_stop(sdTimer);
/* Check type */
sdType = type;
if (sdType)
{
/* Initialization succeded */
efm_spiSd_speed(SD_SPEED_HIGH);
}
else
{
break;
}
/* Allocate buffer */
if ((buf_res = rt_malloc(SD_SECTOR_SIZE)) == RT_NULL)
{
sdcard_debug("SPISD: No memory for sector buffer\n");
break;
}
/* Read the first sector for partition table */
if (dev->read(dev, 0, buf_res, 1) != 1)
{
sdcard_debug("SPISD: Read first sector failed!\n");
break;
}
/* Fetch the partition table */
if (dfs_filesystem_get_partition(&sdPart, buf_res, 0) != RT_EOK)
{
sdPart.offset = 0;
sdPart.size = 0;
sdcard_debug("SPISD: No partition table\n");
}
/* Release buffer */
rt_free(buf_res);
sdcard_debug("SPISD: Init OK, card type %x\n", sdType);
return RT_EOK;
} while (0);
/* Release buffer */
if (buf_res)
{
rt_free(buf_res);
}
efm_spiSd_deinit();
rt_kprintf("SPISD: Init failed!\n");
return -RT_ERROR;
}
/***************************************************************************//**
* @brief
* Open memory card device
*
* @details
*
* @note
*
* @param[in] dev
* Pointer to device descriptor
*
* @param[in] oflag
* Device open flag
*
* @return
* Error code
******************************************************************************/
static rt_err_t rt_spiSd_open(rt_device_t dev, rt_uint16_t oflag)
{
sdcard_debug("SPISD: Open, flag %x\n", sd_device.flag);
return RT_EOK;
}
/***************************************************************************//**
* @brief
* Close memory card device
*
* @details
*
* @note
*
* @param[in] dev
* Pointer to device descriptor
*
* @return
* Error code
******************************************************************************/
static rt_err_t rt_spiSd_close(rt_device_t dev)
{
sdcard_debug("SPISD: Close, flag %x\n", sd_device.flag);
return RT_EOK;
}
/***************************************************************************//**
* @brief
* Read from memory card device
*
* @details
*
* @note
*
* @param[in] dev
* Pointer to device descriptor
*
* @param[in] sector
* Start sector number (LBA)
*
* @param[in] buffer
* Pointer to the buffer
*
* @param[in] count
* Sector count (1..255)
*
* @return
* Number of read sectors
******************************************************************************/
static rt_ssize_t rt_spiSd_read(
rt_device_t dev,
rt_off_t sector,
void *buffer,
rt_size_t count)
{
rt_uint8_t buf_ins[11], buf_res[12];
rt_uint8_t *ptr;
rt_uint8_t cmd, i;
rt_size_t cnt;
ptr = (rt_uint8_t *)buffer;
cnt = count;
sdcard_debug("SPISD: ****** Read Data ******\n");
if (!(sdType & CT_BLOCK))
{
/* Convert to byte address if needed */
sector *= SD_SECTOR_SIZE;
}
do
{
if (cnt == 1)
{
/* Single block read */
cmd = CMD17;
sdcard_debug("SPISD: Read single block\n");
}
else
{
/* Multiple block read */
cmd = CMD18;
sdcard_debug("SPISD: Read multiple blocks\n");
}
if (efm_spiSd_sendCmd(cmd, sector, EFM32_NO_POINTER))
{
sdcard_debug("SPISD: Read command error!\n");
break;
}
/* Read data */
do
{
if (efm_spiSd_readBlock(ptr, SD_SECTOR_SIZE))
{
break;
}
ptr += SD_SECTOR_SIZE;
} while(--cnt);
/* Stop transmission */
if (cmd == CMD18)
{
if (efm_spiSd_sendCmd(CMD12, 0x00000000, EFM32_NO_POINTER))
{
break;
}
}
return (count);
} while(0);
return (0);
}
/***************************************************************************//**
* @brief
* Write to memory card device
*
* @details
*
* @note
*
* @param[in] dev
* Pointer to device descriptor
*
* @param[in] sector
* Start sector number (LBA)
*
* @param[in] buffer
* Pointer to the buffer
*
* @param[in] count
* Sector count (1..255)
*
* @return
* Number of written sectors
******************************************************************************/
static rt_ssize_t rt_spiSd_write (
rt_device_t dev,
rt_off_t sector,
const void *buffer,
rt_size_t count)
{
rt_uint8_t buf_ins[11], buf_res[12];
rt_uint8_t *ptr;
rt_uint8_t cmd, token, i;
rt_size_t cnt;
ptr = (rt_uint8_t *)buffer;
cnt = count;
sdcard_debug("SPISD: ****** Write Data ******\n");
if (!(sdType & CT_BLOCK))
{
/* Convert to byte address if needed */
sector *= SD_SECTOR_SIZE;
}
do
{
if (cnt == 1)
{
/* Single block write */
cmd = CMD24;
token = 0xfe;
sdcard_debug("SPISD: Write single block\n");
}
else
{
/* Multiple block write */
cmd = CMD25;
token = 0xfc;
sdcard_debug("SPISD: Write multiple blocks\n");
if (sdType & CT_SDC)
{
if (efm_spiSd_sendCmd(ACMD23, count, EFM32_NO_POINTER))
{
break;
}
}
}
if (efm_spiSd_sendCmd(cmd, sector, EFM32_NO_POINTER))
{
sdcard_debug("SPISD: Write command error!\n");
break;
}
/* Write data */
do
{
if (efm_spiSd_writeBlock(ptr, token))
{
break;
}
ptr += SD_SECTOR_SIZE;
} while(--cnt);
/* Stop transmission token */
if (efm_spiSd_writeBlock(EFM32_NO_POINTER, 0xfd))
{
break;
}
return (count);
} while(0);
return (0);
}
/***************************************************************************//**
* @brief
* Configure memory card device
*
* @details
*
* @note
*
* @param[in] dev
* Pointer to device descriptor
*
* @param[in] ctrl
* Memory card control command
*
* @param[in] buffer
* Pointer to the buffer of in/out data
*
* @return
* Error code
******************************************************************************/
static rt_err_t rt_spiSd_control (
rt_device_t dev,
rt_uint8_t ctrl,
void *buffer)
{
rt_err_t ret;
rt_uint32_t c_size;
rt_uint8_t n;
rt_uint8_t *buf_res;
ret = -RT_ERROR;
buf_res = RT_NULL;
switch (ctrl)
{
case RT_DEVICE_CTRL_SD_SYNC:
/* Flush dirty buffer if present */
efm_spiSd_cs(1);
efm_spiSd_cs(0);
ret = RT_EOK;
break;
case RT_DEVICE_CTRL_SD_GET_SCOUNT:
{
/* Allocate buffer */
if ((buf_res = rt_malloc(SD_BLOCK_SIZE_CSD)) == RT_NULL)
{
sdcard_debug("SPISD: No memory for RX buffer\n");
break;
}
/* Get number of sectors on the disk (32 bits) */
if (efm_spiSd_sendCmd(CMD9, 0x00000000, buf_res))
{
sdcard_debug("SPISD: Get CSD failed!\n");
break;
}
if ((buf_res[0] >> 6) == 0x01)
{
/* SDv2 */
/* C_SIZE: Bit 48~69 */
c_size = ((rt_uint32_t)(buf_res[7] & 0x3f) << 16) + \
((rt_uint32_t)buf_res[8] << 8) + buf_res[9] + 1;
/* Result = Capacity / Sector Size */
*(rt_uint32_t *)buffer = (rt_uint32_t)c_size << \
(19 - SD_SECTOR_SIZE_SHIFT);
}
else
{
/* SDv1 or MMC */
/* C_SIZE: Bit 62~73 */
c_size = ((rt_uint32_t)(buf_res[6] & 0x03) << 10) + \
((rt_uint16_t)buf_res[7] << 2) + (buf_res[8] >> 6) + 1;
/* READ_BL_LEN: Bit 80~83, C_SIZE_MULT: Bit 47~49 */
n = ((buf_res[9] & 0x03) << 1) + ((buf_res[10] & 0x80) >> 7) + \
2 + (buf_res[5] & 0x0f);
/* Result = Capacity / Sector Size */
*(rt_uint32_t *)buffer = (rt_uint32_t)c_size << \
(n - SD_SECTOR_SIZE_SHIFT);
}
ret = RT_EOK;
break;
}
case RT_DEVICE_CTRL_SD_GET_SSIZE:
/* Get sectors on the disk (16 bits) */
*(rt_uint16_t *)buffer = SD_SECTOR_SIZE;
ret = RT_EOK;
break;
case RT_DEVICE_CTRL_SD_GET_BSIZE:
/* Get erase block size in unit of sectors (32 bits) */
if (sdType & CT_SD2)
{
/* Allocate buffer */
if ((buf_res = rt_malloc(SD_BLOCK_SIZE_SDSTAT)) == RT_NULL)
{
sdcard_debug("SPISD: No memory for RX buffer\n");
break;
}
/* SDv2 */
if (efm_spiSd_sendCmd(ACMD13, 0x00000000, EFM32_NO_POINTER))
{
sdcard_debug("SPISD: Get SD status failed!\n");
break;
}
if (efm_spiSd_readBlock(buf_res, SD_BLOCK_SIZE_SDSTAT))
{
sdcard_debug("SPISD: Read SD status failed!\n");
break;
}
/* AU_SIZE: Bit 428~431 */
*(rt_uint32_t *)buffer = 16UL << ((buf_res[10] >> 4) + 9 - \
SD_SECTOR_SIZE_SHIFT);
}
else
{
/* Allocate buffer */
if ((buf_res = rt_malloc(SD_BLOCK_SIZE_CSD)) == RT_NULL)
{
sdcard_debug("SPISD: No memory for RX buffer\n");
break;
}
/* SDv1 or MMC */
if (efm_spiSd_sendCmd(CMD9, 0x00000000, buf_res))
{
sdcard_debug("SPISD: Get CSD failed!\n");
break;
}
if (sdType & CT_SD1)
{
/* SECTOR_SIZE: Bit 39~45, WRITE_BL_LEN: Bit 22~25 (9, 10 or 11) */
*(rt_uint32_t *)buffer = (((buf_res[10] & 0x3f) << 1) + \
((rt_uint32_t)(buf_res[11] & 0x80) >> 7) + 1) << \
(8 + (buf_res[13] >> 6) - SD_SECTOR_SIZE_SHIFT);
}
else
{
/* ERASE_GRP_SIZE: Bit 42~46, ERASE_GRP_MULT: Bit 37~41 */
*(rt_uint32_t *)buffer = \
((rt_uint16_t)((buf_res[10] & 0x7c) >> 2) + 1) * \
(((buf_res[10] & 0x03) << 3) + \
((buf_res[11] & 0xe0) >> 5) + 1);
}
}
ret = RT_EOK;
break;
case RT_DEVICE_CTRL_SD_GET_TYPE:
/* Get card type flags (1 byte) */
*(rt_uint8_t *)buffer = sdType;
ret = RT_EOK;
break;
case RT_DEVICE_CTRL_SD_GET_CSD:
/* Receive CSD as a data block (16 bytes) */
if (efm_spiSd_sendCmd(CMD9, 0x00000000, buffer))
{
sdcard_debug("SPISD: Get CSD failed!\n");
break;
}
ret = RT_EOK;
break;
case RT_DEVICE_CTRL_SD_GET_CID:
/* Receive CID as a data block (16 bytes) */
if (efm_spiSd_sendCmd(CMD10, 0x00000000, buffer))
{
sdcard_debug("SPISD: Get CID failed!\n");
break;
}
ret = RT_EOK;
break;
case RT_DEVICE_CTRL_SD_GET_OCR:
/* Receive OCR as an R3 resp (4 bytes) */
if (efm_spiSd_sendCmd(CMD58, 0x00000000, buffer))
{
sdcard_debug("SPISD: Get OCR failed!\n");
break;
}
ret = RT_EOK;
break;
case RT_DEVICE_CTRL_SD_GET_SDSTAT:
/* Receive SD statsu as a data block (64 bytes) */
if (efm_spiSd_sendCmd(ACMD13, 0x00000000, buffer))
{
sdcard_debug("SPISD: Get SD status failed!\n");
break;
}
if (efm_spiSd_readBlock(buffer, SD_BLOCK_SIZE_SDSTAT))
{
sdcard_debug("SPISD: Read SD status failed!\n");
break;
}
ret = RT_EOK;
break;
default:
break;
}
if (buf_res)
{
rt_free(buf_res);
}
return ret;
}
/***************************************************************************//**
* @brief
* Initialize all memory card related hardware and register the device to
* kernel
*
* @details
*
* @note
*
* @return
* Error code
******************************************************************************/
rt_err_t efm_spiSd_init(void)
{
struct efm32_usart_device_t *usart;
do
{
/* Find SPI device */
spi = rt_device_find(SPISD_USING_DEVICE_NAME);
if (spi == RT_NULL)
{
sdcard_debug("SPISD: Can't find device %s!\n",
SPISD_USING_DEVICE_NAME);
break;
}
sdcard_debug("SPISD: Find device %s\n", SPISD_USING_DEVICE_NAME);
/* Config chip slect pin */
usart = (struct efm32_usart_device_t *)(spi->user_data);
if (!(usart->state & USART_STATE_AUTOCS))
{
GPIO_PinModeSet(SD_CS_PORT, SD_CS_PIN, gpioModePushPull, 1);
sdAutoCs = false;
}
/* Register SPI SD device */
sd_device.type = RT_Device_Class_MTD;
sd_device.init = rt_spiSd_init;
sd_device.open = rt_spiSd_open;
sd_device.close = rt_spiSd_close;
sd_device.read = rt_spiSd_read;
sd_device.write = rt_spiSd_write;
sd_device.control = rt_spiSd_control;
sd_device.user_data = RT_NULL;
rt_device_register(
&sd_device,
SPISD_DEVICE_NAME,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
sdcard_debug("SPISD: HW init OK, card type %x\n", sdType);
return RT_EOK;
} while (0);
/* Release buffer */
rt_kprintf("SPISD: HW init failed!\n");
return -RT_ERROR;
}
/***************************************************************************//**
* @brief
* De-initialize memory card device
*
* @details
*
* @note
******************************************************************************/
void efm_spiSd_deinit(void)
{
/* Close SPI device */
if (spi != RT_NULL)
{
spi->close(spi);
spi = RT_NULL;
sdcard_debug("SPISD: Close device %s\n", SPISD_USING_DEVICE_NAME);
}
/* Delete timer */
if (sdTimer != RT_NULL)
{
rt_timer_delete(sdTimer);
sdTimer = RT_NULL;
sdcard_debug("SPISD: Delete timer\n");
}
sdcard_debug("SPISD: Deinit OK\n");
}
/*******************************************************************************
* Export to FINSH
******************************************************************************/
#ifdef RT_USING_FINSH
#include <finsh.h>
void list_sd(void)
{
rt_uint8_t buf_res[16];
rt_uint32_t capacity, temp32;
rt_uint16_t temp16;
rt_kprintf(" SD Card on %s\n", SPISD_USING_DEVICE_NAME);
rt_kprintf(" ------------------------------\n");
sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_CID, buf_res);
rt_kprintf(" Manufacturer ID:\t%x\n", buf_res[0]);
rt_kprintf(" OEM/Application ID:\t%x%x\n", buf_res[1], buf_res[2]);
rt_kprintf(" Product revision:\t%x\n", buf_res[8]);
buf_res[8] = 0;
rt_kprintf(" Product name:\t\t%s\n", &buf_res[3]);
rt_kprintf(" Serial number:\t\t%x%x%x%x\n", \
buf_res[9], buf_res[10], buf_res[11], buf_res[12]);
rt_kprintf(" Manufacturing date:\t%d.%d\n", \
2000 + ((buf_res[13] & 0x0F) << 4) + ((buf_res[14] & 0xF0) >> 4), \
buf_res[14] & 0x0F);
rt_kprintf(" Card type:\t\t");
sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_TYPE, buf_res);
if (buf_res[0] == CT_MMC)
{
rt_kprintf("%s\n", "MMC");
}
else if (buf_res[0] == CT_SDC)
{
rt_kprintf("%s\n", "SDXC");
}
else if (buf_res[0] == CT_SD1)
{
rt_kprintf("%s\n", "SDSC");
}
else if (buf_res[0] == CT_SD2)
{
rt_kprintf("%s\n", "SDHC");
}
sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_SSIZE, &temp16);
sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_SCOUNT, &temp32);
capacity = ((temp32 & 0x0000FFFF) * temp16) >> 16;
capacity += ((temp32 >> 16) * temp16);
capacity >>= 4;
rt_kprintf(" Card capacity:\t\t%dMB\n", capacity);
}
FINSH_FUNCTION_EXPORT(list_sd, list the SD card.)
#endif
#endif /* defined(EFM32_USING_SPISD) */
/***************************************************************************//**
* @}
******************************************************************************/
|