cancel
Showing results for 
Search instead for 
Did you mean: 

FATFS name generator for txt file

linas2
Associate II
Posted on November 13, 2012 at 21:20

Hello, i am working on project where i need to save array stored in stm32 to sdcard

problem is i was able to make it work using project, but somehow i was unable to crate new file. Idea that i press button on PA0, and in interrupt i write all data from array to sdcard.

int spektras = 1 ;
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{ 
FATFS fs[1]; // Work area (file system object) for logical drives 
FIL ftxt; // file objects 
char buffer[50]; // file copy buffer 
char name[14];
FRESULT res; ///FatFs function common result code 
UINT bw; // File write count 
f_mount(0, &fs[0]);//
///Create destination file on the drive 0 
sprintf(name,''0:%dspektras.txt'',spektras);
res = f_open(&ftxt, name, FA_CREATE_ALWAYS | FA_WRITE);
long cnt=0; 
while (cnt<1000)
{ 
sprintf(buffer,''%f\n'',sinf(cnt*0.00628318531));
res = f_write(&ftxt, buffer, strlen(buffer), &bw); 
cnt++; 
}
f_close(&ftxt);
f_mount(0, NULL);//
spektras++;
EXTI_ClearITPendingBit(EXTI_Line0);
LCD_Clear(YELLOW);
}
}

i get empty card, but if i don't write %d i get data, but it override data to old txt file. Any ideas how to crate new txt with new name?
5 REPLIES 5
Posted on November 13, 2012 at 21:35

Might want to make this a bit bigger?

  char name[14];

I don't think there is an easy way of creating a unique file name, you could enumerate through the directory and determine what not to use. For example have an 8-digit numeric file name, and go find the current largest file number in the directory, then increment the count for you next one.

Be aware that the root directory of a FAT12/16 partition is defined and finite. Sub-directories will expand, so will be more elastic in that regard.

I would definitely avoid doing this in an interrupt.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
linas2
Associate II
Posted on November 13, 2012 at 21:41

i made array bigger, and name smaller, but same problem.

why write in interrupt is bad idea?

p.s thanks for sharing your fatfs project, it helped alot

vex
Associate
Posted on November 13, 2012 at 22:13

you must consider first of all a larger size for file-name buffer. I would also remove the ':' character from name of a file. And also try to force a disk-flush to be sure that changes take place immediately.

Posted on November 13, 2012 at 22:48

why write in interrupt is bad idea?

Cause it takes a lot of time, and depends on other interrupts firing in the interim. Ideally interrupts should be brief in duration, and signal other tasks to do the real work.

For this to work you'd want the EXTI interrupt to have the lowest priority in the system, and be preemptable.

Same problem what? Is it giving an error, not generating a new file? Do you have long file name support enabled for a non-8.3 filename?

Does ''%05dFOO.TXT'' work?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
linas2
Associate II
Posted on November 13, 2012 at 23:29

bummer, i must clear buffer before using sprintf.

memset(&buffer[0], 0, sizeof(buffer));
sprintf(buffer,''%d.txt'',spektras);
res = f_open(&ftxt, buffer, FA_CREATE_ALWAYS | FA_WRITE);
spektras++;

works like a charm. I bet is compiler problem ( tried and long names, and short names, get same problem, now it works with any name i want, as long is below 8 characters )