Files
stm32-cnn/HARDWARE/FATFS/diskio.c
2024-12-19 14:06:05 +08:00

235 lines
6.1 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2014 */
/*-----------------------------------------------------------------------*/
/* 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 modules to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/
#include "diskio.h"
#include "sdio.h"
#include "malloc.h"
#define SD_CARD 0 // SD卡,卷标为0
#define EX_FLASH 1 // 外部spi flash,卷标为1
#define USB_DISK 3 // U盘,卷标为3
// 对于W25Q128
// 前8M字节给fatfs用,8M字节后,用于存放字库,字库占用6.01M.剩余部分1.99M,给客户自己用
#define FLASH_SECTOR_SIZE 512
#define FLASH_SECTOR_COUNT 1024*8*2 // W25Q128,前8M字节给FATFS占用
#define FLASH_BLOCK_SIZE 8 // 每个BLOCK有8个扇区
FATFS fatfs_sd; // SD卡逻辑驱动器的工作区
FATFS fatfs_flash; // FLASH逻辑驱动器的工作区
FATFS fatfs_usb; // USB逻辑驱动器的工作区
FIL file_sd; // SD卡文件对象
FIL file_flash; // FLASH文件对象
FIL file_usb; // USB文件对象
//-----------------------------------------------------------------
// DWORD get_fattime (void)
//-----------------------------------------------------------------
//
// 函数功能: 用户定义函数为fatfs模块停工当前时间
// 入口参数: 无
// 返 回 值: 无
// 注意事项: 无
//
//-----------------------------------------------------------------
DWORD get_fattime (void)
{
return 0;
}
//-----------------------------------------------------------------
// DSTATUS disk_status (BYTE pdrv)
//-----------------------------------------------------------------
//
// 函数功能: 获得磁盘状态
// 入口参数: BYTE pdrv用于标识驱动器的物理驱动器号
// 返 回 值: 无
// 注意事项: 无
//
//-----------------------------------------------------------------
DSTATUS disk_status(BYTE pdrv)
{
return RES_OK;
}
//-----------------------------------------------------------------
// DSTATUS disk_status (BYTE pdrv)
//-----------------------------------------------------------------
//
// 函数功能: 初始化磁盘
// 入口参数: BYTE pdrv用于标识驱动器的物理驱动器号
// 返 回 值: 无
// 注意事项: 无
//
//-----------------------------------------------------------------
DSTATUS disk_initialize(BYTE pdrv)
{
u8 res=0;
switch(pdrv)
{
case SD_CARD: // SD卡
res=SD_Init(); // SD卡初始化
break;
case USB_DISK: // U盘
break;
default:
res=1;
}
if(res)
return STA_NOINIT;
else
return 0; // 初始化成功
}
//-----------------------------------------------------------------
// DRESULT disk_read (BYTE pdrv,BYTE *buff,DWORD sector,UINT count)
//-----------------------------------------------------------------
//
// 函数功能: 读扇区
// 入口参数: BYTE pdrv磁盘编号0~9
// BYTE *buff数据接收缓冲首地址
// DWORD sector扇区地址
// UINT count需要读取的扇区数
// 返 回 值: 无
// 注意事项: 无
//
//-----------------------------------------------------------------
DRESULT disk_read (BYTE pdrv,BYTE *buff,DWORD sector,UINT count)
{
u8 res=0;
if (!count)
return RES_PARERR;// count不能等于0否则返回参数错误
switch(pdrv)
{
case SD_CARD: // SD卡
res=SD_ReadDisk(buff,sector,count);
while(res) // 读出错
{
SD_Init(); // 重新初始化SD卡
res=SD_ReadDisk(buff,sector,count);
}
break;
case USB_DISK: // U盘
break;
default:
res=1;
}
// 处理返回值将SPI_SD_driver.c的返回值转成ff.c的返回值
if(res==0x00)
return RES_OK;
else
return RES_ERROR;
}
//-----------------------------------------------------------------
// DRESULT disk_write (BYTE pdrv,const BYTE *buff,DWORD sector,UINT count)
//-----------------------------------------------------------------
//
// 函数功能: 写扇区
// 入口参数: BYTE pdrv磁盘编号0~9
// BYTE *buff发送数据首地址
// DWORD sector扇区地址
// UINT count需要写入的扇区数
// 返 回 值: 无
// 注意事项: 无
//
//-----------------------------------------------------------------
DRESULT disk_write (BYTE pdrv,const BYTE *buff,DWORD sector,UINT count)
{
u8 res=0;
if (!count)
return RES_PARERR;// count不能等于0否则返回参数错误
switch(pdrv)
{
case SD_CARD: // SD卡
res=SD_WriteDisk((u8*)buff,sector,count);
while(res) // 写出错
{
SD_Init(); //重新初始化SD卡
res=SD_WriteDisk((u8*)buff,sector,count);
//printf("sd wr error:%d\r\n",res);
}
break;
case USB_DISK: // U盘
break;
default:
res=1;
}
// 处理返回值将SPI_SD_driver.c的返回值转成ff.c的返回值
if(res == 0x00)
return RES_OK;
else
return RES_ERROR;
}
//-----------------------------------------------------------------
// DRESULT disk_ioctl (BYTE pdrv,BYTE cmd,void *buff)
//-----------------------------------------------------------------
//
// 函数功能: 其他表参数的获得
// 入口参数: BYTE pdrv磁盘编号0~9
// BYTE cmd控制代码
// void *buff发送/接收缓冲区指针
// 返 回 值: 无
// 注意事项: 无
//
//-----------------------------------------------------------------
DRESULT disk_ioctl (BYTE pdrv,BYTE cmd,void *buff)
{
DRESULT res;
if(pdrv==SD_CARD)// SD卡
{
switch(cmd)
{
case CTRL_SYNC:
res = RES_OK;
break;
case GET_SECTOR_SIZE:
*(DWORD*)buff = 512;
res = RES_OK;
break;
case GET_BLOCK_SIZE:
*(WORD*)buff = SDCardInfo.CardBlockSize;
res = RES_OK;
break;
case GET_SECTOR_COUNT:
*(DWORD*)buff = SDCardInfo.CardCapacity/512;
res = RES_OK;
break;
default:
res = RES_PARERR;
break;
}
}
else if(pdrv==USB_DISK) // U盘
{
}
else
res=RES_ERROR;// 其他的不支持
return res;
}
void *ff_memalloc (UINT size)
{
return (void*)mymalloc(SRAMEX,size);
}
//释放内存
void ff_memfree (void* mf)
{
myfree(SRAMEX,mf);
}
//-----------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------