1 /*-----------------------------------------------------------------------*/
2 /* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */
3 /*-----------------------------------------------------------------------*/
4 /* If a working storage control module is available, it should be */
5 /* attached to the FatFs via a glue function rather than modifying it. */
6 /* This is an example of glue functions to attach various exsisting */
7 /* storage control modules to the FatFs module with a defined API. */
8 /*-----------------------------------------------------------------------*/
9
10 #include "ff.h" /* Obtains integer types */
11 #include "diskio.h" /* Declarations of disk functions */
12 #ifndef __LITEOS_M__
13 #include "string.h"
14 #include "disk.h"
15 #else
16 #include "ff_gen_drv.h"
17 #if defined ( __GNUC__ )
18 #ifndef __weak
19 #define __weak __attribute__((weak))
20 #endif
21 #endif
22 #endif
23
24 /*-----------------------------------------------------------------------*/
25 /* Get Drive Status */
26 /*-----------------------------------------------------------------------*/
27
28 #ifndef __LITEOS_M__
29 #define CARD_UNPLUGED 1
30 extern int get_cardstatus(int pdrv);
31 #define GET_CARD_STATUS \
32 do { \
33 if (get_cardstatus(pdrv) == 0) \
34 return STA_NOINIT; \
35 } while (0)
36 extern struct tm tm;
37 #endif
38
disk_status(BYTE pdrv)39 DSTATUS disk_status (
40 BYTE pdrv /* Physical drive nmuber to identify the drive */
41 )
42 {
43 #ifndef __LITEOS_M__
44 return 0;
45 #else
46 DSTATUS stat;
47
48 stat = g_diskDrv.drv[pdrv]->disk_status(g_diskDrv.lun[pdrv]);
49 return stat;
50 #endif
51 }
52
53
54
55 /*-----------------------------------------------------------------------*/
56 /* Inidialize a Drive */
57 /*-----------------------------------------------------------------------*/
58
disk_initialize(BYTE pdrv)59 DSTATUS disk_initialize (
60 BYTE pdrv /* Physical drive nmuber to identify the drive */
61 )
62 {
63 #ifndef __LITEOS_M__
64 return 0;
65 #else
66 DSTATUS stat = RES_OK;
67
68 if(g_diskDrv.initialized[pdrv] == 0)
69 {
70 stat = g_diskDrv.drv[pdrv]->disk_initialize(g_diskDrv.lun[pdrv]);
71 }
72 return stat;
73 #endif
74 }
75
76
77
78 /*-----------------------------------------------------------------------*/
79 /* Read Sector(s) */
80 /*-----------------------------------------------------------------------*/
81
disk_read(BYTE pdrv,BYTE * buff,LBA_t sector,UINT count)82 DRESULT disk_read (
83 BYTE pdrv, /* Physical drive nmuber to identify the drive */
84 BYTE *buff, /* Data buffer to store read data */
85 LBA_t sector, /* Start sector in LBA */
86 UINT count /* Number of sectors to read */
87 )
88 {
89 #ifndef __LITEOS_M__
90 int result;
91
92 result = los_part_read((int)pdrv, (void *)buff, sector, (UINT32)count, TRUE);
93
94 if (result == 0)
95 return RES_OK;
96 else
97 return RES_ERROR;
98 #else
99 return (DRESULT)g_diskDrv.drv[pdrv]->disk_read(g_diskDrv.lun[pdrv], buff, sector, count);
100 #endif
101 }
102
103 #ifndef __LITEOS_M__
disk_read_readdir(BYTE pdrv,BYTE * buff,LBA_t sector,UINT count)104 DRESULT disk_read_readdir (
105 BYTE pdrv, /* Physical drive nmuber to identify the drive */
106 BYTE *buff, /* Data buffer to store read data */
107 LBA_t sector, /* Start sector in LBA */
108 UINT count /* Number of sectors to read */
109 )
110 {
111 int result;
112
113 result = los_part_read((int)pdrv, (void *)buff, sector, (UINT32)count, FALSE);
114
115 if (result == 0)
116 return RES_OK;
117 else
118 return RES_ERROR;
119 }
120 #endif
121
122
123 #ifndef __LITEOS_M__
disk_raw_read(int id,void * buff,LBA_t sector,UINT32 count)124 DRESULT disk_raw_read (int id, void *buff, LBA_t sector, UINT32 count)
125 {
126 int result;
127
128 result = los_disk_read(id, buff, sector, count, TRUE);
129
130 if (result == 0)
131 return RES_OK;
132 else
133 return RES_ERROR;
134 }
135 #endif
136
137
138
139 /*-----------------------------------------------------------------------*/
140 /* Write Sector(s) */
141 /*-----------------------------------------------------------------------*/
142
143 #if FF_FS_READONLY == 0
144
disk_write(BYTE pdrv,const BYTE * buff,LBA_t sector,UINT count)145 DRESULT disk_write (
146 BYTE pdrv, /* Physical drive nmuber to identify the drive */
147 const BYTE *buff, /* Data to be written */
148 LBA_t sector, /* Start sector in LBA */
149 UINT count /* Number of sectors to write */
150 )
151 {
152 #ifndef __LITEOS_M__
153 int result;
154
155 result = los_part_write((int)pdrv, (void *)buff, sector, (UINT32)count);
156
157 if (result == 0)
158 return RES_OK;
159 else
160 return RES_ERROR;
161 #else
162 return (DRESULT)g_diskDrv.drv[pdrv]->disk_write(g_diskDrv.lun[pdrv], buff, sector, count);
163 #endif
164 }
165
166 #ifndef __LITEOS_M__
disk_raw_write(int id,void * buff,QWORD sector,UINT32 count)167 DRESULT disk_raw_write(int id, void *buff, QWORD sector, UINT32 count){
168 int result;
169 void *uwBuff = buff;
170
171 result = los_disk_write(id, (const void*)uwBuff, sector, count);
172
173 if (result == 0)
174 return RES_OK;
175 else
176 return RES_ERROR;
177 }
178 #endif
179
180 #endif /* FF_FS_READONLY == 0 */
181
182
183 /*-----------------------------------------------------------------------*/
184 /* Miscellaneous Functions */
185 /*-----------------------------------------------------------------------*/
186
disk_ioctl(BYTE pdrv,BYTE cmd,void * buff)187 DRESULT disk_ioctl (
188 BYTE pdrv, /* Physical drive nmuber (0..) */
189 BYTE cmd, /* Control code */
190 void *buff /* Buffer to send/receive control data */
191 )
192 {
193 #ifndef __LITEOS_M__
194 int result;
195
196 result = los_part_ioctl((int)pdrv, (int)cmd, buff);
197
198 if (result == 0)
199 return RES_OK;
200 else
201 return RES_ERROR;
202 #else
203 return (DRESULT)g_diskDrv.drv[pdrv]->disk_ioctl(g_diskDrv.lun[pdrv], cmd, buff);
204 #endif
205 }
206
207 /*
208 * @brief Gets Time from RTC
209 * @param None
210 * @retval Time in DWORD
211 */
212
213 #ifndef __LITEOS_M__
get_fattime(void)214 DWORD get_fattime (void)
215 {
216 time_t seconds = 0;
217 struct tm local_time = {0};
218
219 seconds = time(NULL);
220
221 if (localtime_r(&seconds, &local_time) == NULL)
222 return 0;
223 if ((local_time.tm_year + 1900) < 1980) { /* year must start at 1980 */
224 return 0;
225 }
226
227 /* get system time */
228 return ((DWORD)(local_time.tm_year - 80) << 25) |
229 ((DWORD)(local_time.tm_mon + 1) << 21) |
230 ((DWORD)local_time.tm_mday << 16) |
231 ((DWORD)local_time.tm_hour << 11) |
232 ((DWORD)local_time.tm_min << 5) |
233 ((DWORD)local_time.tm_sec >> 1);
234 }
235 #else
get_fattime(void)236 __weak DWORD get_fattime (void)
237 {
238 return 0;
239 }
240 #endif
241
242