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