Files
player/Project/Src/FATS/diskio.c
andy eab38c8eff 添加tftp协议
1.usb连接上之后,可以使用tftp协议传输文件
2.解决写入sd卡时如果buff地址不是4字节对齐时数据错位的问题
2025-10-18 23:55:18 +08:00

171 lines
5.3 KiB
C

/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
/*-----------------------------------------------------------------------*/
/* 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" /* FatFs lower layer API */
#include "sdio_sd.h"
#include "stdlib.h"
#include "string.h"
#define SD_Card 0 // SD¿¨
#define SPI_Flash 1 // SPI Flash
#define SD_BLOCKSIZE 512
extern SD_CardInfo SDCardInfo;
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status(
BYTE pdrv /* Physical drive nmuber to identify the drive */
) {
DSTATUS stat = STA_NOINIT;
switch (pdrv) {
case SD_Card:
stat &= ~STA_NOINIT;
return stat;
case SPI_Flash:
return stat;
}
return STA_NOINIT;
}
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize(
BYTE pdrv /* Physical drive nmuber to identify the drive */
) {
DSTATUS stat = STA_NOINIT;
switch (pdrv) {
case SD_Card:
if (SD_Init() == SD_OK) // SD¿¨³õʼ»¯
{
stat &= ~STA_NOINIT;
return stat;
}
case SPI_Flash:
return stat;
}
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 */
DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
) {
SD_Error SD_state = SD_OK;
BYTE *pbuff = buff;
if ((uint32_t)pbuff & 0x3) {
pbuff = malloc(SD_BLOCKSIZE * count);
}
switch (pdrv) {
case SD_Card:
SD_state = SD_ReadMultiBlocks(pbuff, (uint64_t)sector * SD_BLOCKSIZE,
SD_BLOCKSIZE, count);
if (SD_state == SD_OK) {
SD_state = SD_WaitReadOperation();
while (SD_GetStatus() != SD_TRANSFER_OK)
;
}
if (pbuff != buff) {
memcpy(buff, pbuff, SD_BLOCKSIZE * count);
free(pbuff);
}
if (SD_state == SD_OK)
return RES_OK;
else
return RES_PARERR;
case SPI_Flash:
return RES_PARERR;
}
return RES_PARERR;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_write(BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
) {
SD_Error SD_state = SD_OK;
BYTE *pbuff = (BYTE *)buff;
if ((uint32_t)pbuff & 0x3) {
pbuff = malloc(SD_BLOCKSIZE * count);
memcpy(pbuff, buff, SD_BLOCKSIZE * count);
}
switch (pdrv) {
case SD_Card:
SD_state = SD_WriteMultiBlocks(
(uint8_t *)pbuff, (uint64_t)sector * SD_BLOCKSIZE, SD_BLOCKSIZE, count);
if (SD_state == SD_OK) {
SD_state = SD_WaitWriteOperation();
while (SD_GetStatus() != SD_TRANSFER_OK)
;
if (pbuff != buff) {
free(pbuff);
}
if (SD_state == SD_OK)
return RES_OK;
else
return RES_PARERR;
}
case SPI_Flash:
return RES_PARERR;
}
return RES_PARERR;
}
/*-----------------------------------------------------------------------*/
/* 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 = RES_PARERR;
switch (pdrv) {
case SD_Card:
switch (cmd) {
case GET_SECTOR_COUNT:
*(DWORD *)buff = SDCardInfo.CardCapacity / SD_BLOCKSIZE;
break;
// Get R/W sector size (WORD)
case GET_SECTOR_SIZE:
*(WORD *)buff = SD_BLOCKSIZE;
break;
// Get erase block size in unit of sector (DWORD)
case GET_BLOCK_SIZE:
*(DWORD *)buff = SD_BLOCKSIZE;
break;
}
return RES_OK;
case SPI_Flash:
return res;
}
return RES_PARERR;
}
/*-----------------------------------------------------------------------*/
/* ʱ¼ä»ñÈ¡º¯Êý£¬ÔÚÕâÀﲻʹÓ㬷µ»Ø0 */
/*-----------------------------------------------------------------------*/
DWORD get_fattime(void) { return 0; }