Skip to main content
Visitor II
September 19, 2022
Question

Hlo Everyone, Hope youare doing well !! I wanna ask one thing about Fatfs in stm32.

  • September 19, 2022
  • 3 replies
  • 1222 views

I have written a program for write a data in SD card and after that read it. but in my program F_open function returns Fr_DISK_ERROR error and jumps outside the loop and terminates the execution whereas i have declared and defined all the necessary things and files. My card is connected with SPI and i have declared it.

Anyone tell me how can it works....

    This topic has been closed for replies.

    3 replies

    SGoel.2Author
    Visitor II
    September 19, 2022

    I have attached the complete main code of our programme in which i am not getting ny output.

    Please check this code also !!

    void check_SD_interface(void)

    // Reads & writes config.txt file on SD card

    {

     U8 au8Key_pressed, au8StoredVolume;

     UINT aupN_bytes_rwPtr[1];

     short aNBytes_to_write;

     tKeyinfo_t atpKeyPressedInfo[1];

     FRESULT aFResult;

      

     send_str_to_debugUART_n_wait_till_sent("\r\nChecking SDC interface");

      

     // Read / create config.txt file from SDC

     f_mount(&USERFatFs, "" ,0) ; // Returns FR_OK even if there is no SD card!

     // Mounting the file system forcefully does not return FR_OK even when SD card is present!!

     aFResult = f_open(&USERFile, "config.txt", FA_READ);

      if(aFResult != FR_OK)

      {

       // may be it is the first power on for this SD card

       // Try to open file for WRITE

       aFResult = f_open(&USERFile, "config.txt", (FA_WRITE | FA_CREATE_NEW));

       if(aFResult == FR_OK)

       {

        // config file opened for writing

        // Write {"Vol":5}

        // f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); // Write data to the file

        //                01 2345 678

        strcpy((char *)gu8pSdd_rw_buffer,"{\"Vol\":");

        strcat((char *)gu8pSdd_rw_buffer, num_string(INIT_VOLUME_SETTING, UNSIGNED, 1, NO_DECIMAL));

        strcat((char *)gu8pSdd_rw_buffer, "}");

        aNBytes_to_write = strlen((char *)gu8pSdd_rw_buffer);

        // Write data to the file

        aFResult = f_write (&USERFile, (const void *)gu8pSdd_rw_buffer, aNBytes_to_write, aupN_bytes_rwPtr);

        if(aFResult == FR_OK)

        {

         gB_sd_card_present = TRUE;

        }

        f_close(&USERFile);

       }  // config.txt file opened for writing

      }  // f_open() returned error

      else

      {

       // config.txt file opened for reading

       f_read(&USERFile, gu8pSdd_rw_buffer, SDD_SECTOR_SIZE, aupN_bytes_rwPtr);

       if (*aupN_bytes_rwPtr >= 8)

       {

        // At least one config parm is there

        if (strncmp((char *)(gu8pSdd_rw_buffer + 2), "Vol", 3) == 0)

        {

         // It is "Vol" parm, read gu8Set_volume value

         gu8Set_volume = gu8Stored_volume = gu8pSdd_rw_buffer[7] - 0x30;

         // ASSUMED that it is within limits

        } // It is "Vol" parm

        gB_sd_card_present = TRUE;

       }  // 8 or more char in config.txt file

      }  // config.txt file opened for reading

      f_close(&USERFile);  // Checking of SD card & reading/creating config.txt file is over

      au8StoredVolume = INIT_VOLUME_SETTING;

      if (gB_sd_card_present)

      {

       send_str_to_debugUART_n_wait_till_sent("\r\nSuccessfully read/created config.txt file on SDC");

      }

      else

      {

       send_str_to_debugUART_n_wait_till_sent("\r\nUnable to read/created config.txt file on SDC");

       return;

      }

       

     while (1)

     {

      send_str_to_debugUART_n_wait_till_sent("\r\nPress Incr volume / Decrement volume key" \

       "to read storedVolume from config.txt file on SDC and write it back");

    // Remove any pending keys in the key Q

    while (BSP_check_for_key_press(atpKeyPressedInfo)) osDelay(100);

      // Wait for Incr vol / decr vol key   

      while (!BSP_check_for_key_press(atpKeyPressedInfo))

      {

       // No key pressed

       // Check if user pressed Enter on debug terminal

       if ((osEventFlagsGet(debugUartEventHandle) & EVT_ENTER_PRESSED) == EVT_ENTER_PRESSED)

       {

        osEventFlagsClear ( debugUartEventHandle,

               (EVT_ESC_PRESSED | EVT_ENTER_PRESSED | EVT_Y_ENTER_PRESSED | EVT_N_ENTER_PRESSED));

        // Start receiving char from index 0

        return;  // Test over

       }

       osDelay(10);

      }

      // A key pressed

       

      aFResult = f_open(&USERFile, "config.txt", FA_READ);

      if( aFResult == FR_OK)

      {

       f_read(&USERFile, (void *)gu8pSdd_rw_buffer, SDD_SECTOR_SIZE, aupN_bytes_rwPtr);  // Assumed it returns FR_OK

       au8StoredVolume = gu8pSdd_rw_buffer[7] - 0x30;

       f_close(&USERFile);

      }

        

      au8Key_pressed = atpKeyPressedInfo->fu8Kb_val;

      switch (au8Key_pressed)

      {

      case KBVAL_KEY_INCR_VOLUME:

       if (gu8Set_volume < MAX_VOLUME) gu8Set_volume++;

       copy_str_to_debugUart_tx_buffer("\r\nVolume: ");

       cat_str_to_debugUart_tx_buffer(BSP_u16_to_str((U16)gu8Set_volume, REMOVE_LEADING_ZEROES));

       break;

      case KBVAL_KEY_DECR_VOLUME:

       if (gu8Set_volume > MIN_VOLUME) gu8Set_volume--;

       copy_str_to_debugUart_tx_buffer("\r\nVolume: ");

       cat_str_to_debugUart_tx_buffer(BSP_u16_to_str((U16)gu8Set_volume, REMOVE_LEADING_ZEROES));

       break;

      }  // switch (au8Key_pressed)

      send_debug_tx_line_n_wait_till_sent();

       

      // Write the modified gu8Set_volume to SDC

      aFResult = f_open(&USERFile, "config.txt", FA_WRITE);

      if(aFResult != FR_OK)

      {

       // File write error!

       f_close(&USERFile);

       send_str_to_debugUART_n_wait_till_sent("\r\nFile write error!");

       return;

      }

      else

      {

       // config file opened for writing

       // Write {"Vol":au8StoredVolume}

       // f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); // Write data to the file

       //                01 2345 678

       strcpy((char *)gu8pSdd_rw_buffer,"{\"Vol\":");

       strcat((char *)gu8pSdd_rw_buffer, num_string(au8StoredVolume, UNSIGNED, 1, NO_DECIMAL));

       strcat((char *)gu8pSdd_rw_buffer, "}");

       aNBytes_to_write = strlen((char *)gu8pSdd_rw_buffer);

       // Write data to the file

       aFResult = f_write (&USERFile, (const void *)gu8pSdd_rw_buffer, aNBytes_to_write, aupN_bytes_rwPtr);

       f_close(&USERFile);

       if (aFResult == FR_OK)

       {

        send_str_to_debugUART_n_wait_till_sent("\r\nFile written successfully");

       }

       else

       {

        send_str_to_debugUART_n_wait_till_sent("\r\nFile write error!");

        return;

       }

      }  // config.txt file opened for writing

     }  // while (1)

    }

    // ----- end of check_SD_interface() -----

    I have found error in the [[[aFResult = f_open(&USERFile, "config.txt", (FA_WRITE | FA_CREATE_NEW));

       if(aFResult == FR_OK)]]] function and after getting the error execution goes to the end of the else statement.

    Graduate II
    September 19, 2022

    Instrument DISKIO layer, understand why it fails. The file system is dependent on it functioning correctly. ​

    SGoel.2Author
    Visitor II
    September 20, 2022

    @Community member​ But How Can I Check it ??

    Visitor II
    March 4, 2023

    @SGoel.2​ have you found the answer? can you attach your stm32 project here. I'm having same problem