0

I'm trying to work with the micro sd card module and stm32 with spi. I have formatted my micro sd card but the received output (using f_getfree function and Uart), claims that my free space is 0. which cannot be true as I have tried 2 different m-sd cards with different sizes but same results.

    /* USER CODE BEGIN Includes */
    #include "fatfs_sd.h"
    #include "string.h"
    #include "stdio.h"
    /* USER CODE END Includes */
    
    /* Private typedef -----------------------------------------------------------*/
    /* USER CODE BEGIN PTD */
    
    /* USER CODE END PTD */
    
    /* Private define ------------------------------------------------------------*/
    /* USER CODE BEGIN PD */
    /* USER CODE END PD */
    
    /* Private macro -------------------------------------------------------------*/
    /* USER CODE BEGIN PM */
    
    /* USER CODE END PM */
    
    /* Private variables ---------------------------------------------------------*/
    SPI_HandleTypeDef hspi1;
    
    UART_HandleTypeDef huart1;
    
    /* USER CODE BEGIN PV */
    
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    static void MX_GPIO_Init(void);
    static void MX_SPI1_Init(void);
    static void MX_USART1_UART_Init(void);
    /* USER CODE BEGIN PFP */
    
    /* USER CODE END PFP */
    
    /* Private user code ---------------------------------------------------------*/
    /* USER CODE BEGIN 0 */
    FATFS fs;//file system
    FIL fil;//file
    FRESULT fresult;//to store the resualt
    char buffer[1024];//to store data
    
    UINT br,bw;//file read/write count
    
    /*capacity related variable*/
    FATFS *pfs;
    DWORD fre_clust;
    uint32_t total,free_space;
    /*to send the data to uart*/
    void send_uart (char *string)
    {
    uint8_t len = strlen (string);
        HAL_UART_Transmit(&huart1,(uint8_t *) string,len,2000);//transmit in blocking mode
        
    }
    
    int bufsize (char *buf)
    {
    int i=0;
        while(*buf++ != '\0') i++;
    return i;
    
    }
    
    void bufclear(void)
    {
    for(int i=0; i<1024; i++)
        {
        buffer[i]='\0';
        }
    
    }
    
    /* USER CODE END 0 */
    
    /**
      * @brief  The application entry point.
      * @retval int
      */
    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();
    
      /* USER CODE BEGIN Init */
    
      /* USER CODE END Init */
    
      /* Configure the system clock */
      SystemClock_Config();
    
      /* USER CODE BEGIN SysInit */
    
      /* USER CODE END SysInit */
    
      /* Initialize all configured peripherals */
      MX_GPIO_Init();
      MX_SPI1_Init();
      MX_USART1_UART_Init();
      MX_FATFS_Init();
      /* USER CODE BEGIN 2 */
          /*  Mount SD Card */
            fresult = f_mount(&fs,"",0);//(para1:pointer file system,para2:pointer of null,para3:option{when 0 not mounted when 1 force muonted})
    if(fresult != FR_OK) send_uart("error in mouting SD CARD...\n");
    else send_uart("SD CARD mounted successfully...\n");
    //*******************Card capaccity detail****************************//
    
    // Check free space//
    f_getfree("2:", &fre_clust, &pfs);
    
        total = (uint32_t)((pfs->n_fatent - 2) * pfs->csize * 0.5);
      sprintf (buffer,"SD CARD Total Size:\t%lu\n",total);
      send_uart(buffer);
      bufclear();
        free_space = (uint32_t)(fre_clust * pfs->csize * 0.5);
      
         sprintf (buffer,"SD CARD Free Space:\t%lu\n",free_space);
         send_uart(buffer);

Here is what I get in my Hercules :

SD CARD mounted successfully...
SD CARD Total Size:335376466
SD CARD Free Space:0
Markus
  • 5,976
  • 5
  • 6
  • 21

1 Answers1

0

According to ELM-ChaN FatFs documentation, To use f_getfree function you have to pay attention to these three parameters:

  1. FF_FS_NOFSINFO 2 bits.
    If you need to know correct free space on the FAT32 volume, set bit 0 of this option, and f_getfree function at first time after volume mount will force a full FAT scan. Bit 1 controls the use of last allocated cluster number for new allocation.
Value Description
bit0 = 0 Use free cluster count in the FSINFO if available.
bit0 = 1 Do not trust free cluster count in the FSINFO..
bit1 = 0 Use last allocated cluster number in the FSINFO to find a free cluster if available.
bit1 = 1 Do not trust last allocated cluster number in the FSINFO.
  1. FF_FS_READONLY 1 bit.
    Read/Write (0) or Read-only (1). Read-only configuration removes writing API functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename, f_truncate, f_getfree and optional writing functions as well.
  2. FF_FS_MINIMIZE 2 bits.
    This option defines minimization level to remove some basic API functions as follows.
Value Description
0 All basic API functions are available.
1 f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_utime, f_truncate and f_rename function are removed.
2 f_opendir, f_readdir and f_closedir function are removed in addition to 1.
3 f_lseek function is removed in addition to 2.

One more thing from your code, You pass the pointer of logical drive path in f_mount(&fs,"",0) to NULL to use the default drive, But in f_getfree("2:", &fre_clust, &pfs), you pass the logical drive number to "2:".

AliMo
  • 86
  • 6
  • Thanks, AliMo. I checked the configuration files and these parameters seem to be all fine. Still, I cannot find the problem. I also changed the logical drive number in the code and yet I get the same results. – Tara sarpoolaki Aug 10 '22 at 05:32