1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2009-04-17     Bernard      first version.
9  * 2010-07-15     aozima       Modify read/write according new block driver interface.
10  * 2012-02-01     aozima       use new RT-Thread SPI drivers.
11  * 2012-04-11     aozima       get max. data transfer rate from CSD[TRAN_SPEED].
12  * 2012-05-21     aozima       update MMC card support.
13  * 2018-03-09     aozima       fixed CSD Version 2.0 sector count calc.
14  */
15 
16 #include <string.h>
17 #include "dev_spi_msd.h"
18 
19 //#define MSD_TRACE
20 
21 #ifdef MSD_TRACE
22     #define MSD_DEBUG(...)         rt_kprintf("[MSD] %d ", rt_tick_get()); rt_kprintf(__VA_ARGS__);
23 #else
24     #define MSD_DEBUG(...)
25 #endif /* #ifdef MSD_TRACE */
26 
27 #define DUMMY                 0xFF
28 
29 #define CARD_NCR_MAX          9
30 
31 #define CARD_NRC              1
32 #define CARD_NCR              1
33 
34 static struct msd_device  _msd_device;
35 
36 /* function define */
37 static rt_bool_t rt_tick_timeout(rt_tick_t tick_start, rt_tick_t tick_long);
38 
39 static rt_err_t MSD_take_owner(struct rt_spi_device *spi_device);
40 
41 static rt_err_t _wait_token(struct rt_spi_device *device, uint8_t token);
42 static rt_err_t _wait_ready(struct rt_spi_device *device);
43 static rt_err_t  rt_msd_init(rt_device_t dev);
44 static rt_err_t  rt_msd_open(rt_device_t dev, rt_uint16_t oflag);
45 static rt_err_t  rt_msd_close(rt_device_t dev);
46 static rt_ssize_t rt_msd_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
47 static rt_ssize_t rt_msd_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
48 static rt_ssize_t rt_msd_sdhc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
49 static rt_ssize_t rt_msd_sdhc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
50 static rt_err_t rt_msd_control(rt_device_t dev, int cmd, void *args);
51 
MSD_take_owner(struct rt_spi_device * spi_device)52 static rt_err_t MSD_take_owner(struct rt_spi_device *spi_device)
53 {
54     rt_err_t result;
55 
56     result = rt_mutex_take(&(spi_device->bus->lock), RT_WAITING_FOREVER);
57     if (result == RT_EOK)
58     {
59         if (spi_device->bus->owner != spi_device)
60         {
61             /* not the same owner as current, re-configure SPI bus */
62             result = spi_device->bus->ops->configure(spi_device, &spi_device->config);
63             if (result == RT_EOK)
64             {
65                 /* set SPI bus owner */
66                 spi_device->bus->owner = spi_device;
67             }
68         }
69     }
70 
71     return result;
72 }
73 
rt_tick_timeout(rt_tick_t tick_start,rt_tick_t tick_long)74 static rt_bool_t rt_tick_timeout(rt_tick_t tick_start, rt_tick_t tick_long)
75 {
76     rt_tick_t tick_end = tick_start + tick_long;
77     rt_tick_t tick_now = rt_tick_get();
78     rt_bool_t result = RT_FALSE;
79 
80     if (tick_end >= tick_start)
81     {
82         if (tick_now >= tick_end)
83         {
84             result = RT_TRUE;
85         }
86         else
87         {
88             result = RT_FALSE;
89         }
90     }
91     else
92     {
93         if ((tick_now < tick_start) && (tick_now >= tick_end))
94         {
95             result = RT_TRUE;
96         }
97         else
98         {
99             result = RT_FALSE;
100         }
101     }
102 
103     return result;
104 }
105 
crc7(const uint8_t * buf,int len)106 static uint8_t crc7(const uint8_t *buf, int len)
107 {
108     unsigned char   i, j, crc, ch, ch2, ch3;
109 
110     crc = 0;
111 
112     for (i = 0; i < len; i ++)
113     {
114         ch = buf[i];
115 
116         for (j = 0; j < 8; j ++, ch <<= 1)
117         {
118             ch2 = (crc & 0x40) ? 1 : 0;
119             ch3 = (ch & 0x80) ? 1 : 0;
120 
121             if (ch2 ^ ch3)
122             {
123                 crc ^= 0x04;
124                 crc <<= 1;
125                 crc |= 0x01;
126             }
127             else
128             {
129                 crc <<= 1;
130             }
131         }
132     }
133 
134     return crc;
135 }
136 
_send_cmd(struct rt_spi_device * device,uint8_t cmd,uint32_t arg,uint8_t crc,response_type type,uint8_t * response)137 static rt_err_t _send_cmd(
138     struct rt_spi_device *device,
139     uint8_t cmd,
140     uint32_t arg,
141     uint8_t crc,
142     response_type type,
143     uint8_t *response
144 )
145 {
146     struct rt_spi_message message;
147     uint8_t cmd_buffer[8];
148     uint8_t recv_buffer[sizeof(cmd_buffer)];
149     uint32_t i;
150 
151     cmd_buffer[0] = DUMMY;
152     cmd_buffer[1] = (cmd | 0x40);
153     cmd_buffer[2] = (uint8_t)(arg >> 24);
154     cmd_buffer[3] = (uint8_t)(arg >> 16);
155     cmd_buffer[4] = (uint8_t)(arg >> 8);
156     cmd_buffer[5] = (uint8_t)(arg);
157 
158     if (crc == 0x00)
159     {
160         crc = crc7(&cmd_buffer[1], 5);
161         crc = (crc << 1) | 0x01;
162     }
163     cmd_buffer[6] = (crc);
164 
165     cmd_buffer[7] = DUMMY;
166 
167     /* initial message */
168     message.send_buf = cmd_buffer;
169     message.recv_buf = recv_buffer;
170     message.length = sizeof(cmd_buffer);
171     message.cs_take = message.cs_release = 0;
172 
173     _wait_ready(device);
174 
175     /* transfer message */
176     device->bus->ops->xfer(device, &message);
177 
178     for (i = CARD_NCR; i < (CARD_NCR_MAX + 1); i++)
179     {
180         uint8_t send = DUMMY;
181 
182         /* initial message */
183         message.send_buf = &send;
184         message.recv_buf = response;
185         message.length = 1;
186         message.cs_take = message.cs_release = 0;
187 
188         /* transfer message */
189         device->bus->ops->xfer(device, &message);
190 
191         if (0 == (response[0] & 0x80))
192         {
193             break;
194         }
195     } /* wait response */
196 
197     if ((CARD_NCR_MAX + 1) == i)
198     {
199         return -RT_ERROR;//fail
200     }
201 
202     //recieve other byte
203     if (type == response_r1)
204     {
205         return RT_EOK;
206     }
207     else if (type == response_r1b)
208     {
209         rt_tick_t tick_start = rt_tick_get();
210         uint8_t recv;
211 
212         while (1)
213         {
214             /* initial message */
215             message.send_buf = RT_NULL;
216             message.recv_buf = &recv;
217             message.length = 1;
218             message.cs_take = message.cs_release = 0;
219 
220             /* transfer message */
221             device->bus->ops->xfer(device, &message);
222 
223             if (recv == DUMMY)
224             {
225                 return RT_EOK;
226             }
227 
228             if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(2000)))
229             {
230                 return -RT_ETIMEOUT;
231             }
232         }
233     }
234     else if (type == response_r2)
235     {
236         /* initial message */
237         /* Prevent non-aligned address access, use recv_buffer to receive data */
238         message.send_buf = RT_NULL;
239         message.recv_buf = recv_buffer;
240         message.length = 1;
241         message.cs_take = message.cs_release = 0;
242 
243         /* transfer message */
244         device->bus->ops->xfer(device, &message);
245         response[1] = recv_buffer[0];
246     }
247     else if ((type == response_r3) || (type == response_r7))
248     {
249         /* initial message */
250         message.send_buf = RT_NULL;
251         message.recv_buf = recv_buffer;
252         message.length = 4;
253         message.cs_take = message.cs_release = 0;
254 
255         /* transfer message */
256         device->bus->ops->xfer(device, &message);
257         response[1] = recv_buffer[0];
258         response[2] = recv_buffer[1];
259         response[3] = recv_buffer[2];
260         response[4] = recv_buffer[3];
261     }
262     else
263     {
264         return -RT_ERROR; // unknow type?
265     }
266 
267     return RT_EOK;
268 }
269 
_wait_token(struct rt_spi_device * device,uint8_t token)270 static rt_err_t _wait_token(struct rt_spi_device *device, uint8_t token)
271 {
272     struct rt_spi_message message;
273     rt_tick_t tick_start;
274     uint8_t send, recv;
275 
276     tick_start = rt_tick_get();
277 
278     /* wati token */
279     /* initial message */
280     send = DUMMY;
281     message.send_buf = &send;
282     message.recv_buf = &recv;
283     message.length = 1;
284     message.cs_take = message.cs_release = 0;
285 
286     while (1)
287     {
288         /* transfer message */
289         device->bus->ops->xfer(device, &message);
290 
291         if (recv == token)
292         {
293             return RT_EOK;
294         }
295 
296         if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_WAIT_TOKEN_TIMES)))
297         {
298             MSD_DEBUG("[err] wait data start token timeout!\r\n");
299             return -RT_ETIMEOUT;
300         }
301     } /* wati token */
302 }
303 
_wait_ready(struct rt_spi_device * device)304 static rt_err_t _wait_ready(struct rt_spi_device *device)
305 {
306     struct rt_spi_message message;
307     rt_tick_t tick_start;
308     uint8_t send, recv;
309 
310     tick_start = rt_tick_get();
311 
312     send = DUMMY;
313     /* initial message */
314     message.send_buf = &send;
315     message.recv_buf = &recv;
316     message.length = 1;
317     message.cs_take = message.cs_release = 0;
318 
319     while (1)
320     {
321         /* transfer message */
322         device->bus->ops->xfer(device, &message);
323 
324         if (recv == DUMMY)
325         {
326             return RT_EOK;
327         }
328 
329         if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(1000)))
330         {
331             MSD_DEBUG("[err] wait ready timeout!\r\n");
332             return -RT_ETIMEOUT;
333         }
334     }
335 }
336 
_read_block(struct rt_spi_device * device,void * buffer,uint32_t block_size)337 static rt_err_t _read_block(struct rt_spi_device *device, void *buffer, uint32_t block_size)
338 {
339     struct rt_spi_message message;
340     rt_err_t result;
341 
342     /* wati token */
343     result = _wait_token(device, MSD_TOKEN_READ_START);
344     if (result != RT_EOK)
345     {
346         return result;
347     }
348 
349     /* read data */
350     {
351         /* initial message */
352         message.send_buf = RT_NULL;
353         message.recv_buf = buffer;
354         message.length = block_size;
355         message.cs_take = message.cs_release = 0;
356 
357         /* transfer message */
358         device->bus->ops->xfer(device, &message);
359     } /* read data */
360 
361     /* get crc */
362     {
363         uint8_t recv_buffer[2];
364 
365         /* initial message */
366         message.send_buf = RT_NULL;
367         message.recv_buf = recv_buffer;
368         message.length = 2;
369         message.cs_take = message.cs_release = 0;
370 
371         /* transfer message */
372         device->bus->ops->xfer(device, &message);
373     } /* get crc */
374 
375     return RT_EOK;
376 }
377 
_write_block(struct rt_spi_device * device,const void * buffer,uint32_t block_size,uint8_t token)378 static rt_err_t _write_block(struct rt_spi_device *device, const void *buffer, uint32_t block_size, uint8_t token)
379 {
380     struct rt_spi_message message;
381     uint8_t send_buffer[16];
382 
383     rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
384     send_buffer[sizeof(send_buffer) - 1] = token;
385 
386     /* send start block token */
387     {
388         /* initial message */
389         message.send_buf = send_buffer;
390         message.recv_buf = RT_NULL;
391         message.length = sizeof(send_buffer);
392         message.cs_take = message.cs_release = 0;
393 
394         /* transfer message */
395         device->bus->ops->xfer(device, &message);
396     }
397 
398     /* send data */
399     {
400         /* initial message */
401         message.send_buf = buffer;
402         message.recv_buf = RT_NULL;
403         message.length = block_size;
404         message.cs_take = message.cs_release = 0;
405 
406         /* transfer message */
407         device->bus->ops->xfer(device, &message);
408     }
409 
410     /* put crc and get data response */
411     {
412         uint8_t recv_buffer[3];
413         uint8_t response;
414 
415         /* initial message */
416         message.send_buf = send_buffer;
417         message.recv_buf = recv_buffer;
418         message.length = sizeof(recv_buffer);
419         message.cs_take = message.cs_release = 0;
420 
421         /* transfer message */
422         device->bus->ops->xfer(device, &message);
423 
424 //        response = 0x0E & recv_buffer[2];
425         response = MSD_GET_DATA_RESPONSE(recv_buffer[2]);
426         if (response != MSD_DATA_OK)
427         {
428             MSD_DEBUG("[err] write block fail! data response : 0x%02X\r\n", response);
429             return -RT_ERROR;
430         }
431     }
432 
433     /* wati ready */
434     return _wait_ready(device);
435 }
436 
437 #ifdef RT_USING_DEVICE_OPS
438 const static struct rt_device_ops msd_ops =
439 {
440     rt_msd_init,
441     rt_msd_open,
442     rt_msd_close,
443     rt_msd_read,
444     rt_msd_write,
445     rt_msd_control
446 };
447 
448 const static struct rt_device_ops msd_sdhc_ops =
449 {
450     rt_msd_init,
451     rt_msd_open,
452     rt_msd_close,
453     rt_msd_sdhc_read,
454     rt_msd_sdhc_write,
455     rt_msd_control
456 };
457 #endif
458 
459 /* RT-Thread Device Driver Interface */
rt_msd_init(rt_device_t dev)460 static rt_err_t rt_msd_init(rt_device_t dev)
461 {
462     struct msd_device *msd = (struct msd_device *)dev;
463     uint8_t response[MSD_RESPONSE_MAX_LEN];
464     rt_err_t result = RT_EOK;
465     rt_tick_t tick_start;
466     uint32_t OCR;
467 
468     if (msd->spi_device == RT_NULL)
469     {
470         MSD_DEBUG("[err] the SPI SD device has no SPI!\r\n");
471         return -RT_EIO;
472     }
473 
474     /* config spi */
475     {
476         struct rt_spi_configuration cfg;
477         cfg.data_width = 8;
478         cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
479         cfg.max_hz = 1000 * 400; /* 400kbit/s */
480         rt_spi_configure(msd->spi_device, &cfg);
481     } /* config spi */
482 
483     /* init SD card */
484     {
485         struct rt_spi_message message;
486 
487         result = MSD_take_owner(msd->spi_device);
488 
489         if (result != RT_EOK)
490         {
491             goto _exit;
492         }
493 
494         rt_spi_release(msd->spi_device);
495 
496         /* The host shall supply power to the card so that the voltage is reached to Vdd_min within 250ms and
497            start to supply at least 74 SD clocks to the SD card with keeping CMD line to high.
498            In case of SPI mode, CS shall be held to high during 74 clock cycles. */
499         {
500             uint8_t send_buffer[100]; /* 100byte > 74 clock */
501 
502             /* initial message */
503             rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
504             message.send_buf = send_buffer;
505             message.recv_buf = RT_NULL;
506             message.length = sizeof(send_buffer);
507             message.cs_take = message.cs_release = 0;
508 
509             /* transfer message */
510             msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
511         } /* send 74 clock */
512 
513         /* Send CMD0 (GO_IDLE_STATE) to put MSD in SPI mode */
514         {
515             tick_start = rt_tick_get();
516 
517             while (1)
518             {
519                 rt_spi_take(msd->spi_device);
520                 result = _send_cmd(msd->spi_device, GO_IDLE_STATE, 0x00, 0x95, response_r1, response);
521                 rt_spi_release(msd->spi_device);
522 
523                 if ((result == RT_EOK) && (response[0] == MSD_IN_IDLE_STATE))
524                 {
525                     break;
526                 }
527 
528                 if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES)))
529                 {
530                     MSD_DEBUG("[err] SD card goto IDLE mode timeout!\r\n");
531                     result = -RT_ETIMEOUT;
532                     goto _exit;
533                 }
534             }
535 
536             MSD_DEBUG("[info] SD card goto IDLE mode OK!\r\n");
537         } /* Send CMD0 (GO_IDLE_STATE) to put MSD in SPI mode */
538 
539         /* CMD8 */
540         {
541             tick_start = rt_tick_get();
542 
543             do
544             {
545                 rt_spi_take(msd->spi_device);
546                 result = _send_cmd(msd->spi_device, SEND_IF_COND, 0x01AA, 0x87, response_r7, response);
547                 rt_spi_release(msd->spi_device);
548 
549                 if (result == RT_EOK)
550                 {
551                     MSD_DEBUG("[info] CMD8 response : 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\r\n",
552                               response[0], response[1], response[2], response[3], response[4]);
553 
554                     if (response[0] & (1 << 2))
555                     {
556                         /* illegal command, SD V1.x or MMC card */
557                         MSD_DEBUG("[info] CMD8 is illegal command.\r\n");
558                         MSD_DEBUG("[info] maybe Ver1.X SD Memory Card or MMC card!\r\n");
559                         msd->card_type = MSD_CARD_TYPE_SD_V1_X;
560                         break;
561                     }
562                     else
563                     {
564                         /* SD V2.0 or later or SDHC or SDXC memory card! */
565                         MSD_DEBUG("[info] Ver2.00 or later or SDHC or SDXC memory card!\r\n");
566                         msd->card_type = MSD_CARD_TYPE_SD_V2_X;
567                     }
568 
569                     if ((0xAA == response[4]) && (0x00 == response[3]))
570                     {
571                         /* SD2.0 not support current voltage */
572                         MSD_DEBUG("[err] VCA = 0, SD2.0 not surpport current operation voltage range\r\n");
573                         result = -RT_ERROR;
574                         goto _exit;
575                     }
576                 }
577                 else
578                 {
579                     if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(200)))
580                     {
581                         MSD_DEBUG("[err] CMD8 SEND_IF_COND timeout!\r\n");
582                         result = -RT_ETIMEOUT;
583                         goto _exit;
584                     }
585                 }
586             }
587             while (0xAA != response[4]);
588         } /* CMD8 */
589 
590         /* Ver1.X SD Memory Card or MMC card */
591         if (msd->card_type == MSD_CARD_TYPE_SD_V1_X)
592         {
593             rt_bool_t is_sd_v1_x = RT_FALSE;
594             rt_tick_t tick_start;
595 
596             /* try SD Ver1.x */
597             while (1)
598             {
599                 rt_spi_take(msd->spi_device);
600 
601                 result = _send_cmd(msd->spi_device, READ_OCR, 0x00, 0x00, response_r3, response);
602                 if (result != RT_EOK)
603                 {
604                     rt_spi_release(msd->spi_device);
605                     MSD_DEBUG("[info] It maybe SD1.x or MMC But it is Not response to CMD58!\r\n");
606                     goto _exit;
607                 }
608 
609                 if (0 != (response[0] & 0xFE))
610                 {
611                     rt_spi_release(msd->spi_device);
612                     MSD_DEBUG("[info] It look CMD58 as illegal command so it is not SD card!\r\n");
613                     break;
614                 }
615                 rt_spi_release(msd->spi_device);
616 
617                 OCR = response[1];
618                 OCR = (OCR << 8) + response[2];
619                 OCR = (OCR << 8) + response[3];
620                 OCR = (OCR << 8) + response[4];
621                 MSD_DEBUG("[info] OCR is 0x%08X\r\n", OCR);
622 
623                 if (0 == (OCR & (0x1 << 15)))
624                 {
625                     MSD_DEBUG(("[err] SD 1.x But not surpport current voltage\r\n"));
626                     result = -RT_ERROR;
627                     goto _exit;
628                 }
629 
630                 /* --Send ACMD41 to make card ready */
631                 tick_start = rt_tick_get();
632 
633                 /* try CMD55 + ACMD41 */
634                 while (1)
635                 {
636                     if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES_ACMD41)))
637                     {
638                         rt_spi_release(msd->spi_device);
639                         MSD_DEBUG("[info] try CMD55 + ACMD41 timeout! mabey MMC card!\r\n");
640                         break;
641                     }
642 
643                     rt_spi_take(msd->spi_device);
644 
645                     /* CMD55 APP_CMD */
646                     result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x00, response_r1, response);
647                     if (result != RT_EOK)
648                     {
649                         rt_spi_release(msd->spi_device);
650                         continue;
651                     }
652 
653                     if (0 != (response[0] & 0xFE))
654                     {
655                         rt_spi_release(msd->spi_device);
656                         MSD_DEBUG("[info] Not SD card2 , may be MMC\r\n");
657                         break;
658                     }
659 
660                     /* ACMD41 SD_SEND_OP_COND */
661                     result = _send_cmd(msd->spi_device, SD_SEND_OP_COND, 0x00, 0x00, response_r1, response);
662                     if (result != RT_EOK)
663                     {
664                         rt_spi_release(msd->spi_device);
665                         continue;
666                     }
667 
668                     if (0 != (response[0] & 0xFE))
669                     {
670                         rt_spi_release(msd->spi_device);
671                         MSD_DEBUG("[info] Not SD card4 , may be MMC\r\n");
672                         break;
673                     }
674 
675                     if (0 == (response[0] & 0xFF))
676                     {
677                         rt_spi_release(msd->spi_device);
678                         is_sd_v1_x = RT_TRUE;
679                         MSD_DEBUG("[info] It is Ver1.X SD Memory Card!!!\r\n");
680                         break;
681                     }
682                 } /* try CMD55 + ACMD41 */
683 
684                 break;
685             } /* try SD Ver1.x */
686 
687             /* try MMC */
688             if (is_sd_v1_x != RT_TRUE)
689             {
690                 uint32_t i;
691 
692                 MSD_DEBUG("[info] try MMC card!\r\n");
693                 rt_spi_release(msd->spi_device);
694 
695                 /* send dummy clock */
696                 {
697                     uint8_t send_buffer[100];
698 
699                     /* initial message */
700                     rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
701                     message.send_buf = send_buffer;
702                     message.recv_buf = RT_NULL;
703                     message.length = sizeof(send_buffer);
704                     message.cs_take = message.cs_release = 0;
705 
706                     for (i = 0; i < 10; i++)
707                     {
708                         /* transfer message */
709                         msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
710                     }
711                 } /* send dummy clock */
712 
713                 /* send CMD0 goto IDLE state */
714                 tick_start = rt_tick_get();
715                 while (1)
716                 {
717                     rt_spi_take(msd->spi_device);
718                     result = _send_cmd(msd->spi_device, GO_IDLE_STATE, 0x00, 0x95, response_r1, response);
719                     rt_spi_release(msd->spi_device);
720 
721                     if ((result == RT_EOK) && (response[0] == MSD_IN_IDLE_STATE))
722                     {
723                         break;
724                     }
725 
726                     if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES)))
727                     {
728                         MSD_DEBUG("[err] SD card goto IDLE mode timeout!\r\n");
729                         result = -RT_ETIMEOUT;
730                         goto _exit;
731                     }
732                 } /* send CMD0 goto IDLE stat */
733 
734                 /* send CMD1 */
735                 tick_start = rt_tick_get();
736                 while (1)
737                 {
738                     rt_spi_take(msd->spi_device);
739                     result = _send_cmd(msd->spi_device, SEND_OP_COND, 0x00, 0x00, response_r1, response);
740                     rt_spi_release(msd->spi_device);
741 
742                     if ((result == RT_EOK) && (response[0] == MSD_RESPONSE_NO_ERROR))
743                     {
744                         MSD_DEBUG("[info] It is MMC card!!!\r\n");
745                         msd->card_type = MSD_CARD_TYPE_MMC;
746                         break;
747                     }
748 
749                     if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES)))
750                     {
751                         MSD_DEBUG("[err] SD card goto IDLE mode timeout!\r\n");
752                         result = -RT_ETIMEOUT;
753                         goto _exit;
754                     }
755                 } /* send CMD1 */
756             } /* try MMC */
757         }
758         else if (msd->card_type == MSD_CARD_TYPE_SD_V2_X)
759         {
760             rt_spi_take(msd->spi_device);
761 
762             result = _send_cmd(msd->spi_device, READ_OCR, 0x00, 0x00, response_r3, response);
763             if (result != RT_EOK)
764             {
765                 rt_spi_release(msd->spi_device);
766                 MSD_DEBUG("[err] It maybe SD2.0 But it is Not response to CMD58!\r\n");
767                 goto _exit;
768             }
769 
770             if ((response[0] & 0xFE) != 0)
771             {
772                 rt_spi_release(msd->spi_device);
773                 MSD_DEBUG("[err] It look CMD58 as illegal command so it is not SD card!\r\n");
774                 result = -RT_ERROR;
775                 goto _exit;
776             }
777 
778             rt_spi_release(msd->spi_device);
779 
780             OCR = response[1];
781             OCR = (OCR << 8) + response[2];
782             OCR = (OCR << 8) + response[3];
783             OCR = (OCR << 8) + response[4];
784             MSD_DEBUG("[info] OCR is 0x%08X\r\n", OCR);
785 
786             if (0 == (OCR & (0x1 << 15)))
787             {
788                 MSD_DEBUG(("[err] SD 1.x But not surpport current voltage\r\n"));
789                 result = -RT_ERROR;
790                 goto _exit;
791             }
792 
793             /* --Send ACMD41 to make card ready */
794             tick_start = rt_tick_get();
795 
796             /* try CMD55 + ACMD41 */
797             do
798             {
799                 rt_spi_take(msd->spi_device);
800                 if (rt_tick_timeout(tick_start, rt_tick_from_millisecond(CARD_TRY_TIMES_ACMD41)))
801                 {
802                     rt_spi_release(msd->spi_device);
803                     MSD_DEBUG("[err] SD Ver2.x or later try CMD55 + ACMD41 timeout!\r\n");
804                     result = -RT_ERROR;
805                     goto _exit;
806                 }
807 
808                 /* CMD55 APP_CMD */
809                 result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x65, response_r1, response);
810 //                if((result != RT_EOK) || (response[0] == 0x01))
811                 if (result != RT_EOK)
812                 {
813                     rt_spi_release(msd->spi_device);
814                     continue;
815                 }
816 
817                 if ((response[0] & 0xFE) != 0)
818                 {
819                     rt_spi_release(msd->spi_device);
820                     MSD_DEBUG("[err] Not SD ready!\r\n");
821                     result = -RT_ERROR;
822                     goto _exit;
823                 }
824 
825                 /* ACMD41 SD_SEND_OP_COND */
826                 result = _send_cmd(msd->spi_device, SD_SEND_OP_COND, 0x40000000, 0x77, response_r1, response);
827                 if (result != RT_EOK)
828                 {
829                     rt_spi_release(msd->spi_device);
830                     MSD_DEBUG("[err] ACMD41 fail!\r\n");
831                     result = -RT_ERROR;
832                     goto _exit;
833                 }
834 
835                 if ((response[0] & 0xFE) != 0)
836                 {
837                     rt_spi_release(msd->spi_device);
838                     MSD_DEBUG("[info] Not SD card4 , response : 0x%02X\r\n", response[0]);
839 //                    break;
840                 }
841             }
842             while (response[0] != MSD_RESPONSE_NO_ERROR);
843             rt_spi_release(msd->spi_device);
844             /* try CMD55 + ACMD41 */
845 
846             /* --Read OCR again */
847             rt_spi_take(msd->spi_device);
848             result = _send_cmd(msd->spi_device, READ_OCR, 0x00, 0x00, response_r3, response);
849             if (result != RT_EOK)
850             {
851                 rt_spi_release(msd->spi_device);
852                 MSD_DEBUG("[err] It maybe SD2.0 But it is Not response to 2nd CMD58!\r\n");
853                 goto _exit;
854             }
855 
856             if ((response[0] & 0xFE) != 0)
857             {
858                 rt_spi_release(msd->spi_device);
859                 MSD_DEBUG("[err] It look 2nd CMD58 as illegal command so it is not SD card!\r\n");
860                 result = -RT_ERROR;
861                 goto _exit;
862             }
863             rt_spi_release(msd->spi_device);
864 
865             OCR = response[1];
866             OCR = (OCR << 8) + response[2];
867             OCR = (OCR << 8) + response[3];
868             OCR = (OCR << 8) + response[4];
869             MSD_DEBUG("[info] OCR 2nd read is 0x%08X\r\n", OCR);
870 
871             if ((OCR & 0x40000000) != 0)
872             {
873                 MSD_DEBUG("[info] It is SD2.0 SDHC Card!!!\r\n");
874                 msd->card_type = MSD_CARD_TYPE_SD_SDHC;
875             }
876             else
877             {
878                 MSD_DEBUG("[info] It is SD2.0 standard capacity Card!!!\r\n");
879             }
880         } /* MSD_CARD_TYPE_SD_V2_X */
881         else
882         {
883             MSD_DEBUG("[err] SD card type unkonw!\r\n");
884             result = -RT_ERROR;
885             goto _exit;
886         }
887     } /* init SD card */
888 
889     if (msd->card_type == MSD_CARD_TYPE_SD_SDHC)
890     {
891 #ifdef RT_USING_DEVICE_OPS
892         dev->ops   = &msd_sdhc_ops;
893 #else
894         dev->read  = rt_msd_sdhc_read;
895         dev->write = rt_msd_sdhc_write;
896 #endif
897     }
898     else
899     {
900 #ifdef RT_USING_DEVICE_OPS
901         dev->ops   = &msd_ops;
902 #else
903         dev->read  = rt_msd_read;
904         dev->write = rt_msd_write;
905 #endif
906     }
907 
908     /* set CRC */
909     {
910         rt_spi_release(msd->spi_device);
911         rt_spi_take(msd->spi_device);
912 #ifdef MSD_USE_CRC
913         result = _send_cmd(msd->spi_device, CRC_ON_OFF, 0x01, 0x83, response_r1, response);
914 #else
915         result = _send_cmd(msd->spi_device, CRC_ON_OFF, 0x00, 0x91, response_r1, response);
916 #endif
917         rt_spi_release(msd->spi_device);
918         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
919         {
920             MSD_DEBUG("[err] CMD59 CRC_ON_OFF fail! response : 0x%02X\r\n", response[0]);
921             result = -RT_ERROR;
922             goto _exit;
923         }
924     } /* set CRC */
925 
926     /* CMD16 SET_BLOCKLEN */
927     {
928         rt_spi_release(msd->spi_device);
929         rt_spi_take(msd->spi_device);
930         result = _send_cmd(msd->spi_device, SET_BLOCKLEN, SECTOR_SIZE, 0x00, response_r1, response);
931         rt_spi_release(msd->spi_device);
932         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
933         {
934             MSD_DEBUG("[err] CMD16 SET_BLOCKLEN fail! response : 0x%02X\r\n", response[0]);
935             result = -RT_ERROR;
936             goto _exit;
937         }
938         msd->geometry.block_size = SECTOR_SIZE;
939         msd->geometry.bytes_per_sector = SECTOR_SIZE;
940     }
941 
942     /* read CSD */
943     {
944         uint8_t CSD_buffer[MSD_CSD_LEN];
945 
946         rt_spi_take(msd->spi_device);
947 //        result = _send_cmd(msd->spi_device, SEND_CSD, 0x00, 0xAF, response_r1, response);
948         result = _send_cmd(msd->spi_device, SEND_CSD, 0x00, 0x00, response_r1, response);
949 
950         if (result != RT_EOK)
951         {
952             rt_spi_release(msd->spi_device);
953             MSD_DEBUG("[err] CMD9 SEND_CSD timeout!\r\n");
954             goto _exit;
955         }
956 
957         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
958         {
959             rt_spi_release(msd->spi_device);
960             MSD_DEBUG("[err] CMD9 SEND_CSD fail! response : 0x%02X\r\n", response[0]);
961             result = -RT_ERROR;
962             goto _exit;
963         }
964 
965         result = _read_block(msd->spi_device, CSD_buffer, MSD_CSD_LEN);
966         rt_spi_release(msd->spi_device);
967         if (result != RT_EOK)
968         {
969             MSD_DEBUG("[err] read CSD fail!\r\n");
970             goto _exit;
971         }
972 
973         /* Analyze CSD */
974         {
975             uint8_t  CSD_STRUCTURE;
976             uint32_t C_SIZE;
977             uint32_t card_capacity;
978 
979             uint8_t  tmp8;
980             uint16_t tmp16;
981             uint32_t tmp32;
982 
983             /* get CSD_STRUCTURE */
984             tmp8 = CSD_buffer[0] & 0xC0; /* 0b11000000 */
985             CSD_STRUCTURE = tmp8 >> 6;
986 
987             /* MMC CSD Analyze. */
988             if (msd->card_type == MSD_CARD_TYPE_MMC)
989             {
990                 uint8_t C_SIZE_MULT;
991                 uint8_t READ_BL_LEN;
992 
993                 if (CSD_STRUCTURE > 2)
994                 {
995                     MSD_DEBUG("[err] bad CSD Version : %d\r\n", CSD_STRUCTURE);
996                     result = -RT_ERROR;
997                     goto _exit;
998                 }
999 
1000                 if (CSD_STRUCTURE == 0)
1001                 {
1002                     MSD_DEBUG("[info] CSD version No. 1.0\r\n");
1003                 }
1004                 else if (CSD_STRUCTURE == 1)
1005                 {
1006                     MSD_DEBUG("[info] CSD version No. 1.1\r\n");
1007                 }
1008                 else if (CSD_STRUCTURE == 2)
1009                 {
1010                     MSD_DEBUG("[info] CSD version No. 1.2\r\n");
1011                 }
1012 
1013                 /* get TRAN_SPEED 8bit [103:96] */
1014                 tmp8 = CSD_buffer[3];
1015                 tmp8 &= 0x03; /* [2:0] transfer rate unit.*/
1016                 if (tmp8 == 0)
1017                 {
1018                     msd->max_clock = 100 * 1000; /* 0=100kbit/s. */
1019                 }
1020                 else if (tmp8 == 1)
1021                 {
1022                     msd->max_clock = 1 * 1000 * 1000; /* 1=1Mbit/s. */
1023                 }
1024                 else if (tmp8 == 2)
1025                 {
1026                     msd->max_clock = 10 * 1000 * 1000; /* 2=10Mbit/s. */
1027                 }
1028                 else if (tmp8 == 3)
1029                 {
1030                     msd->max_clock = 100 * 1000 * 1000; /* 3=100Mbit/s. */
1031                 }
1032                 if (tmp8 == 0)
1033                 {
1034                     MSD_DEBUG("[info] TRAN_SPEED: 0x%02X, %dkbit/s.\r\n", tmp8, msd->max_clock / 1000);
1035                 }
1036                 else
1037                 {
1038                     MSD_DEBUG("[info] TRAN_SPEED: 0x%02X, %dMbit/s.\r\n", tmp8, msd->max_clock / 1000 / 1000);
1039                 }
1040 
1041                 /* get READ_BL_LEN 4bit [83:80] */
1042                 tmp8 = CSD_buffer[5] & 0x0F; /* 0b00001111; */
1043                 READ_BL_LEN = tmp8;          /* 4 bit */
1044                 MSD_DEBUG("[info] CSD : READ_BL_LEN : %d %dbyte\r\n", READ_BL_LEN, (1 << READ_BL_LEN));
1045 
1046                 /* get C_SIZE 12bit [73:62] */
1047                 tmp16 = CSD_buffer[6] & 0x03; /* get [73:72] 0b00000011 */
1048                 tmp16 = tmp16 << 8;
1049                 tmp16 += CSD_buffer[7];       /* get [71:64] */
1050                 tmp16 = tmp16 << 2;
1051                 tmp8 = CSD_buffer[8] & 0xC0;  /* get [63:62] 0b11000000 */
1052                 tmp8 = tmp8 >> 6;
1053                 tmp16 = tmp16 + tmp8;
1054                 C_SIZE = tmp16;             //12 bit
1055                 MSD_DEBUG("[info] CSD : C_SIZE : %d\r\n", C_SIZE);
1056 
1057                 /* get C_SIZE_MULT 3bit [49:47] */
1058                 tmp8 = CSD_buffer[9] & 0x03;//0b00000011;
1059                 tmp8 = tmp8 << 1;
1060                 tmp8 = tmp8 + ((CSD_buffer[10] & 0x80/*0b10000000*/) >> 7);
1061                 C_SIZE_MULT = tmp8;         // 3 bit
1062                 MSD_DEBUG("[info] CSD : C_SIZE_MULT : %d\r\n", C_SIZE_MULT);
1063 
1064                 /* memory capacity = BLOCKNR * BLOCK_LEN */
1065                 /* BLOCKNR = (C_SIZE+1) * MULT */
1066                 /* MULT = 2^(C_SIZE_MULT+2) */
1067                 /* BLOCK_LEN = 2^READ_BL_LEN */
1068                 card_capacity = (1 << READ_BL_LEN) * ((C_SIZE + 1) * (1 << (C_SIZE_MULT + 2)));
1069                 msd->geometry.sector_count = card_capacity / msd->geometry.bytes_per_sector;
1070                 MSD_DEBUG("[info] card capacity : %d Mbyte\r\n", card_capacity / (1024 * 1024));
1071             }
1072             else /* SD CSD Analyze. */
1073             {
1074                 if (CSD_STRUCTURE == 0)
1075                 {
1076                     uint8_t C_SIZE_MULT;
1077                     uint8_t READ_BL_LEN;
1078 
1079                     MSD_DEBUG("[info] CSD Version 1.0\r\n");
1080 
1081                     /* get TRAN_SPEED 8bit [103:96] */
1082                     tmp8 = CSD_buffer[3];
1083                     if (tmp8 == 0x32)
1084                     {
1085                         msd->max_clock = 1000 * 1000 * 10; /* 10Mbit/s. */
1086                     }
1087                     else if (tmp8 == 0x5A)
1088                     {
1089                         msd->max_clock = 1000 * 1000 * 50; /* 50Mbit/s. */
1090                     }
1091                     else
1092                     {
1093                         msd->max_clock = 1000 * 1000 * 1; /* 1Mbit/s default. */
1094                     }
1095                     MSD_DEBUG("[info] TRAN_SPEED: 0x%02X, %dMbit/s.\r\n", tmp8, msd->max_clock / 1000 / 1000);
1096 
1097                     /* get READ_BL_LEN 4bit [83:80] */
1098                     tmp8 = CSD_buffer[5] & 0x0F; /* 0b00001111; */
1099                     READ_BL_LEN = tmp8;          /* 4 bit */
1100                     MSD_DEBUG("[info] CSD : READ_BL_LEN : %d %dbyte\r\n", READ_BL_LEN, (1 << READ_BL_LEN));
1101 
1102                     /* get C_SIZE 12bit [73:62] */
1103                     tmp16 = CSD_buffer[6] & 0x03; /* get [73:72] 0b00000011 */
1104                     tmp16 = tmp16 << 8;
1105                     tmp16 += CSD_buffer[7];       /* get [71:64] */
1106                     tmp16 = tmp16 << 2;
1107                     tmp8 = CSD_buffer[8] & 0xC0;  /* get [63:62] 0b11000000 */
1108                     tmp8 = tmp8 >> 6;
1109                     tmp16 = tmp16 + tmp8;
1110                     C_SIZE = tmp16;             //12 bit
1111                     MSD_DEBUG("[info] CSD : C_SIZE : %d\r\n", C_SIZE);
1112 
1113                     /* get C_SIZE_MULT 3bit [49:47] */
1114                     tmp8 = CSD_buffer[9] & 0x03;//0b00000011;
1115                     tmp8 = tmp8 << 1;
1116                     tmp8 = tmp8 + ((CSD_buffer[10] & 0x80/*0b10000000*/) >> 7);
1117                     C_SIZE_MULT = tmp8;         // 3 bit
1118                     MSD_DEBUG("[info] CSD : C_SIZE_MULT : %d\r\n", C_SIZE_MULT);
1119 
1120                     /* memory capacity = BLOCKNR * BLOCK_LEN */
1121                     /* BLOCKNR = (C_SIZE+1) * MULT */
1122                     /* MULT = 2^(C_SIZE_MULT+2) */
1123                     /* BLOCK_LEN = 2^READ_BL_LEN */
1124                     card_capacity = (1 << READ_BL_LEN) * ((C_SIZE + 1) * (1 << (C_SIZE_MULT + 2)));
1125                     msd->geometry.sector_count = card_capacity / msd->geometry.bytes_per_sector;
1126                     MSD_DEBUG("[info] card capacity : %d Mbyte\r\n", card_capacity / (1024 * 1024));
1127                 }
1128                 else if (CSD_STRUCTURE == 1)
1129                 {
1130                     MSD_DEBUG("[info] CSD Version 2.0\r\n");
1131 
1132                     /* get TRAN_SPEED 8bit [103:96] */
1133                     tmp8 = CSD_buffer[3];
1134                     if (tmp8 == 0x32)
1135                     {
1136                         msd->max_clock = 1000 * 1000 * 10; /* 10Mbit/s. */
1137                     }
1138                     else if (tmp8 == 0x5A)
1139                     {
1140                         msd->max_clock = 1000 * 1000 * 50; /* 50Mbit/s. */
1141                     }
1142                     else if (tmp8 == 0x0B)
1143                     {
1144                         msd->max_clock = 1000 * 1000 * 100; /* 100Mbit/s. */
1145                         /* UHS50 Card sets TRAN_SPEED to 0Bh (100Mbit/sec), */
1146                         /* for both SDR50 and DDR50 modes. */
1147                     }
1148                     else if (tmp8 == 0x2B)
1149                     {
1150                         msd->max_clock = 1000 * 1000 * 200; /* 200Mbit/s. */
1151                         /* UHS104 Card sets TRAN_SPEED to 2Bh (200Mbit/sec). */
1152                     }
1153                     else
1154                     {
1155                         msd->max_clock = 1000 * 1000 * 1; /* 1Mbit/s default. */
1156                     }
1157                     MSD_DEBUG("[info] TRAN_SPEED: 0x%02X, %dMbit/s.\r\n", tmp8, msd->max_clock / 1000 / 1000);
1158 
1159                     /* get C_SIZE 22bit [69:48] */
1160                     tmp32 = CSD_buffer[7] & 0x3F; /* 0b00111111 */
1161                     tmp32 = tmp32 << 8;
1162                     tmp32 += CSD_buffer[8];
1163                     tmp32 = tmp32 << 8;
1164                     tmp32 += CSD_buffer[9];
1165                     C_SIZE = tmp32;
1166                     MSD_DEBUG("[info] CSD : C_SIZE : %d\r\n", C_SIZE);
1167 
1168                     /* memory capacity = (C_SIZE+1) * 512K byte */
1169                     card_capacity = (C_SIZE + 1) / 2; /* unit : Mbyte */
1170                     msd->geometry.sector_count = (C_SIZE + 1) * 1024; /* 512KB = 1024sector */
1171                     MSD_DEBUG("[info] card capacity : %d.%d Gbyte\r\n", card_capacity / 1024, (card_capacity % 1024) * 100 / 1024);
1172                     MSD_DEBUG("[info] sector_count : %d\r\n", msd->geometry.sector_count);
1173                 }
1174                 else
1175                 {
1176                     MSD_DEBUG("[err] bad CSD Version : %d\r\n", CSD_STRUCTURE);
1177                     result = -RT_ERROR;
1178                     goto _exit;
1179                 }
1180             } /* SD CSD Analyze. */
1181         } /* Analyze CSD */
1182 
1183     } /* read CSD */
1184 
1185     /* config spi to high speed */
1186     {
1187         struct rt_spi_configuration cfg;
1188         cfg.data_width = 8;
1189         cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
1190         cfg.max_hz = msd->max_clock;
1191         rt_spi_configure(msd->spi_device, &cfg);
1192     } /* config spi */
1193 
1194 _exit:
1195     rt_spi_release(msd->spi_device);
1196     rt_mutex_release(&(msd->spi_device->bus->lock));
1197     return result;
1198 }
1199 
rt_msd_open(rt_device_t dev,rt_uint16_t oflag)1200 static rt_err_t rt_msd_open(rt_device_t dev, rt_uint16_t oflag)
1201 {
1202 //    struct msd_device * msd = (struct msd_device *)dev;
1203     return RT_EOK;
1204 }
1205 
rt_msd_close(rt_device_t dev)1206 static rt_err_t rt_msd_close(rt_device_t dev)
1207 {
1208 //    struct msd_device * msd = (struct msd_device *)dev;
1209     return RT_EOK;
1210 }
1211 
rt_msd_read(rt_device_t dev,rt_off_t pos,void * buffer,rt_size_t size)1212 static rt_ssize_t rt_msd_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
1213 {
1214     struct msd_device *msd = (struct msd_device *)dev;
1215     uint8_t response[MSD_RESPONSE_MAX_LEN];
1216     rt_err_t result = RT_EOK;
1217 
1218     result = MSD_take_owner(msd->spi_device);
1219 
1220     if (result != RT_EOK)
1221     {
1222         goto _exit;
1223     }
1224 
1225     /* SINGLE_BLOCK? */
1226     if (size == 1)
1227     {
1228         rt_spi_take(msd->spi_device);
1229 
1230         result = _send_cmd(msd->spi_device, READ_SINGLE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
1231         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1232         {
1233             MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
1234             size = 0;
1235             goto _exit;
1236         }
1237 
1238         result = _read_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector);
1239         if (result != RT_EOK)
1240         {
1241             MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
1242             size = 0;
1243         }
1244     }
1245     else if (size > 1)
1246     {
1247         uint32_t i;
1248 
1249         rt_spi_take(msd->spi_device);
1250 
1251         result = _send_cmd(msd->spi_device, READ_MULTIPLE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
1252         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1253         {
1254             MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
1255             size = 0;
1256             goto _exit;
1257         }
1258 
1259         for (i = 0; i < size; i++)
1260         {
1261             result = _read_block(msd->spi_device,
1262                                  (uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
1263                                  msd->geometry.bytes_per_sector);
1264             if (result != RT_EOK)
1265             {
1266                 MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
1267                 size = i;
1268                 break;
1269             }
1270         }
1271 
1272         /* send CMD12 stop transfer */
1273         result = _send_cmd(msd->spi_device, STOP_TRANSMISSION, 0x00, 0x00, response_r1b, response);
1274         if (result != RT_EOK)
1275         {
1276             MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK, send stop token fail!\r\n");
1277         }
1278     } /* READ_MULTIPLE_BLOCK */
1279 
1280 _exit:
1281     /* release and exit */
1282     rt_spi_release(msd->spi_device);
1283     rt_mutex_release(&(msd->spi_device->bus->lock));
1284 
1285     return size;
1286 }
1287 
rt_msd_sdhc_read(rt_device_t dev,rt_off_t pos,void * buffer,rt_size_t size)1288 static rt_ssize_t rt_msd_sdhc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
1289 {
1290     struct msd_device *msd = (struct msd_device *)dev;
1291     uint8_t response[MSD_RESPONSE_MAX_LEN];
1292     rt_err_t result = RT_EOK;
1293 
1294     result = MSD_take_owner(msd->spi_device);
1295 
1296     if (result != RT_EOK)
1297     {
1298         goto _exit;
1299     }
1300 
1301     /* SINGLE_BLOCK? */
1302     if (size == 1)
1303     {
1304         rt_spi_take(msd->spi_device);
1305 
1306         result = _send_cmd(msd->spi_device, READ_SINGLE_BLOCK, pos, 0x00, response_r1, response);
1307         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1308         {
1309             MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
1310             size = 0;
1311             goto _exit;
1312         }
1313 
1314         result = _read_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector);
1315         if (result != RT_EOK)
1316         {
1317             MSD_DEBUG("[err] read SINGLE_BLOCK #%d fail!\r\n", pos);
1318             size = 0;
1319         }
1320     }
1321     else if (size > 1)
1322     {
1323         uint32_t i;
1324 
1325         rt_spi_take(msd->spi_device);
1326 
1327         result = _send_cmd(msd->spi_device, READ_MULTIPLE_BLOCK, pos, 0x00, response_r1, response);
1328         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1329         {
1330             MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
1331             size = 0;
1332             goto _exit;
1333         }
1334 
1335         for (i = 0; i < size; i++)
1336         {
1337             result = _read_block(msd->spi_device,
1338                                  (uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
1339                                  msd->geometry.bytes_per_sector);
1340             if (result != RT_EOK)
1341             {
1342                 MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK #%d fail!\r\n", pos);
1343                 size = i;
1344                 break;
1345             }
1346         }
1347 
1348         /* send CMD12 stop transfer */
1349         result = _send_cmd(msd->spi_device, STOP_TRANSMISSION, 0x00, 0x00, response_r1b, response);
1350         if (result != RT_EOK)
1351         {
1352             MSD_DEBUG("[err] read READ_MULTIPLE_BLOCK, send stop token fail!\r\n");
1353         }
1354     } /* READ_MULTIPLE_BLOCK */
1355 
1356 _exit:
1357     /* release and exit */
1358     rt_spi_release(msd->spi_device);
1359     rt_mutex_release(&(msd->spi_device->bus->lock));
1360 
1361     return size;
1362 }
1363 
rt_msd_write(rt_device_t dev,rt_off_t pos,const void * buffer,rt_size_t size)1364 static rt_ssize_t rt_msd_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
1365 {
1366     struct msd_device *msd = (struct msd_device *)dev;
1367     uint8_t response[MSD_RESPONSE_MAX_LEN];
1368     rt_err_t result;
1369 
1370     result = MSD_take_owner(msd->spi_device);
1371 
1372     if (result != RT_EOK)
1373     {
1374         MSD_DEBUG("[err] get SPI owner fail!\r\n");
1375         goto _exit;
1376     }
1377 
1378 
1379     /* SINGLE_BLOCK? */
1380     if (size == 1)
1381     {
1382         rt_spi_take(msd->spi_device);
1383         result = _send_cmd(msd->spi_device, WRITE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
1384         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1385         {
1386             MSD_DEBUG("[err] CMD WRITE_BLOCK fail!\r\n");
1387             size = 0;
1388             goto _exit;
1389         }
1390 
1391         result = _write_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector, MSD_TOKEN_WRITE_SINGLE_START);
1392         if (result != RT_EOK)
1393         {
1394             MSD_DEBUG("[err] write SINGLE_BLOCK #%d fail!\r\n", pos);
1395             size = 0;
1396         }
1397     }
1398     else if (size > 1)
1399     {
1400         struct rt_spi_message message;
1401         uint32_t i;
1402 
1403         rt_spi_take(msd->spi_device);
1404 
1405 #ifdef MSD_USE_PRE_ERASED
1406         if (msd->card_type != MSD_CARD_TYPE_MMC)
1407         {
1408             /* CMD55 APP_CMD */
1409             result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x00, response_r1, response);
1410             if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1411             {
1412                 MSD_DEBUG("[err] CMD55 APP_CMD fail!\r\n");
1413                 size = 0;
1414                 goto _exit;
1415             }
1416 
1417             /* ACMD23 Pre-erased */
1418             result = _send_cmd(msd->spi_device, SET_WR_BLK_ERASE_COUNT, size, 0x00, response_r1, response);
1419             if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1420             {
1421                 MSD_DEBUG("[err] ACMD23 SET_BLOCK_COUNT fail!\r\n");
1422                 size = 0;
1423                 goto _exit;
1424             }
1425         }
1426 #endif
1427 
1428         result = _send_cmd(msd->spi_device, WRITE_MULTIPLE_BLOCK, pos * msd->geometry.bytes_per_sector, 0x00, response_r1, response);
1429         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1430         {
1431             MSD_DEBUG("[err] CMD WRITE_MULTIPLE_BLOCK fail!\r\n");
1432             size = 0;
1433             goto _exit;
1434         }
1435 
1436         /* write all block */
1437         for (i = 0; i < size; i++)
1438         {
1439             result = _write_block(msd->spi_device,
1440                                   (const uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
1441                                   msd->geometry.bytes_per_sector,
1442                                   MSD_TOKEN_WRITE_MULTIPLE_START);
1443             if (result != RT_EOK)
1444             {
1445                 MSD_DEBUG("[err] write SINGLE_BLOCK #%d fail!\r\n", pos);
1446                 size = i;
1447                 break;
1448             }
1449         } /* write all block */
1450 
1451         /* send stop token */
1452         {
1453             uint8_t send_buffer[18];
1454 
1455             rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
1456             send_buffer[sizeof(send_buffer) - 1] = MSD_TOKEN_WRITE_MULTIPLE_STOP;
1457 
1458             /* initial message */
1459             message.send_buf = send_buffer;
1460             message.recv_buf = RT_NULL;
1461             message.length = sizeof(send_buffer);
1462             message.cs_take = message.cs_release = 0;
1463 
1464             /* transfer message */
1465             msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
1466         }
1467 
1468         /* wait ready */
1469         result = _wait_ready(msd->spi_device);
1470         if (result != RT_EOK)
1471         {
1472             MSD_DEBUG("[warning] wait WRITE_MULTIPLE_BLOCK stop token ready timeout!\r\n");
1473         }
1474     } /* size > 1 */
1475 
1476 _exit:
1477     /* release and exit */
1478     rt_spi_release(msd->spi_device);
1479     rt_mutex_release(&(msd->spi_device->bus->lock));
1480 
1481     return size;
1482 }
1483 
rt_msd_sdhc_write(rt_device_t dev,rt_off_t pos,const void * buffer,rt_size_t size)1484 static rt_ssize_t rt_msd_sdhc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
1485 {
1486     struct msd_device *msd = (struct msd_device *)dev;
1487     uint8_t response[MSD_RESPONSE_MAX_LEN];
1488     rt_err_t result;
1489 
1490     result = MSD_take_owner(msd->spi_device);
1491 
1492     if (result != RT_EOK)
1493     {
1494         goto _exit;
1495     }
1496 
1497     /* SINGLE_BLOCK? */
1498     if (size == 1)
1499     {
1500         rt_spi_take(msd->spi_device);
1501         result = _send_cmd(msd->spi_device, WRITE_BLOCK, pos, 0x00, response_r1, response);
1502         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1503         {
1504             MSD_DEBUG("[err] CMD WRITE_BLOCK fail!\r\n");
1505             size = 0;
1506             goto _exit;
1507         }
1508 
1509         result = _write_block(msd->spi_device, buffer, msd->geometry.bytes_per_sector, MSD_TOKEN_WRITE_SINGLE_START);
1510         if (result != RT_EOK)
1511         {
1512             MSD_DEBUG("[err] write SINGLE_BLOCK #%d fail!\r\n", pos);
1513             size = 0;
1514         }
1515     }
1516     else if (size > 1)
1517     {
1518         struct rt_spi_message message;
1519         uint32_t i;
1520 
1521         rt_spi_take(msd->spi_device);
1522 
1523 #ifdef MSD_USE_PRE_ERASED
1524         /* CMD55 APP_CMD */
1525         result = _send_cmd(msd->spi_device, APP_CMD, 0x00, 0x00, response_r1, response);
1526         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1527         {
1528             MSD_DEBUG("[err] CMD55 APP_CMD fail!\r\n");
1529             size = 0;
1530             goto _exit;
1531         }
1532 
1533         /* ACMD23 Pre-erased */
1534         result = _send_cmd(msd->spi_device, SET_WR_BLK_ERASE_COUNT, size, 0x00, response_r1, response);
1535         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1536         {
1537             MSD_DEBUG("[err] ACMD23 SET_BLOCK_COUNT fail!\r\n");
1538             size = 0;
1539             goto _exit;
1540         }
1541 #endif
1542 
1543         result = _send_cmd(msd->spi_device, WRITE_MULTIPLE_BLOCK, pos, 0x00, response_r1, response);
1544         if ((result != RT_EOK) || (response[0] != MSD_RESPONSE_NO_ERROR))
1545         {
1546             MSD_DEBUG("[err] CMD WRITE_MULTIPLE_BLOCK fail!\r\n");
1547             size = 0;
1548             goto _exit;
1549         }
1550 
1551         /* write all block */
1552         for (i = 0; i < size; i++)
1553         {
1554             result = _write_block(msd->spi_device,
1555                                   (const uint8_t *)buffer + msd->geometry.bytes_per_sector * i,
1556                                   msd->geometry.bytes_per_sector,
1557                                   MSD_TOKEN_WRITE_MULTIPLE_START);
1558             if (result != RT_EOK)
1559             {
1560                 MSD_DEBUG("[err] write MULTIPLE_BLOCK #%d fail!\r\n", pos);
1561                 size = i;
1562                 break;
1563             }
1564         } /* write all block */
1565 
1566         /* send stop token */
1567         {
1568             uint8_t send_buffer[18];
1569 
1570             rt_memset(send_buffer, DUMMY, sizeof(send_buffer));
1571             send_buffer[sizeof(send_buffer) - 1] = MSD_TOKEN_WRITE_MULTIPLE_STOP;
1572 
1573             /* initial message */
1574             message.send_buf = send_buffer;
1575             message.recv_buf = RT_NULL;
1576             message.length = sizeof(send_buffer);
1577             message.cs_take = message.cs_release = 0;
1578 
1579             /* transfer message */
1580             msd->spi_device->bus->ops->xfer(msd->spi_device, &message);
1581         }
1582 
1583         result = _wait_ready(msd->spi_device);
1584         if (result != RT_EOK)
1585         {
1586             MSD_DEBUG("[warning] wait WRITE_MULTIPLE_BLOCK stop token ready timeout!\r\n");
1587         }
1588     } /* size > 1 */
1589 
1590 _exit:
1591     /* release and exit */
1592     rt_spi_release(msd->spi_device);
1593     rt_mutex_release(&(msd->spi_device->bus->lock));
1594 
1595     return size;
1596 }
1597 
rt_msd_control(rt_device_t dev,int cmd,void * args)1598 static rt_err_t rt_msd_control(rt_device_t dev, int cmd, void *args)
1599 {
1600     struct msd_device *msd = (struct msd_device *)dev;
1601 
1602     RT_ASSERT(dev != RT_NULL);
1603 
1604     if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
1605     {
1606         struct rt_device_blk_geometry *geometry;
1607 
1608         geometry = (struct rt_device_blk_geometry *)args;
1609         if (geometry == RT_NULL) return -RT_ERROR;
1610 
1611         geometry->bytes_per_sector = msd->geometry.bytes_per_sector;
1612         geometry->block_size = msd->geometry.block_size;
1613         geometry->sector_count = msd->geometry.sector_count;
1614     }
1615 
1616     return RT_EOK;
1617 }
1618 
msd_init(const char * sd_device_name,const char * spi_device_name)1619 rt_err_t msd_init(const char *sd_device_name, const char *spi_device_name)
1620 {
1621     rt_err_t result = RT_EOK;
1622     struct rt_spi_device *spi_device;
1623 
1624     spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
1625     if (spi_device == RT_NULL)
1626     {
1627         MSD_DEBUG("spi device %s not found!\r\n", spi_device_name);
1628         return -RT_ENOSYS;
1629     }
1630     rt_memset(&_msd_device, 0, sizeof(_msd_device));
1631     _msd_device.spi_device = spi_device;
1632 
1633     /* register sdcard device */
1634     _msd_device.parent.type    = RT_Device_Class_Block;
1635 
1636     _msd_device.geometry.bytes_per_sector = 0;
1637     _msd_device.geometry.sector_count = 0;
1638     _msd_device.geometry.block_size = 0;
1639 
1640 #ifdef RT_USING_DEVICE_OPS
1641     _msd_device.parent.ops     = &msd_ops;
1642 #else
1643     _msd_device.parent.init    = rt_msd_init;
1644     _msd_device.parent.open    = rt_msd_open;
1645     _msd_device.parent.close   = rt_msd_close;
1646     _msd_device.parent.read    = RT_NULL;
1647     _msd_device.parent.write   = RT_NULL;
1648     _msd_device.parent.control = rt_msd_control;
1649 #endif
1650 
1651     /* no private, no callback */
1652     _msd_device.parent.user_data = RT_NULL;
1653     _msd_device.parent.rx_indicate = RT_NULL;
1654     _msd_device.parent.tx_complete = RT_NULL;
1655 
1656     result = rt_device_register(&_msd_device.parent, sd_device_name,
1657                                 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
1658 
1659     return result;
1660 }
1661