/*-----------------------------------------------------------------------*/ /* 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 "usr_drv_sd_spi.h" /* Definitions of physical drive number for each drive */ #define DEV_SDHC 0 /* Example: Map Ramdisk to physical drive 0 */ /*-----------------------------------------------------------------------*/ /* Get Drive Status */ /*-----------------------------------------------------------------------*/ DSTATUS disk_status ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { switch (pdrv) { case DEV_SDHC: return RES_OK; } return STA_NOINIT; } /*-----------------------------------------------------------------------*/ /* Inidialize a Drive */ /*-----------------------------------------------------------------------*/ DSTATUS disk_initialize ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { uint8_t result; switch (pdrv) { case DEV_SDHC : result = usr_drv_sd_init(); if (result != 0x00) { usr_drv_sd_spi_set_speed_mode(USR_DRV_SD_HIGH_SPEED_MODE); usr_drv_sd_spi_read_write_byte(0xff); break; } return RES_OK; } return STA_NOINIT; } /*-----------------------------------------------------------------------*/ /* 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 DEV_SDHC : result = usr_drv_sd_read_disk(buff, sector, count); if (result != 0) { usr_drv_sd_spi_read_write_byte(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 DEV_SDHC : // translate the arguments here result = usr_drv_sd_write_disk((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 == DEV_SDHC)//SD卡 { switch(cmd) { case CTRL_SYNC: usr_drv_sd_spi_cs(0); if(usr_drv_sd_wait_ready() == 0) { result = RES_OK; } else { result = RES_ERROR; } usr_drv_sd_spi_cs(1); break; case GET_SECTOR_SIZE: *(WORD*)buff = 512; result = RES_OK; break; case GET_BLOCK_SIZE: *(WORD*)buff = 8; result = RES_OK; break; case GET_SECTOR_COUNT: *(DWORD*)buff = usr_drv_sd_get_sector_count(); result = RES_OK; break; default: result = RES_PARERR; break; } } else { result = RES_ERROR;//其他的不支持 } return result; } /*---------------------------------------------------------*/ /* User Provided Timer Function for FatFs module */ /*---------------------------------------------------------*/ /* This is a real time clock service to be called from */ /* FatFs module. Any valid time must be returned even if */ /* the system does not support a real time clock. */ DWORD get_fattime (void) { return ((2010UL-1980) << 25) /* Year = 2010 */ | (11UL << 21) /* Month = 11 */ | (2UL << 16) /* Day = 2 */ | (15U << 11) /* Hour = 15 */ | (0U << 5) /* Min = 0 */ | (0U >> 1) /* Sec = 0 */ ; }