Files
pwm32e_pro/component/xc6xx_drivers/Drivers/USB/FatFS/src/diskio.c
T
moyuhai 41a602f89c V1.0
2026-06-09 16:39:17 +08:00

232 lines
6.2 KiB
C

/*!
* \file diskio.c
*
* \brief Target disk I/O implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "diskio.h"
#include "ff_gen_drv.h"
#include "msc_flash.h"
#include "time_conv.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------
-----------------------------------------*/
#if defined(__GNUC__)
#ifndef __weak
#define __weak __attribute__((weak))
#endif
#endif
#define DEV_USB 0 /* Example: Map USB MSD to physical drive 0 */
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------
-----------------------------------------*/
extern Disk_drvTypeDef disk;
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------
-----------------------------------------*/
/**
* @brief Gets Disk Status
* @param pdrv: Physical drive number (0..)
* @retval DSTATUS: Operation status
*/
DSTATUS disk_status(BYTE pdrv /* Physical drive number to identify the drive */
)
{
DSTATUS stat = STA_NOINIT;
// stat = disk.drv[pdrv]->disk_status(disk.lun[pdrv]);
if (FAT_FLASH_ID == Fat_Flash_ReadID()) {
stat = RES_OK;
} else {
stat = STA_NOINIT;
}
return stat;
}
/**
* @brief Initializes a Drive
* @param pdrv: Physical drive number (0..)
* @retval DSTATUS: Operation status
*/
DSTATUS
disk_initialize(BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat = STA_NOINIT;
switch (pdrv) {
case DEV_USB: {
if (true == Disk_Init())
stat = RES_OK;
} break;
}
// if(disk.is_initialized[pdrv] == 0)
// {
// disk.is_initialized[pdrv] = 1;
// stat = disk.drv[pdrv]->disk_initialize(disk.lun[pdrv]);
// }
return stat;
}
/**
* @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
*/
DRESULT disk_read(BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to read */
)
{
DRESULT res = RES_ERROR;
switch (pdrv) {
case DEV_USB: {
if (Disk_UnitRead(sector, count, buff) == true) {
res = RES_OK;
}
} break;
}
// res = disk.drv[pdrv]->disk_read(disk.lun[pdrv], buff, sector, count);
return res;
}
/**
* @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
*/
#if _USE_WRITE == 1
DRESULT disk_write(BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to write */
)
{
DRESULT res = RES_ERROR;
switch (pdrv) {
case DEV_USB: {
if (Disk_UnitWrite(sector, count, (uint8_t *)buff) == true) {
res = RES_OK;
}
} break;
}
// res = disk.drv[pdrv]->disk_write(disk.lun[pdrv], buff, sector, count);
return res;
}
#endif /* _USE_WRITE == 1 */
/**
* @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
*/
#if _USE_IOCTL == 1
DRESULT disk_ioctl(BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res = RES_PARERR;
switch (pdrv) {
case DEV_USB: {
switch (cmd) {
case CTRL_SYNC:
res = RES_OK;
break;
case GET_SECTOR_COUNT: {
*(DWORD *)buff = Disk_UnitCount();
res = RES_OK;
break;
}
case GET_SECTOR_SIZE: {
*(DWORD *)buff = Disk_UnitSize();
res = RES_OK;
break;
}
case GET_BLOCK_SIZE: {
*(DWORD *)buff = 1;
res = RES_OK;
break;
}
default:
res = RES_PARERR;
break;
}
} break;
}
// res = disk.drv[pdrv]->disk_ioctl(disk.lun[pdrv], cmd, buff);
return res;
}
#endif /* _USE_IOCTL == 1 */
/**
* @brief Gets Time from RTC
* @param None
* @retval Time in DWORD
*/
__weak DWORD get_fattime(void)
{
uint32_t fattime;
// utc sec convert to utc date
DateTime_t tmp_date = UTC_SecToDate(Utc_TotalSec);
tmp_date = UTC_DateToZoneDate(TIME_ZONE, YEAR_BASE, tmp_date.year,
tmp_date.month, tmp_date.day, tmp_date.hour,
tmp_date.min, tmp_date.sec, tmp_date.week);
fattime = ((YEAR_BASE + tmp_date.year - 1980) << 25) |
(tmp_date.month << 21) | (tmp_date.day << 16) |
(tmp_date.hour << 11) | (tmp_date.min << 5) | (tmp_date.sec >> 1);
return fattime;
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/