Files
mkrtos-real/mkrtos_user/server/fs/fatfs/ram_disk_drv/diskio.c

234 lines
6.2 KiB
C
Raw Normal View History

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 */
2023-09-21 21:44:17 +08:00
#include <stdio.h>
#include "blk_drv_cli.h"
#include "ns_cli.h"
#include "u_factory.h"
#include "u_vmam.h"
#include "u_hd_man.h"
#include "u_sleep.h"
#include <string.h>
#include "u_share_mem.h"
2023-09-07 23:47:34 +08:00
/* Definitions of physical drive number for each drive */
#define DEV_MK_BLOCK 0 /* Example: Map Ramdisk to physical drive 0 */
2023-09-07 23:47:34 +08:00
static obj_handler_t dev_hd;
static obj_handler_t shm_hd;
static addr_t dev_shm_mem;
static addr_t dev_shm_size;
static blk_drv_info_t blk_info;
int disk_set_dev_path(int pdrv, const char *dev)
{
int ret;
switch (DEV_MK_BLOCK)
{
case DEV_MK_BLOCK:
{
int try_cn = 0;
again:
ret = ns_query_svr(dev, &dev_hd, 1);
if (ret < 0)
{
try_cn++;
if (try_cn > 100)
{
return ret;
}
u_sleep_ms(5);
goto again;
}
ret = blk_drv_cli_info(dev_hd, &blk_info);
if (ret < 0)
{
return ret;
}
}
break;
default:
return -1;
}
return 0;
}
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_MK_BLOCK:
2023-09-09 12:58:57 +08:00
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_MK_BLOCK:
{
msg_tag_t tag;
shm_hd = handler_alloc();
if (shm_hd == HANDLER_INVALID)
{
printf("handler alloc failed.\n");
return RES_ERROR;
}
tag = facotry_create_share_mem(FACTORY_PROT, vpage_create_raw3(KOBJ_ALL_RIGHTS, 0, shm_hd),
SHARE_MEM_CNT_BUDDY_CNT, blk_info.blk_size);
if (msg_tag_get_val(tag) < 0)
{
handler_free(shm_hd);
printf("share mem create failed.\n");
return RES_ERROR;
}
tag = share_mem_map(shm_hd, vma_addr_create(VPAGE_PROT_RW, VMA_ADDR_RESV, 0), &dev_shm_mem, &dev_shm_size);
if (msg_tag_get_val(tag) < 0)
{
handler_del_umap(shm_hd);
printf("share mem map failed.\n");
return RES_ERROR;
}
2023-09-09 12:58:57 +08:00
stat = RES_OK;
2023-09-07 23:47:34 +08:00
// translate the reslut code here
return stat;
}
}
2023-09-07 23:47:34 +08:00
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_MK_BLOCK:
2023-09-07 23:47:34 +08:00
// translate the reslut code here
for (umword_t i = sector; i < sector + count; i++)
{
if (blk_drv_cli_read(dev_hd, shm_hd, blk_info.blk_size, i) < 0)
{
return RES_ERROR;
}
memcpy(buff + (i - sector) * blk_info.blk_size, (void *)dev_shm_mem, blk_info.blk_size);
2023-09-09 12:58:57 +08:00
}
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_MK_BLOCK:
2023-09-07 23:47:34 +08:00
// translate the arguments here
for (umword_t i = sector; i < sector + count; i++)
{
memcpy((void *)dev_shm_mem, buff + (i - sector) * blk_info.blk_size, blk_info.blk_size);
if (blk_drv_cli_write(dev_hd, shm_hd, blk_info.blk_size, i) < 0)
{
return RES_ERROR;
}
2023-09-09 12:58:57 +08:00
}
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_MK_BLOCK:
{
2023-09-09 12:58:57 +08:00
switch (cmd)
{ // fatfs内核使用cmd调用
case GET_SECTOR_COUNT: // sector count, 传入sect_cnt
*(DWORD *)buff = blk_info.blk_nr;
2023-09-09 12:58:57 +08:00
return RES_OK;
case GET_SECTOR_SIZE: // sector size, 传入block size(SD),单位bytes
*(DWORD *)buff = blk_info.blk_size;
2023-09-09 12:58:57 +08:00
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;
}
}
2023-09-09 12:58:57 +08:00
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
}