电子大神的日记本,供应链专家的功夫茶盘,在这里记录、分享与共鸣。

登录以开始

sd卡驱动+FAT文件系统(二) (转)

3、FatFS文件系统移植
   FatFS也是一个开源项目,首页
   它是一种完全免费开源的FAT文件系统模块,专门为小型的嵌入式系统而设计。它完全用标准C语言编写,所以具有良好的硬件平台独立性,可以移植到8051、PIC、AVR、SH、Z80、H8、ARM等系列单片机上而只需做简单的修改。它支持FATl2、FATl6和FAT32,支持多个存储媒介;有独立的缓冲区,可以对多个文件进行读/写,并特别对8位单片机和16位单片机做了优化
   整个移植过程十分顺利。
  
   主要是修改了diskio.c文件中的几个文件,结果如下:
       /*-----------------------------------------------------------------------*/
        /* 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 "sd_raw.h"
        #include "diskio.h"
       
        /*-----------------------------------------------------------------------*/
        /* Correspondence between physical drive number and physical drive.      */
       
        #define ATA        0
        #define MMC        1
        #define USB        2
       
        /*-----------------------------------------------------------------------*/
        /* Inidialize a Drive                                                    */
       
        DSTATUS disk_initialize (
            BYTE drv                /* Physical drive nmuber (0..) */
        )
        {
            DSTATUS res = RES_OK;
            drv = drv;
           
            if(!sd_raw_init())
                res = RES_ERROR;
           
            return res;
        }
       
        /*-----------------------------------------------------------------------*/
        /* Return Disk Status                                                    */
       
        DSTATUS disk_status (
            BYTE drv        /* Physical drive nmuber (0..) */
        )
        {
            DSTATUS stat = RES_OK;
            drv = drv;
       
            return stat;
        }
       
       
       
        /*-----------------------------------------------------------------------*/
        /* 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_OK;
            drv = drv;   
           
            if(!sd_raw_read((sector << 9), buff, (count << 9)))
                res = RES_ERROR;
           
            return res;
        }
       
       

        /*-----------------------------------------------------------------------*/
        /* 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_OK;
            drv = drv;
       
            if(!sd_raw_write((sector << 9), buff, (count << 9)))
                res = RES_ERROR;
           
            return res;
        }
        #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 = RES_OK;
            struct sd_raw_info info;
            drv = drv;
           
            if(!sd_raw_get_info(&info))
                return RES_ERROR;
           
            switch(ctrl)
            {
                case CTRL_SYNC:
                    break;
                   
                case GET_SECTOR_SIZE:
                    *((DWORD *)buff) = 512;
                    break;
                       
                case GET_SECTOR_COUNT:
                    *((DWORD *)buff) = info.capacity >> 9;
                    break;
                   
                case GET_BLOCK_SIZE:
                    *((DWORD *)buff) = 512;
                    break;
            }
           
            return res;
        }
       
        DWORD get_fattime (void)
        {
            return (29 << 25) + (8 << 21) + (8 << 16);
        }

  经过测试,可以正确读出SD卡中的文件系统。

博主
vincent_v@yeah.net
vincent_v's Blog
vincent_v
点击跳转