我正在尝试使用micro-sd卡模块和带有spi的stm32。我已经格式化了我的micro-sd卡,但收到的输出(使用f_getfree函数和Uart(声称我的可用空间为0。这不可能是真的,因为我已经尝试了两个不同大小但结果相同的m-sd卡。
/* 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++ != ' ') i++;
return i;
}
void bufclear(void)
{
for(int i=0; i<1024; i++)
{
buffer[i]=' ';
}
}
/* 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%lun",total);
send_uart(buffer);
bufclear();
free_space = (uint32_t)(fre_clust * pfs->csize * 0.5);
sprintf (buffer,"SD CARD Free Space:t%lun",free_space);
send_uart(buffer);
以下是我的大力神:
SD CARD mounted successfully...
SD CARD Total Size:335376466
SD CARD Free Space:0
根据ELM ChaN FatFs文档,要使用f_getfree
函数,必须注意以下三个参数:
- FF_FS_NOFSINFO 2位
如果您需要知道FAT32卷上的正确可用空间,请设置此选项的位0,卷装载后第一次使用f_getfree
功能将强制进行完整的FAT扫描。比特1控制最后分配的簇号用于新分配的使用
值 | 描述 |
---|---|
bit0=0 | 如果可用,请在FSINFO中使用免费群集计数 |
bit0=1 | 不要信任FSINFO中的空闲集群计数 |
bit1=0 | 使用FSINFO中最后分配的集群编号查找可用的可用集群 |
bit1=1 | 不要相信FSINFO中最后分配的簇号 |