esp-idf 4.0 I can't use fatfs functions directly

andrej32
Posts: 28
Joined: Fri Apr 19, 2019 8:22 am

esp-idf 4.0 I can't use fatfs functions directly

Postby andrej32 » Tue Nov 19, 2019 6:19 am

I can't use fatfs functions directly, only C-style functions work. Meanwhile the documentation says I can use them ( https://docs.espressif.com/projects/esp ... e/storage/ )

For example, I tested code:

Config:

Code: Select all

sdmmc_card_t* card;
esp_vfs_fat_sdmmc_mount_config_t mount_config;

sdmmc_host_t host = SDMMC_HOST_DEFAULT();
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();

gpio_set_pull_mode(CMD_SD, GPIO_PULLUP_ONLY);   // CMD
gpio_set_pull_mode(DATA0_SD, GPIO_PULLUP_ONLY);    // D0
gpio_set_pull_mode(DATA1_SD, GPIO_PULLUP_ONLY);    // D1
gpio_set_pull_mode(DATA2_SD, GPIO_PULLUP_ONLY);   // D2
gpio_set_pull_mode(DATA3_SD, GPIO_PULLUP_ONLY);   // D3

mount_config.format_if_mount_failed = false;
mount_config.max_files = 5;
mount_config.allocation_unit_size = 16*1024;

esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
Test with C-style and FatFs functions:

Code: Select all

uint8_t TestSD (sdmmc_card_t* p_card)
{
	FIL f;
	FILE* fi;
	FRESULT fres;

	printf("Opening file\n");

	fi = fopen("/sdcard/hello.txt", "w");
	if (fi == NULL)
	{
		printf("Failed to open file for writing\n");
		return 1;
	}

	fclose(fi);

	printf("File written\n");

	vTaskDelay(100);

	printf("Creat file\n");

	vTaskDelay(100);

	fres = f_open(&f,"/hel.txt", FA_CREATE_ALWAYS | FA_WRITE);
	if (fres != FR_OK)
	{
		printf("Failed to create file for writing\n");
		return 1;
	}

	printf("File created\n");

	f_close(&f);
	
	return 0;
}
Terminal:
  • Opening file
    CORRUPT HEAP: multi_heap.c:432 detected at 0x3ffbe1cc
    abort() was called at PC 0x40087c93 on core 0

    ELF file SHA256: ef2ab7615c054010f1454fee4f030d283d8866b0a26d20595f00ccf6f4882bf8

    Backtrace: 0x40084f29:0x3ffbe000 0x4008529d:0x3ffbe020 0x40087c93:0x3ffbe040 0x40082181:0x3ffbe060 0x400821b1:0x3ffbe080 0x4008ac7d:0x3ffbe0a0 0x4008872f:0x3ffbe0c0 0x40088984:0x3ffbe0e0 0x4008286a:0x3ffbe100 0x40082894:0x3ffbe120 0x400829e1:0x3ffbe150 0x4000be35:0x3ffbe170 |<-CORRUPTED

    Rebooting...
If I use only C-style functions in code, all work well.
else if I use only FatFs functions in code, no work, error message and reboot.

How do I use FatFs functions directly? I need this way of working with SD card.
Last edited by andrej32 on Tue Nov 19, 2019 7:17 am, edited 1 time in total.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: esp-idf 4.0 I can't use fatfs functions directly

Postby ESP_Angus » Tue Nov 19, 2019 6:53 am

Do you get compiler warnings when compiling this code? There should be compiler warnings, because the wrong pointer type is being passed to the FATFS functions - this is what causes the heap corruption at runtime.

The fix is to change this line:

Code: Select all

	FIL* f;
To:

Code: Select all

	FIL f;

andrej32
Posts: 28
Joined: Fri Apr 19, 2019 8:22 am

Re: esp-idf 4.0 I can't use fatfs functions directly

Postby andrej32 » Tue Nov 19, 2019 7:16 am

I'm sorry, I do not understand how it happened, but really in the code of course FIL f - not pointer. I corrected the text of the message

andrej32
Posts: 28
Joined: Fri Apr 19, 2019 8:22 am

Re: esp-idf 4.0 I can't use fatfs functions directly

Postby andrej32 » Tue Nov 19, 2019 7:31 am

Errors in the code of work with fatfs cannot be, since the same code successfully works on other microcontroller. The problem is in support direct use of fatfs functions in idf-esp 4.0 betta 1. Perhaps need to somewhere point to that will be used functions fatfs without VFS, perhaps this need to to do under configuring SD maps and mounting file system? As you can see I use the all-in-one function
esp_vfs_fat_sdmmc_mount of the sdcard example esp-idf.

andrej32
Posts: 28
Joined: Fri Apr 19, 2019 8:22 am

Re: esp-idf 4.0 I can't use fatfs functions directly

Postby andrej32 » Tue Nov 19, 2019 1:00 pm

The problem is partially solved. The problem was the insufficient size of the task stack. I don't understand why using FatFs functions requires a lot more memory than using VFS functions.

If I use FatFs:

Code: Select all

FIL fa;
FRESULT fres;

fres = f_open(&fa,"hello.txt", FA_WRITE);
	if (fres != FR_OK)
	{
		printf("Failed to create file for writing\n");

	}
	else
	{
		printf("File created\n");
	}

	f_close(&fa);
the task requires a stack 4128 bytes larger than when using:

Code: Select all

FILE* fi;

	fi = fopen("/sdcard/hello.txt", "w");
	if (fi == NULL)
	{
		printf("Failed to open file for writing\n");
		return 1;
	}
	else
	{
		printf("File created\n");
	}

	fclose(fi);
	
Can anyone suggest why this is happening?

Who is online

Users browsing this forum: chegewara, Google [Bot], Majestic-12 [Bot] and 151 guests