1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015, Freescale Semiconductor, Inc.
4  * Copyright 2016 NXP
5  * All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted (subject to the limitations in the disclaimer below) provided
10  *  that the following conditions are met:
11  *
12  * o Redistributions of source code must retain the above copyright notice, this list
13  *   of conditions and the following disclaimer.
14  *
15  * o Redistributions in binary form must reproduce the above copyright notice, this
16  *   list of conditions and the following disclaimer in the documentation and/or
17  *   other materials provided with the distribution.
18  *
19  * o Neither the name of the copyright holder nor the names of its
20  *   contributors may be used to endorse or promote products derived from this
21  *   software without specific prior written permission.
22  *
23  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifdef CONFIG_AOS_FATFS_SUPPORT_MMC
37 #include <assert.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <aos/kernel.h>
41 
42 #include "sd_disk/sd_disk.h"
43 #include "aos/hal/sd.h"
44 
45 static sd_dev_t sdmmc;
SDMMC_initialize()46 DSTATUS  SDMMC_initialize()
47 {
48     DSTATUS stat = STA_NOINIT;
49 
50     memset(&sdmmc, 0, sizeof(sd_dev_t));
51     if (hal_sd_init(&sdmmc) == RES_OK) {
52         stat &= ~STA_NOINIT;
53     }
54 
55     return stat;
56 }
57 
SDMMC_status()58 DSTATUS SDMMC_status()
59 {
60     hal_sd_stat status;
61     if (hal_sd_stat_get(&sdmmc, &status) == 0) {
62         if (status == SD_STAT_TRANSFER)
63             return 0;
64     }
65 
66     return STA_NOINIT;
67 }
68 
SDMMC_read(BYTE * buff,DWORD sector,UINT count)69 DRESULT SDMMC_read(BYTE *buff, DWORD sector, UINT count)
70 {
71     DRESULT res = RES_OK;
72 
73     if (hal_sd_blks_read(&sdmmc, buff, sector, count, AOS_WAIT_FOREVER) != 0) {
74         res = RES_ERROR;
75     }
76 
77     return res;
78 }
79 
SDMMC_write(BYTE * buff,DWORD sector,UINT count)80 DRESULT SDMMC_write(BYTE *buff, DWORD sector, UINT count)
81 {
82     DRESULT res = RES_OK;
83 
84     if (hal_sd_blks_write(&sdmmc, buff, sector, count, AOS_WAIT_FOREVER) != 0) {
85         res = RES_ERROR;
86     }
87 
88     return res;
89 }
90 
SDMMC_ioctl(BYTE cmd,void * buff)91 DRESULT SDMMC_ioctl(BYTE cmd, void *buff)
92 {
93     DRESULT       res = RES_ERROR;
94     hal_sd_info_t info;
95 
96     if (SDMMC_status() & STA_NOINIT)
97         return RES_NOTRDY;
98 
99     switch (cmd) {
100     case CTRL_SYNC:
101         res = RES_OK;
102         break;
103 
104 #if FF_USE_MKFS
105     case GET_SECTOR_COUNT:
106         if (hal_sd_info_get(&sdmmc, &info) == 0) {
107             *(DWORD *)buff = info.blk_nums;
108             res            = RES_OK;
109         }
110         break;
111 
112     case GET_BLOCK_SIZE:
113         *(DWORD *)buff = 1;
114         res            = RES_OK;
115         break;
116 
117     case GET_FORMAT_OPTION:
118         *(BYTE *)buff = FM_ANY;
119         res           = RES_OK;
120         break;
121 #endif
122 
123     case GET_SECTOR_SIZE:
124         if (hal_sd_info_get(&sdmmc, &info) == 0) {
125             *(WORD *)buff = info.blk_size;
126             res           = RES_OK;
127         }
128         break;
129     default:
130         res = RES_PARERR;
131     }
132 
133     return res;
134 }
135 
136 #endif
137 
138 
139