/*-----------------------------------------------------------------------*/1/* Low level disk I/O module skeleton for FatFs */2/* (C) ChaN, 2016 */3/* (C) CTCaer, 2018-2020 */4/*-----------------------------------------------------------------------*/5/* If a working storage control module is available, it should be */6/* attached to the FatFs via a glue function rather than modifying it. */7/* This is an example of glue functions to attach various exsisting */8/* storage control modules to the FatFs module with a defined API. */9/*-----------------------------------------------------------------------*/1011#include <string.h>1213#include <bdk.h>1415#include <libs/fatfs/diskio.h> /* FatFs lower layer API */1617static u32 sd_rsvd_sectors = 0;18static u32 ramdisk_sectors = 0;19static u32 emummc_sectors = 0;2021/*-----------------------------------------------------------------------*/22/* Get Drive Status */23/*-----------------------------------------------------------------------*/24DSTATUS disk_status (25BYTE pdrv /* Physical drive nmuber to identify the drive */26)27{28return 0;29}3031/*-----------------------------------------------------------------------*/32/* Inidialize a Drive */33/*-----------------------------------------------------------------------*/34DSTATUS disk_initialize (35BYTE pdrv /* Physical drive nmuber to identify the drive */36)37{38return 0;39}4041/*-----------------------------------------------------------------------*/42/* Read Sector(s) */43/*-----------------------------------------------------------------------*/44DRESULT disk_read (45BYTE pdrv, /* Physical drive nmuber to identify the drive */46BYTE *buff, /* Data buffer to store read data */47DWORD sector, /* Start sector in LBA */48UINT count /* Number of sectors to read */49)50{51switch (pdrv)52{53case DRIVE_SD:54return sdmmc_storage_read(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;55case DRIVE_RAM:56return ram_disk_read(sector, count, (void *)buff);57case DRIVE_EMMC:58return sdmmc_storage_read(&emmc_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;59case DRIVE_BIS:60case DRIVE_EMU:61return nx_emmc_bis_read(sector, count, (void *)buff) ? RES_OK : RES_ERROR;62}6364return RES_ERROR;65}6667/*-----------------------------------------------------------------------*/68/* Write Sector(s) */69/*-----------------------------------------------------------------------*/70DRESULT disk_write (71BYTE pdrv, /* Physical drive nmuber to identify the drive */72const BYTE *buff, /* Data to be written */73DWORD sector, /* Start sector in LBA */74UINT count /* Number of sectors to write */75)76{77switch (pdrv)78{79case DRIVE_SD:80return sdmmc_storage_write(&sd_storage, sector, count, (void *)buff) ? RES_OK : RES_ERROR;81case DRIVE_RAM:82return ram_disk_write(sector, count, (void *)buff);83case DRIVE_EMMC:84case DRIVE_BIS:85return RES_WRPRT;86case DRIVE_EMU:87return nx_emmc_bis_write(sector, count, (void *)buff) ? RES_OK : RES_ERROR;88}8990return RES_ERROR;91}9293/*-----------------------------------------------------------------------*/94/* Miscellaneous Functions */95/*-----------------------------------------------------------------------*/96DRESULT disk_ioctl (97BYTE pdrv, /* Physical drive nmuber (0..) */98BYTE cmd, /* Control code */99void *buff /* Buffer to send/receive control data */100)101{102DWORD *buf = (DWORD *)buff;103104if (pdrv == DRIVE_SD)105{106switch (cmd)107{108case GET_SECTOR_COUNT:109*buf = sd_storage.sec_cnt - sd_rsvd_sectors;110break;111case GET_BLOCK_SIZE:112*buf = 32768; // Align to 16MB.113break;114}115}116else if (pdrv == DRIVE_RAM)117{118switch (cmd)119{120case GET_SECTOR_COUNT:121*buf = ramdisk_sectors;122break;123case GET_BLOCK_SIZE:124*buf = 2048; // Align to 1MB.125break;126}127}128else if (pdrv == DRIVE_EMU)129{130switch (cmd)131{132case GET_SECTOR_COUNT:133*buf = emummc_sectors;134break;135case GET_BLOCK_SIZE:136*buf = 32768; // Align to 16MB.137break;138}139}140else // Catch all for unknown devices.141{142switch (cmd)143{144case CTRL_SYNC:145break;146case GET_SECTOR_COUNT:147case GET_BLOCK_SIZE:148*buf = 0; // Zero value to force default or abort.149break;150}151}152153return RES_OK;154}155156DRESULT disk_set_info (157BYTE pdrv, /* Physical drive nmuber (0..) */158BYTE cmd, /* Control code */159void *buff /* Buffer to send/receive control data */160)161{162DWORD *buf = (DWORD *)buff;163164if (cmd == SET_SECTOR_COUNT)165{166switch (pdrv)167{168case DRIVE_SD:169sd_rsvd_sectors = *buf;170break;171case DRIVE_RAM:172ramdisk_sectors = *buf;173break;174case DRIVE_EMU:175emummc_sectors = *buf;176break;177}178}179180return RES_OK;181}182183184