2023-09-07 23:47:34 +08:00
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
/* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
/* 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. */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
#include "ff.h" /* Obtains integer types */
|
|
|
|
|
|
#include "diskio.h" /* Declarations of disk functions */
|
2023-10-02 00:22:13 +08:00
|
|
|
|
#include "w25q64.h"
|
2023-09-21 21:44:17 +08:00
|
|
|
|
#include <stdio.h>
|
2023-09-07 23:47:34 +08:00
|
|
|
|
/* Definitions of physical drive number for each drive */
|
2023-10-02 00:22:13 +08:00
|
|
|
|
#define DEV_EXT_FLASH 0 /* Example: Map Ramdisk to physical drive 0 */
|
2023-09-07 23:47:34 +08:00
|
|
|
|
|
2023-10-02 00:22:13 +08:00
|
|
|
|
#define FLASH_SECTOR_SIZE 4096
|
|
|
|
|
|
u16 FLASH_SECTOR_COUNT = 2048; // W25Q64,前12M字节给FATFS占用
|
|
|
|
|
|
#define FLASH_BLOCK_SIZE 1 // 每个BLOCK有8个扇区
|
2023-09-07 23:47:34 +08:00
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
/* Get Drive Status */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
DSTATUS disk_status(
|
|
|
|
|
|
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
2023-09-07 23:47:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
DSTATUS stat;
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
switch (pdrv)
|
|
|
|
|
|
{
|
2023-10-02 00:22:13 +08:00
|
|
|
|
case DEV_EXT_FLASH:
|
2023-09-09 12:58:57 +08:00
|
|
|
|
result = 0;
|
2023-10-02 00:22:13 +08:00
|
|
|
|
if (W25QXX_ReadID() != W25Q64)
|
|
|
|
|
|
{
|
|
|
|
|
|
stat = RES_ERROR;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
stat = RES_OK;
|
|
|
|
|
|
}
|
2023-09-07 23:47:34 +08:00
|
|
|
|
return stat;
|
|
|
|
|
|
}
|
|
|
|
|
|
return STA_NOINIT;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
/* Inidialize a Drive */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
DSTATUS disk_initialize(
|
|
|
|
|
|
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
2023-09-07 23:47:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
DSTATUS stat;
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
switch (pdrv)
|
|
|
|
|
|
{
|
2023-10-02 00:22:13 +08:00
|
|
|
|
case DEV_EXT_FLASH:
|
2023-09-09 12:58:57 +08:00
|
|
|
|
result = 0;
|
2023-10-02 00:22:13 +08:00
|
|
|
|
W25QXX_Init();
|
2023-09-07 23:47:34 +08:00
|
|
|
|
// translate the reslut code here
|
2023-10-02 00:22:13 +08:00
|
|
|
|
return disk_status(pdrv);
|
2023-09-07 23:47:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
return STA_NOINIT;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
/* Read Sector(s) */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
DRESULT disk_read(
|
|
|
|
|
|
BYTE pdrv, /* Physical drive nmuber to identify the drive */
|
|
|
|
|
|
BYTE *buff, /* Data buffer to store read data */
|
|
|
|
|
|
LBA_t sector, /* Start sector in LBA */
|
|
|
|
|
|
UINT count /* Number of sectors to read */
|
2023-09-07 23:47:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
DRESULT res;
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
switch (pdrv)
|
|
|
|
|
|
{
|
2023-10-02 00:22:13 +08:00
|
|
|
|
case DEV_EXT_FLASH:
|
|
|
|
|
|
for (; count > 0; count--)
|
|
|
|
|
|
{
|
|
|
|
|
|
W25QXX_Read(buff, sector * FLASH_SECTOR_SIZE, FLASH_SECTOR_SIZE);
|
|
|
|
|
|
sector++;
|
|
|
|
|
|
buff += FLASH_SECTOR_SIZE;
|
2023-09-09 12:58:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
2023-09-07 23:47:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return RES_PARERR;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
/* Write Sector(s) */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
#if FF_FS_READONLY == 0
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
DRESULT disk_write(
|
|
|
|
|
|
BYTE pdrv, /* Physical drive nmuber to identify the drive */
|
|
|
|
|
|
const BYTE *buff, /* Data to be written */
|
|
|
|
|
|
LBA_t sector, /* Start sector in LBA */
|
|
|
|
|
|
UINT count /* Number of sectors to write */
|
2023-09-07 23:47:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
switch (pdrv)
|
|
|
|
|
|
{
|
2023-10-02 00:22:13 +08:00
|
|
|
|
case DEV_EXT_FLASH:
|
|
|
|
|
|
for (; count > 0; count--)
|
|
|
|
|
|
{
|
|
|
|
|
|
W25QXX_Erase_Sector(sector); // 擦除这个扇区
|
|
|
|
|
|
W25QXX_Write_NoCheck((u8 *)buff, sector * FLASH_SECTOR_SIZE, FLASH_SECTOR_SIZE);
|
|
|
|
|
|
sector++;
|
|
|
|
|
|
buff += FLASH_SECTOR_SIZE;
|
2023-09-09 12:58:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
2023-09-07 23:47:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return RES_PARERR;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
/* Miscellaneous Functions */
|
|
|
|
|
|
/*-----------------------------------------------------------------------*/
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
DRESULT disk_ioctl(
|
|
|
|
|
|
BYTE pdrv, /* Physical drive nmuber (0..) */
|
|
|
|
|
|
BYTE cmd, /* Control code */
|
|
|
|
|
|
void *buff /* Buffer to send/receive control data */
|
2023-09-07 23:47:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
DRESULT res;
|
|
|
|
|
|
int result;
|
|
|
|
|
|
|
2023-09-09 12:58:57 +08:00
|
|
|
|
switch (pdrv)
|
|
|
|
|
|
{
|
2023-10-02 00:22:13 +08:00
|
|
|
|
case DEV_EXT_FLASH:
|
2023-09-09 12:58:57 +08:00
|
|
|
|
switch (cmd)
|
|
|
|
|
|
{ // fatfs内核使用cmd调用
|
|
|
|
|
|
case GET_SECTOR_COUNT: // sector count, 传入sect_cnt
|
2023-10-02 00:22:13 +08:00
|
|
|
|
*(DWORD *)buff = FLASH_SECTOR_COUNT;
|
2023-09-09 12:58:57 +08:00
|
|
|
|
return RES_OK;
|
|
|
|
|
|
case GET_SECTOR_SIZE: // sector size, 传入block size(SD),单位bytes
|
2025-03-23 17:35:56 +08:00
|
|
|
|
*(WORD *)buff = FLASH_SECTOR_SIZE;
|
2023-09-09 12:58:57 +08:00
|
|
|
|
return RES_OK;
|
2023-10-02 00:22:13 +08:00
|
|
|
|
case GET_BLOCK_SIZE: // block size, 由上文可得,对于SD2.0卡最大8192,最小 1
|
|
|
|
|
|
*(DWORD *)buff = FLASH_BLOCK_SIZE; // 单位为 sector(FatFs)
|
2023-09-09 12:58:57 +08:00
|
|
|
|
return RES_OK;
|
|
|
|
|
|
case CTRL_SYNC: // 同步命令,貌似FatFs内核用来判断写操作是否完成
|
|
|
|
|
|
return RES_OK;
|
|
|
|
|
|
}
|
|
|
|
|
|
default:
|
2023-10-02 00:22:13 +08:00
|
|
|
|
// printf("No device %d.\n", pdrv);
|
2023-09-09 12:58:57 +08:00
|
|
|
|
break;
|
2023-09-07 23:47:34 +08:00
|
|
|
|
}
|
2023-09-09 12:58:57 +08:00
|
|
|
|
return RES_PARERR; // 默认返回参数错误
|
2023-09-07 23:47:34 +08:00
|
|
|
|
}
|