cancel
Showing results for 
Search instead for 
Did you mean: 

I can only write to SD card once (SPI-fatfs)

NBlac
Associate III

I am using a stm32f407 and i use this code inside a loop:

/* Mount SD Card */
  if(f_mount(&fs, "", 0) != FR_OK)
    _Error_Handler(__FILE__, __LINE__);
  
  /* Open file to write */
  if(f_open(&fil, "first.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE) != FR_OK)
    _Error_Handler(__FILE__, __LINE__);
  
  /* Check free space */
  if(f_getfree("", &fre_clust, &pfs) != FR_OK)
    _Error_Handler(__FILE__, __LINE__);
  
  total = (uint32_t)((pfs->n_fatent - 2) * pfs->csize * 0.5);
  fre = (uint32_t)(fre_clust * pfs->csize * 0.5);   
    
  /* Free space is less than 1kb */
  if(fre < 1)
    _Error_Handler(__FILE__, __LINE__);  
  
  /* Writing text */
	sprintf(counter_buff, "%.10f", count);
	 f_puts("\n", &fil); 
  f_puts("STM32 SD Card I/O Example via SPI\n", &fil);  
  f_puts(counter_buff, &fil);
	 f_puts("\n", &fil); 
 
  /* Close file */
  if(f_close(&fil) != FR_OK)
    _Error_Handler(__FILE__, __LINE__);  
  
  /* Open file to read */
  if(f_open(&fil, "first.txt", FA_READ) != FR_OK)
    _Error_Handler(__FILE__, __LINE__);
  
  while(f_gets(buffer, sizeof(buffer), &fil))
  {
    //printf("%s", buffer);
  }
  
  /* Close file */
  if(f_close(&fil) != FR_OK)
    _Error_Handler(__FILE__, __LINE__);     
  
  /* Unmount SDCARD */
  if(f_mount(NULL, "", 1) != FR_OK)
    _Error_Handler(__FILE__, __LINE__);  
  HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_0);

When i open the file from the sd card on my pc i only see this:

1.35

STM32 SD Card I/O Example via SPI

1.35

The led keeps on toggling so the code is running.

Any ideas?

thanks

1 ACCEPTED SOLUTION

Accepted Solutions

What is it supposed to do in your mind?

If you want it to add lines, perhaps f_seek() to the end of the file.

How does the count advance? Is it really a float?

  if (f_open(&fil, "LOG.TXT", FA_OPEN_ALWAYS | FA_WRITE) == FR_OK)
  {
    UINT BytesWritten;
    const char string[] = "Another line gets added\r\n";
 
    f_lseek(&fil, f_size(&fil));
    f_write(&fil, string, strlen(string), &BytesWritten);
    f_close(&fil);
  } 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2

What is it supposed to do in your mind?

If you want it to add lines, perhaps f_seek() to the end of the file.

How does the count advance? Is it really a float?

  if (f_open(&fil, "LOG.TXT", FA_OPEN_ALWAYS | FA_WRITE) == FR_OK)
  {
    UINT BytesWritten;
    const char string[] = "Another line gets added\r\n";
 
    f_lseek(&fil, f_size(&fil));
    f_write(&fil, string, strlen(string), &BytesWritten);
    f_close(&fil);
  } 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
NBlac
Associate III

I thought that just by adding "f_puts("\n", &fil);" it would change line and next data would be written after that. But now i realized it doesn't. I used your code for writing to the card and it works great.

thanks a lot