Some Problems of FATFS

来源:互联网 发布:淘宝店掌柜名怎么改 编辑:程序博客网 时间:2024/06/05 03:42

Module: FATFS R0.12b

Problem Summary:
The character conversion from Unicode to ASCII/OEM(especially in 936(Simplified Chinese GBK) ) when _LFN_UNICODE==1.

Environment: MDK5.10.0.2

Description:
Firstly, thank you for your FAT filesystem module FATFS, ChaN, I like it very much.

I didn’t know FATFS can convert Unicode to ASCII/OEM automatically when reading a LFN of a file in the situation of _LFN_UNICODE==0 before, so I have to write a function to convert a LFN which is obtained from filinfo.fname, the member of FILINFO type structure, by

f_readdir(DIR* DirObject, FILINFO* FileInfo)

from Unicode to ASCII/OEM in the situation of _LFN_UNICODE==1. Recently, I have found a problem whether it is a bug or not and I expect to ask you for help.

I create a file with Chinese LFN in SD card. I can see the correct file name in SD Card by computer. However, I fail to display the same string in UART debug software except the English file name. The related part of code, files’ name displayed in computer and files’ name displayed in UART debug software are shown as follow.
这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

In order to find the problem, taking the LFN “English中文.txt” as an example, I trace into the f_readdir() and get the fileinfo.fname, then I obtain the array of fname[] compared with the code of the same file name coded in Unicode shown in following fig.
这里写图片描述

As is shown, the 1st byte of a DBC is cut out. Then, I trace into the funciton get_fileinfo() which is called by f_readdir(). After rough reading, I find the part of code in get_fileinfo() could cut out the 1st byte in the situation of _LFN_UNICODE==1 but no any problem in the situation of _LFN_UNICODE==0. The part of code is shown in following fig.
这里写图片描述

I try replacing the part of code with the following code and set _LFN_UNICODE==1 then the UART debug software print Chinese file name successfully. I also try printing Chinese file name in the situation of _LFN_UNICODE==0 and it works as well.

while ((w = *lfn++) != 0) {      /* Get an LFN character */#if !_LFN_UNICODE  w = ff_convert(w, 0);      /* Unicode -> OEM */  if (w == 0) { i = 0; break; }   /* No LFN if it could not be converted */  if (i >= _MAX_LFN) { i = 0; break; }   /* No LFN if buffer overflow */  if (_DF1S && w >= 0x100) {   /* Put 1st byte if it is a DBCS (always false at SBCS cfg) */    fno->fname[i++] = (char)(w >> 8);  }  fno->fname[i++] = (char)w;#else  if (i >= _MAX_LFN) { i = 0; break; }   /* No LFN if buffer overflow */  fno->fname[i++] = w;#endif}fno->fname[i] = 0;   /* Terminate the LFN */

That is all my description, thanks for reading.