移植最新版本FATFS R0.13 到STM32F105RB的内部flash diskio.c源码

来源:互联网 发布:apache优秀的开源项目 编辑:程序博客网 时间:2024/06/05 22:57

/———————————————————————–/
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
/———————————————————————–/
/* 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 “stm32f10x_flash.h”

include “diskio.h” /* FatFs lower layer API */

include “flash.h”

/* Definitions of physical drive number for each drive */

define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */

define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */

define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */

/———————————————————————–/
// Ãû³Æ: RAM_disk_status
// ²ÎÊý: ÎÞ
// ·µ»Ø: FLASHÊÇ·ñ¿É²Ù×÷µÄ״̬: д±£»¤-STA_PROTECT£¬¿É¶Á¿Éд-0
// ¹¦ÄÜ: »ñÈ¡FLASHµÄ״̬
// ×÷Õß: lao xian zhong
// ÈÕÆÚ: 2017.08.31
/———————————————————————–/
DSTATUS RAM_disk_status(void)
{
return 0;
}
/———————————————————————–/
/* Get Drive Status */
/———————————————————————–/

DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
int result;

switch (pdrv) {case DEV_RAM :     result = RAM_disk_status();    // translate the reslut code here    stat = result;    return stat;case DEV_MMC :    //result = MMC_disk_status();    // translate the reslut code here    return stat;case DEV_USB :    //result = USB_disk_status();    // translate the reslut code here    return stat;}return STA_NOINIT;

}

/———————————————————————–/
// Ãû³Æ: RAM_disk_initialize
// ²ÎÊý: ÎÞ
// ·µ»Ø: ״̬
// ¹¦ÄÜ: flash³õʼ»¯
// ×÷Õß: lao xian zhong
// ÈÕÆÚ: 2017.08.31
/———————————————————————–/
DSTATUS RAM_disk_initialize(void)
{
DSTATUS stat;
Flash_Init();
stat = 0;
return stat;
}

/———————————————————————–/
/* Inidialize a Drive */
/———————————————————————–/

DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
int result;

switch (pdrv) {case DEV_RAM :    result = RAM_disk_initialize();    // translate the reslut code here    stat = result;    return stat;case DEV_MMC :    //result = MMC_disk_initialize();    // translate the reslut code here    return stat;case DEV_USB :    //result = USB_disk_initialize();    // translate the reslut code here    return stat;}return STA_NOINIT;

}

/———————————————————————–/
// Ãû³Æ: RAM_disk_read
// ²ÎÊý: Data buffer,Start sector,Number of sectors to read
// ·µ»Ø: ״̬
// ¹¦ÄÜ: flash¶Áº¯Êý
// ×÷Õß: lao xian zhong
// ÈÕÆÚ: 2017.08.31
/———————————————————————–/
DSTATUS RAM_disk_read(BYTE buff,/ Data buffer to store read data */
DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
)
{
DSTATUS stat;
uint32_t FlashAddress ;
uint32_t ReadEndAddress;
FlashAddress = BANK1_WRITE_START_ADDR + FLASH_PAGE_SIZE * sector; //¶ÁµÄÆðʼµØÖ· = µÚ¼¸¸öÒ³ * ÿ¸öÒ³µÄ×Ö½ÚÊý
ReadEndAddress = FlashAddress + FLASH_PAGE_SIZE * count;
while(FlashAddress < ReadEndAddress)
{
(buff++) = (BYTE)((__IO uint32_t*) FlashAddress & (uint32_t)0xFF);
(buff++) = (BYTE)(((__IO uint32_t*) FlashAddress >> 8) & (uint32_t)0xFF);
(buff++) = (BYTE)(((__IO uint32_t*) FlashAddress >> 16) & (uint32_t)0xFF);
(buff++) = (BYTE)(((__IO uint32_t*) FlashAddress >> 24) & (uint32_t)0xFF);
FlashAddress += 4;
}
stat = 0;
return stat;
}

/———————————————————————–/
/* Read Sector(s) */
/———————————————————————–/

DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE buff, / Data buffer to store read data */
DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
)
{
DRESULT res;
int result;

switch (pdrv) {case DEV_RAM :    // translate the arguments here    result = RAM_disk_read(buff, sector, count);    if(!result)    res = RES_OK;    // translate the reslut code here    return res;case DEV_MMC :    // translate the arguments here    //result = MMC_disk_read(buff, sector, count);    // translate the reslut code here    return res;case DEV_USB :    // translate the arguments here    //result = USB_disk_read(buff, sector, count);    // translate the reslut code here    return res;}return RES_PARERR;

}

/———————————————————————–/
// Ãû³Æ: RAM_disk_write
// ²ÎÊý: Data buffer,Start sector,Number of sectors to read
// ·µ»Ø: ״̬
// ¹¦ÄÜ: flashдº¯Êý
// ×÷Õß: lao xian zhong
// ÈÕÆÚ: 2017.08.31
/———————————————————————–/
DSTATUS RAM_disk_write(const BYTE buff,/ Data buffer to store read data */
DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
)
{
DSTATUS stat;
uint32_t FlashAddress ;
uint32_t EraseCounter;
uint32_t NbrOfPage;
uint32_t Wdata;
DWORD StartAddr = BANK1_WRITE_START_ADDR + FLASH_PAGE_SIZE * sector;//FLASH_PAGE_SIZE * count;
DWORD EndAddr = StartAddr + FLASH_PAGE_SIZE * count;//FLASH_PAGE_SIZE * count * sector;
/* Unlock the Flash Bank1 Program Erase controller */
FLASH_UnlockBank1();

/* Define the number of page to be erased */
NbrOfPage = (EndAddr - StartAddr) / FLASH_PAGE_SIZE;

/* Clear All pending flags */
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);

/* Erase the FLASH pages */
for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
{
FLASHStatus = FLASH_ErasePage(StartAddr + (FLASH_PAGE_SIZE * EraseCounter));
}

/* Program Flash Bank1 */
FlashAddress = StartAddr;

while((FlashAddress < EndAddr) && (FLASHStatus == FLASH_COMPLETE))
{
Wdata = (uint32_t)(buff) | (uint32_t)(((buff+1)) << 8)| (uint32_t)(((buff+2)) << 16)| (uint32_t)(((buff+3)) << 24);
FLASHStatus = FLASH_ProgramWord(FlashAddress, Wdata);
FlashAddress = FlashAddress + 4;
buff = buff + 4;
}

FLASH_LockBank1();
stat = 0;
return stat;
}
/———————————————————————–/
/* Write Sector(s) */
/———————————————————————–/

DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE buff, / Data to be written */
DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
)
{
DRESULT res;
int result;

switch (pdrv) {case DEV_RAM :    // translate the arguments here    result = RAM_disk_write(buff, sector, count);    // translate the reslut code here    if(!result)    res = RES_OK;    return res;case DEV_MMC :    // translate the arguments here    //result = MMC_disk_write(buff, sector, count);    // translate the reslut code here    return res;case DEV_USB :    // translate the arguments here    //result = USB_disk_write(buff, sector, count);    // translate the reslut code here    return res;}return RES_PARERR;

}

/———————————————————————–/
// Ãû³Æ: RAM_disk_ioctl
// ²ÎÊý: Data buffer,Start sector,Number of sectors to read
// ·µ»Ø:״̬
// ¹¦ÄÜ: flash¿ØÖƺ¯Êý
// ×÷Õß: lao xian zhong
// ÈÕÆÚ: 2017.08.31
/———————————————————————–/
DSTATUS RAM_disk_ioctl(BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void buff / Buffer to send/receive control data */
)
{
DSTATUS stat;
switch(cmd)
{
/* Generic command (Used by FatFs) */
case CTRL_SYNC: stat = 0; break; /* Complete pending write process (needed at _FS_READONLY == 0) */
case GET_SECTOR_COUNT: ((DWORD)buff) = 192;stat = 0; break; /* Get media size (needed at _USE_MKFS == 1) */
//case GET_SECTOR_SIZE: ((WORD)buff) = 1024; stat = 0; break; /* Get sector size (needed at _MAX_SS != _MIN_SS) */
case GET_BLOCK_SIZE: ((WORD)buff) =1;stat = 0; break; /调试发现,该参数是以扇区为单位的,可能和以往的版本不一样。 Get erase block size (needed at _USE_MKFS == 1) /
case CTRL_TRIM: stat = 0; break; /* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) */

    /* Generic command (Not used by FatFs) */    case CTRL_POWER :           stat = 0;   break;  /* Get/Set power status */    case CTRL_LOCK  :           stat = 0;   break;  /* Lock/Unlock media removal */    case CTRL_EJECT :           stat = 0;   break;  /* Eject media */    case CTRL_FORMAT:           stat = 0;   break;  /* Create physical format on the media */    /* MMC/SDC specific ioctl command */    case MMC_GET_TYPE:          stat = 0;   break;  /* Get card type */    case MMC_GET_CSD:           stat = 0;   break;  /* Get CSD */    case MMC_GET_CID:           stat = 0;   break;  /* Get CID */    case MMC_GET_OCR:           stat = 0;   break;  /* Get OCR */    case MMC_GET_SDSTAT:        stat = 0;   break;  /* Get SD status */    case ISDIO_READ :           stat = 0;   break;  /* Read data form SD iSDIO register */    case ISDIO_WRITE:           stat = 0;   break;  /* Write data to SD iSDIO register */    case ISDIO_MRITE:           stat = 0;   break;  /* Masked write data to SD iSDIO register */    /* ATA/CF specific ioctl command */    case ATA_GET_REV:           stat = 0;   break;  /* Get F/W revision */    case ATA_GET_MODEL:         stat = 0;   break;  /* Get model name */    case ATA_GET_SN:                stat = 0;break; /* Get serial number */    }return stat;

}
/———————————————————————–/
/* Miscellaneous Functions */
/———————————————————————–/

DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void buff / Buffer to send/receive control data */
)
{
DRESULT res;
int result;

switch (pdrv) {case DEV_RAM :    // Process of the command for the RAM drive    result = RAM_disk_ioctl(pdrv, cmd, buff);    if(!result)    res = RES_OK;    return res;case DEV_MMC :    // Process of the command for the MMC/SD card    return res;case DEV_USB :    // Process of the command the USB drive    return res;}return RES_PARERR;

}

/———————————————————/
/* User provided RTC function for FatFs module */
/———————————————————/
/* This is a real time clock service to be called back */
/* from FatFs module. */

if !FF_FS_NORTC && !FF_FS_READONLY

unsigned long get_fattime (void)
{
return 0;
}

endif