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