-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathdev_spi_msd.c
More file actions
1660 lines (1418 loc) · 52.4 KB
/
Copy pathdev_spi_msd.c
File metadata and controls
1660 lines (1418 loc) · 52.4 KB
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
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2009-04-17 Bernard first version.
* 2010-07-15 aozima Modify read/write according new block driver interface.
* 2012-02-01 aozima use new RT-Thread SPI drivers.
* 2012-04-11 aozima get max. data transfer rate from CSD[TRAN_SPEED].
* 2012-05-21 aozima update MMC card support.
* 2018-03-09 aozima fixed CSD Version 2.0 sector count calc.
*/
#include <string.h>
#include "dev_spi_msd.h"
//#define MSD_TRACE
#ifdef MSD_TRACE
#define MSD_DEBUG(...) rt_kprintf("[MSD] %d ", rt_tick_get()); rt_kprintf(__VA_ARGS__);
#else
#define MSD_DEBUG(...)
#endif /* #ifdef MSD_TRACE */
#define DUMMY 0xFF
#define CARD_NCR_MAX 9
#define CARD_NRC 1
#define CARD_NCR 1
static struct msd_device _msd_device;
/* function define */
static rt_bool_t rt_tick_timeout(rt_tick_t tick_start, rt_tick_t tick_long);
static rt_err_t MSD_take_owner(struct rt_spi_device *spi_device);
static rt_err_t _wait_token(struct rt_spi_device *device, uint8_t token);
static rt_err_t _wait_ready(struct rt_spi_device *device);
static rt_err_t rt_msd_init(rt_device_t dev);
static rt_err_t rt_msd_open(rt_device_t dev, rt_uint16_t oflag);
static rt_err_t rt_msd_close(rt_device_t dev);
static rt_ssize_t rt_msd_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
static rt_ssize_t rt_msd_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
static rt_ssize_t rt_msd_sdhc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
static rt_ssize_t rt_msd_sdhc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
static rt_err_t rt_msd_control(rt_device_t dev, int cmd, void *args);
static rt_err_t MSD_take_owner(struct rt_spi_device *spi_device)
{
rt_err_t result;
result = rt_mutex_take(&(spi_device->bus->lock), RT_WAITING_FOREVER);
if (result == RT_EOK)
{
if (spi_device->bus->owner != spi_device)
{
/* not the same owner as current, re-configure SPI bus */
result = spi_device->bus->ops->configure(spi_device, &spi_device->config);
if (result == RT_EOK)
{
/* set SPI bus owner */
spi_device->bus->owner = spi_device;
}
}
}
return result;
}
static rt_bool_t rt_tick_timeout(rt_tick_t tick_start, rt_tick_t tick_long)
{
rt_tick_t tick_end = tick_start + tick_long;
rt_tick_t tick_now = rt_tick_get();
rt_bool_t result = RT_FALSE;
if (tick_end >= tick_start)
{
if (tick_now >= tick_end)
{
result = RT_TRUE;
}
else
{
result = RT_FALSE;
}
}
else
{
if ((tick_now < tick_start) && (tick_now >= tick_end))
{
result = RT_TRUE;
}
else
{
result = RT_FALSE;
}
}
return result;
}
static uint8_t crc7(const uint8_t *buf, int len)
{
unsigned char i, j, crc, ch, ch2, ch3;
crc = 0;
for (i = 0; i < len; i ++)
{
ch = buf[i];
for (j = 0; j < 8; j ++, ch <<= 1)
{
ch2 = (crc & 0x40) ? 1 : 0;
ch3 = (ch & 0x80) ? 1 : 0;
if (ch2 ^ ch3)
{
crc ^= 0x04;
crc <<= 1;
crc |= 0x01;
}
else
{
crc <<= 1;
}
}
}
return crc;
}
static rt_err_t _send_cmd(
struct rt_spi_device *device,
uint8_t cmd,
uint32_t arg,
uint8_t crc,
response_type type,
uint8_t *response
)
{
struct rt_spi_message message;
uint8_t cmd_buffer[8];
uint8_t recv_buffer[sizeof(cmd_buffer)];
uint32_t i;
cmd_buffer[0] = DUMMY;
cmd_buffer[1] = (cmd | 0x40);
cmd_buffer[2] = (uint8_t)(arg >> 24);
cmd_buffer[3] = (uint8_t)(arg >> 16);
cmd_buffer[4] = (uint8_t)(arg >> 8);
cmd_buffer[5] = (uint8_t)(arg);
if (crc == 0x00)
{
crc = crc7(&cmd_buffer[1], 5);
crc = (crc << 1) | 0x01;
}
cmd_buffer[6] = (crc);
cmd_buffer[7] = DUMMY;
/* initial message */
message.send_buf = cmd_buffer;
message.recv_buf = recv_buffer;
message.length = sizeof(cmd_buffer);
message.cs_take = message.cs_release = 0;
_wait_ready(device);
/* transfer message */
device->bus->ops->xfer(device, &message);
for (i = CARD_NCR; i < (CARD_NCR_MAX + 1); i++)
{
uint8_t send = DUMMY;
/* initial message */
message.send_buf = &send;
message.recv_buf = response;
message.length = 1;
message.cs_take = message.cs_release = 0;
/* transfer message */
device->bus->ops->xfer(device, &message);
if (0 == (response[0] & 0x80))
{
break;
}
} /* wait response */
if ((CARD_NCR_MAX + 1) == i)
{
return -RT_ERROR;//fail
}
//receive other byte
if (type == response_r1)
{
return RT_EOK;
}
else if (type == response_r1b)
{
rt_tick_t tick_start = rt_tick_get();
uint8_t recv;
while (1)
{
/* initial message */
message.send_buf = RT_NULL;
message.recv_buf = &recv;
message.length = 1;
message.cs_take = message.cs_release = 0;
/* transfer message */
device->bus->ops->xfer(device, &message);
if (recv == DUMMY)
{
return RT_EOK;
}
if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(2000)))
{
return -RT_ETIMEOUT;
}
}
}
else if (type == response_r2)
{
/* initial message */
/* Prevent non-aligned address access, use recv_buffer to receive data */
message.send_buf = RT_NULL;
message.recv_buf = recv_buffer;
message.length = 1;
message.cs_take = message.cs_release = 0;
/* transfer message */
device->bus->ops->xfer(device, &message);
response[1] = recv_buffer[0];
}
else if ((type == response_r3) || (type == response_r7))
{
/* initial message */
message.send_buf = RT_NULL;
message.recv_buf = recv_buffer;
message.length = 4;
message.cs_take = message.cs_release = 0;
/* transfer message */
device->bus->ops->xfer(device, &message);
response[1] = recv_buffer[0];
response[2] = recv_buffer[1];
response[3] = recv_buffer[2];
response[4] = recv_buffer[3];
}
else
{
return -RT_ERROR; // unknow type?
}
return RT_EOK;
}
static rt_err_t _wait_token(struct rt_spi_device *device, uint8_t token)
{
struct rt_spi_message message;
rt_tick_t tick_start;
uint8_t send, recv;
tick_start = rt_tick_get();
/* wati token */
/* initial message */
send = DUMMY;
message.send_buf = &send;
message.recv_buf = &recv;
message.length = 1;
message.cs_take = message.cs_release = 0;
while (1)
{
/* transfer message */
device->bus->ops->xfer(device, &message);
if (recv == token)
{
return RT_EOK;
}
if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_WAIT_TOKEN_TIMES)))
{
MSD_DEBUG("[err] wait data start token timeout!\r\n");
return -RT_ETIMEOUT;
}
} /* wati token */
}
static rt_err_t _wait_ready(struct rt_spi_device *device)
{
struct rt_spi_message message;
rt_tick_t tick_start;
uint8_t send, recv;
tick_start = rt_tick_get();
send = DUMMY;
/* initial message */
message.send_buf = &send;
message.recv_buf = &recv;
message.length = 1;
message.cs_take = message.cs_release = 0;
while (1)
{
/* transfer message */
device->bus->ops->xfer(device, &message);
if (recv == DUMMY)
{
return RT_EOK;
}
if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(1000)))
{
MSD_DEBUG("[err] wait ready timeout!\r\n");
return -RT_ETIMEOUT;
}
}
}
static rt_err_t _read_block(struct rt_spi_device *device, void *buffer, uint32_t block_size)
{
struct rt_spi_message message;
rt_err_t result;
/* wati token */
result = _wait_token(device, MSD_TOKEN_READ_START);
if (result != RT_EOK)
{
return result;
}
/* read data */
{
/* initial message */
message.send_buf = RT_NULL;
message.recv_buf = buffer;
message.length = block_size;
message.cs_take = message.cs_release = 0;
/* transfer message */
device->bus->ops->xfer(device, &message);
} /* read data */
/* get crc */
{
uint8_t recv_buffer[2];
/* initial message */
message.send_buf = RT_NULL;
message.recv_buf = recv_buffer;
message.length = 2;
message.cs_take = message.cs_release = 0;
/* transfer message */
device->bus->ops->xfer(device, &message);
} /* get crc */
return RT_EOK;
}
static rt_err_t _write_block(struct rt_spi_device *device, const void *buffer, uint32_t block_size, uint8_t token)
{
struct rt_spi_message message;
uint8_t send_buffer[16];
rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
send_buffer[sizeof(send_buffer) - 1] = token;
/* send start block token */
{
/* initial message */
message.send_buf = send_buffer;
message.recv_buf = RT_NULL;
message.length = sizeof(send_buffer);
message.cs_take = message.cs_release = 0;
/* transfer message */
device->bus->ops->xfer(device, &message);
}
/* send data */
{
/* initial message */
message.send_buf = buffer;
message.recv_buf = RT_NULL;
message.length = block_size;
message.cs_take = message.cs_release = 0;
/* transfer message */
device->bus->ops->xfer(device, &message);
}
/* put crc and get data response */
{
uint8_t recv_buffer[3];
uint8_t response;
/* initial message */
message.send_buf = send_buffer;
message.recv_buf = recv_buffer;
message.length = sizeof(recv_buffer);
message.cs_take = message.cs_release = 0;
/* transfer message */
device->bus->ops->xfer(device, &message);
// response = 0x0E & recv_buffer[2];
response = MSD_GET_DATA_RESPONSE(recv_buffer[2]);
if (response != MSD_DATA_OK)
{
MSD_DEBUG("[err] write block fail! data response : 0x%02X\r\n", response);
return -RT_ERROR;
}
}
/* wati ready */
return _wait_ready(device);
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops msd_ops =
{
rt_msd_init,
rt_msd_open,
rt_msd_close,
rt_msd_read,
rt_msd_write,
rt_msd_control
};
const static struct rt_device_ops msd_sdhc_ops =
{
rt_msd_init,
rt_msd_open,
rt_msd_close,
rt_msd_sdhc_read,
rt_msd_sdhc_write,
rt_msd_control
};
#endif
/* RT-Thread Device Driver Interface */
static rt_err_t rt_msd_init(rt_device_t dev)
{
struct msd_device *msd = (struct msd_device *)dev;
uint8_t response[MSD_RESPONSE_MAX_LEN];
rt_err_t result = RT_EOK;
rt_tick_t tick_start;
uint32_t OCR;
if (msd->spi_device == RT_NULL)
{
MSD_DEBUG("[err] the SPI SD device has no SPI!\r\n");
return -RT_EIO;
}
/* config spi */
{
struct rt_spi_configuration cfg;
cfg.data_width = 8;
cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
cfg.max_hz = 1000 * 400; /* 400kbit/s */
rt_spi_configure(msd->spi_device, &cfg);
} /* config spi */
/* init SD card */
{
struct rt_spi_message message;
result = MSD_take_owner(msd->spi_device);
if (result != RT_EOK)
{
goto _exit;
}
rt_spi_release(msd->spi_device);
/* The host shall supply power to the card so that the voltage is reached to Vdd_min within 250ms and
start to supply at least 74 SD clocks to the SD card with keeping CMD line to high.
In case of SPI mode, CS shall be held to high during 74 clock cycles. */
{
uint8_t send_buffer[100]; /* 100byte > 74 clock */
/* initial message */
rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
message.send_buf = send_buffer;
message.recv_buf = RT_NULL;
message.length = sizeof(send_buffer);
message.cs_take = message.cs_release = 0;
/* transfer message */
msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
} /* send 74 clock */
/* Send CMD0 (GO_IDLE_STATE) to put MSD in SPI mode */
{
tick_start = rt_tick_get();
while (1)
{
rt_spi_take(msd->spi_device);
result = _send_cmd(msd->spi_device, GO_IDLE_STATE, 0x00, 0x95, response_r1, response);
rt_spi_release(msd->spi_device);
if ((result == RT_EOK) && (response[0] == MSD_IN_IDLE_STATE))
{
break;
}
if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES)))
{
MSD_DEBUG("[err] SD card goto IDLE mode timeout!\r\n");
result = -RT_ETIMEOUT;
goto _exit;
}
}
MSD_DEBUG("[info] SD card goto IDLE mode OK!\r\n");
} /* Send CMD0 (GO_IDLE_STATE) to put MSD in SPI mode */
/* CMD8 */
{
tick_start = rt_tick_get();
do
{
rt_spi_take(msd->spi_device);
result = _send_cmd(msd->spi_device, SEND_IF_COND, 0x01AA, 0x87, response_r7, response);
rt_spi_release(msd->spi_device);
if (result == RT_EOK)
{
MSD_DEBUG("[info] CMD8 response : 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\r\n",
response[0], response[1], response[2], response[3], response[4]);
if (response[0] & (1 << 2))
{
/* illegal command, SD V1.x or MMC card */
MSD_DEBUG("[info] CMD8 is illegal command.\r\n");
MSD_DEBUG("[info] maybe Ver1.X SD Memory Card or MMC card!\r\n");
msd->card_type = MSD_CARD_TYPE_SD_V1_X;
break;
}
else
{
/* SD V2.0 or later or SDHC or SDXC memory card! */
MSD_DEBUG("[info] Ver2.00 or later or SDHC or SDXC memory card!\r\n");
msd->card_type = MSD_CARD_TYPE_SD_V2_X;
}
if ((0xAA == response[4]) && (0x00 == response[3]))
{
/* SD2.0 not support current voltage */
MSD_DEBUG("[err] VCA = 0, SD2.0 not surpport current operation voltage range\r\n");
result = -RT_ERROR;
goto _exit;
}
}
else
{
if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(200)))
{
MSD_DEBUG("[err] CMD8 SEND_IF_COND timeout!\r\n");
result = -RT_ETIMEOUT;
goto _exit;
}
}
}
while (0xAA != response[4]);
} /* CMD8 */
/* Ver1.X SD Memory Card or MMC card */
if (msd->card_type == MSD_CARD_TYPE_SD_V1_X)
{
rt_bool_t is_sd_v1_x = RT_FALSE;
rt_tick_t tick_start;
/* try SD Ver1.x */
while (1)
{
rt_spi_take(msd->spi_device);
result = _send_cmd(msd->spi_device, READ_OCR, 0x00, 0x00, response_r3, response);
if (result != RT_EOK)
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[info] It maybe SD1.x or MMC But it is Not response to CMD58!\r\n");
goto _exit;
}
if (0 != (response[0] & 0xFE))
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[info] It look CMD58 as illegal command so it is not SD card!\r\n");
break;
}
rt_spi_release(msd->spi_device);
OCR = response[1];
OCR = (OCR << 8) + response[2];
OCR = (OCR << 8) + response[3];
OCR = (OCR << 8) + response[4];
MSD_DEBUG("[info] OCR is 0x%08X\r\n", OCR);
if (0 == (OCR & (0x1 << 15)))
{
MSD_DEBUG(("[err] SD 1.x But not surpport current voltage\r\n"));
result = -RT_ERROR;
goto _exit;
}
/* --Send ACMD41 to make card ready */
tick_start = rt_tick_get();
/* try CMD55 + ACMD41 */
while (1)
{
if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES_ACMD41)))
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[info] try CMD55 + ACMD41 timeout! mabey MMC card!\r\n");
break;
}
rt_spi_take(msd->spi_device);
/* CMD55 APP_CMD */
result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x00, response_r1, response);
if (result != RT_EOK)
{
rt_spi_release(msd->spi_device);
continue;
}
if (0 != (response[0] & 0xFE))
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[info] Not SD card2 , may be MMC\r\n");
break;
}
/* ACMD41 SD_SEND_OP_COND */
result = _send_cmd(msd->spi_device, SD_SEND_OP_COND, 0x00, 0x00, response_r1, response);
if (result != RT_EOK)
{
rt_spi_release(msd->spi_device);
continue;
}
if (0 != (response[0] & 0xFE))
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[info] Not SD card4 , may be MMC\r\n");
break;
}
if (0 == (response[0] & 0xFF))
{
rt_spi_release(msd->spi_device);
is_sd_v1_x = RT_TRUE;
MSD_DEBUG("[info] It is Ver1.X SD Memory Card!!!\r\n");
break;
}
} /* try CMD55 + ACMD41 */
break;
} /* try SD Ver1.x */
/* try MMC */
if (is_sd_v1_x != RT_TRUE)
{
uint32_t i;
MSD_DEBUG("[info] try MMC card!\r\n");
rt_spi_release(msd->spi_device);
/* send dummy clock */
{
uint8_t send_buffer[100];
/* initial message */
rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
message.send_buf = send_buffer;
message.recv_buf = RT_NULL;
message.length = sizeof(send_buffer);
message.cs_take = message.cs_release = 0;
for (i = 0; i < 10; i++)
{
/* transfer message */
msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
}
} /* send dummy clock */
/* send CMD0 goto IDLE state */
tick_start = rt_tick_get();
while (1)
{
rt_spi_take(msd->spi_device);
result = _send_cmd(msd->spi_device, GO_IDLE_STATE, 0x00, 0x95, response_r1, response);
rt_spi_release(msd->spi_device);
if ((result == RT_EOK) && (response[0] == MSD_IN_IDLE_STATE))
{
break;
}
if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES)))
{
MSD_DEBUG("[err] SD card goto IDLE mode timeout!\r\n");
result = -RT_ETIMEOUT;
goto _exit;
}
} /* send CMD0 goto IDLE stat */
/* send CMD1 */
tick_start = rt_tick_get();
while (1)
{
rt_spi_take(msd->spi_device);
result = _send_cmd(msd->spi_device, SEND_OP_COND, 0x00, 0x00, response_r1, response);
rt_spi_release(msd->spi_device);
if ((result == RT_EOK) && (response[0] == MSD_RESPONSE_NO_ERROR))
{
MSD_DEBUG("[info] It is MMC card!!!\r\n");
msd->card_type = MSD_CARD_TYPE_MMC;
break;
}
if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES)))
{
MSD_DEBUG("[err] SD card goto IDLE mode timeout!\r\n");
result = -RT_ETIMEOUT;
goto _exit;
}
} /* send CMD1 */
} /* try MMC */
}
else if (msd->card_type == MSD_CARD_TYPE_SD_V2_X)
{
rt_spi_take(msd->spi_device);
result = _send_cmd(msd->spi_device, READ_OCR, 0x00, 0x00, response_r3, response);
if (result != RT_EOK)
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[err] It maybe SD2.0 But it is Not response to CMD58!\r\n");
goto _exit;
}
if ((response[0] & 0xFE) != 0)
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[err] It look CMD58 as illegal command so it is not SD card!\r\n");
result = -RT_ERROR;
goto _exit;
}
rt_spi_release(msd->spi_device);
OCR = response[1];
OCR = (OCR << 8) + response[2];
OCR = (OCR << 8) + response[3];
OCR = (OCR << 8) + response[4];
MSD_DEBUG("[info] OCR is 0x%08X\r\n", OCR);
if (0 == (OCR & (0x1 << 15)))
{
MSD_DEBUG(("[err] SD 1.x But not surpport current voltage\r\n"));
result = -RT_ERROR;
goto _exit;
}
/* --Send ACMD41 to make card ready */
tick_start = rt_tick_get();
/* try CMD55 + ACMD41 */
do
{
rt_spi_take(msd->spi_device);
if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES_ACMD41)))
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[err] SD Ver2.x or later try CMD55 + ACMD41 timeout!\r\n");
result = -RT_ERROR;
goto _exit;
}
/* CMD55 APP_CMD */
result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x65, response_r1, response);
// if((result != RT_EOK) || (response[0] == 0x01))
if (result != RT_EOK)
{
rt_spi_release(msd->spi_device);
continue;
}
if ((response[0] & 0xFE) != 0)
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[err] Not SD ready!\r\n");
result = -RT_ERROR;
goto _exit;
}
/* ACMD41 SD_SEND_OP_COND */
result = _send_cmd(msd->spi_device, SD_SEND_OP_COND, 0x40000000, 0x77, response_r1, response);
if (result != RT_EOK)
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[err] ACMD41 fail!\r\n");
result = -RT_ERROR;
goto _exit;
}
if ((response[0] & 0xFE) != 0)
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[info] Not SD card4 , response : 0x%02X\r\n", response[0]);
// break;
}
}
while (response[0] != MSD_RESPONSE_NO_ERROR);
rt_spi_release(msd->spi_device);
/* try CMD55 + ACMD41 */
/* --Read OCR again */
rt_spi_take(msd->spi_device);
result = _send_cmd(msd->spi_device, READ_OCR, 0x00, 0x00, response_r3, response);
if (result != RT_EOK)
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[err] It maybe SD2.0 But it is Not response to 2nd CMD58!\r\n");
goto _exit;
}
if ((response[0] & 0xFE) != 0)
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[err] It look 2nd CMD58 as illegal command so it is not SD card!\r\n");
result = -RT_ERROR;
goto _exit;
}
rt_spi_release(msd->spi_device);
OCR = response[1];
OCR = (OCR << 8) + response[2];
OCR = (OCR << 8) + response[3];
OCR = (OCR << 8) + response[4];
MSD_DEBUG("[info] OCR 2nd read is 0x%08X\r\n", OCR);
if ((OCR & 0x40000000) != 0)
{
MSD_DEBUG("[info] It is SD2.0 SDHC Card!!!\r\n");
msd->card_type = MSD_CARD_TYPE_SD_SDHC;
}
else
{
MSD_DEBUG("[info] It is SD2.0 standard capacity Card!!!\r\n");
}
} /* MSD_CARD_TYPE_SD_V2_X */
else
{
MSD_DEBUG("[err] SD card type unkonw!\r\n");
result = -RT_ERROR;
goto _exit;
}
} /* init SD card */
if (msd->card_type == MSD_CARD_TYPE_SD_SDHC)
{
#ifdef RT_USING_DEVICE_OPS
dev->ops = &msd_sdhc_ops;
#else
dev->read = rt_msd_sdhc_read;
dev->write = rt_msd_sdhc_write;
#endif
}
else
{
#ifdef RT_USING_DEVICE_OPS
dev->ops = &msd_ops;
#else
dev->read = rt_msd_read;
dev->write = rt_msd_write;
#endif
}
/* set CRC */
{
rt_spi_release(msd->spi_device);
rt_spi_take(msd->spi_device);
#ifdef MSD_USE_CRC
result = _send_cmd(msd->spi_device, CRC_ON_OFF, 0x01, 0x83, response_r1, response);
#else
result = _send_cmd(msd->spi_device, CRC_ON_OFF, 0x00, 0x91, response_r1, response);
#endif
rt_spi_release(msd->spi_device);
if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
{
MSD_DEBUG("[err] CMD59 CRC_ON_OFF fail! response : 0x%02X\r\n", response[0]);
result = -RT_ERROR;
goto _exit;
}
} /* set CRC */
/* CMD16 SET_BLOCKLEN */
{
rt_spi_release(msd->spi_device);
rt_spi_take(msd->spi_device);
result = _send_cmd(msd->spi_device, SET_BLOCKLEN, SECTOR_SIZE, 0x00, response_r1, response);
rt_spi_release(msd->spi_device);
if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
{
MSD_DEBUG("[err] CMD16 SET_BLOCKLEN fail! response : 0x%02X\r\n", response[0]);
result = -RT_ERROR;
goto _exit;
}
msd->geometry.block_size = SECTOR_SIZE;
msd->geometry.bytes_per_sector = SECTOR_SIZE;
}
/* read CSD */
{
uint8_t CSD_buffer[MSD_CSD_LEN];
rt_spi_take(msd->spi_device);
// result = _send_cmd(msd->spi_device, SEND_CSD, 0x00, 0xAF, response_r1, response);
result = _send_cmd(msd->spi_device, SEND_CSD, 0x00, 0x00, response_r1, response);
if (result != RT_EOK)
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[err] CMD9 SEND_CSD timeout!\r\n");
goto _exit;
}
if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
{
rt_spi_release(msd->spi_device);
MSD_DEBUG("[err] CMD9 SEND_CSD fail! response : 0x%02X\r\n", response[0]);
result = -RT_ERROR;
goto _exit;
}
result = _read_block(msd->spi_device, CSD_buffer, MSD_CSD_LEN);
rt_spi_release(msd->spi_device);
if (result != RT_EOK)
{
MSD_DEBUG("[err] read CSD fail!\r\n");
goto _exit;
}
/* Analyze CSD */
{
uint8_t CSD_STRUCTURE;
uint32_t C_SIZE;
uint32_t card_capacity;
uint8_t tmp8;
uint16_t tmp16;
uint32_t tmp32;
/* get CSD_STRUCTURE */
tmp8 = CSD_buffer[0] & 0xC0; /* 0b11000000 */
CSD_STRUCTURE = tmp8 >> 6;
/* MMC CSD Analyze. */
if (msd->card_type == MSD_CARD_TYPE_MMC)
{
uint8_t C_SIZE_MULT;
uint8_t READ_BL_LEN;
if (CSD_STRUCTURE > 2)
{
MSD_DEBUG("[err] bad CSD Version : %d\r\n", CSD_STRUCTURE);
result = -RT_ERROR;
goto _exit;
}
if (CSD_STRUCTURE == 0)