cancel
Showing results for 
Search instead for 
Did you mean: 

uSD card write operation stops after some time.

SGupt.7
Associate II

Hello everyone,

I am student and using the FATFS middleware to log sensor data to a uSD card using stm32f746g dev board. The data gets logged only for the first few minutes (8 minutes) and then the f_write() returns a status as FR_LOCKED (16).

What could be a possible reason to this and is there a way to overcome this issue?

I have attached my custom file write function below for reference.

Appreciate your help and thanks in advance.

Regards.

FRESULT uSD_file_write(char *ptr_filename, char *ptr_write_buffer) {
	FRESULT res;
	FIL myFile;
	uint32_t bytes_written;
 
	// Check uSD card is mounted or not
	res = uSD_is_mount();
	if(res != FR_OK) {
		return res;
	}
 
	// 1. try to open file in append and write mode
	//res = f_open(&myFile, ptr_filename, FA_OPEN_EXISTING | FA_OPEN_APPEND | FA_WRITE);
	res = f_open(&myFile, ptr_filename, FA_OPEN_APPEND | FA_WRITE);
	if(res != FR_OK) {
		return res;
	}
 
	// 2. write data to file
	res = f_write(&myFile, ptr_write_buffer, strlen(ptr_write_buffer), (void *)&bytes_written);
	//res = f_sync(&myFile); // Flushes cached info of a writing file (NOT NECESSARY THOUGH!!)
 
	// 3. close file
	f_close(&myFile);
 
	return res;
}
FRESULT uSD_is_mount(void) {
	return f_mount(&mySDFatFs, SDPath, MOUNT_IMMEDIATELY);
}

3 REPLIES 3

All top-level stuff.

Not sure I'd repeatedly mount the media.

Make sure only one thread deals with files and SDIO operation. Multiple concurrent operation isn't viable, use must be arbitrated and serialized.

You might want to instrument the DISKIO layer to understand if there is a failure there that propagates with this error.

Look at the FatFs source to better understand potential reasoning for FR_LOCKED.

Use a current version of the FatFs middleware, not a 3-4 year old one.

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

Hello,

Thank you very much for the prompt reply.

Currently, I am not using FreeRTOS. So there is just one thread that does all the tasks.

I am not completely sure about the FATFS version, but according to the sd_diskio.c file, it is mentioned as v2.1.4.

Regards.

Hello everyone,

An update on the problem that I was facing with uSD card where data could not be written to a file after few minutes.

A possible solution that seems to work for me is that I repeatedly mount and unmount the SD card (through software) after each write operation/command.

For my requirement, I need to write data on the uSD card periodically after every few minutes.

Hope this could be of some help to others facing similar kind of issue.

Regards.