2016-06-03 04:37 PM
I developed a board using STM32F411 chip with uSD card.The code is generated by cubemx. only add a little bit code in main(). The hardware is OK. I can correctly write a file to sd card. But the weird thing is that the filename accepts 7 chars, not 6 chars. If I use 6 chars in the filename, the character encoding is totally different.
In the testing I only change the filename. I attached two files generated by the chip. I can't read the first the characters in the first file. The problem is the encoding of the files are different. the thing is why the encoding are different. I didn't change anything. Actually I use ANSI in the setting. My question is 1.why the program uses two different encodings
for the files
just because I use different filenames
? The file with 7 chars in filename always uses utf-8 encoding.2. how to correctly set the encoding
.Actually in the file I specify it to use ANSI encoding, as in ffconf.h. Why the program uses others?
Please help.The two txt files and the ffconf.h are enclosed as attachment.
int
main(
void
)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_SDIO_SD_Init();
MX_TIM9_Init();
// MX_FATFS_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
/* USER CODE BEGIN WHILE */
FRESULT res;
/* FatFs function common result code */
uint32_t byteswritten, bytesread,byteswrittentotal;
/* File write/read counts */
uint8_t wtext[] =
''testing using own code. This is STM32 working with FatFs..2 ''
;
/* File write buffer . note it will produce a null symbol at the line end*/
uint8_t rtext[100];
char
textbuf[200];
//string buffer
char
filename[]=
''testatxt''
;
//filename can be >8, depends on setting /* File read buffer */
/*##-1- Link the micro SD disk I/O driver ##################################*/
if
(FATFS_LinkDriver(&SD_Driver, SD_Path) == 0)
//SD_Path was defined in fatfs.c by cubemx
{
/*##-2- Register the file system object to the FatFs module ##############*/
if
(f_mount(&SDFatFs, (TCHAR
const
*)SD_Path, 0) != FR_OK)
{
/* FatFs Initialization Error */
Error_Handler();
}
else
{
/* ##-4- Create and Open a new text file object with write access #####*/
if
(f_open(&MyFile, filename, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
/* 'STMTXT' file Open for write Error */
Error_Handler();
}
else
{
/*##-5- Write data to the text file ################################*/
res = f_write(&MyFile, wtext,
sizeof
(wtext), (
void
*)&byteswritten);
byteswrittentotal=byteswritten;
//write capacity info as testing
BSP_SD_GetCardInfo(&uSdCardInfotmp);
sprintf(textbuf,
''\nThe capacity of the card is:%''
PRIu64
''B\n''
,uSdCardInfotmp.CardCapacity);
//export it to char buffer first. In this way, no null symbol
f_write(&MyFile, textbuf, strlen(textbuf), (
void
*)&byteswritten);
byteswrittentotal+=byteswritten;
sprintf(textbuf,
''\n this is for file: %s \n''
,filename);
//export it to char buffer first. In this way, no null symbol
f_write(&MyFile, textbuf, strlen(textbuf), (
void
*)&byteswritten);
byteswrittentotal+=byteswritten;
if
((byteswritten == 0) || (res != FR_OK))
{
/* 'STMTXT' file Write or EOF Error */
Error_Handler();
}
else
{
/*##-6- Close the open text file #################################*/
f_close(&MyFile);
/*##-7- Open the text file object with read access ###############*/
if
(f_open(&MyFile, filename, FA_READ) != FR_OK)
{
/* 'STMTXT' file Open for read Error */
Error_Handler();
}
else
{
/*##-8- Read data from the text file ###########################*/
res = f_read(&MyFile, rtext,
sizeof
(rtext), (UINT*)&bytesread);
if
((bytesread == 0) || (res != FR_OK))
{
/* 'STMTXT' file Read or EOF Error */
Error_Handler();
}
else
{
/*##-9- Close the open text file #############################*/
f_close(&MyFile);
/*##-10- Compare read data with the expected data ############*/
if
((bytesread != byteswritten))
{
/* Read data is different from the expected data */
Error_Handler();
}
else
{
/* Success of the demo: no error occurrence */
// BSP_LED_On(LED1);
}
}
}
}
}
}
}
/*##-11- Unlink the RAM disk I/O driver ####################################*/
FATFS_UnLinkDriver(SD_Path);
#encoding #usd
Solved! Go to Solution.
2016-06-04 08:52 PM
I find the problem.
In the code, I set the reading buffer to be a too small size as uint8_t rtext[100]; Actually the data are kind of 134bytes. for unkown reason the program produces very werid behavior. after I change it to rtext[200], everything is working well. The encoding of the file now is ANSI, which is supposed to be. We can control the program to write the text with specific encoding, which is defined in ffconf.h. Again sometime I know the weird behavior should be caused by something else. but it is just hard to figure out. Anyway the good thing is that user canuse cubemx to generate code for microsd
card with little code only in main() For convenience of others, I attached the ioc file. And copy the code from the main() below, one you get a working code for microsd card. Note it is for stm32f4 for other chip, one can do the similar thing in cubemx.int
main(
void
)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_SDIO_SD_Init();
MX_TIM9_Init();
// MX_FATFS_Init(); //instead we call FATFS_LinkDriver directly blow
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
/* USER CODE BEGIN WHILE */
FRESULT res;
/* FatFs function common result code */
uint32_t byteswritten, bytesread,byteswrittentotal;
/* File write/read counts */
uint8_t wtext[] =
''testing using own code. This is STM32 working with FatFs..2 ''
;
/* File write buffer . note it will produce a null symbol at the line end*/
uint8_t rtext[200];
char
textbuf[200];
//string buffer
char
filename[]=
''testatxt''
;
//filename can be >8, depends on setting /* File read buffer */
/*##-1- Link the micro SD disk I/O driver ##################################*/
if
(FATFS_LinkDriver(&SD_Driver, SD_Path) == 0)
//SD_Path was defined in fatfs.c by cubemx
{
/*##-2- Register the file system object to the FatFs module ##############*/
if
(f_mount(&SDFatFs, (TCHAR
const
*)SD_Path, 0) != FR_OK)
{
/* FatFs Initialization Error */
Error_Handler();
}
else
{
/* ##-4- Create and Open a new text file object with write access #####*/
if
(f_open(&MyFile, filename, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
/* 'STMTXT' file Open for write Error */
Error_Handler();
}
else
{
/*##-5- Write data to the text file ################################*/
res = f_write(&MyFile, wtext,
sizeof
(wtext), (
void
*)&byteswritten);
byteswrittentotal=byteswritten;
//write capacity info as testing
BSP_SD_GetCardInfo(&uSdCardInfotmp);
sprintf(textbuf,
''
The capacity of the card is:%''
PRIu64
''B
''
,uSdCardInfotmp.CardCapacity);
//export it to char buffer first. In this way, no null symbol
f_write(&MyFile, textbuf, strlen(textbuf), (
void
*)&byteswritten);
byteswrittentotal+=byteswritten;
sprintf(textbuf,
''
this is for file: %s
''
,filename);
//export it to char buffer first. In this way, no null symbol
f_write(&MyFile, textbuf, strlen(textbuf), (
void
*)&byteswritten);
byteswrittentotal+=byteswritten;
if
((byteswritten == 0) || (res != FR_OK))
{
/* 'STMTXT' file Write or EOF Error */
Error_Handler();
}
else
{
/*##-6- Close the open text file #################################*/
f_close(&MyFile);
/*##-7- Open the text file object with read access ###############*/
if
(f_open(&MyFile, filename, FA_READ) != FR_OK)
{
/* 'STMTXT' file Open for read Error */
Error_Handler();
}
else
{
/*##-8- Read data from the text file ###########################*/
res = f_read(&MyFile, rtext,
sizeof
(rtext), (UINT*)&bytesread);
if
((bytesread == 0) || (res != FR_OK))
{
/* 'STMTXT' file Read or EOF Error */
Error_Handler();
}
else
{
/*##-9- Close the open text file #############################*/
f_close(&MyFile);
/*##-10- Compare read data with the expected data ############*/
if
((bytesread != byteswritten))
{
/* Read data is different from the expected data */
Error_Handler();
}
else
{
/* Success of the demo: no error occurrence */
// BSP_LED_On(LED1);
}
}
}
}
}
}
}
/*##-11- Unlink the RAM disk I/O driver ####################################*/
FATFS_UnLinkDriver(SD_Path);
while
(1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
2016-06-04 10:49 AM
There is no explicit encoding information embedded within text files. There is only the raw text. Typically, the interpreter (in this case Notepad) can correctly guess it based on the composition of the text in the file, but sometimes it gets it wrong. The solution here would be to either use a different interpreter or to select the ''UTF-8'' encoding when it gets it wrong.
2016-06-04 08:52 PM
I find the problem.
In the code, I set the reading buffer to be a too small size as uint8_t rtext[100]; Actually the data are kind of 134bytes. for unkown reason the program produces very werid behavior. after I change it to rtext[200], everything is working well. The encoding of the file now is ANSI, which is supposed to be. We can control the program to write the text with specific encoding, which is defined in ffconf.h. Again sometime I know the weird behavior should be caused by something else. but it is just hard to figure out. Anyway the good thing is that user canuse cubemx to generate code for microsd
card with little code only in main() For convenience of others, I attached the ioc file. And copy the code from the main() below, one you get a working code for microsd card. Note it is for stm32f4 for other chip, one can do the similar thing in cubemx.int
main(
void
)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_SDIO_SD_Init();
MX_TIM9_Init();
// MX_FATFS_Init(); //instead we call FATFS_LinkDriver directly blow
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
/* USER CODE BEGIN WHILE */
FRESULT res;
/* FatFs function common result code */
uint32_t byteswritten, bytesread,byteswrittentotal;
/* File write/read counts */
uint8_t wtext[] =
''testing using own code. This is STM32 working with FatFs..2 ''
;
/* File write buffer . note it will produce a null symbol at the line end*/
uint8_t rtext[200];
char
textbuf[200];
//string buffer
char
filename[]=
''testatxt''
;
//filename can be >8, depends on setting /* File read buffer */
/*##-1- Link the micro SD disk I/O driver ##################################*/
if
(FATFS_LinkDriver(&SD_Driver, SD_Path) == 0)
//SD_Path was defined in fatfs.c by cubemx
{
/*##-2- Register the file system object to the FatFs module ##############*/
if
(f_mount(&SDFatFs, (TCHAR
const
*)SD_Path, 0) != FR_OK)
{
/* FatFs Initialization Error */
Error_Handler();
}
else
{
/* ##-4- Create and Open a new text file object with write access #####*/
if
(f_open(&MyFile, filename, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
/* 'STMTXT' file Open for write Error */
Error_Handler();
}
else
{
/*##-5- Write data to the text file ################################*/
res = f_write(&MyFile, wtext,
sizeof
(wtext), (
void
*)&byteswritten);
byteswrittentotal=byteswritten;
//write capacity info as testing
BSP_SD_GetCardInfo(&uSdCardInfotmp);
sprintf(textbuf,
''
The capacity of the card is:%''
PRIu64
''B
''
,uSdCardInfotmp.CardCapacity);
//export it to char buffer first. In this way, no null symbol
f_write(&MyFile, textbuf, strlen(textbuf), (
void
*)&byteswritten);
byteswrittentotal+=byteswritten;
sprintf(textbuf,
''
this is for file: %s
''
,filename);
//export it to char buffer first. In this way, no null symbol
f_write(&MyFile, textbuf, strlen(textbuf), (
void
*)&byteswritten);
byteswrittentotal+=byteswritten;
if
((byteswritten == 0) || (res != FR_OK))
{
/* 'STMTXT' file Write or EOF Error */
Error_Handler();
}
else
{
/*##-6- Close the open text file #################################*/
f_close(&MyFile);
/*##-7- Open the text file object with read access ###############*/
if
(f_open(&MyFile, filename, FA_READ) != FR_OK)
{
/* 'STMTXT' file Open for read Error */
Error_Handler();
}
else
{
/*##-8- Read data from the text file ###########################*/
res = f_read(&MyFile, rtext,
sizeof
(rtext), (UINT*)&bytesread);
if
((bytesread == 0) || (res != FR_OK))
{
/* 'STMTXT' file Read or EOF Error */
Error_Handler();
}
else
{
/*##-9- Close the open text file #############################*/
f_close(&MyFile);
/*##-10- Compare read data with the expected data ############*/
if
((bytesread != byteswritten))
{
/* Read data is different from the expected data */
Error_Handler();
}
else
{
/* Success of the demo: no error occurrence */
// BSP_LED_On(LED1);
}
}
}
}
}
}
}
/*##-11- Unlink the RAM disk I/O driver ####################################*/
FATFS_UnLinkDriver(SD_Path);
while
(1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
2016-06-07 02:16 AM
Hi mao,
Thank you for the contribution and khnowledge share. I would like to mention that the .ioc file has not been attached. Would you reattach. -Hannibal-