167 lines
4.8 KiB
C
167 lines
4.8 KiB
C
/*-----------------------------------------------------------------------*/
|
||
/* 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. */
|
||
/*-----------------------------------------------------------------------*/
|
||
|
||
#include "ff.h" /* Obtains integer types */
|
||
#include "diskio.h" /* Declarations of disk functions */
|
||
#include "w25q64.h"
|
||
#include <stdio.h>
|
||
/* Definitions of physical drive number for each drive */
|
||
#define DEV_EXT_FLASH 0 /* Example: Map Ramdisk to physical drive 0 */
|
||
|
||
#define FLASH_SECTOR_SIZE 4096
|
||
u16 FLASH_SECTOR_COUNT = 2048; // W25Q64,前12M字节给FATFS占用
|
||
#define FLASH_BLOCK_SIZE 1 // 每个BLOCK有8个扇区
|
||
/*-----------------------------------------------------------------------*/
|
||
/* Get Drive Status */
|
||
/*-----------------------------------------------------------------------*/
|
||
|
||
DSTATUS disk_status(
|
||
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
||
)
|
||
{
|
||
DSTATUS stat;
|
||
int result;
|
||
|
||
switch (pdrv)
|
||
{
|
||
case DEV_EXT_FLASH:
|
||
result = 0;
|
||
if (W25QXX_ReadID() != W25Q64)
|
||
{
|
||
stat = RES_ERROR;
|
||
}
|
||
else
|
||
{
|
||
stat = RES_OK;
|
||
}
|
||
return stat;
|
||
}
|
||
return STA_NOINIT;
|
||
}
|
||
|
||
/*-----------------------------------------------------------------------*/
|
||
/* Inidialize a Drive */
|
||
/*-----------------------------------------------------------------------*/
|
||
|
||
DSTATUS disk_initialize(
|
||
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
||
)
|
||
{
|
||
DSTATUS stat;
|
||
int result;
|
||
|
||
switch (pdrv)
|
||
{
|
||
case DEV_EXT_FLASH:
|
||
result = 0;
|
||
W25QXX_Init();
|
||
// translate the reslut code here
|
||
return disk_status(pdrv);
|
||
}
|
||
return STA_NOINIT;
|
||
}
|
||
|
||
/*-----------------------------------------------------------------------*/
|
||
/* Read Sector(s) */
|
||
/*-----------------------------------------------------------------------*/
|
||
|
||
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 */
|
||
)
|
||
{
|
||
DRESULT res;
|
||
|
||
switch (pdrv)
|
||
{
|
||
case DEV_EXT_FLASH:
|
||
for (; count > 0; count--)
|
||
{
|
||
W25QXX_Read(buff, sector * FLASH_SECTOR_SIZE, FLASH_SECTOR_SIZE);
|
||
sector++;
|
||
buff += FLASH_SECTOR_SIZE;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
return RES_PARERR;
|
||
}
|
||
|
||
/*-----------------------------------------------------------------------*/
|
||
/* Write Sector(s) */
|
||
/*-----------------------------------------------------------------------*/
|
||
|
||
#if FF_FS_READONLY == 0
|
||
|
||
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 */
|
||
)
|
||
{
|
||
int result;
|
||
|
||
switch (pdrv)
|
||
{
|
||
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;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
return RES_PARERR;
|
||
}
|
||
|
||
#endif
|
||
|
||
/*-----------------------------------------------------------------------*/
|
||
/* Miscellaneous Functions */
|
||
/*-----------------------------------------------------------------------*/
|
||
|
||
DRESULT disk_ioctl(
|
||
BYTE pdrv, /* Physical drive nmuber (0..) */
|
||
BYTE cmd, /* Control code */
|
||
void *buff /* Buffer to send/receive control data */
|
||
)
|
||
{
|
||
DRESULT res;
|
||
int result;
|
||
|
||
switch (pdrv)
|
||
{
|
||
case DEV_EXT_FLASH:
|
||
switch (cmd)
|
||
{ // fatfs内核使用cmd调用
|
||
case GET_SECTOR_COUNT: // sector count, 传入sect_cnt
|
||
*(DWORD *)buff = FLASH_SECTOR_COUNT;
|
||
return RES_OK;
|
||
case GET_SECTOR_SIZE: // sector size, 传入block size(SD),单位bytes
|
||
*(DWORD *)buff = FLASH_SECTOR_SIZE;
|
||
return RES_OK;
|
||
case GET_BLOCK_SIZE: // block size, 由上文可得,对于SD2.0卡最大8192,最小 1
|
||
*(DWORD *)buff = FLASH_BLOCK_SIZE; // 单位为 sector(FatFs)
|
||
return RES_OK;
|
||
case CTRL_SYNC: // 同步命令,貌似FatFs内核用来判断写操作是否完成
|
||
return RES_OK;
|
||
}
|
||
default:
|
||
// printf("No device %d.\n", pdrv);
|
||
break;
|
||
}
|
||
return RES_PARERR; // 默认返回参数错误
|
||
}
|