Stacey
Stacey

Reputation: 11

Use fatfs in stm32, the "f_open" function can't create file normally。

I find a strange problem of fatfs api, in main.c, the api f_open create file normally, it create file "write.txt" and "hehehe.txt" but not create file in other c file

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 */
  RTC_TimeTypeDef sTime = {0};
  RTC_DateTypeDef sDate = {0};
  HAL_StatusTypeDef hal_stat = HAL_ERROR;
  FRESULT f_ret = FR_INVALID_PARAMETER;
  char szTmp[64] = {0};
  UINT ulWritten = 0;
  /* 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_FATFS_Init();
  MX_I2C1_Init();
  MX_RTC_Init();
  /* USER CODE BEGIN 2 */
  HAL_Delay(500);

  hal_stat = HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BCD);
  sprintf(szTmp, "%04d%04d%04d\n", sTime.Hours, sTime.Minutes, sTime.Seconds);
  
  f_ret = f_mount(&fs, "", 0);
  if(f_ret != FR_OK){
    return f_ret;
  }
  FRESULT res;

  /* Set the file as read-only */
  f_ret = f_open(&fil, "write.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
  if(f_ret != FR_OK){
    return f_ret;
  }
  f_ret = f_close(&fil);
  f_ret = f_open(&fil, "hehehe.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
  if(f_ret != FR_OK){
    return f_ret;
  }
  f_ret = f_close(&fil);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

  unsigned char *str = (unsigned char *)"test";
    ZssLog(str);
    zssLogINFO("<%s  %d> %s(arg: %s)", __FILE__, __LINE__, __FUNCTION__, "");

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

in other c file. the api f_open can't create file,it can create dir g_logdir normally, and no error number dir created, so i think f_open return FR_OK, but don't create "hahaha.txt" in sd_card root directory.

f_mkdir("g_logdir");
char buf[32] = {0};
f_ret = f_open(&fil, "hahaha.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
if(f_ret != FR_OK){
   f_mkdir(itoa(f_ret, buf, 10));
   return 0;
}

Everyone, Did you encounter this problems?

Upvotes: 0

Views: 104

Answers (0)

Related Questions