野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12526|回复: 6

1.FATFS0.10_flash 例程 求解

[复制链接]
发表于 2016-1-9 09:31:07 | 显示全部楼层 |阅读模式
this is a fatfs test demo
f_mount res_flash=3
串口返回的信息是这样,驱动器被写保护了吗?

/*************************   flash ÎļtÏμí3   *********************************************/
                /* Register work area for each volume (Always succeeds regardless of disk status) */
                res_flash = f_mount(&fs,"0:",1);
                printf("\r\n f_mount res_flash=%d \r\n",res_flash);

                if(res_flash ==FR_NO_FILESYSTEM)
                {
                        res_flash=f_mkfs("0:",0,4096);
                        printf("\r\nmkfs res_flash=%d",res_flash);
                        res_flash = f_mount(&fs,"0:",0);
                        res_flash = f_mount(&fs,"0:",1);
                }

这段代码不懂,求解。

回复

使用道具 举报

发表于 2016-1-9 09:47:55 | 显示全部楼层
res_flash=3 错误对应
FR_NOT_READY,                        /* (3) The physical drive cannot work */

diskio.c文件几个接口函数执行出错
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-1-9 09:56:08 | 显示全部楼层
亽亼 发表于 2016-1-9 09:47
res_flash=3 错误对应
FR_NOT_READY,                        /* (3) The physical drive cannot work */

/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2013        */
/*-----------------------------------------------------------------------*/
/* 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 module to the FatFs module with a defined API.        */
/*-----------------------------------------------------------------------*/

#include "diskio.h"                /* FatFs lower layer API */
#include "ff.h"


#ifndef FATFS_FLASH_SPI
        #define FATFS_FLASH_SPI                                1
#endif

#ifndef TM_FATFS_CUSTOM_FATTIME
        #define TM_FATFS_CUSTOM_FATTIME                0
#endif

#ifndef FATFS_USE_SDIO
        #define FATFS_USE_SDIO                        0
#endif

/* Include SD card files if is enabled */
#if FATFS_USE_SDIO == 1
        #include "bsp_sdio_sdcard.h"
#endif

#if FATFS_FLASH_SPI == 1
        #include "fatfs_flash_spi.h"
#endif


/* Definitions of physical drive number for each media */
#define ATA                        1
#define SPI_FLASH                0

#define SD_BLOCK_SIZE            512 /* Block Size in Bytes */
//extern SD_CardInfo SDCardInfo;
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
        BYTE pdrv                                /* Physical drive nmuber (0..) */
)
{
        DSTATUS status = STA_NOINIT;
        FLASH_DEBUG_FUNC() ;
        switch (pdrv) {
                case ATA:        /* SD CARD */
                        #if FATFS_USE_SDIO == 1
                            FLASH_DEBUG("SD init");
                                  status = SD_Init();
                                        if (status!=SD_OK )
                                        {
                                                status = STA_NOINIT;
                                        }
                                        else
                                        {
                                                status = RES_OK;
                                        }
                        #endif
                        break;
               
                case SPI_FLASH:
                        #if        FATFS_FLASH_SPI ==1
                        FLASH_DEBUG("SPI FLASH init");
                        status = TM_FATFS_FLASH_SPI_disk_initialize();
                        #endif
                        break;

                default:
                        status = STA_NOINIT;
        }
       
        return status;
}



/*-----------------------------------------------------------------------*/
/* Get Disk Status                                                       */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
        BYTE pdrv                /* Physical drive nmuber (0..) */
)
{

        DSTATUS status = STA_NOINIT;
       
        switch (pdrv) {
                case ATA:        /* SD CARD */
                        #if FATFS_USE_SDIO == 1
                                status = RES_OK;
                        #endif
                        break;

                case SPI_FLASH:
                        #if        FATFS_FLASH_SPI ==1
                        status = TM_FATFS_FLASH_SPI_disk_status();        /* SDIO communication */
                        #endif
                        break;

                default:
                        status = STA_NOINIT;
        }
       
        return status;
}



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

DRESULT disk_read (
        BYTE pdrv,                /* Physical drive nmuber (0..) */
        BYTE *buff,                /* Data buffer to store read data */
        DWORD sector,        /* Sector address (LBA) */
        UINT count                /* Number of sectors to read (1..128) */
)
{
        DRESULT status = RES_PARERR;
        switch (pdrv) {
                case ATA:        /* SD CARD */
                        #if FATFS_USE_SDIO == 1
                                if (count > 1)
                                {
                                        SD_ReadMultiBlocks(buff, sector*SD_BLOCK_SIZE, SD_BLOCK_SIZE, count);                               
                                        /* Check if the Transfer is finished */
                                        SD_WaitReadOperation();  //Ñ-»·2éÑˉdma′«êäêÇ·ñ½áêø                               
                                        /* Wait until end of DMA transfer */
                                        while(SD_GetStatus() != SD_TRANSFER_OK);
                                }
                                else
                                {                                       
                                        SD_ReadBlock(buff, sector*SD_BLOCK_SIZE, SD_BLOCK_SIZE);
                                        /* Check if the Transfer is finished */
                                        SD_WaitReadOperation();  //Ñ-»·2éÑˉdma′«êäêÇ·ñ½áêø                               
                                        /* Wait until end of DMA transfer */
                                        while(SD_GetStatus() != SD_TRANSFER_OK);
                                }
                                status = RES_OK;
                        #endif
                        break;

                case SPI_FLASH:
                                        #if        FATFS_FLASH_SPI ==1
                                        status = TM_FATFS_FLASH_SPI_disk_read(buff, sector, count);        /* SDIO communication */
                                        #endif
                break;
                default:
                        status = RES_PARERR;
        }
       
        return status;
}



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

#if _USE_WRITE
DRESULT disk_write (
        BYTE pdrv,                        /* Physical drive nmuber (0..) */
        const BYTE *buff,        /* Data to be written */
        DWORD sector,                /* Sector address (LBA) */
        UINT count                        /* Number of sectors to write (1..128) */
)
{
        DRESULT status = RES_PARERR;
        if (!count) {
                return RES_PARERR;                /* Check parameter */
        }
       
        switch (pdrv) {
                case ATA:        /* SD CARD */
                        #if FATFS_USE_SDIO == 1
                                        if (count > 1)
                                        {
                                                SD_WriteMultiBlocks((uint8_t *)buff, sector*SD_BLOCK_SIZE,SD_BLOCK_SIZE, count);                                               
                                                /* Check if the Transfer is finished */
                                                SD_WaitWriteOperation();           //μè′ydma′«êä½áêø
                                                while(SD_GetStatus() != SD_TRANSFER_OK); //μè′ysdioμ½sd¿¨′«êä½áêø
                                        }
                                        else
                                        {
                                                SD_WriteBlock((uint8_t *)buff,sector*SD_BLOCK_SIZE,SD_BLOCK_SIZE);                                               
                                                /* Check if the Transfer is finished */
                                                SD_WaitWriteOperation();           //μè′ydma′«êä½áêø
                                                while(SD_GetStatus() != SD_TRANSFER_OK); //μè′ysdioμ½sd¿¨′«êä½áêø
                                        }
                                        status = RES_OK;
                        #endif
                        break;

                case SPI_FLASH:
                                        #if        FATFS_FLASH_SPI ==1
                                        status = TM_FATFS_FLASH_SPI_disk_write((BYTE *)buff, sector, count);        /* SDIO communication */
                                        #endif
                break;
                default:
                        status = RES_PARERR;
        }
       
        return status;
}
#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

#if _USE_IOCTL
DRESULT disk_ioctl (
        BYTE pdrv,                /* Physical drive nmuber (0..) */
        BYTE cmd,                /* Control code */
        void *buff                /* Buffer to send/receive control data */
)
{
        DRESULT status = RES_PARERR;
        switch (pdrv) {
                case ATA:        /* SD CARD */
                        #if FATFS_USE_SDIO == 1
                                switch (cmd)
                                {                               
                                        case GET_SECTOR_SIZE :     // Get R/W sector size (WORD)
                                                *(WORD * )buff = 512;
                                        break;
                                        case GET_BLOCK_SIZE:      // Get erase block size in unit of sector (DWORD)
                                                *(DWORD * )buff = SDCardInfo.CardBlockSize;
                                        break;
                                        case GET_SECTOR_COUNT:
                                                *(DWORD * )buff = SDCardInfo.CardCapacity/SDCardInfo.CardBlockSize;
                                        case CTRL_SYNC :
                                        break;
                                }
                                status = RES_OK;
                        #endif
                        break;

                case SPI_FLASH:
                                        #if        FATFS_FLASH_SPI ==1
                                        status = TM_FATFS_FLASH_SPI_disk_ioctl(cmd, buff);        /* SDIO communication */
                                        #endif
                break;
                default:
                        status = RES_PARERR;
        }
        return status;
}
#endif

__weak DWORD get_fattime(void) {
        /* Returns current time packed into a DWORD variable */
        return          ((DWORD)(2013 - 1980) << 25)        /* Year 2013 */
                        | ((DWORD)7 << 21)                                /* Month 7 */
                        | ((DWORD)28 << 16)                                /* Mday 28 */
                        | ((DWORD)0 << 11)                                /* Hour 0 */
                        | ((DWORD)0 << 5)                                /* Min 0 */
                        | ((DWORD)0 >> 1);                                /* Sec 0 */
}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-1-9 09:56:36 | 显示全部楼层
亽亼 发表于 2016-1-9 09:47
res_flash=3 错误对应
FR_NOT_READY,                        /* (3) The physical drive cannot work */

我是直接运行例子的喔。
回复 支持 反对

使用道具 举报

发表于 2016-1-9 11:01:27 | 显示全部楼层
你先跑spi flash测试程序(没有文件系统的)看看是否正常
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-1-9 11:43:19 | 显示全部楼层
亽亼 发表于 2016-1-9 11:01
你先跑spi flash测试程序(没有文件系统的)看看是否正常

正常。可以读写
回复 支持 反对

使用道具 举报

发表于 2016-1-9 12:07:51 | 显示全部楼层
那就没道理了
你对比两个工程的spi flash驱动有没有什么不同,特别是引脚
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-9-23 23:22 , Processed in 0.040082 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表