硬汉嵌入式论坛

 找回密码
 立即注册
查看: 5744|回复: 5
收起左侧

[有问必答] 请教下,如何使用fatfs 挂载3个器件自己折腾了一下没有成功

[复制链接]

680

主题

3480

回帖

5545

积分

论坛元老

积分
5545
发表于 2015-8-5 16:40:11 | 显示全部楼层 |阅读模式
请教下,如何使用fatfs 挂载3个器件自己折腾了一下没有成功
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
115694
QQ
发表于 2015-8-5 18:08:08 | 显示全部楼层
有,参考我们V5的这个例子即可:
1.png
回复

使用道具 举报

680

主题

3480

回帖

5545

积分

论坛元老

积分
5545
 楼主| 发表于 2015-8-7 22:22:27 | 显示全部楼层

回 eric2013 的帖子

eric2013:有,参考我们V5的这个例子即可:
(2015-08-05 18:08)
好的,非常感谢感谢啊
回复

使用道具 举报

680

主题

3480

回帖

5545

积分

论坛元老

积分
5545
 楼主| 发表于 2015-8-9 20:59:40 | 显示全部楼层

回 eric2013 的帖子

eric2013:
有,参考我们V5的这个例子即可:
你好,我使用v5 的 V5-107d_FatFS文件系统例程(SD卡、NAND Flash和U盘)(V1.2a)     这个里面的程序进行测试,结果只有 sd 卡是
正确的,其他的 nonr flash , usb 都是错误的,不知道是怎么回事 ??

nand flash 单独的读写完全正确(不使用 fatfs系统) 。


部分程序如下 :


/* Definitions of physical drive number for each media */
#define FS_SD        0
#define FS_NAND    1
#define FS_USB        2


DSTATUS disk_initialize (
    BYTE pdrv                /* Physical drive nmuber (0..) */
)
{
    DSTATUS stat = STA_NOINIT;

    switch (pdrv)
    {
        case FS_SD :        /* SD卡 */
            if (SD_Init() == SD_OK)
            {
                stat = RES_OK;
            }
            else
            {
                stat = STA_NODISK;
            }
            break;

        case FS_NAND :        /* NAND Flash */
            if (NAND_Init() == NAND_OK)
            {
                stat = RES_OK;
            }
            else
            {
                /* 如果初始化失败,请执行低级格式化 */
                printf("NAND_Init() Error!  \\r\\n");
                stat = RES_ERROR;
            }
            break;

        case FS_USB :        /* STM32 USB Host 口外接U盘 */
            if(HCD_IsDeviceConnected(&USB_OTG_Core))
            {
                stat &= ~STA_NOINIT;
            }
            break;

        default :
            break;
    }
    return stat;
}



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

DSTATUS disk_status (
    BYTE pdrv        /* Physical drive nmuber (0..) */
)
{
    DSTATUS stat = STA_NOINIT;

    switch (pdrv)
    {
        case FS_SD :
            stat = 0;
            break;

        case FS_NAND :
            stat = 0;
            break;

        case FS_USB :
            stat = 0;
            break;

        default:
            break;
    }
    return stat;
}

/*-----------------------------------------------------------------------*/
/* 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) */
    BYTE count        /* Number of sectors to read (1..128) */
)
{
    DRESULT res;

    switch (pdrv)
    {
        case FS_SD :
        {
            SD_Error Status = SD_OK;

            if (count == 1)
            {
                Status = SD_ReadBlock(buff, sector << 9 , SECTOR_SIZE);
            }
            else
            {
                Status = SD_ReadMultiBlocks(buff, sector << 9 , SECTOR_SIZE, count);
            }
            if (Status != SD_OK)
            {
                res = RES_ERROR;
                break;
            }

        #ifdef SD_DMA_MODE
            /* SDIO工作在DMA模式,需要检查操作DMA传输是否完成 */
            Status = SD_WaitReadOperation();
            if (Status != SD_OK)
            {
                res = RES_ERROR;
                break;
            }

            while(SD_GetStatus() != SD_TRANSFER_OK);
        #endif

            res = RES_OK;
            break;
        }

        case FS_NAND :
            if (NAND_OK == NAND_ReadMultiSectors(buff, sector, 512, count))
            {
                res = RES_OK;
            }
            else
            {
                printf("NAND_ReadMultiSectors() Error! sector = %d, count = %d \\r\\n", sector, count);
                res = RES_ERROR;
            }
            break;

        case FS_USB :
            //res = USB_disk_read(buff, sector, count);
            {
                BYTE status = USBH_MSC_OK;

                //if (Stat & STA_NOINIT)     return RES_NOTRDY;

                if (HCD_IsDeviceConnected(&USB_OTG_Core))
                {
                    do
                    {
                        status = USBH_MSC_Read10(&USB_OTG_Core, buff,sector,512 * count);
                        USBH_MSC_HandleBOTXfer(&USB_OTG_Core ,&USB_Host);

                        if (!HCD_IsDeviceConnected(&USB_OTG_Core))
                        {
                            break;
                        }
                    }
                    while (status == USBH_MSC_BUSY );
                }

                if (status == USBH_MSC_OK)
                {
                    res = RES_OK;
                }
                else
                {
                    res = RES_ERROR;
                }
            }
            break;

        default:
            res = RES_PARERR;
            break;
    }
    return res;
}

/*-----------------------------------------------------------------------*/
/* 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) */
    BYTE count            /* Number of sectors to write (1..128) */
)
{
    DRESULT res;

    switch (pdrv)
    {
        case FS_SD :
        {
            SD_Error Status = SD_OK;

            if (count == 1)
            {
                Status = SD_WriteBlock((uint8_t *)buff, sector << 9 ,SECTOR_SIZE);

                if (Status != SD_OK)
                {
                    res = RES_ERROR;
                    break;
                }

            #ifdef SD_DMA_MODE
                /* SDIO工作在DMA模式,需要检查操作DMA传输是否完成 */
                Status = SD_WaitReadOperation();
                if (Status != SD_OK)
                {
                    res = RES_ERROR;
                    break;
                }
                while(SD_GetStatus() != SD_TRANSFER_OK);
            #endif
                res = RES_OK;
            }
            else
            {
                /* 此处存在疑问: 扇区个数如果写 count ,将导致最后1个block无法写入 */
                //Status = SD_WriteMultiBlocks((uint8_t *)buff, sector << 9 ,SECTOR_SIZE, count);
                Status = SD_WriteMultiBlocks((uint8_t *)buff, sector << 9 ,SECTOR_SIZE, count + 1);

                if (Status != SD_OK)
                {
                    res = RES_ERROR;
                    break;
                }

            #ifdef SD_DMA_MODE
                /* SDIO工作在DMA模式,需要检查操作DMA传输是否完成 */
                Status = SD_WaitReadOperation();
                if (Status != SD_OK)
                {
                    res = RES_ERROR;
                    break;
                }
                while(SD_GetStatus() != SD_TRANSFER_OK);
            #endif
                res = RES_OK;
            }
            break;
        }

        case FS_NAND :
            if (NAND_OK == NAND_WriteMultiSectors((uint8_t *)buff, sector, 512, count))
            {
                res = RES_OK;
            }
            else
            {
                printf("NAND_WriteMultiSectors() Error! sector = %d, count = %d \\r\\n", sector, count);
                res = RES_ERROR;
            }
            break;

        case FS_USB :
            //res = USB_disk_write(buff, sector, count);
            {
                BYTE status = USBH_MSC_OK;

                //if (drv || !count) return RES_PARERR;

                //if (Stat & STA_NOINIT) return RES_NOTRDY;
                //if (Stat & STA_PROTECT) return RES_WRPRT;

                if (HCD_IsDeviceConnected(&USB_OTG_Core))
                {
                    do
                    {
                        status = USBH_MSC_Write10(&USB_OTG_Core,(BYTE*)buff,sector, 512 * count);
                        USBH_MSC_HandleBOTXfer(&USB_OTG_Core, &USB_Host);

                        if(!HCD_IsDeviceConnected(&USB_OTG_Core))
                        {
                            break;
                        }
                    }
                    while(status == USBH_MSC_BUSY );

                }

                if (status == USBH_MSC_OK)
                {
                    res = RES_OK;
                }
                else
                {
                    res = RES_ERROR;
                }
            }
            break;

        default:
            res = RES_PARERR;
            break;
    }
    return res;
}
#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 res;

    switch (pdrv) {
    case FS_SD :
        /* SD卡磁盘容量: SDCardInfo.CardCapacity */
        res = RES_OK;
        return res;

    case FS_NAND :
        {
            DRESULT res = RES_OK;

            res = RES_ERROR;
            switch (cmd)
            {
                case CTRL_SYNC :        /* Make sure that no pending write process */
                    res = RES_OK;
                    break;

                case GET_SECTOR_COUNT :    /* Get number of sectors on the disk (DWORD) */
                    *(DWORD*)buff = 262144;
                    res = RES_OK;
                    break;

                case GET_SECTOR_SIZE :    /* Get R/W sector size (WORD) */
                    *(WORD*)buff = 512;
                    res = RES_OK;
                    break;

                case GET_BLOCK_SIZE :    /* Get erase block size in unit of sector (DWORD) */\\
                    *(DWORD*)buff = 512;
                    res = RES_OK;
                    break;

                default:
                    res = RES_PARERR;
                    break;
            }
            return res;            
        }


    case FS_USB :
        {
            DRESULT res = RES_OK;

            //if (drv) return RES_PARERR;
            res = RES_ERROR;

            //if (Stat & STA_NOINIT) return RES_NOTRDY;
            switch (cmd)
            {
                case CTRL_SYNC :        /* Make sure that no pending write process */
                    res = RES_OK;
                    break;

                case GET_SECTOR_COUNT :    /* Get number of sectors on the disk (DWORD) */
                    *(DWORD*)buff = (DWORD) USBH_MSC_Param.MSCapacity;
                    res = RES_OK;
                    break;

                case GET_SECTOR_SIZE :    /* Get R/W sector size (WORD) */
                    *(WORD*)buff = 512;
                    res = RES_OK;
                    break;

                case GET_BLOCK_SIZE :    /* Get erase block size in unit of sector (DWORD) */\\
                    *(DWORD*)buff = 512;
                    res = RES_OK;
                    break;

                default:
                    res = RES_PARERR;
                    break;
            }
            return res;
        }

    }
    return RES_PARERR;
}
#endif


// =======================================================================================


/* 用于测试读写速度 */
#define TEST_FILE_LEN            (2*1024*1024)    /* 用于测试的文件长度 */
#define BUF_SIZE                (4*1024)        /* 每次读写SD卡的最大数据长度 */
uint8_t g_TestBuf[BUF_SIZE];

/* 仅允许本文件内调用的函数声明 */
static void DispMenu(void);
static void ViewRootDir(uint8_t _DiskVolume);
static void CreateNewFile(uint8_t _DiskVolume);
static void ReadFileData(uint8_t _DiskVolume);
static void CreateDir(uint8_t _DiskVolume);
static void DeleteDirFile(uint8_t _DiskVolume);
static void WriteFileTest(void);

/*
*********************************************************************************************************
*    函 数 名: DemoFatFS
*    功能说明: FatFS文件系统演示主程序
*    形    参:无
*    返 回 值: 无
*********************************************************************************************************
*/
void DemoFatFS(void)
{
    uint8_t cmd;
    uint8_t ucVolume = 0;

      /* Init Host Library */
    #ifdef USE_USB_OTG_FS
        USBH_Init(&USB_OTG_Core,
            USB_OTG_FS_CORE_ID,
            &USB_Host,
            &USBH_MSC_cb,
            &USR_cb);
    #else
        USBH_Init(&USB_OTG_Core,
            USB_OTG_HS_CORE_ID,
            &USB_Host,
            &USBH_MSC_cb,
            &USR_cb);
    #endif

    /* 打印命令列表,用户可以通过串口操作指令 */
    DispMenu();
    while (1)
    {
        bsp_Idle();        /* 这个函数在bsp.c文件。用户可以修改这个函数实现CPU休眠和喂狗 */

        USBH_Process(&USB_OTG_Core, &USB_Host);

        if (comGetChar(COM1, &cmd))    /* 从串口读入一个字符(非阻塞方式) */
        {
            printf("\\r\\n");
            switch (cmd)
            {
                case '1':
                    printf("【1 - 显示SD卡根目录文件】\\r\\n");
                    ViewRootDir(FS_SD);            /* 显示SD卡根目录下的文件名 */
                    printf("【1 - 显示NAND Flash根目录文件】\\r\\n");
                    ViewRootDir(FS_NAND);        /* 显示U盘根目录下的文件名 */
                    printf("【1 - 显示U盘根目录文件】\\r\\n");
                    ViewRootDir(FS_USB);        /* 显示U盘根目录下的文件名 */
                    break;

                case '2':
                    printf("【2 - CreateNewFile】\\r\\n");
                    CreateNewFile(FS_SD);    /* 创建一个新文件,写入一个字符串 */
                    CreateNewFile(FS_NAND);    /* 创建一个新文件,写入一个字符串 */
                    CreateNewFile(FS_USB);    /* 创建一个新文件,写入一个字符串 */
                    break;

                case '3':
                    printf("【3 - ReadFileData】\\r\\n");
                    ReadFileData(FS_SD);        /* 读取根目录下armfly.txt的内容 */
                    ReadFileData(FS_NAND);        /* 读取根目录下armfly.txt的内容 */
                    ReadFileData(FS_USB);        /* 读取根目录下armfly.txt的内容 */
                    break;

                case '4':
                    printf("【4 - CreateDir】\\r\\n");
                    CreateDir(FS_SD);        /* 创建目录 */
                    CreateDir(FS_NAND);        /* 创建目录 */
                    CreateDir(FS_USB);        /* 创建目录 */
                    break;

                case '5':
                    printf("【5 - DeleteDirFile】\\r\\n");
                    DeleteDirFile(FS_SD);    /* 删除目录和文件 */
                    DeleteDirFile(FS_NAND);    /* 删除目录和文件 */
                    DeleteDirFile(FS_USB);    /* 删除目录和文件 */
                    break;

                case '6':
                    printf("【6 - TestSpeed】\\r\\n");
                    WriteFileTest();    /* 速度测试 */
                    break;

                case '0':
                    printf("Start Format(Low Level) NAND Flash......\\r\\n");
                    NAND_Format();
                    printf("NAND Flash Format Ok\\r\\n");
                    break;

                case 'V':        /* 切换当前盘符 */
                case 'v':
                    if (ucVolume == FS_SD)
                    {
                        ucVolume = FS_NAND;
                    }
                    else if (ucVolume == FS_NAND)
                    {
                        ucVolume = FS_USB;
                    }
                    else
                    {
                        ucVolume = FS_SD;
                    }
                    break;

                default:
                    DispMenu();
                    break;
            }
        }    /* comGetChar(COM1, &cmd) */
    }
}

/*
*********************************************************************************************************
*    函 数 名: DispMenu
*    功能说明: 显示操作提示菜单
*    形    参:无
*    返 回 值: 无
*********************************************************************************************************
*/
static void DispMenu(void)
{
    printf("\\r\\n------------------------------------------------\\r\\n");
    printf("请选择操作命令:\\r\\n");
    printf("1 - 显示根目录下的文件列表\\r\\n");
    printf("2 - 创建一个新文件armfly.txt\\r\\n");
    printf("3 - 读armfly.txt文件的内容\\r\\n");
    printf("4 - 创建目录\\r\\n");
    printf("5 - 删除文件和目录\\r\\n");
    printf("6 - 读写文件速度测试\\r\\n");
    printf("0 - NAND Flash 低级格式化\\r\\n");
}

/*
*********************************************************************************************************
*    函 数 名: ViewRootDir
*    功能说明: 显示SD卡根目录下的文件名
*    形    参: _ucVolume : 磁盘卷标 0 - 2
*    返 回 值: 无
*********************************************************************************************************
*/
static void ViewRootDir(uint8_t _ucVolume)
{
    /* 本函数使用的局部变量占用较多,请修改启动文件,保证堆栈空间够用 */
    FRESULT result;
    FATFS fs;
    DIR DirInf;
    FILINFO FileInf;
    uint32_t cnt = 0;
    char lfname[256];
    char path[32];

     /* 挂载文件系统 */
    result = f_mount(_ucVolume, &fs);    /* Mount a logical drive */
    if (result != FR_OK)
    {
        printf("挂载文件系统失败 (%d)\\r\\n", result);
    }

    /* 打开根文件夹 */
    sprintf(path, "%d:/", _ucVolume);     /* 1: 表示盘符 */
    result = f_opendir(&DirInf, path);
    if (result != FR_OK)
    {
        printf("打开根目录失败 (%d)\\r\\n", result);
        return;
    }

    /* 读取当前文件夹下的文件和目录 */
    FileInf.lfname = lfname;
    FileInf.lfsize = 256;

    printf("属性        |  文件大小 | 短文件名 | 长文件名\\r\\n");
    for (cnt = 0; ;cnt++)
    {
        result = f_readdir(&DirInf,&FileInf);         /* 读取目录项,索引会自动下移 */
        if (result != FR_OK || FileInf.fname[0] == 0)
        {
            break;
        }

        if (FileInf.fname[0] == '.')
        {
            continue;
        }

        /* 判断是文件还是子目录 */
        if (FileInf.fattrib & AM_DIR)
        {
            printf("(0x%02d)目录  ", FileInf.fattrib);
        }
        else
        {
            printf("(0x%02d)文件  ", FileInf.fattrib);
        }

        /* 打印文件大小, 最大4G */
        printf(" %10d", FileInf.fsize);

        printf("  %s |", FileInf.fname);    /* 短文件名 */

        printf("  %s\\r\\n", (char *)FileInf.lfname);    /* 长文件名 */
    }

    /* 卸载文件系统 */
    f_mount(_ucVolume, NULL);
}

/*
*********************************************************************************************************
*    函 数 名: CreateNewFile
*    功能说明: 在SD卡创建一个新文件,文件内容填写“www.armfly.com”
*    形    参:无
*    返 回 值: 无
*********************************************************************************************************
*/
static void CreateNewFile(uint8_t _ucVolume)
{
    /* 本函数使用的局部变量占用较多,请修改启动文件,保证堆栈空间够用 */
    FRESULT result;
    FATFS fs;
    FIL file;
    DIR DirInf;
    uint32_t bw;
    char path[32];

     /* 挂载文件系统 */
    result = f_mount(_ucVolume, &fs);            /* Mount a logical drive */
    if (result != FR_OK)
    {
        printf("挂载文件系统失败 (%d)\\r\\n", result);
    }

    /* 打开根文件夹 */
    sprintf(path, "%d:/", _ucVolume);
    result = f_opendir(&DirInf, path);
    if (result != FR_OK)
    {
        printf("打开根目录失败 (%d)\\r\\n", result);
        return;
    }

    /* 打开文件 */
    sprintf(path, "%d:/armfly.txt", _ucVolume);
    result = f_open(&file, path, FA_CREATE_ALWAYS | FA_WRITE);

    /* 写一串数据 */
    result = f_write(&file, "FatFS Write Demo   www.armfly.com \\r\\n", 34, &bw);
    if (result == FR_OK)
    {
        printf("%s 文件写入成功\\r\\n", path);
    }
    else
    {
        printf("%s 文件写入失败\\r\\n", path);
    }

    /* 关闭文件*/
    f_close(&file);

    /* 卸载文件系统 */
    f_mount(_ucVolume, NULL);
}

/*
*********************************************************************************************************
*    函 数 名: ReadFileData
*    功能说明: 读取文件armfly.txt前128个字符,并打印到串口
*    形    参:无
*    返 回 值: 无
*********************************************************************************************************
*/
static void ReadFileData(uint8_t _ucVolume)
{
    /* 本函数使用的局部变量占用较多,请修改启动文件,保证堆栈空间够用 */
    FRESULT result;
    FATFS fs;
    FIL file;
    DIR DirInf;
    uint32_t bw;
    char buf[128];
    char path[32];

     /* 挂载文件系统 */
    result = f_mount(_ucVolume, &fs);            /* Mount a logical drive */
    if (result != FR_OK)
    {
        printf("挂载文件系统失败(%d)\\r\\n", result);
    }

    /* 打开根文件夹 */
    sprintf(path, "%d:/", _ucVolume);
    result = f_opendir(&DirInf, path);     /* 1: 表示盘符 */
    if (result != FR_OK)
    {
        printf("打开根目录失败(%d)\\r\\n", result);
        return;
    }

    /* 打开文件 */
    sprintf(path, "%d:/armfly.txt", _ucVolume);
    result = f_open(&file, path, FA_OPEN_EXISTING | FA_READ);
    if (result !=  FR_OK)
    {
        printf("Don't Find File : %s\\r\\n", path);
        return;
    }

    /* 读取文件 */
    result = f_read(&file, &buf, sizeof(buf) - 1, &bw);
    if (bw > 0)
    {
        buf[bw] = 0;
        printf("\\r\\n%s 文件内容 : \\r\\n%s\\r\\n", path,buf);
    }
    else
    {
        printf("\\r\\n %s文件内容 : \\r\\n", path);
    }

    /* 关闭文件*/
    f_close(&file);

    /* 卸载文件系统 */
    f_mount(_ucVolume, NULL);
}

/*
*********************************************************************************************************
*    函 数 名: CreateDir
*    功能说明: 在SD卡根目录创建Dir1和Dir2目录,在Dir1目录下创建子目录Dir1_1
*    形    参:无
*    返 回 值: 无
*********************************************************************************************************
*/
static void CreateDir(uint8_t _ucVolume)
{
    /* 本函数使用的局部变量占用较多,请修改启动文件,保证堆栈空间够用 */
    FRESULT result;
    FATFS fs;
    char path[32];

     /* 挂载文件系统 */
    result = f_mount(_ucVolume, &fs);            /* Mount a logical drive */
    if (result != FR_OK)
    {
        printf("挂载文件系统失败 (%d)\\r\\n", result);
    }

    /* 创建目录/Dir1 */
    sprintf(path, "%d:/Dir1", _ucVolume);
    result = f_mkdir(path);
    if (result == FR_OK)
    {
        printf("f_mkdir %s Ok\\r\\n", path);
    }
    else if (result == FR_EXIST)
    {
        printf("%s 目录已经存在(%d)\\r\\n",path, result);
    }
    else
    {
        printf("f_mkdir %s 失败 (%d)\\r\\n",path, result);
        return;
    }

    /* 创建目录/Dir2 */
    sprintf(path, "%d:/Dir2", _ucVolume);
    result = f_mkdir(path);
    if (result == FR_OK)
    {
        printf("f_mkdir %s Ok\\r\\n", path);
    }
    else if (result == FR_EXIST)
    {
        printf("%s 目录已经存在(%d)\\r\\n", path, result);
    }
    else
    {
        printf("f_mkdir %s 失败 (%d)\\r\\n", path, result);
        return;
    }

    /* 创建子目录 /Dir1/Dir1_1       注意:创建子目录Dir1_1时,必须先创建好Dir1 */
    sprintf(path, "%d://Dir1/Dir1_1", _ucVolume);
    result = f_mkdir(path);
    if (result == FR_OK)
    {
        printf("f_mkdir %s 成功\\r\\n", path);
    }
    else if (result == FR_EXIST)
    {
        printf("%s 目录已经存在 (%d)\\r\\n",path, result);
    }
    else
    {
        printf("f_mkdir %s 失败 (%d)\\r\\n",path, result);
        return;
    }

    /* 卸载文件系统 */
    f_mount(_ucVolume, NULL);
}

/*
*********************************************************************************************************
*    函 数 名: DeleteDirFile
*    功能说明: 删除SD卡根目录下的 armfly.txt 文件和 Dir1,Dir2 目录
*    形    参:无
*    返 回 值: 无
*********************************************************************************************************
*/
static void DeleteDirFile(uint8_t _ucVolume)
{
    /* 本函数使用的局部变量占用较多,请修改启动文件,保证堆栈空间够用 */
    FRESULT result;
    FATFS fs;
    char FileName[13];
    uint8_t i;
    char path[32];

     /* 挂载文件系统 */
    result = f_mount(_ucVolume, &fs);            /* Mount a logical drive */
    if (result != FR_OK)
    {
        printf("挂载文件系统失败 (%d)\\r\\n", result);
    }


    /* 删除目录/Dir1 【因为还存在目录非空(存在子目录),所以这次删除会失败】*/
    sprintf(path, "%d:/Dir1", _ucVolume);
    result = f_unlink(path);
    if (result == FR_OK)
    {
        printf("删除目录%s成功\\r\\n", path);
    }
    else if (result == FR_NO_FILE)
    {
        printf("没有发现文件或目录 :%s\\r\\n", path);
    }
    else
    {
        printf("删除%s失败(错误代码 = %d) 文件只读或目录非空\\r\\n",path, result);
    }

    /* 先删除目录/Dir1/Dir1_1 */
    sprintf(path, "%d:/Dir1/Dir1_1", _ucVolume);
    result = f_unlink(path);
    if (result == FR_OK)
    {
        printf("删除子目录%s成功\\r\\n", path);
    }
    else if ((result == FR_NO_FILE) || (result == FR_NO_PATH))
    {
        printf("没有发现文件或目录 :%s\\r\\n", path);
    }
    else
    {
        printf("删除子目录%s失败(错误代码 = %d) 文件只读或目录非空\\r\\n", path, result);
    }

    /* 先删除目录/Dir1 */
    sprintf(path, "%d:/Dir1", _ucVolume);
    result = f_unlink(path);
    if (result == FR_OK)
    {
        printf("删除目录%s成功\\r\\n", path);
    }
    else if (result == FR_NO_FILE)
    {
        printf("没有发现文件或目录 :%s\\r\\n", path);
    }
    else
    {
        printf("删除%s失败(错误代码 = %d) 文件只读或目录非空\\r\\n",path, result);
    }

    /* 删除目录/Dir2 */
    sprintf(path, "%d:/Dir2", _ucVolume);
    result = f_unlink(path);
    if (result == FR_OK)
    {
        printf("删除目录 %s 成功\\r\\n", path);
    }
    else if (result == FR_NO_FILE)
    {
        printf("没有发现文件或目录 :%s\\r\\n", path);
    }
    else
    {
        printf("删除%s失败(错误代码 = %d) 文件只读或目录非空\\r\\n", path, result);
    }

    /* 删除文件 armfly.txt */
    result = f_unlink("1:/armfly.txt");
    if (result == FR_OK)
    {
        printf("删除文件 armfly.txt 成功\\r\\n");
    }
    else if (result == FR_NO_FILE)
    {
        printf("没有发现文件或目录 :%s\\r\\n", "armfly.txt");
    }
    else
    {
        printf("删除armfly.txt失败(错误代码 = %d) 文件只读或目录非空\\r\\n", result);
    }

    /* 删除文件 speed1.txt */
    for (i = 0; i < 20; i++)
    {
        sprintf(FileName, "%d:/Speed%02d.txt", _ucVolume, i);    /* 每写1次,序号递增 */
        result = f_unlink(FileName);
        if (result == FR_OK)
        {
            printf("删除文件%s成功\\r\\n", FileName);
        }
        else if (result == FR_NO_FILE)
        {
            printf("没有发现文件:%s\\r\\n", FileName);
        }
        else
        {
            printf("删除%s文件失败(错误代码 = %d) 文件只读或目录非空\\r\\n", FileName, result);
        }
    }

    /* 卸载文件系统 */
    f_mount(_ucVolume, NULL);
}

/*
*********************************************************************************************************
*    函 数 名: WriteFileTest
*    功能说明: 测试文件读写速度
*    形    参:无
*    返 回 值: 无
*********************************************************************************************************
*/
static void WriteFileTest(void)
{
    /* 本函数使用的局部变量占用较多,请修改启动文件,保证堆栈空间够用 */
    FRESULT result;
    FATFS fs;
    FIL file;
    DIR DirInf;
    uint32_t bw;
    uint32_t i,k;
    uint32_t runtime1,runtime2,timelen;
    uint8_t err = 0;
    char TestFileName[13];
    static uint8_t s_ucTestSn = 0;

    for (i = 0; i < sizeof(g_TestBuf); i++)
    {
        g_TestBuf = (i / 512) + '0';
    }

      /* 挂载文件系统 */
    result = f_mount(FS_NAND, &fs);            /* Mount a logical drive */
    if (result != FR_OK)
    {
        printf("挂载文件系统失败 (%d)\\r\\n", result);
    }

    /* 打开根文件夹 */
    result = f_opendir(&DirInf, "1:/"); /* 如果不带参数,则从当前目录开始 */
    if (result != FR_OK)
    {
        printf("打开根目录失败 (%d)\\r\\n", result);
        return;
    }

    /* 打开文件 */
    sprintf(TestFileName, "1:/Speed%02d.txt", s_ucTestSn++);        /* 每写1次,序号递增 */
    result = f_open(&file, TestFileName, FA_CREATE_ALWAYS | FA_WRITE);

    /* 写一串数据 */
    printf("开始写文件%s %dKB ...\\r\\n", TestFileName, TEST_FILE_LEN / 1024);
    runtime1 = bsp_GetRunTime();    /* 读取系统运行时间 */
    for (i = 0; i < TEST_FILE_LEN / BUF_SIZE; i++)
    {
        result = f_write(&file, g_TestBuf, sizeof(g_TestBuf), &bw);
        if (result == FR_OK)
        {
            if (((i + 1) % 8) == 0)
            {
                printf(".");
            }
        }
        else
        {
            err = 1;
            printf("%s文件写失败\\r\\n", TestFileName);
            break;
        }
    }
    runtime2 = bsp_GetRunTime();    /* 读取系统运行时间 */

    if (err == 0)
    {
        timelen = (runtime2 - runtime1);
        printf("\\r\\n  写耗时 : %dms   平均写速度 : %dB/S (%dKB/S)\\r\\n",
            timelen,
            (TEST_FILE_LEN * 1000) / timelen,
            ((TEST_FILE_LEN / 1024) * 1000) / timelen);
    }

    f_close(&file);        /* 关闭文件*/


    /* 开始读文件测试 */
    result = f_open(&file, TestFileName, FA_OPEN_EXISTING | FA_READ);
    if (result !=  FR_OK)
    {
        printf("没有找到文件: %s\\r\\n", TestFileName);
        return;
    }

    printf("开始读文件 %dKB ...\\r\\n", TEST_FILE_LEN / 1024);
    runtime1 = bsp_GetRunTime();    /* 读取系统运行时间 */
    for (i = 0; i < TEST_FILE_LEN / BUF_SIZE; i++)
    {
        result = f_read(&file, g_TestBuf, sizeof(g_TestBuf), &bw);
        if (result == FR_OK)
        {
            if (((i + 1) % 8) == 0)
            {
                printf(".");
            }

            /* 比较写入的数据是否正确,此语句会导致读卡速度结果降低到 3.5MBytes/S */
            for (k = 0; k < sizeof(g_TestBuf); k++)
            {
                if (g_TestBuf[k] != (k / 512) + '0')
                {
                      err = 1;
                    printf("Speed1.txt 文件读成功,但是数据出错\\r\\n");
                    break;
                }
            }
            if (err == 1)
            {
                break;
            }
        }
        else
        {
            err = 1;
            printf("Speed1.txt 文件读失败\\r\\n");
            break;
        }
    }
    runtime2 = bsp_GetRunTime();    /* 读取系统运行时间 */

    if (err == 0)
    {
        timelen = (runtime2 - runtime1);
        printf("\\r\\n  读耗时 : %dms   平均读速度 : %dB/S (%dKB/S)\\r\\n", timelen,
            (TEST_FILE_LEN * 1000) / timelen, ((TEST_FILE_LEN / 1024) * 1000) / timelen);
    }

    /* 关闭文件*/
    f_close(&file);

    /* 卸载文件系统 */
    f_mount(FS_NAND, NULL);
}









/*
*********************************************************************************************************
*    函 数 名: main
*    功能说明: c程序入口
*    形    参:无
*    返 回 值: 错误代码(无需处理)
*********************************************************************************************************
*/
int main(void)
{
    /*
        ST固件库中的启动文件已经执行了 SystemInit() 函数,该函数在 system_stm32f4xx.c 文件,主要功能是
    配置CPU系统的时钟,内部Flash访问时序,配置FSMC用于外部SRAM
    */

    bsp_Init();        /* 硬件初始化 */
    PrintfLogo();    /* 打印例程信息到串口1 */

    DemoFatFS();    /* FatFS文件系统演示程序 */
}






图片 截图如下:

1.png 2.png 3.png 4.png





请教下出现上面的情况一般是什么原因啊 ??




回复

使用道具 举报

116

主题

800

回帖

1148

积分

至尊会员

积分
1148
QQ
发表于 2015-8-10 11:20:24 | 显示全部楼层
没格式化吧
回复

使用道具 举报

680

主题

3480

回帖

5545

积分

论坛元老

积分
5545
 楼主| 发表于 2015-8-10 14:25:14 | 显示全部楼层

回 jcx0324 的帖子

jcx0324:没格式化吧 (2015-08-10 11:20)
你好,usb, sd 卡都正常,

但是唯独这个 NAND  flash 不行,程序可以读出 nand flash 芯片的ID,而且串口也发生 0 进行 格式化,
返回也是ok,程序仿真进行格式化也是OK的。

还望领导多多指教啊 ??
回复

使用道具 举报

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

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-5-12 21:23 , Processed in 0.233286 second(s), 27 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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