fatfs: Add Ramdisk support

This commit is contained in:
CTCaer 2020-04-30 15:03:25 +03:00
parent c8d6da5a23
commit a2bb576c03
5 changed files with 86 additions and 11 deletions

View file

@ -47,6 +47,7 @@ DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
/* Generic command (Used by FatFs) */ /* Generic command (Used by FatFs) */
#define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */ #define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */
#define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */ #define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */
#define SET_SECTOR_COUNT 1 /* Set media size (needed at FF_USE_MKFS == 1) */
#define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */ #define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */
#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */ #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */
#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */ #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */

View file

@ -12,6 +12,7 @@
#include "diskio.h" /* FatFs lower layer API */ #include "diskio.h" /* FatFs lower layer API */
#include "../../../../common/memory_map.h" #include "../../../../common/memory_map.h"
#include "../../storage/nx_sd.h" #include "../../storage/nx_sd.h"
#include "../../storage/ramdisk.h"
#include "../../storage/sdmmc.h" #include "../../storage/sdmmc.h"
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
@ -44,7 +45,15 @@ DRESULT disk_read (
UINT count /* Number of sectors to read */ UINT count /* Number of sectors to read */
) )
{ {
return sdmmc_storage_read(&sd_storage, sector, count, buff) ? RES_OK : RES_ERROR; switch (pdrv)
{
case DRIVE_SD:
return sdmmc_storage_read(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;
case DRIVE_RAM:
return ram_disk_read(sector, count, (void *)buff);
}
return RES_ERROR;
} }
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
@ -57,17 +66,74 @@ DRESULT disk_write (
UINT count /* Number of sectors to write */ UINT count /* Number of sectors to write */
) )
{ {
switch (pdrv)
{
case DRIVE_SD:
return sdmmc_storage_write(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR; return sdmmc_storage_write(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;
case DRIVE_RAM:
return ram_disk_write(sector, count, (void *)buff);
}
return RES_ERROR;
} }
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */ /* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
static u32 part_rsvd_size = 0;
DRESULT disk_ioctl ( DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */ BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */ BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */ void *buff /* Buffer to send/receive control data */
) )
{ {
DWORD *buf = (DWORD *)buff;
if (pdrv == DRIVE_SD)
{
switch (cmd)
{
case GET_SECTOR_COUNT:
*buf = sd_storage.sec_cnt - part_rsvd_size;
break;
case GET_BLOCK_SIZE:
*buf = 32768; // Align to 16MB.
break;
}
}
else if (pdrv == DRIVE_RAM)
{
switch (cmd)
{
case GET_SECTOR_COUNT:
*buf = RAM_DISK_SZ >> 9; // 1GB.
break;
case GET_BLOCK_SIZE:
*buf = 2048; // Align to 1MB.
break;
}
}
return RES_OK;
}
DRESULT disk_set_info (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DWORD *buf = (DWORD *)buff;
if (pdrv == DRIVE_SD)
{
switch (cmd)
{
case SET_SECTOR_COUNT:
part_rsvd_size = *buf;
break;
}
}
return RES_OK; return RES_OK;
} }

View file

@ -23,6 +23,11 @@ typedef enum {
RES_PARERR /* 4: Invalid Parameter */ RES_PARERR /* 4: Invalid Parameter */
} DRESULT; } DRESULT;
typedef enum {
DRIVE_SD = 0,
DRIVE_RAM = 1,
} DDRIVE;
/*---------------------------------------*/ /*---------------------------------------*/
/* Prototypes for disk control functions */ /* Prototypes for disk control functions */
@ -33,6 +38,7 @@ DSTATUS disk_status (BYTE pdrv);
DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count); DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
DRESULT disk_set_info (BYTE pdrv, BYTE cmd, void *buff);
/* Disk Status Bits (DSTATUS) */ /* Disk Status Bits (DSTATUS) */
@ -47,6 +53,7 @@ DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
/* Generic command (Used by FatFs) */ /* Generic command (Used by FatFs) */
#define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */ #define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */
#define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */ #define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */
#define SET_SECTOR_COUNT 1 /* Set media size (needed at FF_USE_MKFS == 1) */
#define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */ #define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */
#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */ #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */
#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */ #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */

View file

@ -38,7 +38,7 @@
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */ / f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
#define FF_USE_MKFS 0 #define FF_USE_MKFS 1
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */ /* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
#define FF_FASTFS 1 #define FF_FASTFS 1
@ -60,7 +60,7 @@
/ (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */ / (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */
#define FF_USE_LABEL 0 #define FF_USE_LABEL 1
/* This option switches volume label functions, f_getlabel() and f_setlabel(). /* This option switches volume label functions, f_getlabel() and f_setlabel().
/ (0:Disable or 1:Enable) */ / (0:Disable or 1:Enable) */
@ -155,7 +155,7 @@
*/ */
#define FF_FS_RPATH 0 #define FF_FS_RPATH 1
/* This option configures support for relative path. /* This option configures support for relative path.
/ /
/ 0: Disable relative path and remove related functions. / 0: Disable relative path and remove related functions.
@ -168,12 +168,13 @@
/ Drive/Volume Configurations / Drive/Volume Configurations
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#define FF_VOLUMES 1 #define FF_VOLUMES 2
/* Number of volumes (logical drives) to be used. (1-10) */ /* Number of volumes (logical drives) to be used. (1-10) */
#define FF_STR_VOLUME_ID 0 #define FF_STR_VOLUME_ID 1
#define FF_VOLUME_STRS "sd" // Order is important. Any change to order, must also be reflected to diskio drive enum.
#define FF_VOLUME_STRS "sd","ram"
/* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings. /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
/ When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive / When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
/ number in the path name. FF_VOLUME_STRS defines the volume ID strings for each / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each
@ -241,7 +242,7 @@
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */ / Note that enabling exFAT discards ANSI C (C89) compatibility. */
#define FF_FS_NORTC 1 #define FF_FS_NORTC 0
#define FF_NORTC_MON 1 #define FF_NORTC_MON 1
#define FF_NORTC_MDAY 1 #define FF_NORTC_MDAY 1
#define FF_NORTC_YEAR 2019 #define FF_NORTC_YEAR 2019

View file

@ -151,7 +151,7 @@ bool sd_mount()
else else
{ {
sd_init_done = true; sd_init_done = true;
res = f_mount(&sd_fs, "", 1); res = f_mount(&sd_fs, "sd:", 1);
if (res == FR_OK) if (res == FR_OK)
{ {
sd_mounted = true; sd_mounted = true;
@ -173,7 +173,7 @@ void sd_unmount(bool deinit)
if (sd_init_done && sd_mounted) if (sd_init_done && sd_mounted)
{ {
f_mount(NULL, "", 1); f_mount(NULL, "sd:", 1);
sd_mounted = false; sd_mounted = false;
} }
if (sd_init_done && deinit) if (sd_init_done && deinit)