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