- We have to mount and un-mount sdCard on regular basis
- First time we do this -> no leak
- 2nd time mounting doesn't leak memory either
- 2nd un-mount of the sdCard leaks memory
Chip : ESP32 (rev 1)
IDF : ESP-IDF v5.1.3-416-gd23b7a0361
Reproduce Code
- #include "esp_vfs_fat.h"
- #include "freertos/portmacro.h"
- #include "sdmmc_cmd.h"
- #include "driver/sdmmc_host.h"
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/unistd.h>
- #include <dirent.h>
- #define MAX_CHAR_SIZE 128
- static const char *TAG = "SPI BUS Aquisition : ";
- #define MOUNT_POINT "/sdcard"
- #define Flash_Access_Frequency CONFIG_HOST_MAX_FREQUENCY
- #define SPI_THRESHOLD CONFIG_SPI_BUS_TIME_THRESHOLD
- #define SPI_TASK_FREQUENCY CONFIG_SPI_BUS_TASK_FREQUENCY
- sdmmc_card_t *card;
- sdmmc_host_t host = SDSPI_HOST_DEFAULT();
- const char mount_point[] = MOUNT_POINT;
- volatile int64_t start_time = 0 ;
- volatile int64_t elapsed_time = 0 ;
- bool Can_TakeOver_SPI_BUS = false ;
- int List_Files_ON_Flash()
- {
- DIR *d ;
- struct dirent *dir;
- d = opendir(MOUNT_POINT);
- if (d)
- {
- printf("###############################\n");
- printf("### Listing Files on Flash ..\n");
- printf("###############################\n");
- int rank = 1 ;
- while ((dir = readdir(d)) != NULL)
- {
- printf("%d. %s\n",rank, dir->d_name);
- rank += 1;
- }
- printf("###############################\n\n");
- closedir(d);
- ESP_LOGI(TAG, "File written");
- }
- return 0;
- }
- int Init_SPI_BUS()
- {
- esp_err_t ret;
- esp_vfs_fat_sdmmc_mount_config_t mount_config = {
- .format_if_mount_failed = false,
- .max_files = 5,
- .allocation_unit_size = 16 * 1024};
- ESP_LOGI(TAG, "Initializing SD card");
- ESP_LOGI(TAG, "Using SPI peripheral");
- sdmmc_host_t host = SDMMC_HOST_DEFAULT();
- host.max_freq_khz = Flash_Access_Frequency ; // 1000;
- // This initializes the slot without card detect (CD) and write protect (WP) signals.
- // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
- sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
- // Set bus width to use:
- slot_config.width = 1;
- slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
- ESP_LOGI(TAG, "Mounting filesystem");
- ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
- if (ret != ESP_OK)
- {
- if (ret == ESP_FAIL)
- {
- ESP_LOGE(TAG, "Failed to mount filesystem. "
- "If you want the card to be formatted, set the "
- "CONFIG_FORMAT_IF_MOUNT_FAILED menuconfig option.");
- }
- else
- {
- ESP_LOGE(TAG,
- "Failed to initialize the card (%s). "
- "Make sure SD card lines have pull-up resistors in place.",
- esp_err_to_name(ret));
- }
- return -1 ;
- }
- ESP_LOGI(TAG, "Filesystem mounted");
- printf("###############################\n");
- // Card has been initialized, print its properties
- sdmmc_card_print_info(stdout, card);
- printf("###############################\n");
- return 0 ;
- }
- int Free_SPI_BUS()
- {
- if (esp_vfs_fat_sdcard_unmount(mount_point, card) == ESP_OK)
- ESP_LOGI(TAG, "\033[1;33mSuccessfully Unmounted sdCard !\033[0m");
- return 0;
- }
- void Task_TakeOver_SPI_BUS(void* arrow)
- {
- for(;;)
- {
- ESP_LOGI(TAG, "###### Exceeded Threshold Time #######");
- Init_SPI_BUS();
- // List_Files_ON_Flash();
- Free_SPI_BUS();
- heap_caps_check_integrity_all(true);
- ESP_LOGI(TAG,"\033[1;33m ************************\033[0m");
- ESP_LOGI(TAG,"\033[1;33m Free memory: %0.3f Kb\033[0m ", esp_get_free_heap_size()/1000.0);
- ESP_LOGI(TAG,"\033[1;33m ************************\033[0m");
- vTaskDelay( 2000 / portTICK_PERIOD_MS);
- }
- }
- void app_main(void)
- {
- // esp_log_level_set("gpio", ESP_LOG_ERROR);
- // esp_log_level_set("sdspi_transaction", ESP_LOG_ERROR);
- xTaskCreate(Task_TakeOver_SPI_BUS, "xTask_SPI", 2048, NULL, 5, NULL);
- }
Memory log from MQTT (from the original project)
Chip Started
Wifi Started
Getting Auth Info , Memory : 161Kb
Getting Auth Info ..... Success !, Memory : 171Kb
Free Memory at ALL_FILES_UPLOADED , Memory : 175.068 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 175.056 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 175.040 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 175.024 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 175.008 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.992 bytes
!Ahora mismo : Sun Apr 21 01:39:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 174.976 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.960 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.944 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.924 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.904 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.880 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.864 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.840 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.824 bytes
!Ahora mismo : Sun Apr 21 01:40:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 174.808 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.792 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.988 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.972 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.956 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.940 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.924 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.900 bytes
!Ahora mismo : Sun Apr 21 01:41:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 174.884 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.868 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.852 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.836 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.812 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.796 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.780 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.764 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.748 bytes
!Ahora mismo : Sun Apr 21 01:42:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 174.732 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.716 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.696 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.680 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.664 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.648 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.632 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.616 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.476 bytes
!Ahora mismo : Sun Apr 21 01:43:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 174.584 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.568 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.552 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.536 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.520 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.504 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.488 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.472 bytes
!Ahora mismo : Sun Apr 21 01:44:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 174.456 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.440 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.424 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.408 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.392 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.376 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.360 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.344 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.328 bytes
!Ahora mismo : Sun Apr 21 01:45:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 174.312 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.296 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.280 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.264 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.248 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.232 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.216 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.196 bytes
!Ahora mismo : Sun Apr 21 01:46:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 174.180 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.164 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.148 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.132 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.104 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.088 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.072 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.056 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.040 bytes
!Ahora mismo : Sun Apr 21 01:47:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 174.024 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 174.008 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.992 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.964 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.948 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.932 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.916 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.900 bytes
!Ahora mismo : Sun Apr 21 01:48:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 173.884 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.868 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.852 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.836 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.820 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.804 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.788 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.772 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.632 bytes
!Ahora mismo : Sun Apr 21 01:49:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 173.740 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.724 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.708 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.692 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.676 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.660 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.644 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.628 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.612 bytes
!Ahora mismo : Sun Apr 21 01:50:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 173.596 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.580 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.564 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.548 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.532 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.516 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.500 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.484 bytes
!Ahora mismo : Sun Apr 21 01:51:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 173.468 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.452 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.436 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.420 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.404 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.388 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.372 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.356 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.340 bytes
!Ahora mismo : Sun Apr 21 01:52:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 173.324 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.308 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.292 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.276 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.260 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.244 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.228 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.212 bytes
!Ahora mismo : Sun Apr 21 01:53:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 173.196 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.180 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.164 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.148 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.132 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.116 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.100 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.084 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.068 bytes
!Ahora mismo : Sun Apr 21 01:54:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 173.052 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.036 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.020 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 173.004 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.988 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.972 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.956 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.924 bytes
!Ahora mismo : Sun Apr 21 01:55:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 172.900 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.884 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.744 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.852 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.836 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.820 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.804 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.788 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.772 bytes
!Ahora mismo : Sun Apr 21 01:56:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 172.756 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.740 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.716 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.700 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.684 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.668 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.652 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.636 bytes
!Ahora mismo : Sun Apr 21 01:57:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 172.620 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.604 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.588 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.572 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.556 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.540 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.524 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.508 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.492 bytes
!Ahora mismo : Sun Apr 21 01:58:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 172.476 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.460 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.444 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.428 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.412 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.396 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.256 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.364 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.348 bytes
!Ahora mismo : Sun Apr 21 01:59:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 172.332 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.192 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.300 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.280 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.264 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.248 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.232 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.216 bytes
!Ahora mismo : Sun Apr 21 02:00:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 172.200 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.184 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.168 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.152 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.136 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 171.996 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.104 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.076 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.060 bytes
!Ahora mismo : Sun Apr 21 02:01:01 UTC 2024, ¡Hola Mundo!
Free Memory at ALL_FILES_UPLOADED , Memory : 172.044 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.028 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 172.012 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 171.996 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 171.980 bytes
Free Memory at ALL_FILES_UPLOADED , Memory : 171.964 bytes
This keeps happening till the memory is eroded
My Tests show that this only happens after the 2nd Un-mount of sdCard
I have traced the free memory after 1st Time Mount and then Release
and after the 2nd Time mount , and it typically deducted about 27k
but then after the 2nd Un-mount the above behavior happens
I included a Test project
I hope I'm doing something wrong here , cause we do need this to work