image/svg+xml
Brteve's API for EveApps
Version 0.0.1
The reference document for common folder of EveApps project.
 
Loading...
Searching...
No Matches
diskio_ft9xx.c
Go to the documentation of this file.
1
32/*-----------------------------------------------------------------------*/
33/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2014 */
34/*-----------------------------------------------------------------------*/
35/* If a working storage control module is available, it should be */
36/* attached to the FatFs via a glue function rather than modifying it. */
37/* This is an example of glue functions to attach various exsisting */
38/* storage control modules to the FatFs module with a defined API. */
39/*-----------------------------------------------------------------------*/
40
41#if defined(__FT900__) || defined(__FT930__)
42
43#include "ff.h" /* FatFs lower layer API */
44#include "diskio.h" /* FatFs lower layer API */
45#include "ft900_sdhost.h"
46#include <string.h>
47
48#define _USE_WRITE 1 /* 1: Enable disk_write function */
49#define _USE_IOCTL 1 /* 1: Enable disk_ioctl fucntion */
50
51
52// ============================================================================================== //
53// G L O B A L V A R I A B L E S //
54// ============================================================================================== //
55static int sd_init = 1; // Already initialized externally, only reinitialize when lost
56static int sd_ready = 0;
57
58// A 32 bit aligned buffer of 512 bytes to copy non-aligned sectors to/from FatFs
59// It's OK to use the same buffer for read/write as FatFS will never read and write at the same time.
60// Even when accessed from multiple threads (FS_REENTRANCY == 1)
61static unsigned long __attribute__ ((aligned (32))) temp[128];
62
63/* FatFS Functions ******************/
64
69 DSTATUS stat = 0;
70
71 if (sd_ready || !sd_init) {
72 // sdhost_sys_init();
73 sdhost_init();
74 sd_init = 1;
75 sd_ready = 0;
76 }
77
78 if (sdhost_card_detect() != SDHOST_CARD_INSERTED) {
79 return STA_NOINIT | STA_NODISK;
80 }
81
82 if (sdhost_card_init() != SDHOST_OK) {
83 stat = STA_NOINIT;
84 sd_init = 0;
85 } else {
86 sd_ready = 1;
87 }
88
89 return stat;
90}
91
96 DSTATUS stat = 0;
97 SDHOST_STATUS sdHostStatus;
98
99 if (!sd_init) {
100 return STA_NOINIT;
101 }
102
103 sdHostStatus = sdhost_card_detect();
104
105 if (sdHostStatus != SDHOST_CARD_INSERTED) {
106 if (sd_ready) {
107 sd_ready = 0;
108 sdhost_init();
109 }
110 }
111
112 if (!sd_ready) {
113 stat |= STA_NOINIT;
114 }
115
116 if (sdHostStatus == SDHOST_CARD_REMOVED) {
117 stat |= STA_NODISK;
118 }
119
120 return stat;
121}
122
129DRESULT disk_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) {
130 DRESULT res = RES_OK;
131 SDHOST_STATUS sdHostStatus = SDHOST_OK;
132 int i;
133
134 if (((unsigned int) buff & 3) != 0) {
135
136 //print("%%%%%%%%%% READ - DISKIO - buffer NOT 32 bit aligned %%%%%%%%%%%%%%%");
137 for (i = 0; i < count; i++) {
138 sdHostStatus = sdhost_transfer_data(SDHOST_READ, (void*) temp,
139 SDHOST_BLK_SIZE, sector);
140 memmove(buff, temp, sizeof(temp));
141 sector++;
142 buff += sizeof(temp);
143 }
144 } else {
145 sdHostStatus = sdhost_transfer_data(SDHOST_READ, (void*) buff,
146 SDHOST_BLK_SIZE * count, sector);
147 }
148
149 if (sdHostStatus != SDHOST_OK) {
150 res = RES_ERROR;
151 }
152
153 return res;
154}
155
156#if _USE_WRITE
163DRESULT disk_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) {
164 DRESULT res = RES_OK;
165 // Non-aligned writes are inefficient because of this additional move to temp buffer. But thankfully they are rate in FatFS
166 SDHOST_STATUS sdHostStatus = SDHOST_OK;
167 int i;
168
169 if (((unsigned int) buff & 3) != 0) {
170
171 // print("%%%%%%%%%% WRITE - DISKIO - buffer NOT 32 bit aligned %%%%%%%%%%%%%%%");
172
173 for (i = 0; i < count; i++) {
174 memmove(temp, buff, sizeof(temp));
175 sdHostStatus = sdhost_transfer_data(SDHOST_WRITE, (void*) temp,
176 SDHOST_BLK_SIZE, sector);
177 sector++;
178 buff += sizeof(temp);
179 }
180 } else {
181 sdHostStatus = sdhost_transfer_data(SDHOST_WRITE, (void*) buff,
182 SDHOST_BLK_SIZE * count, sector);
183 }
184
185 if (sdHostStatus != SDHOST_OK) {
186 res = RES_ERROR;
187 }
188
189 return res;
190}
191#endif
192
193#if _USE_IOCTL
199DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
200
201 DRESULT res = RES_PARERR;
202 sdhost_context_t* sd_context = sdhost_get_context();
203 BYTE* csd;
204 DWORD csize;
205 BYTE n;
206
207 switch (cmd) {
208 case CTRL_SYNC:
209 res = RES_OK;
210 break;
211
212 case GET_SECTOR_COUNT: /* Get number of sectors on the disk (WORD) */
213 csd = (BYTE*) &sd_context->CSD[0];
214 if ((csd[14] >> 6) == 1) // The last byte in the structure indicated the table version number
215 { /* SDv2? */
216 // For a CSDv2 Table, the size of user memory is defined as (C_SIZE + 1) * 512 * 1024
217 // So to number of (512b) sectors = (C_SIZE + 1) * 1024
218 // See SD Physical Layer specification v4.10 page 123
219 csize = csd[5] + ((WORD) csd[6] << 8) + 1;
220 *(DWORD*) buff = (DWORD) csize << 10;
221
222 } else { /* SDv1 or MMCv3 */
223 // For a CSDv1 Table, the size of user memory is defined as Capacity = (C_SIZE + 1) * MULT * BLOCK_LEN
224 // MULT = 0x0001 << (C_SIZE_MULT + 2) and BLOCK_LEN = 0x0001 << READ_BL_LEN
225 // So to number of (512b) sectors = Capacity >> 9
226 // See SD Physical Layer specification v4.10 page 118
227
228 n = (csd[9] & 15) + ((csd[4] & 128) >> 7) + ((csd[5] & 3) << 1) + 2;
229 csize = (csd[6] >> 6) + ((WORD) csd[7] << 2)
230 + ((WORD) (csd[8] & 3) << 10) + 1;
231 *(DWORD*) buff = (DWORD) csize << (n - 9);
232 }
233
234 res = RES_OK;
235
236 break;
237
238 case GET_SECTOR_SIZE: /* Get sectors on the disk (WORD) */
239 *(WORD*) buff = 512;
240 res = RES_OK;
241 break;
242
243 case GET_BLOCK_SIZE: /* Get erase block size in unit of sectors (DWORD) */
244 csd = (BYTE*) &sd_context->CSD[0];
245 if (sd_context->isSDSCCard == false) { /* SDv2? */
246 unsigned long __attribute__ ((aligned (32))) data[64 / 4];
247 sdhost_get_card_status_reg(data);
248
249 *(DWORD*) buff = 16UL << (*((uint8_t*) data + 10) >> 4);
250 res = RES_OK;
251
252 } else { /* SDv1 */
253
254 WORD sec_size = (((csd[4] & 63) << 1) + ((WORD) (csd[3] & 128) >> 7)
255 + 1);
256 // Find WRITE_BL_LEN in units of 512 Bytes
257 uint8_t wbl_len_sec = ((csd[1] >> 6) | ((csd[2] & 0x3) << 2)) - 9;
258 *(DWORD*) buff = sec_size << wbl_len_sec;
259
260 res = RES_OK;
261
262 }
263 break;
264
265 case MMC_GET_TYPE: /* Get card type flags (1 byte) */
266 buff = &sd_context->isSDSCCard;
267 res = RES_OK;
268 break;
269
270 case MMC_GET_CSD: /* Receive CSD as a data block (16 bytes) */
271 csd = (BYTE*) &sd_context->CSD[0];
272 for (int i = 0; i < 16; i++) {
273 *((BYTE*) buff + i) = *(csd + i);
274 }
275 res = RES_OK;
276 break;
277
278 case MMC_GET_CID: /* Receive CID as a data block (16 bytes) */
279 csd = (BYTE*) &sd_context->CID[0];
280 for (int i = 0; i < 16; i++) {
281 *((BYTE*) buff + i) = *(csd + i);
282 }
283 res = RES_OK;
284 break;
285
286 case MMC_GET_OCR: /* Receive OCR as an R3 resp (4 bytes) */
287 csd = (BYTE*) &sd_context->OCR;
288 for (int i = 0; i < 4; i++) {
289 *((BYTE*) buff + i) = *(csd + i);
290 }
291 res = RES_OK;
292 break;
293
294 case MMC_GET_SDSTAT: /* Receive SD status as a data block (64 bytes) */
295 if (sd_context->isSDSCCard == false) { /* SDv2? */
296 uint32_t __attribute__ ((aligned (32))) data[64 / 4];
297 sdhost_get_card_status_reg(data);
298 for (int i = 0; i < 64; i++) {
299 *((BYTE*) buff + i) = *((BYTE*) data + i);
300 }
301 res = RES_OK;
302 }
303 break;
304 default:
305 res = RES_PARERR;
306 }
307
308 return res;
309
310}
311#endif
312
313#if _FS_READONLY == 0
322//DWORD get_fattime(void) {
323// return 0; /* Invalid timestamp */
324//}
325#endif
326#endif //deffined(__FT900__) || defined(__FT930__)
327
unsigned int uint32_t
unsigned char uint8_t
static ft_void_t ft_uint32_t * cmd
Definition FT_Gpu_Hal.h:184
DRESULT disk_read(BYTE pdrv, BYTE *buff, LBA_t sector, UINT count)
DSTATUS disk_initialize(BYTE pdrv)
#define MMC_GET_CID
Definition diskio.h:61
#define CTRL_SYNC
Definition diskio.h:46
#define GET_SECTOR_COUNT
Definition diskio.h:47
#define MMC_GET_SDSTAT
Definition diskio.h:63
DSTATUS disk_status(BYTE pdrv)
DRESULT
Definition diskio.h:16
@ RES_OK
Definition diskio.h:17
@ RES_ERROR
Definition diskio.h:18
@ RES_PARERR
Definition diskio.h:21
DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
#define MMC_GET_TYPE
Definition diskio.h:59
#define STA_NOINIT
Definition diskio.h:38
DRESULT disk_write(BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count)
#define GET_SECTOR_SIZE
Definition diskio.h:48
BYTE DSTATUS
Definition diskio.h:13
#define MMC_GET_CSD
Definition diskio.h:60
#define GET_BLOCK_SIZE
Definition diskio.h:49
#define STA_NODISK
Definition diskio.h:39
#define MMC_GET_OCR
Definition diskio.h:62
unsigned short WORD
Definition ff.h:60
unsigned int UINT
Definition ff.h:58
unsigned char BYTE
Definition ff.h:59
unsigned long DWORD
Definition ff.h:61