[RISC-V MCU 应用开发] 【RISC-V MCU CH32V103测评】10:FATFS移植Demo

[复制链接]
 楼主| 740071911 发表于 2020-12-2 09:21 | 显示全部楼层 |阅读模式
本帖最后由 740071911 于 2022-1-7 09:12 编辑

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

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

1.PNG

这里是目录的下半截图

2.PNG

接口文件diskio.c的移植


  1. /* Includes ------------------------------------------------------------------*/
  2. #include "diskio.h"
  3. #include "ffconf.h"
  4. #include "spi_sd.h"

  5. /* Private typedef -----------------------------------------------------------*/
  6. /* Private define ------------------------------------------------------------*/
  7. #define BLOCK_SIZE 512 /* Block Size in Bytes */

  8. /* Private variables ---------------------------------------------------------*/
  9. /* Private function prototypes -----------------------------------------------*/
  10. /* Private functions ---------------------------------------------------------*/

  11. /**
  12.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Initializes a Disk
  13.   * @param  pdrv: Physical drive number
  14.   * @retval DSTATUS: Operation status
  15.   */
  16. DSTATUS disk_initialize(BYTE pdrv)
  17. {
  18.     SD_Error res = SD_RESPONSE_FAILURE;
  19.     res =  SD_Init();
  20.     return ((DSTATUS)res);
  21. }

  22. /**
  23.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Gets Disk Status
  24.   * @param  pdrv: Physical drive number
  25.   * @retval DSTATUS: Operation status
  26.   */
  27. DSTATUS disk_status (BYTE pdrv)
  28. {
  29.     if (pdrv) return STA_NOINIT; /* Supports only single drive */
  30.     return 0;
  31. }

  32. /**
  33.   * @brief  Reads Sector
  34.   * @param  pdrv: Physical drive number
  35.   * @param  *buff: Data buffer to store read data
  36.   * @param  sector: Sector address (LBA)
  37.   * @param  count: Number of sectors to read
  38.   * @retval DRESULT: Operation result
  39.   */
  40. DRESULT disk_read (BYTE pdrv, BYTE*buff, DWORD sector, UINT count)
  41. {
  42.     SD_ReadBlock(buff, sector << 9, BLOCK_SIZE);
  43.     return RES_OK;
  44. }

  45. /**
  46.   * @brief  Writes Sector
  47.   * @param  pdrv: Physical drive number
  48.   * @param  *buff: Data to be written
  49.   * @param  sector: Sector address (LBA)
  50.   * @param  count: Number of sectors to write
  51.   * @retval DRESULT: Operation result
  52.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]   The FatFs module will issue multiple sector transfer request
  53.   *         (count > 1) to the disk I/O layer. The disk function should process
  54.   *         the multiple sector transfer properly Do. not translate it into
  55.   *         multiple single sector transfers to the media, or the data read/write
  56.   *         performance may be drasticaly decreased.
  57.   */
  58. #if _USE_WRITE == 1
  59. DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count)
  60. {
  61.     SD_WriteBlock((BYTE *)buff, sector << 9, BLOCK_SIZE);
  62.    
  63.     return RES_OK;
  64. }
  65. #endif /* _USE_WRITE == 1 */

  66. /**
  67.   * @brief  Get current time
  68.   * @param  none
  69.   * @retval DWORD: Current time
  70.   */
  71. DWORD get_fattime ()
  72. {
  73.   return   ((2006UL-1980) << 25) /* Year = 2006 */
  74.           | (2UL << 21)          /* Month = Feb */
  75.           | (9UL << 16)          /* Day = 9 */
  76.           | (22U << 11)          /* Hour = 22 */
  77.           | (30U << 5)           /* Min = 30 */
  78.           | (0U >> 1)            /* Sec = 0 */
  79.               ;
  80. }

  81. /*-----------------------------------------------------------------------*/
  82. /* Miscellaneous Functions                                               */
  83. /*-----------------------------------------------------------------------*/

  84. /**
  85.   * @brief  I/O control operation
  86.   * @param  pdrv: Physical drive number
  87.   * @param  cmd: Control code
  88.   * @param  *buff: Buffer to send/receive control data
  89.   * @retval DRESULT: Operation result
  90.   */
  91. #if _USE_IOCTL != 0
  92. DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
  93. {
  94.     DRESULT res = RES_OK;
  95.     switch (cmd)
  96.     {
  97.         /* Get number of sectors on the disk (DWORD) */
  98.         case GET_SECTOR_COUNT :
  99.         *(DWORD*)buff = 131072; /* 4*1024*32 = 131072 */
  100.         res = RES_OK;
  101.         break;
  102.         
  103.         /* Get R/W sector size (WORD) */
  104.         case GET_SECTOR_SIZE :
  105.         *(WORD*)buff = BLOCK_SIZE;
  106.         res = RES_OK;
  107.         break;
  108.         
  109.         /* Get erase block size in unit of sector (DWORD) */
  110.         case GET_BLOCK_SIZE :
  111.         *(DWORD*)buff = 32;
  112.         res = RES_OK;
  113.     }
  114.     return res;
  115. }
  116. #endif /* _USE_IOCTL == 1 */



main.c主函数的实现:

3.PNG

同样,最后附上demo

SD_FATFS_R0.10.rar (714.47 KB, 下载次数: 19)



jinglixixi 发表于 2020-12-2 21:15 | 显示全部楼层
本帖最后由 jinglixixi 于 2020-12-2 21:24 编辑

感谢分享!这是我在相同测试环境下,最成功的代码。
ok.jpg
 楼主| 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 ...

就是显示不全 捕获.PNG

评论

有个波浪线看到吗  发表于 2020-12-3 16:37
jinglixixi 发表于 2020-12-3 19:04 | 显示全部楼层
本帖最后由 jinglixixi 于 2020-12-3 19:05 编辑

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

评论

没有全读出来了  发表于 2020-12-3 19:51
 楼主| 740071911 发表于 2020-12-3 19:50 | 显示全部楼层
jinglixixi 发表于 2020-12-3 19:04
这个问题可以这样看,在显示不全的时候与显示全的时候,文件名都占8个字符,也就是说在读取文件名时是应该 ...

捕获.PNG

英文长文件名是支持的,只是不支持中文的长文件名
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 | 显示全部楼层
这个支持多大的内存卡   
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:想低声说句不在乎,可会飞的心总是在高处!

48

主题

887

帖子

5

粉丝
快速回复 在线客服 返回列表 返回顶部