2023-09-11 09:30:56 +00:00
|
|
|
#include <furi.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal.h>
|
2023-09-11 09:30:56 +00:00
|
|
|
#include "user_diskio.h"
|
2023-02-26 11:28:51 +00:00
|
|
|
#include "sector_cache.h"
|
2021-05-18 09:23:14 +00:00
|
|
|
|
2023-03-20 13:09:10 +00:00
|
|
|
static DSTATUS driver_initialize(BYTE pdrv);
|
|
|
|
static DSTATUS driver_status(BYTE pdrv);
|
|
|
|
static DRESULT driver_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count);
|
|
|
|
static DRESULT driver_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
|
|
|
|
static DRESULT driver_ioctl(BYTE pdrv, BYTE cmd, void* buff);
|
|
|
|
|
|
|
|
Diskio_drvTypeDef sd_fatfs_driver = {
|
|
|
|
driver_initialize,
|
|
|
|
driver_status,
|
|
|
|
driver_read,
|
|
|
|
driver_write,
|
|
|
|
driver_ioctl,
|
2021-05-18 09:23:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initializes a Drive
|
|
|
|
* @param pdrv: Physical drive number (0..)
|
|
|
|
* @retval DSTATUS: Operation status
|
|
|
|
*/
|
2023-03-20 13:09:10 +00:00
|
|
|
static DSTATUS driver_initialize(BYTE pdrv) {
|
2023-04-06 02:26:33 +00:00
|
|
|
UNUSED(pdrv);
|
|
|
|
return RES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Gets Disk Status
|
|
|
|
* @param pdrv: Physical drive number (0..)
|
|
|
|
* @retval DSTATUS: Operation status
|
|
|
|
*/
|
|
|
|
static DSTATUS driver_status(BYTE pdrv) {
|
2023-09-11 09:30:56 +00:00
|
|
|
UNUSED(pdrv);
|
|
|
|
DSTATUS status = 0;
|
|
|
|
if(furi_hal_sd_get_card_state() != FuriStatusOk) {
|
|
|
|
status = STA_NOINIT;
|
|
|
|
}
|
2021-05-18 09:23:14 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Reads Sector(s)
|
|
|
|
* @param pdrv: Physical drive number (0..)
|
|
|
|
* @param *buff: Data buffer to store read data
|
|
|
|
* @param sector: Sector address (LBA)
|
|
|
|
* @param count: Number of sectors to read (1..128)
|
|
|
|
* @retval DRESULT: Operation result
|
|
|
|
*/
|
2023-03-20 13:09:10 +00:00
|
|
|
static DRESULT driver_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(pdrv);
|
2023-09-11 09:30:56 +00:00
|
|
|
FuriStatus status = furi_hal_sd_read_blocks((uint32_t*)buff, (uint32_t)(sector), count);
|
|
|
|
return status == FuriStatusOk ? RES_OK : RES_ERROR;
|
2021-05-18 09:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Writes Sector(s)
|
|
|
|
* @param pdrv: Physical drive number (0..)
|
|
|
|
* @param *buff: Data to be written
|
|
|
|
* @param sector: Sector address (LBA)
|
|
|
|
* @param count: Number of sectors to write (1..128)
|
|
|
|
* @retval DRESULT: Operation result
|
|
|
|
*/
|
2023-03-20 13:09:10 +00:00
|
|
|
static DRESULT driver_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(pdrv);
|
2023-09-11 09:30:56 +00:00
|
|
|
FuriStatus status = furi_hal_sd_write_blocks((uint32_t*)buff, (uint32_t)(sector), count);
|
|
|
|
return status == FuriStatusOk ? RES_OK : RES_ERROR;
|
2021-05-18 09:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief I/O control operation
|
|
|
|
* @param pdrv: Physical drive number (0..)
|
|
|
|
* @param cmd: Control code
|
|
|
|
* @param *buff: Buffer to send/receive control data
|
|
|
|
* @retval DRESULT: Operation result
|
|
|
|
*/
|
2023-03-20 13:09:10 +00:00
|
|
|
static DRESULT driver_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
|
2021-05-18 09:23:14 +00:00
|
|
|
DRESULT res = RES_ERROR;
|
2023-09-11 09:30:56 +00:00
|
|
|
FuriHalSdInfo sd_info;
|
2021-05-18 09:23:14 +00:00
|
|
|
|
2023-09-11 09:30:56 +00:00
|
|
|
DSTATUS status = driver_status(pdrv);
|
2023-04-06 02:26:33 +00:00
|
|
|
if(status & STA_NOINIT) return RES_NOTRDY;
|
|
|
|
|
2021-05-18 09:23:14 +00:00
|
|
|
switch(cmd) {
|
|
|
|
/* Make sure that no pending write process */
|
|
|
|
case CTRL_SYNC:
|
|
|
|
res = RES_OK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Get number of sectors on the disk (DWORD) */
|
|
|
|
case GET_SECTOR_COUNT:
|
2023-09-11 09:30:56 +00:00
|
|
|
furi_hal_sd_info(&sd_info);
|
|
|
|
*(DWORD*)buff = sd_info.logical_block_count;
|
2021-05-18 09:23:14 +00:00
|
|
|
res = RES_OK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Get R/W sector size (WORD) */
|
|
|
|
case GET_SECTOR_SIZE:
|
2023-09-11 09:30:56 +00:00
|
|
|
furi_hal_sd_info(&sd_info);
|
|
|
|
*(WORD*)buff = sd_info.logical_block_size;
|
2021-05-18 09:23:14 +00:00
|
|
|
res = RES_OK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Get erase block size in unit of sector (DWORD) */
|
|
|
|
case GET_BLOCK_SIZE:
|
2023-09-11 09:30:56 +00:00
|
|
|
furi_hal_sd_info(&sd_info);
|
|
|
|
*(DWORD*)buff = sd_info.logical_block_size;
|
2021-05-18 09:23:14 +00:00
|
|
|
res = RES_OK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
res = RES_PARERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|