首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

SD_STM32_SPI驱动+FatFs文件系统(4)

SD_STM32_SPI驱动+FatFs文件系统(4)

三、FatFs移植示例。
支持格式化文件系统、读写、目录等。目前下面的代码只支持一个设备,如若想支持多个设备的文件系统,比如在SD卡、SPI FLASH中均实现文件系统,改动下面代码即可:
对每个Physical drive nmuber响应;底层的驱动只需要提供初始化(包括获取设备的容量)、读写即可。如果希望实现NFS、串口硬盘等,只需要把这几个接口通过通信链路(网络、串口等)映射过去。
[cpp] view plaincopy

  • /*-----------------------------------------------------------------------*/
  • /* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2007        */
  • /*-----------------------------------------------------------------------*/
  • /* This is a stub disk I/O module that acts as front end of the existing */
  • /* disk I/O modules and attach it to FatFs module with common interface. */
  • /*-----------------------------------------------------------------------*/

  • #include "diskio.h"
  • #include "stm32f10x.h"
  • #include "SD_drive.h"

  • /*-----------------------------------------------------------------------*/
  • /* Correspondence between physical drive number and physical drive.      */

  • #define ATA     0
  • #define MMC     1
  • #define USB     2

  • static
    int                          bus_in_sdio;  


  • /**
  • * @brief 检查sd卡是否就绪
  • * @return 0:检测成功
  • *        -1:检测失败
  • */
  • static
    int check_sdcard(void)  
  • {  
  •     return check_maincard();  
  • }  


  • /*-----------------------------------------------------------------------*/
  • /* Inidialize a Drive                                                    */

  • DSTATUS disk_initialize (  
  •     BYTE drv                /* Physical drive nmuber (0..) */
  • )  
  • {  
  •     if( check_sdcard() == 0 )  
  •     {  
  •         bus_in_sdio         = 1;  
  •         return RES_OK;  
  •     }  
  •     return RES_ERROR;  
  • }  

  • DSTATUS disk_unmount(BYTE drv)  
  • {  
  •     bus_in_sdio         = 0;  
  •     return RES_OK;  
  • }  


  • DWORD get_fattime ()  
  • {  
  •     return 0;  
  • }  
  • /*-----------------------------------------------------------------------*/
  • /* Return Disk Status                                                    */

  • DSTATUS disk_status (  
  •     BYTE drv        /* Physical drive nmuber (0..) */
  • )  
  • {  
  •     if(bus_in_sdio == 0)  
  •     {  
  •         return disk_initialize(drv);  
  •     }  

  •     //SD_GetStatus(); // == 0 )

  •     //return RES_ERROR;

  •     return RES_OK;  
  • }  

  • /*-----------------------------------------------------------------------*/
  • /* Read Sector(s)                                                        */

  • DRESULT disk_read (  
  •     BYTE drv,       /* Physical drive nmuber (0..) */
  •     BYTE *buff,     /* Data buffer to store read data */
  •     DWORD sector,   /* Sector address (LBA) */
  •     BYTE count      /* Number of sectors to read (1..255) */
  • )  
  • {  
  •     DRESULT res;  
  •     res = (DRESULT)(read_card(sector, count, buff));  
  •     if( res != 0 )  
  •         return RES_ERROR;  
  •     else
  •         return RES_OK;  
  • }  



  • /*-----------------------------------------------------------------------*/
  • /* Write Sector(s)                                                       */

  • #if _READONLY == 0
  • DRESULT disk_write (  
  •     BYTE drv,           /* Physical drive nmuber (0..) */
  •     const
    BYTE *buff,   /* Data to be written */
  •     DWORD sector,       /* Sector address (LBA) */
  •     BYTE count          /* Number of sectors to write (1..255) */
  • )  
  • {  
  •     DRESULT res;  
  •     res = (DRESULT)(write_card(sector, count, (unsigned char*)buff));  
  •     if( res == 0)  
  •         return RES_OK;  
  •     else
  •         return RES_ERROR;  
  • }  
  • #endif /* _READONLY */

  • /*-----------------------------------------------------------------------*/
  • /* Miscellaneous Functions                                               */
  • // 返回磁盘状态
  • DRESULT disk_ioctl (  
  •     BYTE drv,       /* Physical drive nmuber (0..) */
  •     BYTE ctrl,      /* Control code */
  •     void *buff      /* Buffer to send/receive control data */
  • )  
  • {  
  •     DRESULT res;  
  •     if (drv)  
  •     {  
  •         return RES_PARERR; //仅支持单磁盘操作,否则返回参数错误
  •     }  
  •     //FATFS目前版本仅需处理CTRL_SYNC,GET_SECTOR_COUNT,GET_BLOCK_SIZ三个命令
  •     switch(ctrl)  
  •     {  
  •     case CTRL_SYNC:  

  •             res = RES_OK;  

  •         break;  

  •     case GET_BLOCK_SIZE:  
  •         *(WORD*)buff = 1;  
  •         res = RES_OK;  
  •         break;  

  •     case GET_SECTOR_COUNT: //读卡容量
  •         *(DWORD*)buff = sd_state.capacity;  
  •         break;  

  •     default:  
  •         res = RES_PARERR;  
  •         break;  
  •     }  
  •     return res;  
  • }  
继承事业,薪火相传
返回列表