• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         return (DRESULT)g_diskDrv.drv[pdrv]->disk_read(g_diskDrv.lun[pdrv], buff, sector, count);
99 #endif
100 }
101 
102 #ifndef __LITEOS_M__
disk_read_readdir(BYTE pdrv,BYTE * buff,LBA_t sector,UINT count)103 DRESULT disk_read_readdir (
104 	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
105 	BYTE *buff,		/* Data buffer to store read data */
106 	LBA_t sector,		/* Start sector in LBA */
107 	UINT count		/* Number of sectors to read */
108 )
109 {
110 	int result;
111 
112 	result = los_part_read((int)pdrv, (void *)buff, sector, (UINT32)count, FALSE);
113 
114 	if (result == 0)
115 		return RES_OK;
116 	else
117 		return RES_ERROR;
118 }
119 #endif
120 
121 
122 #ifndef __LITEOS_M__
disk_raw_read(int id,void * buff,LBA_t sector,UINT32 count)123 DRESULT disk_raw_read (int id, void *buff, LBA_t sector, UINT32 count)
124 {
125 	int result;
126 
127 	result = los_disk_read(id, buff, sector, count, TRUE);
128 
129 	if (result == 0)
130 		return RES_OK;
131 	else
132 		return RES_ERROR;
133 }
134 #endif
135 
136 
137 
138 /*-----------------------------------------------------------------------*/
139 /* Write Sector(s)                                                       */
140 /*-----------------------------------------------------------------------*/
141 
142 #if FF_FS_READONLY == 0
143 
disk_write(BYTE pdrv,const BYTE * buff,LBA_t sector,UINT count)144 DRESULT disk_write (
145 	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
146 	const BYTE *buff,	/* Data to be written */
147 	LBA_t sector,		/* Start sector in LBA */
148 	UINT count			/* Number of sectors to write */
149 )
150 {
151 #ifndef __LITEOS_M__
152 	int result;
153 
154 	result = los_part_write((int)pdrv, (void *)buff, sector, (UINT32)count);
155 
156 	if (result == 0)
157 		return RES_OK;
158 	else
159 		return RES_ERROR;
160 #else
161         return (DRESULT)g_diskDrv.drv[pdrv]->disk_write(g_diskDrv.lun[pdrv], buff, sector, count);
162 #endif
163 }
164 
165 #ifndef __LITEOS_M__
disk_raw_write(int id,void * buff,QWORD sector,UINT32 count)166 DRESULT disk_raw_write(int id, void *buff, QWORD sector, UINT32 count){
167 	int result;
168 	void *uwBuff = buff;
169 
170 	result = los_disk_write(id, (const void*)uwBuff, sector, count);
171 
172 	if (result == 0)
173 		return RES_OK;
174 	else
175 		return RES_ERROR;
176 }
177 #endif
178 
179 #endif /* FF_FS_READONLY == 0 */
180 
181 
182 /*-----------------------------------------------------------------------*/
183 /* Miscellaneous Functions                                               */
184 /*-----------------------------------------------------------------------*/
185 
disk_ioctl(BYTE pdrv,BYTE cmd,void * buff)186 DRESULT disk_ioctl (
187 	BYTE pdrv,		/* Physical drive nmuber (0..) */
188 	BYTE cmd,		/* Control code */
189 	void *buff		/* Buffer to send/receive control data */
190 )
191 {
192 #ifndef __LITEOS_M__
193 	int result;
194 
195 	result = los_part_ioctl((int)pdrv, (int)cmd, buff);
196 
197 	if (result == 0)
198 		return RES_OK;
199 	else
200 		return RES_ERROR;
201 #else
202         return (DRESULT)g_diskDrv.drv[pdrv]->disk_ioctl(g_diskDrv.lun[pdrv], cmd, buff);
203 #endif
204 }
205 
206  /*
207   * @brief  Gets Time from RTC
208   * @param  None
209   * @retval Time in DWORD
210   */
211 
212 #ifndef __LITEOS_M__
get_fattime(void)213 DWORD get_fattime (void)
214 {
215 	time_t seconds = 0;
216 	struct tm local_time = {0};
217 
218 	seconds = time(NULL);
219 
220 	if (localtime_r(&seconds, &local_time) == NULL)
221 		return 0;
222 	if ((local_time.tm_year + 1900) < 1980) {	/* year must start at 1980 */
223 		return 0;
224 	}
225 
226 	/* get system time */
227 	return  ((DWORD)(local_time.tm_year - 80) << 25) |
228 			((DWORD)(local_time.tm_mon + 1) << 21) |
229 			((DWORD)local_time.tm_mday << 16) |
230 			((DWORD)local_time.tm_hour << 11) |
231 			((DWORD)local_time.tm_min << 5) |
232 			((DWORD)local_time.tm_sec >> 1);
233 }
234 #else
get_fattime(void)235 __weak DWORD get_fattime (void)
236 {
237 	return 0;
238 }
239 #endif
240 
241