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 /*-----------------------------------------------------------------------*/
12 #include "ff.h" /* Obtains integer types */
13 #include "diskio.h" /* Declarations of disk functions */
15 /* Definitions of physical drive number for each drive */
16 #define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */
17 #define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
18 #define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */
20 void Log(const char *fmt
, ...);
22 static UINT8 g_MbrSector
[512];
26 void disk_io_set_param(HANDLE Handle
, UINT64 SectorCount
)
29 g_SectorCount
= SectorCount
;
32 /*-----------------------------------------------------------------------*/
33 /* Get Drive Status */
34 /*-----------------------------------------------------------------------*/
37 BYTE pdrv
/* Physical drive nmuber to identify the drive */
47 result
= RAM_disk_status();
49 // translate the reslut code here
54 result
= MMC_disk_status();
56 // translate the reslut code here
61 result
= USB_disk_status();
63 // translate the reslut code here
73 /*-----------------------------------------------------------------------*/
74 /* Inidialize a Drive */
75 /*-----------------------------------------------------------------------*/
77 DSTATUS
disk_initialize (
78 BYTE pdrv
/* Physical drive nmuber to identify the drive */
88 result
= RAM_disk_initialize();
90 // translate the reslut code here
95 result
= MMC_disk_initialize();
97 // translate the reslut code here
102 result
= USB_disk_initialize();
104 // translate the reslut code here
114 /*-----------------------------------------------------------------------*/
116 /*-----------------------------------------------------------------------*/
119 BYTE pdrv
, /* Physical drive nmuber to identify the drive */
120 BYTE
*buff
, /* Data buffer to store read data */
121 LBA_t sector
, /* Start sector in LBA */
122 UINT count
/* Number of sectors to read */
127 LARGE_INTEGER liCurrentPosition
;
129 liCurrentPosition
.QuadPart
= sector
* 512;
130 SetFilePointerEx(g_hPhyDrive
, liCurrentPosition
, &liCurrentPosition
, FILE_BEGIN
);
132 bRet
= ReadFile(g_hPhyDrive
, buff
, count
* 512, &dwSize
, NULL
);
134 if (dwSize
!= count
* 512)
136 Log("ReadFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u", bRet
, count
* 512, dwSize
, GetLastError());
141 memcpy(buff
, g_MbrSector
, sizeof(g_MbrSector
));
149 /*-----------------------------------------------------------------------*/
150 /* Write Sector(s) */
151 /*-----------------------------------------------------------------------*/
153 #if FF_FS_READONLY == 0
156 BYTE pdrv
, /* Physical drive nmuber to identify the drive */
157 const BYTE
*buff
, /* Data to be written */
158 LBA_t sector
, /* Start sector in LBA */
159 UINT count
/* Number of sectors to write */
164 LARGE_INTEGER liCurrentPosition
;
169 memcpy(g_MbrSector
, buff
, sizeof(g_MbrSector
));
180 liCurrentPosition
.QuadPart
= sector
* 512;
181 SetFilePointerEx(g_hPhyDrive
, liCurrentPosition
, &liCurrentPosition
, FILE_BEGIN
);
183 bRet
= WriteFile(g_hPhyDrive
, buff
, count
* 512, &dwSize
, NULL
);
185 if (dwSize
!= count
* 512)
187 Log("WriteFile error bRet:%u WriteSize:%u dwSize:%u ErrCode:%u", bRet
, count
* 512, dwSize
, GetLastError());
196 /*-----------------------------------------------------------------------*/
197 /* Miscellaneous Functions */
198 /*-----------------------------------------------------------------------*/
201 BYTE pdrv
, /* Physical drive nmuber (0..) */
202 BYTE cmd
, /* Control code */
203 void *buff
/* Buffer to send/receive control data */
210 //FILE_FLAG_NO_BUFFERING & FILE_FLAG_WRITE_THROUGH was set, no need to sync
213 case GET_SECTOR_COUNT
:
215 *(LBA_t
*)buff
= g_SectorCount
;
218 case GET_SECTOR_SIZE
:
239 // Process of the command for the RAM drive
245 // Process of the command for the MMC/SD card
251 // Process of the command the USB drive