I don’t see how I can attach anything here.
This is the code. It’s based on the SD-Card example with minor changes.
The code contains the lines with the file name
hello123.txt and hello1234.txt
8.3 works, 9.3 does not work
I tried this on different development boards and with two SD-Card modules.
#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include "esp_err.h"
#include "esp_log.h" //Log messages
#include "esp_vfs_fat.h"
#include "driver/sdmmc_host.h"
#include "driver/sdspi_host.h"
#include "sdmmc_cmd.h"
#include <inttypes.h> //Needed for int64_t esp_timer_get_time()
static const char *TAG = "example";
// This example can use SDMMC and SPI peripherals to communicate with SD card.
// By default, SDMMC peripheral is used.
// To enable SPI mode, uncomment the following line:
//#define USE_SPI_MODE
// When testing SD and SPI modes, keep in mind that once the card has been
// initialized in SPI mode, it can not be reinitialized in SD mode without
// toggling power to the card.
void app_main(void)
{
ESP_LOGI(TAG, "Initializing SD card");
ESP_LOGI(TAG, "Using SDMMC peripheral");
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
//Docu https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/sdmmc_host.html
//Docu slot: slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)
//Docu Default slot = SDMMC_HOST_SLOT_1 works, SDMMC_HOST_SLOT_0 can't be used because used by flash!
// To use 1-line SD mode
host.flags = SDMMC_HOST_FLAG_1BIT;
// 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();
// GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups.
// Internal pull-ups are not sufficient. However, enabling internal pull-ups
// does make a difference some boards, so we do that here.
gpio_set_pull_mode(15, GPIO_PULLUP_ONLY); // CMD, needed in 4- and 1- line modes
gpio_set_pull_mode(2, GPIO_PULLUP_ONLY); // D0, needed in 4- and 1-line modes
gpio_set_pull_mode(4, GPIO_PULLUP_ONLY); // D1, needed in 4-line mode only
gpio_set_pull_mode(12, GPIO_PULLUP_ONLY); // D2, needed in 4-line mode only
gpio_set_pull_mode(13, GPIO_PULLUP_ONLY); // D3, needed in 4- and 1-line modes
// Options for mounting the filesystem.
// If format_if_mount_failed is set to true, SD card will be partitioned and
// formatted in case when mounting fails.
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = false,
.max_files = 5
};
// Use settings defined above to initialize SD card and mount FAT filesystem.
// Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
sdmmc_card_t* card;
esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
ESP_LOGE(TAG, "Failed to mount filesystem. ");
} else {
ESP_LOGE(TAG, "Failed to initialize the card (%d). ", ret);
}
return;
}
// Card has been initialized, print its properties
//Sample Output
//Name: SE16G
//Type: SDHC/SDXC
//Speed: 20 MHz
//Size: 15193MB
//sdmmc_card_print_info(stdout, card);
//char *fileName1 = "/sdcard/log20190913170134.txt"; //file name must be like "/sdcard/xxx"
//char *fileName1 = "/sdcard/Log201909261.txt"; //file name must be like "/sdcard/xxx"
//char *fileName1 = "/sdcard/hello.txt"; //file name must be like "/sdcard/xxx" //Works
char *fileName1 = "/sdcard/hello1234.txt"; //file name must be like "/sdcard/xxx" //Failed to open file for append␛[0m
//char *fileName1 = "/sdcard/hello123.txt"; //file name must be like "/sdcard/xxx" //
ESP_LOGI(TAG, "Opening file");
FILE* f = fopen(fileName1, "a");
//FILE* f = fopen(fileName1, "w");
//FILE* f = fopen("/sdcard/Log201909262.txt", "w"); //No!
//FILE* f = fopen("/sdcard/hello.txt", "w");
if (f == NULL) {
ESP_LOGE(TAG, "Failed to open file for append");
//ESP_LOGE(TAG, "Failed to open file for write");
return;
}
int64_t t = 0;
for (unsigned long i = 1; i <= 1090; i++)
{
t = esp_timer_get_time();
fprintf(f, "%010" PRId64, t);
fprintf(f, " ");
fprintf(f, "%10lu\n", i);
if (0 == i % 100){
//fflush(f);
ESP_LOGI(TAG, "Close and Open file");
fclose(f);
f = fopen(fileName1, "a");
}
}
//fclose(f);
ESP_LOGI(TAG, "File written");
// All done, unmount partition and disable SDMMC or SPI peripheral
esp_vfs_fat_sdmmc_unmount();
ESP_LOGI(TAG, "Card unmounted");
}
And this is the platformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = espidf
monitor_speed = 115200
build_flags = -DCONFIG_FATFS_MAX_LFN=255