20240909-DXSPX-emb/Src/Usr/Driver/UpDriver/fatfs/diskio.c

199 lines
6.1 KiB
C
Raw Normal View History

/*-----------------------------------------------------------------------*/
/* 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 "DrvRtcI2c.h"
#include "DrvSdSpi.h"
/* Definitions of physical drive number for each drive */
#define SD_CARD 0
#define EX_FLASH 1
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber (0..) */
)
{
return 0;
}
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
uint8_t res=0;
switch(pdrv)
{
case SD_CARD://SD卡
res = Drv_SdInit();
if(res)// SPI的bug,在sd卡操作失败的时候如果不执行下面的语句,可能导致SPI读写异常
{
Drv_SdSpiSetSpeed(USR_DRV_SD_HIGH_SPEED_MODE);
Drv_SdSpiRWByte(0xff);//提供额外的8个时钟
}
break;
case EX_FLASH://外部flash
break;
default:
res=1;
}
if(res)return STA_NOINIT;
else return 0; //初始化成功
}
/*-----------------------------------------------------------------------*/
/* 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 */
)
{
uint8_t result;
if (!count) {
return RES_PARERR;// count不能等于0否则返回参数错误
}
switch (pdrv) {
case SD_CARD :
result = Drv_SdReadDisk(buff, sector, count);
if (result != 0) {
Drv_SdSpiRWByte(0xff);
return RES_ERROR;
}
return RES_OK;
}
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 */
)
{
uint8_t result;
switch (pdrv) {
case SD_CARD :
// translate the arguments here
result = Drv_SdWriteDisk((uint8_t*)buff, sector, count);
if (result == 0x00) {
return RES_OK;
} else {
return RES_ERROR;
}
}
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 result;
if(pdrv == SD_CARD)
{
switch(cmd)
{
case CTRL_SYNC:
Drv_SdSpiCs(0);
if(Drv_SdWaitReady() == 0) {
result = RES_OK;
} else {
result = RES_ERROR;
}
Drv_SdSpiCs(1);
break;
/* 由于SD卡的SECTOR与BLOCK跟Fatfs中的含义不同Fatfs的SECTOR等于
SD卡的BLOCKFatfs的SECTOR为固定值512 byteFatfs的block
size即是SD卡AUsize/SD卡的最小BLOCK5121GB的AUsizemax
4MB1~8192
*/
case GET_SECTOR_SIZE:
*(WORD*)buff = 512; // 因为在宏定义设置中已经设置设置为512。
result = RES_OK;
break;
case GET_BLOCK_SIZE:
// 单位为 sector(FatFs)取值范围为1~8192(8192根据SD存储大小变化而变化)
*(WORD*)buff = 8;
result = RES_OK;
break;
case GET_SECTOR_COUNT:
*(DWORD*)buff = Drv_SdGetSectorCount();
result = RES_OK;
break;
default:
result = RES_PARERR;
break;
}
} else {
result = RES_ERROR;
}
return result;
}
DWORD get_fattime (void)
{
USR_DRV_RTC_I2C_TIME time;
if (0 == DrvRtcI2CGetTime(&time)) {
if (time.year != 0 && time.month != 0 && time.date != 0) {
return (((uint32_t)time.year - 1980) << 25)
| ((uint32_t)time.month << 21)
| ((uint32_t)time.date << 16)
| ((uint32_t)time.hour << 11)
| ((uint32_t)time.minute << 5)
| ((uint32_t)time.second);
}
}
return ((2023UL-1980) << 25) /* Year = 2010 */
| (5UL << 21) /* Month = 11 */
| (5UL << 16) /* Day = 2 */
| (10U << 11) /* Hour = 15 */
| (17U << 5) /* Min = 0 */
| (5U >> 1) /* Sec = 0 */
;
}