在进行FatFs文件系统的移植时,正常更改了diskio.c中相应的存储硬件的使用函数,但是在编写主函数使用f_mount函数挂载外部FLASH时仍然不能够使用,并且报错11, FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
以下为diskio.c中文件的更改
#include "diskio.h" /* FatFs lower layer API */
#include "bsp_spi_flash.h"
//#include "usbdisk.h" /* Example: Header file of existing USB MSD control module */
//#include "atadrive.h" /* Example: Header file of existing ATA harddisk control module */
//#include "sdcard.h" /* Example: Header file of existing MMC/SDC contorl module */
/* Definitions of physical drive number for each drive */
//#define ATA 0 /* Example: Map ATA harddisk to physical drive 0 */
//#define MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
//#define USB 2 /* Example: Map USB MSD to physical drive 2 */
#define sFLASH_ID 0XEF4017 //W25Q64
#define SD_CARD 0
#define SPI_FLASH 1
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
DSTATUS stat;
// int result;
switch (pdrv) {
case SD_CARD :
return stat;
case SPI_FLASH :
/* SPI Flash状态检测:读取SPI Flash 设备ID */
if(sFLASH_ID == SPI_Read_ID())
{
/* 设备ID读取结果正确 */
return 0;
}
else
{
/* 设备ID读取结果错误 */
return STA_NOINIT;
}
}
return STA_NOINIT;
}
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
uint16_t i;
DSTATUS status = STA_NOINIT;
switch (pdrv) {
case SD_CARD: /* SD CARD */
break;
case SPI_FLASH: /* SPI Flash */
/* 初始化SPI Flash */
SPI_FLASH_Init();
/* 延时一小段时间 */
i=500;
while(--i);
/* 唤醒SPI Flash */
SPI_Flash_WAKEUP();
/* 获取SPI Flash芯片状态 */
status=disk_status(SPI_FLASH);
break;
default:
status = STA_NOINIT;
}
return status;
}
/*-----------------------------------------------------------------------*/
/* 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, /* Sector address in LBA */
UINT count /* Number of sectors to read */
)
{
DRESULT status = RES_PARERR;
switch (pdrv) {
case SD_CARD: /* SD CARD */
break;
case SPI_FLASH:
/* 扇区偏移3,外部Flash文件系统空间放在SPI Flash后面5MB空间 */
sector+=770;
SPI_FLASH_BufferRead(buff, sector <<12, count<<12);
status = RES_OK;
break;
default:
status = RES_PARERR;
}
return status;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
#if _USE_WRITE
DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to write */
)
{
uint32_t write_addr;
DRESULT status = RES_PARERR;
if (!count) {
return RES_PARERR; /* Check parameter */
}
switch (pdrv) {
case SD_CARD: /* SD CARD */
break;
case SPI_FLASH:
/* 扇区偏移3MB,外部Flash文件系统空间放在SPI Flash后面5MB空间 */
sector+=770;
write_addr = sector<<12;
SPI_Erase_Sector(write_addr);
SPI_FLASH_BufferWrite((u8 *)buff,write_addr,count<<12);
status = RES_OK;
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 SD_CARD: /* SD CARD */
break;
case SPI_FLASH:
switch (cmd) {
/* 扇区数量:1280*4096/1024/1024=5(MB) */
case GET_SECTOR_COUNT:
*(DWORD * )buff = 1280;
break;
/* 扇区大小 */
case GET_SECTOR_SIZE :
*(WORD * )buff = 4096;
break;
/* 同时擦除扇区个数 */
case GET_BLOCK_SIZE :
*(DWORD * )buff = 1;
break;
}
status = RES_OK;
break;
default:
status = RES_PARERR;
}
return status;
}
#endif
DWORD get_fattime (void)
{
return 0;
}
在多次查找后得出了原因:当存在多个访问介质时,要修改ffconf.h头文件下的#define _VOLUMES 2 的值,在我的实验中,定义了SD卡和FLASH两种存储介质,因此要定义该值为2。
更改ffconf.h中的#define _VOLUMES 的值为2
我的主函数中的代码
串口的结果
此时,对于f_mount的11号错误已经修改完毕
但是我们发现程序执行时仍按有错误,错误代码为FR_NOT_ENABLED, /* (12) The volume has no work area */,根据提示我们可以知道,磁盘已经没有区域进行操作,也就是说当我们的磁盘中已经存在了文件系统,不必要再次进行初始化。
以下是主函数的代码。
如有错误,请多多指正!