2023-09-07 23:47:34 +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. */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
#include "ff.h" /* Obtains integer types */
|
|
|
|
|
|
#include "diskio.h" /* Declarations of disk functions */
|
|
|
|
|
|
#include "ram_disk.h"
|
2023-09-21 21:44:17 +08:00
|
|
|
|
#include <stdio.h>
|
2023-09-07 23:47:34 +08:00
|
|
|
|
/* Definitions of physical drive number for each drive */
|
2023-09-09 12:58:57 +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_USB 2 /* Example: Map USB MSD to physical drive 2 */
|
2023-09-07 23:47:34 +08:00
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
/* Get Drive Status */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
DSTATUS disk_status(
|
|
|
|
|
|
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
2023-09-07 23:47:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
DSTATUS stat;
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
switch (pdrv)
|
|
|
|
|
|
{
|
|
|
|
|
|
case DEV_RAM:
|
|
|
|
|
|
result = 0;
|
2023-09-07 23:47:34 +08:00
|
|
|
|
|
|
|
|
|
|
// translate the reslut code here
|
2023-09-09 12:58:57 +08:00
|
|
|
|
stat = RES_OK;
|
2023-09-07 23:47:34 +08:00
|
|
|
|
return stat;
|
|
|
|
|
|
}
|
|
|
|
|
|
return STA_NOINIT;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
/* Inidialize a Drive */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
DSTATUS disk_initialize(
|
|
|
|
|
|
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
2023-09-07 23:47:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
DSTATUS stat;
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
switch (pdrv)
|
|
|
|
|
|
{
|
|
|
|
|
|
case DEV_RAM:
|
|
|
|
|
|
result = 0;
|
|
|
|
|
|
stat = RES_OK;
|
2023-09-07 23:47:34 +08:00
|
|
|
|
// translate the reslut code here
|
|
|
|
|
|
return stat;
|
|
|
|
|
|
}
|
|
|
|
|
|
return STA_NOINIT;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
/* Read Sector(s) */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
2023-09-09 12:58:57 +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 */
|
2023-09-07 23:47:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
DRESULT res;
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
switch (pdrv)
|
|
|
|
|
|
{
|
|
|
|
|
|
case DEV_RAM:
|
2023-09-07 23:47:34 +08:00
|
|
|
|
// translate the reslut code here
|
2023-09-09 12:58:57 +08:00
|
|
|
|
if (ram_disk_read(buff, sector, count) < 0) {
|
|
|
|
|
|
return RES_ERROR;
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
2023-09-07 23:47:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return RES_PARERR;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
/* Write Sector(s) */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
#if FF_FS_READONLY == 0
|
|
|
|
|
|
|
2023-09-09 12:58:57 +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 */
|
2023-09-07 23:47:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
switch (pdrv)
|
|
|
|
|
|
{
|
|
|
|
|
|
case DEV_RAM:
|
2023-09-07 23:47:34 +08:00
|
|
|
|
// translate the arguments here
|
|
|
|
|
|
|
2023-09-21 21:44:17 +08:00
|
|
|
|
if (ram_disk_write((uint8_t *)buff, sector, count) < 0) {
|
2023-09-09 12:58:57 +08:00
|
|
|
|
return RES_ERROR;
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
2023-09-07 23:47:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return RES_PARERR;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
/* Miscellaneous Functions */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
DRESULT disk_ioctl(
|
|
|
|
|
|
BYTE pdrv, /* Physical drive nmuber (0..) */
|
|
|
|
|
|
BYTE cmd, /* Control code */
|
|
|
|
|
|
void *buff /* Buffer to send/receive control data */
|
2023-09-07 23:47:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
DRESULT res;
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
switch (pdrv)
|
|
|
|
|
|
{
|
|
|
|
|
|
case DEV_RAM:
|
|
|
|
|
|
switch (cmd)
|
|
|
|
|
|
{ // fatfs内核使用cmd调用
|
|
|
|
|
|
case GET_SECTOR_COUNT: // sector count, 传入sect_cnt
|
|
|
|
|
|
*(DWORD *)buff = ram_disk_sector_nr();
|
|
|
|
|
|
return RES_OK;
|
|
|
|
|
|
case GET_SECTOR_SIZE: // sector size, 传入block size(SD),单位bytes
|
|
|
|
|
|
*(DWORD *)buff = 512;
|
|
|
|
|
|
return RES_OK;
|
|
|
|
|
|
case GET_BLOCK_SIZE: // block size, 由上文可得,对于SD2.0卡最大8192,最小 1
|
|
|
|
|
|
*(DWORD *)buff = 1; // 单位为 sector(FatFs)
|
|
|
|
|
|
return RES_OK;
|
|
|
|
|
|
case CTRL_SYNC: // 同步命令,貌似FatFs内核用来判断写操作是否完成
|
|
|
|
|
|
return RES_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
default:
|
|
|
|
|
|
printf("No device %d.\n", pdrv);
|
|
|
|
|
|
break;
|
2023-09-07 23:47:34 +08:00
|
|
|
|
}
|
2023-09-09 12:58:57 +08:00
|
|
|
|
return RES_PARERR; // 默认返回参数错误
|
2023-09-07 23:47:34 +08:00
|
|
|
|
}
|