I am practicing the microSD card on a PCB board with the STM32F405 chip. The testing program is generated by STM32CubeMX. The code is very easy to understand.
The problem is that an error happens when it goes to file open. But the weird thing is that if I reset the chip by pulling the NRST pin to low manually, the chip restarts and works smoothly.
Any clue?
/* 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(); // We call FATFS_LinkDriver directly to initialize the fatfs
MX_USB_DEVICE_Init();
TimeDelay_Init();
/* USER CODE BEGIN 2 */
GREEN_LED_ON;RED_LED_ON;
TM_DelayMillis(500);
GREEN_LED_OFF;RED_LED_OFF;
TM_DelayMillis(1000);
/* USER CODE BEGIN 2 */
// Before it starts, wait for computer to connect
while(SHIFT_BUTTON==1){ // Wait for the shift button to be pushed
;
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread, byteswrittentotal; /* File write/read counts */
uint8_t rtext[200];
char textbuf[200]; //string buffer
char filename[]="fileW4.txt"; // Filename length should be <= 8 /* File read buffer */
/* ##-1- Link the microSD disk I/O driver ################################## */
if(FATFS_LinkDriver(&SD_Driver, SD_Path) == 0) // We call FATFS_LinkDriver directly to initialize the fatfs
{
sprintf(usbbuffer, "\n---FATFS_LinkDriver OK ---\n");//export it to char buffer first.
SendTextMsgToUSB(usbbuffer); // Send the txt back
/* ##-2- Register the file system object to the FatFs module ############## */
if(f_mount(&SDFatFs, (TCHAR const*)SD_Path, 0) != FR_OK)
{
/* FatFs Initialization Error */
sprintf(usbbuffer, "\n---f_mount error ---\n"); // Export it to char buffer first.
SendTextMsgToUSB(usbbuffer); // Send the txt back
Error_Handler();
}
else
{
/* ##-3- Create a FAT file system (format) on the logical drive ######### */
/* WARNING: Formatting the uSD card will delete all content on the device */
// if(f_mkfs((TCHAR const*)SDPath, 0, 0) != FR_OK) // No need to format, so comment it
//f_mkfs((TCHAR const*)SDPath, 0, 0);
sprintf(usbbuffer, "\n---ready to open file ---\n"); // Export it to char buffer first.
SendTextMsgToUSB(usbbuffer); // Send the txt back
/* ##-4- Create and Open a new text file object with write access ##### */
res=f_open(&MyFile, filename, FA_CREATE_ALWAYS | FA_WRITE);
if (res!= FR_OK)
{
/* 'STM32.TXT' file Open for write Error */
sprintf(usbbuffer, "\n---Open file error ---\n");//export it to char buffer first.
SendTextMsgToUSB(usbbuffer); //send the txt back
Error_Handler();
}
else
{
/* ##-5- Write data to the text file ################################ */
sprintf(usbbuffer, "\n---File is open. Ready to write contents ---\n"); // Export it to char buffer first.
SendTextMsgToUSB(usbbuffer); // Send the txt back
// Write capacity info as testing
BSP_SD_GetCardInfo(&uSdCardInfotmp);
sprintf(textbuf, "\nThe capacity of the card is:%"PRIu64"B\nThis is for file:%s", uSdCardInfotmp.CardCapacity,filename); // Export it to char buffer first. In this way, no null symbol
res=f_write(&MyFile, textbuf, strlen(textbuf), (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Write or EOF Error */
Error_Handler();
}
sprintf(usbbuffer, "\n---First line was written %s ---\n", textbuf); // Export it to char buffer first.
SendTextMsgToUSB(usbbuffer); // Send the txt back
// Now we test the fast file writing of small bytes.
unsigned char testtimer=0;
for(int i=0; i<20000; i++)
{
sprintf(textbuf, "i=%d,data=%d\n", i, i); // Export it to char buffer first.
uint32_t bytecount=MyWriteToFile(&MyFile, textbuf, strlen(textbuf));
byteswritten += bytecount;
if(bytecount==0){
f_close(&MyFile);Error_Handler();
}
}
byteswritten += MyFlushDatatoFile(&MyFile); // Close file
f_close(&MyFile);
sprintf(usbbuffer, "\n---File is closed ---\n"); // Export it to char buffer first.
SendTextMsgToUSB(usbbuffer); // Send the txt back
GREEN_LED_ON;
}
}
}
/* ##-11- Unlink the RAM disk I/O driver #################################### */
FATFS_UnLinkDriver(SD_Path);
Best Answer
Afer some testing and searching, I found the problem. It is a bug in the file generated by cubemx. Here is the details.
If you want to read more details, please read the thread:https://community.st.com/thread/34299-microsd-card-problem
Here is the code: