-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdiskio.c
197 lines (171 loc) · 4.78 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
/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be */
/* attached to the FatFs via a glue function rather than modifying it. */
/* This is an example of glue functions to attach various exsisting */
/* storage control modules to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/
#include "ff.h" /* Obtains integer types */
#include "diskio.h" /* Declarations of disk functions */
#include "sdcard.h"
#if 0
#include <stdio.h>
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...)
#endif
static DSTATUS status = STA_NOINIT;
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
DSTATUS
disk_status(BYTE pdrv)
{
uint8_t stat;
uint8_t ret;
debug("disk_status(%u):\r\n", pdrv);
if (pdrv != 0)
return STA_NOINIT | STA_NODISK;
#if 1
if (status & STA_NOINIT)
return status;
ret = sd_status(&stat);
debug("sd_status() = %02x (%02x)\r\n", ret, stat);
if (ret == 0xFF)
status = STA_NOINIT | STA_NODISK;
else if (ret != 0x00 || stat != 0x00)
status = STA_NOINIT;
#endif
return status;
}
/*-----------------------------------------------------------------------*/
/* Initialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS
disk_initialize(BYTE pdrv)
{
uint8_t ret;
debug("disk_initialize(%u):\r\n", pdrv);
if (pdrv != 0)
return STA_NOINIT | STA_NODISK;
ret = sd_wakeup();
debug("sd_wakeup() = %x\r\n", ret);
if (ret == 0x00)
status = 0;
return status;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT
disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
{
DRESULT res;
uint8_t ret;
debug("disk_read(%u, buff, %lu, %u):\r\n",
pdrv, sector, count);
if (pdrv != 0)
return RES_PARERR;
if (status & STA_NOINIT)
return RES_NOTRDY;
for (; count > 0; count--) {
ret = sd_readblock(sector, buff);
if (ret != 0x00) {
debug("sd_readblock(%lu, buff) = %u\r\n",
sector, ret);
break;
}
sector += 1;
buff += 512;
}
switch (ret) {
case 0x00: res = RES_OK; break;
default: res = RES_ERROR; break;
}
return res;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
#if FF_FS_READONLY == 0
DRESULT
disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
{
DRESULT res;
uint8_t ret;
debug("disk_write(%u, buff, %lu, %u):\r\n",
pdrv, sector, count);
if (pdrv != 0)
return RES_PARERR;
if (status & STA_NOINIT)
return RES_NOTRDY;
for (; count > 0; count--) {
ret = sd_writeblock(sector, buff);
if (ret != 0x00) {
debug("sd_writeblock(%lu, buff) = %u\r\n",
sector, ret);
break;
}
sector += 1;
buff += 512;
}
switch (ret) {
case 0x00: res = RES_OK; break;
default: res = RES_ERROR; break;
}
return res;
}
#endif
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
#if FF_FS_READONLY == 0 || FF_MIN_SS != FF_MAX_SS
DRESULT
disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
{
DRESULT res = RES_PARERR;
if (pdrv != 0)
return res;
switch (cmd) {
/* Generic command (Used by FatFs) */
#if FF_FS_READONLY == 0
case CTRL_SYNC: /* Complete pending write process */
res = RES_OK;
break;
#endif
#if FF_USE_MKFS == 1
case GET_SECTOR_COUNT: { /* Get media size */
DWORD *count = buff;
switch (sd_getblocks(count)) {
case 0x00: res = RES_OK; break;
case 0xFF: res = RES_NOTRDY; break;
default: res = RES_ERROR; break;
}
break;
}
#endif
#if FF_MIN_SS != FF_MAX_SS
case GET_SECTOR_SIZE: { /* Get sector size */
WORD *size = buff;
*size = 512;
res = RES_OK;
break;
}
#endif
#if FF_USE_MKFS == 1
case GET_BLOCK_SIZE: { /* Get erase block size */
WORD *size = buff;
*size = sd_eraseblocksize();
res = RES_OK;
break;
#endif
#if FF_USE_TRIM == 1
case CTRL_TRIM: /* Inform device that the data on the block of sectors is no longer used */
res = RES_OK;
break;
#endif
}
return res;
}
#endif