forked from lhdjply/f1c200s_library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskio.c
217 lines (200 loc) · 4.93 KB
/
diskio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "diskio.h" /* Declarations of disk functions */
#include "common.h"
extern DATA mydata;
extern SD_CardInfo SDCardInfo;
extern USB_NOCACHE_RAM_SECTION struct usbh_msc *active_msc_class;
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status(
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS status = STA_NOINIT;
switch (pdrv)
{
case SD_CARD: /* SD CARD */
status &= ~STA_NOINIT;
break;
case USB:
if (active_msc_class == NULL)
{
status = STA_NOINIT;
}
else
{
status = RES_OK;
}
break;
default:
status = STA_NOINIT;
}
return status;
}
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize(
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS status = STA_NOINIT;
switch (pdrv)
{
case SD_CARD: /* SD CARD */
if (SD_Init(SDIO0) == SD_OK)
{
status &= ~STA_NOINIT;
}
status &= ~STA_NOINIT;
break;
case USB:
if (active_msc_class == NULL)
{
status = STA_NOINIT;
}
else
{
status = RES_OK;
}
break;
default:
status = STA_NOINIT;
}
return status;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_read(
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
)
{
DRESULT status;
switch (pdrv)
{
case SD_CARD:
status = SD_ReadDisk(SDIO0, buff, sector, count);
break;
case USB:
status = usbh_msc_scsi_read10(active_msc_class, sector, buff, count);
break;
default:
status = RES_PARERR;
}
return status;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
#if FF_FS_READONLY == 0
DRESULT disk_write(
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
)
{
DRESULT status;
if (!count)
{
return RES_PARERR; /* Check parameter */
}
switch (pdrv)
{
case SD_CARD:
status = SD_WriteDisk(SDIO0, (uint8_t *)buff, sector, count);
break;
case USB:
status = usbh_msc_scsi_write10(active_msc_class, sector, buff, count);
break;
default:
status = RES_PARERR;
}
return status;
}
#endif
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
DRESULT disk_ioctl(
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT status = RES_PARERR;
switch (pdrv)
{
case SD_CARD:
if (status & STA_NOINIT)
return RES_NOTRDY;
switch (cmd)
{
case CTRL_SYNC:
status = RES_OK;
break;
case GET_SECTOR_SIZE:
*(DWORD *)buff = 512;
status = RES_OK;
break;
case GET_SECTOR_COUNT:
*(DWORD *)buff = SDCardInfo.CardCapacity / 512;
status = RES_OK;
break;
case GET_BLOCK_SIZE:
*(DWORD *)buff = SDCardInfo.CardBlockSize;
status = RES_OK;
break;
default:
status = RES_PARERR;
break;
}
break;
case USB:
switch (cmd)
{
case CTRL_SYNC:
status = RES_OK;
break;
case GET_SECTOR_SIZE:
*(WORD *)buff = active_msc_class->blocksize;
status = RES_OK;
break;
case GET_BLOCK_SIZE:
*(DWORD *)buff = 1;
status = RES_OK;
break;
case GET_SECTOR_COUNT:
*(DWORD *)buff = active_msc_class->blocknum;
status = RES_OK;
break;
default:
status = RES_PARERR;
break;
}
break;
default:
status = RES_PARERR;
}
return status;
}
// 获得时间
// User defined function to give a current time to fatfs module */
// 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
// 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
DWORD get_fattime(void)
{
DWORD time_buff = 0;
// 按照FatFs的时间格式组合
time_buff |= ((mydata.real_time.year - 1980) << 25); // 年
time_buff |= (mydata.real_time.month << 21); // 月
time_buff |= (mydata.real_time.day << 16); // 日
time_buff |= (mydata.real_time.hour << 11); // 时
time_buff |= (mydata.real_time.minute << 5); // 分
time_buff |= (mydata.real_time.second / 2); // 秒
return time_buff;
}