740071911 发表于 2020-12-2 09:21

【RISC-V MCU CH32V103测评】10:FATFS移植Demo

本帖最后由 740071911 于 2022-1-7 09:12 编辑

对FatFS移植,主要是将diskio.c接口和ffconf.h的配置
先来看看效果:

因为目录比较多,分两张截图



这里是目录的下半截图



接口文件diskio.c的移植


/* Includes ------------------------------------------------------------------*/
#include "diskio.h"
#include "ffconf.h"
#include "spi_sd.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define BLOCK_SIZE 512 /* Block Size in Bytes */

/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
* @briefInitializes a Disk
* @parampdrv: Physical drive number
* @retval DSTATUS: Operation status
*/
DSTATUS disk_initialize(BYTE pdrv)
{
    SD_Error res = SD_RESPONSE_FAILURE;
    res =SD_Init();
    return ((DSTATUS)res);
}

/**
* @briefGets Disk Status
* @parampdrv: Physical drive number
* @retval DSTATUS: Operation status
*/
DSTATUS disk_status (BYTE pdrv)
{
    if (pdrv) return STA_NOINIT; /* Supports only single drive */
    return 0;
}

/**
* @briefReads Sector
* @parampdrv: Physical drive number
* @param*buff: Data buffer to store read data
* @paramsector: Sector address (LBA)
* @paramcount: Number of sectors to read
* @retval DRESULT: Operation result
*/
DRESULT disk_read (BYTE pdrv, BYTE*buff, DWORD sector, UINT count)
{
    SD_ReadBlock(buff, sector << 9, BLOCK_SIZE);
    return RES_OK;
}

/**
* @briefWrites Sector
* @parampdrv: Physical drive number
* @param*buff: Data to be written
* @paramsector: Sector address (LBA)
* @paramcount: Number of sectors to write
* @retval DRESULT: Operation result
* @NOTE   The FatFs module will issue multiple sector transfer request
*         (count > 1) to the disk I/O layer. The disk function should process
*         the multiple sector transfer properly Do. not translate it into
*         multiple single sector transfers to the media, or the data read/write
*         performance may be drasticaly decreased.
*/
#if _USE_WRITE == 1
DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count)
{
    SD_WriteBlock((BYTE *)buff, sector << 9, BLOCK_SIZE);
   
    return RES_OK;
}
#endif /* _USE_WRITE == 1 */

/**
* @briefGet current time
* @paramnone
* @retval DWORD: Current time
*/
DWORD get_fattime ()
{
return   ((2006UL-1980) << 25) /* Year = 2006 */
          | (2UL << 21)          /* Month = Feb */
          | (9UL << 16)          /* Day = 9 */
          | (22U << 11)          /* Hour = 22 */
          | (30U << 5)         /* Min = 30 */
          | (0U >> 1)            /* Sec = 0 */
            ;
}

/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                             */
/*-----------------------------------------------------------------------*/

/**
* @briefI/O control operation
* @parampdrv: Physical drive number
* @paramcmd: Control code
* @param*buff: Buffer to send/receive control data
* @retval DRESULT: Operation result
*/
#if _USE_IOCTL != 0
DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
{
    DRESULT res = RES_OK;
    switch (cmd)
    {
      /* Get number of sectors on the disk (DWORD) */
      case GET_SECTOR_COUNT :
      *(DWORD*)buff = 131072; /* 4*1024*32 = 131072 */
      res = RES_OK;
      break;
      
      /* Get R/W sector size (WORD) */
      case GET_SECTOR_SIZE :
      *(WORD*)buff = BLOCK_SIZE;
      res = RES_OK;
      break;
      
      /* Get erase block size in unit of sector (DWORD) */
      case GET_BLOCK_SIZE :
      *(DWORD*)buff = 32;
      res = RES_OK;
    }
    return res;
}
#endif /* _USE_IOCTL == 1 */



main.c主函数的实现:



同样,最后附上demo





jinglixixi 发表于 2020-12-2 21:15

本帖最后由 jinglixixi 于 2020-12-2 21:24 编辑

感谢分享!这是我在相同测试环境下,最成功的代码。

740071911 发表于 2020-12-3 08:49

jinglixixi 发表于 2020-12-2 21:15
感谢分享!这是我在相同测试环境下,最成功的代码。

你有空试试长文件名,我还没弄

zhengfish 发表于 2020-12-3 09:12

看看...

jinglixixi 发表于 2020-12-3 09:36

740071911 发表于 2020-12-3 08:49
你有空试试长文件名,我还没弄

你看看下面这个是否能借用一下来辅助解决,此外使用长文件的特殊点是啥。
https://bbs.21ic.com/icview-3049580-1-1.html

740071911 发表于 2020-12-3 16:36

jinglixixi 发表于 2020-12-3 09:36
你看看下面这个是否能借用一下来辅助解决,此外使用长文件的特殊点是啥。
https://bbs.21ic.com/icview-3 ...

就是显示不全

jinglixixi 发表于 2020-12-3 19:04

本帖最后由 jinglixixi 于 2020-12-3 19:05 编辑

740071911 发表于 2020-12-3 16:36
就是显示不全
这个问题可以这样看,在显示不全的时候与显示全的时候,文件名都占8个字符,也就是说在读取文件名时是应该是正确的,只是在显示时做了压缩处理,以~代替了因8个字符限制的而无法显示的内容。换句话说,就是你为显示文件名的变量扩展了宽度或是动态的长度结构就可达到目的。

740071911 发表于 2020-12-3 19:50

jinglixixi 发表于 2020-12-3 19:04
这个问题可以这样看,在显示不全的时候与显示全的时候,文件名都占8个字符,也就是说在读取文件名时是应该 ...



英文长文件名是支持的,只是不支持中文的长文件名

jinglixixi 发表于 2020-12-3 22:25

740071911 发表于 2020-12-3 19:50
英文长文件名是支持的,只是不支持中文的长文件名

那你能把中文的长文件名的编码存起来吗,看看原因所在。看中文文件名是能显示汉字,且只有8个字符长度,而英文却超过8个字符长度,证明两者分配的存储宽度不一样。

740071911 发表于 2020-12-4 08:53

jinglixixi 发表于 2020-12-3 22:25
那你能把中文的长文件名的编码存起来吗,看看原因所在。看中文文件名是能显示汉字,且只有8个字符长度, ...

编码那就要100多K的数组,flash总共才64K

jinglixixi 发表于 2020-12-4 10:12

740071911 发表于 2020-12-4 08:53
编码那就要100多K的数组,flash总共才64K

可以做个小实验,假设只有一个文件,且是长中文文件名,然后提取其文件名的内容,这样不是能把容量压下来吗,再适当将文件名的存放空间与长中文文件名的程度匹配来显示文件看是否有效。

740071911 发表于 2020-12-4 11:44

jinglixixi 发表于 2020-12-4 10:12
可以做个小实验,假设只有一个文件,且是长中文文件名,然后提取其文件名的内容,这样不是能把容量压下来 ...

恩可以试试

caizhiwei 发表于 2020-12-5 21:10

赞一个,今天找了一张128MB的 tf卡,来做实验的

发呆二极管 发表于 2021-12-24 23:45

学习一下~

hcn001 发表于 2021-12-29 11:02

666666666666

kkxxzz 发表于 2021-12-29 18:48

okokok!!!!!!!!!!!!

kkzz 发表于 2022-1-2 11:12

查看一下资料。      

hudi008 发表于 2022-1-2 11:12

fat32和fatfs有什么区别?

lzmm 发表于 2022-1-2 11:12

可以使用CSV格式吗   

minzisc 发表于 2022-1-2 11:13

这个支持多大的内存卡   
页: [1] 2
查看完整版本: 【RISC-V MCU CH32V103测评】10:FATFS移植Demo