2021-04-13 19:23:11 +08:00
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2019 */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* 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. */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
#include "ff.h" /* Obtains integer types */
|
|
|
|
#include "diskio.h" /* Declarations of disk functions */
|
2021-04-13 19:23:11 +08:00
|
|
|
#include "stddef.h"
|
|
|
|
|
|
|
|
/* Definitions of physical drive number for each drive */
|
2022-10-21 10:17:49 +08:00
|
|
|
#define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */
|
|
|
|
#define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
|
|
|
|
#define DEV_FLASH 2 /* Example: Map USB MSD to physical drive 2 */
|
|
|
|
#define DEV_USB 3 /* Example: Map USB MSD to physical drive 2 */
|
2021-04-13 19:23:11 +08:00
|
|
|
|
|
|
|
FATFS_DiskioDriverTypeDef pDiskioDriver = {
|
2021-06-20 12:25:46 +08:00
|
|
|
.RAM_disk_status = NULL,
|
|
|
|
.MMC_disk_status = NULL,
|
|
|
|
.USB_disk_status = NULL,
|
|
|
|
.RAM_disk_initialize = NULL,
|
|
|
|
.MMC_disk_initialize = NULL,
|
|
|
|
.USB_disk_initialize = NULL,
|
|
|
|
.RAM_disk_read = NULL,
|
|
|
|
.MMC_disk_read = NULL,
|
|
|
|
.USB_disk_read = NULL,
|
|
|
|
.RAM_disk_write = NULL,
|
|
|
|
.MMC_disk_write = NULL,
|
|
|
|
.USB_disk_write = NULL,
|
|
|
|
.RAM_disk_ioctl = NULL,
|
|
|
|
.MMC_disk_ioctl = NULL,
|
|
|
|
.USB_disk_ioctl = NULL,
|
2021-04-13 19:23:11 +08:00
|
|
|
.Translate_Result_Code = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* init driver callback */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
void disk_driver_callback_init(FATFS_DiskioDriverTypeDef *pNewDriver)
|
|
|
|
{
|
2021-09-23 19:21:38 +08:00
|
|
|
if (pNewDriver->RAM_disk_status)
|
|
|
|
pDiskioDriver.RAM_disk_status = pNewDriver->RAM_disk_status;
|
|
|
|
if (pNewDriver->MMC_disk_status)
|
|
|
|
pDiskioDriver.MMC_disk_status = pNewDriver->MMC_disk_status;
|
2022-10-21 10:17:49 +08:00
|
|
|
if (pNewDriver->FLASH_disk_status)
|
|
|
|
pDiskioDriver.FLASH_disk_status = pNewDriver->FLASH_disk_status;
|
2021-09-23 19:21:38 +08:00
|
|
|
if (pNewDriver->USB_disk_status)
|
|
|
|
pDiskioDriver.USB_disk_status = pNewDriver->USB_disk_status;
|
|
|
|
if (pNewDriver->RAM_disk_initialize)
|
|
|
|
pDiskioDriver.RAM_disk_initialize = pNewDriver->RAM_disk_initialize;
|
|
|
|
if (pNewDriver->MMC_disk_initialize)
|
|
|
|
pDiskioDriver.MMC_disk_initialize = pNewDriver->MMC_disk_initialize;
|
2022-10-21 10:17:49 +08:00
|
|
|
if (pNewDriver->FLASH_disk_initialize)
|
|
|
|
pDiskioDriver.FLASH_disk_initialize = pNewDriver->FLASH_disk_initialize;
|
2021-09-23 19:21:38 +08:00
|
|
|
if (pNewDriver->USB_disk_initialize)
|
|
|
|
pDiskioDriver.USB_disk_initialize = pNewDriver->USB_disk_initialize;
|
|
|
|
if (pNewDriver->RAM_disk_read)
|
|
|
|
pDiskioDriver.RAM_disk_read = pNewDriver->RAM_disk_read;
|
|
|
|
if (pNewDriver->MMC_disk_read)
|
|
|
|
pDiskioDriver.MMC_disk_read = pNewDriver->MMC_disk_read;
|
2022-10-21 10:17:49 +08:00
|
|
|
if (pNewDriver->FLASH_disk_read)
|
|
|
|
pDiskioDriver.FLASH_disk_read = pNewDriver->FLASH_disk_read;
|
2021-09-23 19:21:38 +08:00
|
|
|
if (pNewDriver->USB_disk_read)
|
|
|
|
pDiskioDriver.USB_disk_read = pNewDriver->USB_disk_read;
|
|
|
|
if (pNewDriver->RAM_disk_write)
|
|
|
|
pDiskioDriver.RAM_disk_write = pNewDriver->RAM_disk_write;
|
|
|
|
if (pNewDriver->MMC_disk_write)
|
|
|
|
pDiskioDriver.MMC_disk_write = pNewDriver->MMC_disk_write;
|
2022-10-21 10:17:49 +08:00
|
|
|
if (pNewDriver->FLASH_disk_write)
|
|
|
|
pDiskioDriver.FLASH_disk_write = pNewDriver->FLASH_disk_write;
|
2021-09-23 19:21:38 +08:00
|
|
|
if (pNewDriver->USB_disk_write)
|
|
|
|
pDiskioDriver.USB_disk_write = pNewDriver->USB_disk_write;
|
|
|
|
if (pNewDriver->RAM_disk_ioctl)
|
|
|
|
pDiskioDriver.RAM_disk_ioctl = pNewDriver->RAM_disk_ioctl;
|
|
|
|
if (pNewDriver->MMC_disk_ioctl)
|
|
|
|
pDiskioDriver.MMC_disk_ioctl = pNewDriver->MMC_disk_ioctl;
|
2022-10-21 10:17:49 +08:00
|
|
|
if (pNewDriver->FLASH_disk_ioctl)
|
|
|
|
pDiskioDriver.FLASH_disk_ioctl = pNewDriver->FLASH_disk_ioctl;
|
2021-09-23 19:21:38 +08:00
|
|
|
if (pNewDriver->USB_disk_ioctl)
|
|
|
|
pDiskioDriver.USB_disk_ioctl = pNewDriver->USB_disk_ioctl;
|
|
|
|
if (pNewDriver->Translate_Result_Code)
|
|
|
|
pDiskioDriver.Translate_Result_Code = pNewDriver->Translate_Result_Code;
|
2021-04-13 19:23:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Get Drive Status */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
DSTATUS disk_status(
|
|
|
|
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
2021-04-13 19:23:11 +08:00
|
|
|
)
|
|
|
|
{
|
2021-06-20 12:25:46 +08:00
|
|
|
DSTATUS stat = STA_NOINIT;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
switch (pdrv) {
|
|
|
|
case DEV_RAM:
|
|
|
|
if (pDiskioDriver.RAM_disk_status) {
|
|
|
|
result = pDiskioDriver.RAM_disk_status();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2021-04-13 19:23:11 +08:00
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
case DEV_MMC:
|
|
|
|
if (pDiskioDriver.MMC_disk_status) {
|
|
|
|
result = pDiskioDriver.MMC_disk_status();
|
|
|
|
}
|
2021-04-13 19:23:11 +08:00
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
break;
|
|
|
|
|
2022-10-21 10:17:49 +08:00
|
|
|
case DEV_FLASH:
|
|
|
|
if (pDiskioDriver.FLASH_disk_status) {
|
|
|
|
result = pDiskioDriver.FLASH_disk_status();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2021-06-20 12:25:46 +08:00
|
|
|
case DEV_USB:
|
|
|
|
if (pDiskioDriver.USB_disk_status) {
|
|
|
|
result = pDiskioDriver.USB_disk_status();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return STA_NOINIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* translate the reslut code here */
|
|
|
|
if (pDiskioDriver.Translate_Result_Code) {
|
|
|
|
stat = pDiskioDriver.Translate_Result_Code(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat;
|
|
|
|
}
|
2021-04-13 19:23:11 +08:00
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Inidialize a Drive */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
DSTATUS disk_initialize(
|
|
|
|
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
2021-04-13 19:23:11 +08:00
|
|
|
)
|
|
|
|
{
|
2021-06-20 12:25:46 +08:00
|
|
|
DSTATUS stat = STA_NOINIT;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
switch (pdrv) {
|
|
|
|
case DEV_RAM:
|
|
|
|
if (pDiskioDriver.RAM_disk_initialize) {
|
|
|
|
result = pDiskioDriver.RAM_disk_initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEV_MMC:
|
|
|
|
if (pDiskioDriver.MMC_disk_initialize) {
|
|
|
|
result = pDiskioDriver.MMC_disk_initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2022-10-21 10:17:49 +08:00
|
|
|
case DEV_FLASH:
|
|
|
|
if (pDiskioDriver.FLASH_disk_initialize) {
|
|
|
|
result = pDiskioDriver.FLASH_disk_initialize();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
case DEV_USB:
|
|
|
|
if (pDiskioDriver.USB_disk_initialize) {
|
|
|
|
result = pDiskioDriver.USB_disk_initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2021-04-13 19:23:11 +08:00
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
default:
|
|
|
|
return STA_NOINIT;
|
|
|
|
}
|
2021-04-13 19:23:11 +08:00
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
/* translate the reslut code here */
|
|
|
|
if (pDiskioDriver.Translate_Result_Code) {
|
|
|
|
stat = pDiskioDriver.Translate_Result_Code(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat;
|
|
|
|
}
|
2021-04-13 19:23:11 +08:00
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Read Sector(s) */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
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 */
|
2021-04-13 19:23:11 +08:00
|
|
|
)
|
|
|
|
{
|
2021-06-20 12:25:46 +08:00
|
|
|
DSTATUS stat = STA_NOINIT;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
switch (pdrv) {
|
|
|
|
case DEV_RAM:
|
|
|
|
if (pDiskioDriver.RAM_disk_read) {
|
|
|
|
result = pDiskioDriver.RAM_disk_read(buff, sector, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEV_MMC:
|
|
|
|
if (pDiskioDriver.MMC_disk_read) {
|
|
|
|
result = pDiskioDriver.MMC_disk_read(buff, sector, count);
|
|
|
|
}
|
2021-04-13 19:23:11 +08:00
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
break;
|
2021-04-13 19:23:11 +08:00
|
|
|
|
2022-10-21 10:17:49 +08:00
|
|
|
case DEV_FLASH:
|
|
|
|
if (pDiskioDriver.FLASH_disk_read) {
|
|
|
|
result = pDiskioDriver.FLASH_disk_read(buff, sector, count);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
case DEV_USB:
|
|
|
|
if (pDiskioDriver.USB_disk_read) {
|
|
|
|
result = pDiskioDriver.USB_disk_read(buff, sector, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return RES_PARERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* translate the reslut code here */
|
|
|
|
if (pDiskioDriver.Translate_Result_Code) {
|
|
|
|
stat = pDiskioDriver.Translate_Result_Code(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat;
|
|
|
|
}
|
2021-04-13 19:23:11 +08:00
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Write Sector(s) */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#if FF_FS_READONLY == 0
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
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 */
|
2021-04-13 19:23:11 +08:00
|
|
|
)
|
|
|
|
{
|
2021-06-20 12:25:46 +08:00
|
|
|
DSTATUS stat = STA_NOINIT;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
switch (pdrv) {
|
|
|
|
case DEV_RAM:
|
|
|
|
if (pDiskioDriver.RAM_disk_write) {
|
|
|
|
result = pDiskioDriver.RAM_disk_write(buff, sector, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEV_MMC:
|
|
|
|
if (pDiskioDriver.MMC_disk_write) {
|
|
|
|
result = pDiskioDriver.MMC_disk_write(buff, sector, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2022-10-21 10:17:49 +08:00
|
|
|
case DEV_FLASH:
|
|
|
|
if (pDiskioDriver.FLASH_disk_write) {
|
|
|
|
result = pDiskioDriver.FLASH_disk_write(buff, sector, count);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
case DEV_USB:
|
|
|
|
if (pDiskioDriver.USB_disk_write) {
|
|
|
|
result = pDiskioDriver.USB_disk_write(buff, sector, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return RES_PARERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* translate the reslut code here */
|
|
|
|
if (pDiskioDriver.Translate_Result_Code) {
|
|
|
|
stat = pDiskioDriver.Translate_Result_Code(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat;
|
2021-04-13 19:23:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
/* Miscellaneous Functions */
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
DRESULT disk_ioctl(
|
|
|
|
BYTE pdrv, /* Physical drive nmuber (0..) */
|
|
|
|
BYTE cmd, /* Control code */
|
|
|
|
void *buff /* Buffer to send/receive control data */
|
2021-04-13 19:23:11 +08:00
|
|
|
)
|
|
|
|
{
|
2021-06-20 12:25:46 +08:00
|
|
|
DSTATUS stat = STA_NOINIT;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
switch (pdrv) {
|
|
|
|
case DEV_RAM:
|
|
|
|
if (pDiskioDriver.RAM_disk_ioctl) {
|
|
|
|
result = pDiskioDriver.RAM_disk_ioctl(cmd, buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEV_MMC:
|
|
|
|
if (pDiskioDriver.MMC_disk_ioctl) {
|
|
|
|
result = pDiskioDriver.MMC_disk_ioctl(cmd, buff);
|
|
|
|
}
|
2021-04-13 19:23:11 +08:00
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
break;
|
|
|
|
|
2022-10-21 10:17:49 +08:00
|
|
|
case DEV_FLASH:
|
|
|
|
if (pDiskioDriver.FLASH_disk_ioctl) {
|
|
|
|
result = pDiskioDriver.FLASH_disk_ioctl(cmd, buff);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-06-20 12:25:46 +08:00
|
|
|
case DEV_USB:
|
|
|
|
if (pDiskioDriver.USB_disk_ioctl) {
|
|
|
|
result = pDiskioDriver.USB_disk_ioctl(cmd, buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return RES_PARERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* translate the reslut code here */
|
|
|
|
if (pDiskioDriver.Translate_Result_Code) {
|
|
|
|
stat = pDiskioDriver.Translate_Result_Code(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat;
|
|
|
|
}
|