优化: 为fatfs提供统一的设备注册接口,且在linux测试用例中测试通过

This commit is contained in:
hqm 2022-08-11 22:41:05 +08:00 committed by qmfr
parent e9beefeb42
commit 5aade2d78c
100 changed files with 445 additions and 422 deletions

View File

@ -1,234 +0,0 @@
/*-----------------------------------------------------------------------*/
/* 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. */
/*-----------------------------------------------------------------------*/
#include "ff.h" /* Obtains integer types */
#include "diskio.h" /* Declarations of disk functions */
#include "fatfs_port.h"
/* Definitions of physical drive number for each drive */
#define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */
#define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
int result;
switch (pdrv) {
case DEV_RAM :
result = RAM_disk_status();
if(result == 0)
{
stat = RES_OK;
}
else
{
stat = RES_NOTRDY;
}
return stat;
case DEV_MMC :
result = MMC_disk_status();
if(result == 0)
{
stat = RES_OK;
}
else
{
stat = RES_NOTRDY;
}
return stat;
}
return STA_NOINIT;
}
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
int result;
switch (pdrv) {
case DEV_RAM :
result = RAM_disk_initialize();
result = result;
stat = disk_status(pdrv);
return stat;
case DEV_MMC :
result = MMC_disk_initialize();
stat = disk_status(pdrv);
return stat;
}
return STA_NOINIT;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
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 */
)
{
DRESULT res;
int result;
switch (pdrv) {
case DEV_RAM :
result = RAM_disk_read(buff, sector, count);
if(result == 0)
{
res = RES_OK;
}
else
{
res = RES_ERROR;
}
return res;
case DEV_MMC :
result = MMC_disk_read(buff, sector, count);
if(result == 0)
{
res = RES_OK;
}
else
{
res = RES_ERROR;
}
return res;
}
return RES_PARERR;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
#if FF_FS_READONLY == 0
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 */
)
{
DRESULT res;
int result;
switch (pdrv) {
case DEV_RAM :
result = RAM_disk_write(buff, sector, count);
if(result == 0)
{
res = RES_OK;
}
else
{
res = RES_ERROR;
}
return res;
case DEV_MMC :
result = MMC_disk_write(buff, sector, count);
if(result == 0)
{
res = RES_OK;
}
else
{
res = RES_ERROR;
}
return res;
}
return RES_PARERR;
}
#endif
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res;
switch (pdrv) {
case DEV_RAM :
switch(cmd)
{
case CTRL_SYNC:
break;
case GET_SECTOR_COUNT:
*(WORD*)buff = RAM_DISK_SECTION_COUNT;
break;
case GET_SECTOR_SIZE:
*(WORD*)buff = RAM_DISK_SECTION_SIZE;
break;
case GET_BLOCK_SIZE:
*(WORD*)buff = 1;
break;
case CTRL_TRIM:
break;
}
res = RES_OK;
return res;
case DEV_MMC :
switch(cmd)
{
case CTRL_SYNC:
break;
case GET_SECTOR_COUNT:
*(WORD*)buff = MMC_DISK_SECTION_COUNT;
break;
case GET_SECTOR_SIZE:
*(WORD*)buff = MMC_DISK_SECTION_SIZE;
break;
case GET_BLOCK_SIZE:
*(WORD*)buff = 1;
break;
case CTRL_TRIM:
break;
}
res = RES_OK;
return res;
}
return RES_PARERR;
}

View File

@ -13,7 +13,9 @@ OBJ += xcmd.o \
ff.o \
ffunicode.o \
fatfs_port.o \
fs_cmds.o
fs_cmds.o\
fatfs_disk_mmc.o\
fatfs_disk_ram.o
VPATH := ../../src \
../../extensions/test \
@ -21,8 +23,8 @@ VPATH := ../../src \
../../extensions/ex_list \
../../extensions/ex_cmds \
../../extensions/net_cmds \
./FatFs/source \
../../extensions/fs_cmds \
../../extensions/fs_cmds/FatFs/source \
INC += -I./ \
-I../../inc \
@ -31,7 +33,7 @@ INC += -I./ \
-I../../extensions/ex_list \
-I../../extensions/ex_cmds \
-I../../extensions/net_cmds \
-IFatFs/source \
-I../../extensions/fs_cmds/FatFs/source \
-I../../extensions/fs_cmds \
OBJ_WITH_BUILD_DIR:=$(addprefix build/,$(OBJ))

View File

@ -0,0 +1,133 @@
#include "fatfs_disk_mmc.h"
#include "fatfs_port.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "diskio.h"
#define MMC_DISK_SECTION_SIZE (512)
#define MMC_DISK_SECTION_COUNT (1024)
static int mmc_fd = -1;
static DiskIo_t disk;
static FATFS g_fs;
static BYTE g_work[FF_MAX_SS];
static DSTATUS m_disk_status(void)
{
return RES_OK;
}
static DSTATUS m_disk_initialize(void)
{
if(access("./fatfs.img", F_OK) != 0)
{
mmc_fd = open("./fatfs.img", O_CREAT|O_RDWR);
if(mmc_fd >= 0)
{
char buf[MMC_DISK_SECTION_SIZE] = {0};
for(DWORD i=0; i<MMC_DISK_SECTION_COUNT; i++)
{
write(mmc_fd, buf, MMC_DISK_SECTION_SIZE);
}
}
else
{
return RES_NOTRDY;
}
close(mmc_fd);
}
return RES_OK;
}
static DRESULT m_disk_read(BYTE *buff, LBA_t sector, UINT count)
{
DRESULT ret = RES_ERROR;
mmc_fd = open("./fatfs.img", O_RDWR);
if(mmc_fd >= 0)
{
lseek(mmc_fd, sector*MMC_DISK_SECTION_SIZE, L_SET);
int read_len = read(mmc_fd, buff, count*MMC_DISK_SECTION_SIZE);
if(read_len == count*MMC_DISK_SECTION_SIZE)
{
ret = RES_OK;
}
}
close(mmc_fd);
return ret;
}
static DRESULT m_disk_write(const BYTE *buff, LBA_t sector, UINT count)
{
DRESULT ret = RES_ERROR;
mmc_fd = open("./fatfs.img", O_RDWR);
if(mmc_fd >= 0)
{
lseek(mmc_fd, sector*MMC_DISK_SECTION_SIZE, L_SET);
int write_len = write(mmc_fd, buff, count*MMC_DISK_SECTION_SIZE);
if(write_len == count*MMC_DISK_SECTION_SIZE)
{
ret = RES_OK;
}
}
close(mmc_fd);
return ret;
}
static DRESULT m_disk_ioctl(BYTE cmd, void *buff)
{
switch(cmd)
{
case CTRL_SYNC:
break;
case GET_SECTOR_COUNT:
*(WORD*)buff = MMC_DISK_SECTION_COUNT;
break;
case GET_SECTOR_SIZE:
*(WORD*)buff = MMC_DISK_SECTION_SIZE;
break;
case GET_BLOCK_SIZE:
*(WORD*)buff = 1;
break;
case CTRL_TRIM:
break;
}
return RES_OK;
}
int mmc_disk_init(void)
{
FRESULT res;
disk.disk_name = "MMC Disk";
disk.disk_initialize = m_disk_initialize;
disk.disk_ioctl = m_disk_ioctl;
disk.disk_read = m_disk_read;
disk.disk_status = m_disk_status;
disk.disk_write = m_disk_write;
if(f_disk_regist(&disk, 1) == -1)
{
printf("MMC disk regist ERROR!\r\n");
return FR_DISK_ERR;
}
res = f_mount(&g_fs, "1:", 1); //挂载文件系统 "1:"就是挂载的设备号为1的设备
if (res == FR_NO_FILESYSTEM) //FR_NO_FILESYSTEM值为13表示没有有效的设备
{
res = f_mkfs("1:", 0, g_work, sizeof(g_work));
res = f_mount(NULL, "1:", 1); //取消文件系统
res = f_mount(&g_fs, "1:", 1); //挂载文件系统
}
if(res == FR_OK)
{
printf("MMC fs init OK!\r\n");
}
else
{
printf("MMC fs init ERROR!\r\n");
}
return res;
}

View File

@ -0,0 +1,8 @@
#ifndef FATFS_DISK_MMC_H
#define FATFS_DISK_MMC_H
#include "ff.h"
int mmc_disk_init(void);
#endif

View File

@ -0,0 +1,88 @@
#include "fatfs_disk_mmc.h"
#include "fatfs_port.h"
#include <string.h>
static DiskIo_t disk;
#define RAM_DISK_SECTION_SIZE (512)
#define RAM_DISK_SECTION_COUNT (1024)
static char *ram_buf[RAM_DISK_SECTION_COUNT][RAM_DISK_SECTION_SIZE];
static FATFS g_fs;
static BYTE g_work[FF_MAX_SS];
static DSTATUS m_disk_status(void)
{
return 0;
}
static DSTATUS m_disk_initialize(void)
{
return 0;
}
static DRESULT m_disk_read(BYTE *buff, LBA_t sector, UINT count)
{
memcpy(buff, &ram_buf[sector], count * RAM_DISK_SECTION_SIZE);
return 0;
}
static DRESULT m_disk_write(const BYTE *buff, LBA_t sector, UINT count)
{
memcpy(&ram_buf[sector], buff, count * RAM_DISK_SECTION_SIZE);
return 0;
}
static DRESULT m_disk_ioctl(BYTE cmd, void *buff)
{
switch(cmd)
{
case CTRL_SYNC:
break;
case GET_SECTOR_COUNT:
*(WORD*)buff = RAM_DISK_SECTION_COUNT;
break;
case GET_SECTOR_SIZE:
*(WORD*)buff = RAM_DISK_SECTION_SIZE;
break;
case GET_BLOCK_SIZE:
*(WORD*)buff = 1;
break;
case CTRL_TRIM:
break;
}
return RES_OK;
}
int ram_disk_init(void)
{
FRESULT res;
disk.disk_name = "RAM Disk";
disk.disk_initialize = m_disk_initialize;
disk.disk_ioctl = m_disk_ioctl;
disk.disk_read = m_disk_read;
disk.disk_status = m_disk_status;
disk.disk_write = m_disk_write;
if(f_disk_regist(&disk, 0) == -1)
{
printf("RAM disk regist ERROR!\r\n");
return FR_DISK_ERR;
}
res = f_mount(&g_fs, "0:", 1); //挂载文件系统 "1:"就是挂载的设备号为1的设备
if (res == FR_NO_FILESYSTEM) //FR_NO_FILESYSTEM值为13表示没有有效的设备
{
res = f_mkfs("0:", 0, g_work, sizeof(g_work));
res = f_mount(NULL, "0:", 1); //取消文件系统
res = f_mount(&g_fs, "0:", 1); //挂载文件系统
}
if(res == FR_OK)
{
printf("RAM fs init OK!\r\n");
}
else
{
printf("RAM fs init ERROR!\r\n");
}
return res;
}

View File

@ -0,0 +1,8 @@
#ifndef FATFS_DISK_RAM_H
#define FATFS_DISK_RAM_H
#include "ff.h"
int ram_disk_init(void);
#endif

View File

@ -1,101 +0,0 @@
/*
* @Author: your name
* @Date: 2021-10-09 13:55:09
* @LastEditTime: 2021-10-09 17:04:56
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/example/linux/fatfs_port.c
*/
#include "fatfs_port.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
char *ram_buf[RAM_DISK_SECTION_COUNT][RAM_DISK_SECTION_SIZE];
int mmc_fd = -1;
int RAM_disk_status(void)
{
return 0;
}
int MMC_disk_status(void)
{
return 0;
}
int RAM_disk_initialize(void)
{
return 0;
}
int MMC_disk_initialize(void)
{
if(access("./fatfs.img", F_OK) != 0)
{
mmc_fd = open("./fatfs.img", O_CREAT|O_RDWR);
if(mmc_fd >= 0)
{
char buf[MMC_DISK_SECTION_SIZE] = {0};
for(DWORD i=0; i<MMC_DISK_SECTION_COUNT; i++)
{
write(mmc_fd, buf, MMC_DISK_SECTION_SIZE);
}
}
else
{
return -1;
}
close(mmc_fd);
}
return 0;
}
int RAM_disk_read(BYTE *buff, LBA_t sector, UINT count)
{
memcpy(buff, &ram_buf[sector], count * RAM_DISK_SECTION_SIZE);
return 0;
}
int MMC_disk_read(BYTE *buff, LBA_t sector, UINT count)
{
int ret = -1;
mmc_fd = open("./fatfs.img", O_RDWR);
if(mmc_fd >= 0)
{
lseek(mmc_fd, sector*MMC_DISK_SECTION_SIZE, L_SET);
int read_len = read(mmc_fd, buff, count*MMC_DISK_SECTION_SIZE);
if(read_len == count*MMC_DISK_SECTION_SIZE)
{
ret = 0;
}
}
close(mmc_fd);
return ret;
}
int RAM_disk_write(const BYTE *buff, LBA_t sector, UINT count)
{
memcpy(&ram_buf[sector], buff, count * RAM_DISK_SECTION_SIZE);
return 0;
}
int MMC_disk_write(const BYTE *buff, LBA_t sector, UINT count)
{
int ret = -1;
mmc_fd = open("./fatfs.img", O_RDWR);
if(mmc_fd >= 0)
{
lseek(mmc_fd, sector*MMC_DISK_SECTION_SIZE, L_SET);
int write_len = write(mmc_fd, buff, count*MMC_DISK_SECTION_SIZE);
if(write_len == count*MMC_DISK_SECTION_SIZE)
{
ret = 0;
}
}
close(mmc_fd);
return ret;
}

View File

@ -1,33 +0,0 @@
/*
* @Author: your name
* @Date: 2021-10-09 13:55:20
* @LastEditTime: 2021-10-09 15:51:46
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/example/linux/fatfs_port.h
*/
#ifndef FATFS_PORT_H
#define FATFS_PORT_H
#include "ff.h"
#define RAM_DISK_SECTION_SIZE (512)
#define RAM_DISK_SECTION_COUNT (1024)
#define MMC_DISK_SECTION_SIZE (512)
#define MMC_DISK_SECTION_COUNT (1024)
int RAM_disk_status(void);
int MMC_disk_status(void);
int RAM_disk_initialize(void);
int MMC_disk_initialize(void);
int RAM_disk_read(BYTE *buff, LBA_t sector, UINT count);
int MMC_disk_read(BYTE *buff, LBA_t sector, UINT count);
int RAM_disk_write(const BYTE *buff, LBA_t sector, UINT count);
int MMC_disk_write(const BYTE *buff, LBA_t sector, UINT count);
#endif

View File

@ -18,11 +18,9 @@
#include "fs_cmds.h"
#include "socket_cmds.h"
#include "ff.h"
#include "fatfs_disk_mmc.h"
#include "fatfs_disk_ram.h"
FATFS g_fs_ram;
FATFS g_fs_mmc;
BYTE g_work_ram[FF_MAX_SS]; //一定是一个全局变量
BYTE g_work_mmc[FF_MAX_SS]; //一定是一个全局变量
FIL g_fp;
int getch(void)
@ -98,56 +96,11 @@ void fatfs_test(char* path)
}
}
int ram_fatfs_init(void)
{
FRESULT res; //局部变量
res = f_mount(&g_fs_ram, "0:", 1); //挂载文件系统 "1:"就是挂载的设备号为1的设备
if (res == FR_NO_FILESYSTEM) //FR_NO_FILESYSTEM值为13表示没有有效的设备
{
res = f_mkfs("0:", 0, g_work_ram, sizeof(g_work_ram));
res = f_mount(NULL, "0:", 1); //取消文件系统
res = f_mount(&g_fs_ram, "0:", 1); //挂载文件系统
}
if(res == FR_OK)
{
printf("RAM fs init OK!\r\n");
}
else
{
printf("RAM fs init ERROR!\r\n");
}
return res;
}
int mmc_fatfs_init(void)
{
FRESULT res; //局部变量
res = f_mount(&g_fs_mmc, "1:", 1); //挂载文件系统 "1:"就是挂载的设备号为1的设备
if (res == FR_NO_FILESYSTEM) //FR_NO_FILESYSTEM值为13表示没有有效的设备
{
res = f_mkfs("1:", 0, g_work_mmc, sizeof(g_work_mmc));
res = f_mount(NULL, "1:", 1); //取消文件系统
res = f_mount(&g_fs_mmc, "1:", 1); //挂载文件系统
}
if(res == FR_OK)
{
printf("MMC fs init OK!\r\n");
}
else
{
printf("MMC fs init ERROR!\r\n");
}
return res;
}
int main(void)
{
xcmd_init(cmd_get_char, cmd_put_char);
ram_fatfs_init();
mmc_fatfs_init();
ram_disk_init();
mmc_disk_init();
test_cmd_init();
test_keys_init();
user_keys_init();

View File

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

Before

Width:  |  Height:  |  Size: 1.0 KiB

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB

View File

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

View File

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View File

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 68 KiB

After

Width:  |  Height:  |  Size: 68 KiB

View File

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@ -0,0 +1,119 @@
/*-----------------------------------------------------------------------*/
/* 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. */
/*-----------------------------------------------------------------------*/
#include "ff.h" /* Obtains integer types */
#include "diskio.h" /* Declarations of disk functions */
#include "fatfs_port.h"
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DiskIo_t * disk = NULL;
disk = f_disk_get(pdrv);
if(disk)
{
return disk->disk_status();
}
return STA_NOINIT;
}
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DiskIo_t * disk = NULL;
disk = f_disk_get(pdrv);
if(disk)
{
return disk->disk_initialize();
}
return STA_NOINIT;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
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 */
)
{
DiskIo_t * disk = NULL;
disk = f_disk_get(pdrv);
if(disk)
{
return disk->disk_read(buff, sector, count);
}
return RES_PARERR;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
#if FF_FS_READONLY == 0
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 */
)
{
DiskIo_t * disk = NULL;
disk = f_disk_get(pdrv);
if(disk)
{
return disk->disk_write(buff, sector, count);
}
return RES_PARERR;
}
#endif
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DiskIo_t * disk = NULL;
disk = f_disk_get(pdrv);
if(disk)
{
return disk->disk_ioctl(cmd, buff);
}
return RES_PARERR;
}

View File

@ -0,0 +1,50 @@
/*
* @Author: your name
* @Date: 2021-10-09 13:55:09
* @LastEditTime: 2021-10-09 17:04:56
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/example/linux/fatfs_port.c
*/
#include "fatfs_port.h"
#include <stdio.h>
static DiskIo_t * g_Disks[FF_VOLUMES] = {0};
int f_disk_regist(DiskIo_t * disk, int id)
{
if(!disk)
{
return -1;
}
if(id<0)
{
for(int i=0; i<FF_VOLUMES; i++)
{
if(!g_Disks[id])
{
g_Disks[i] = disk;
return i;
}
}
}
else
{
if((id < FF_VOLUMES) && !g_Disks[id])
{
g_Disks[id] = disk;
return id;
}
}
return -1;
}
DiskIo_t * f_disk_get(int id)
{
if(id < FF_VOLUMES)
{
return g_Disks[id];
}
return NULL;
}

View File

@ -0,0 +1,30 @@
/*
* @Author: your name
* @Date: 2021-10-09 13:55:20
* @LastEditTime: 2021-10-09 15:51:46
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /xcmd/example/linux/fatfs_port.h
*/
#ifndef FATFS_PORT_H
#define FATFS_PORT_H
#include "ff.h"
#include "diskio.h"
#include <stdio.h>
typedef struct __DiskIo
{
char * disk_name;
DSTATUS (*disk_status)(void);
DSTATUS (*disk_initialize)(void);
DRESULT (*disk_read)(BYTE *buff, LBA_t sector, UINT count);
DRESULT (*disk_write)(const BYTE *buff, LBA_t sector, UINT count);
DRESULT (*disk_ioctl) (BYTE cmd, void *buff);
}DiskIo_t;
int f_disk_regist(DiskIo_t * disk, int id);
DiskIo_t * f_disk_get(int id);
#endif