mirror of
https://github.com/espressif/esp-idf.git
synced 2026-04-27 19:13:21 +00:00
feat(cmakev2/test): add fatfs_example and hello_world_example
Add two components: fatfs_example and hello_world_example. These are simply copies of the main components from the existing examples. Enhance the test_executable test to create targets for two executables, which can be built after CMake configuration. ``` cmake --build build/ --target hello_world_example cmake --build build/ --target fatfs_example ``` Additionally, move the test_component_priority to the end of the test chain, as it modifies the esp_system component, which interferes with the linkage of the executables. Signed-off-by: Frantisek Hrbata <frantisek.hrbata@espressif.com>
This commit is contained in:
@@ -15,7 +15,6 @@ add_custom_target(flash)
|
||||
|
||||
idf_project_init()
|
||||
|
||||
|
||||
# Test component priority
|
||||
function(test_component_priority)
|
||||
# Set the idf component to be replaced with a testing component of higher
|
||||
@@ -205,14 +204,27 @@ function(test_include_component)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# Add two executables fatfs_example and hello_world_example
|
||||
# After configuration these can be build with
|
||||
# cmake --build build/ --target hello_world_example
|
||||
# cmake --build build/ --target fatfs_example
|
||||
function(test_executable)
|
||||
add_executable(test_executable components/component1/component1.c)
|
||||
idf_build_library(INTERFACE test_executable_lib)
|
||||
target_link_libraries(test_executable PRIVATE test_executable_lib)
|
||||
idf_build_get_property(build_dir BUILD_DIR)
|
||||
file(TOUCH "${build_dir}/test_executable.c")
|
||||
|
||||
add_executable(fatfs_example "${build_dir}/test_executable.c")
|
||||
idf_build_library(INTERFACE fatfs_lib
|
||||
COMPONENTS fatfs_example)
|
||||
target_link_libraries(fatfs_example PRIVATE fatfs_lib)
|
||||
|
||||
add_executable(hello_world_example "${build_dir}/test_executable.c")
|
||||
idf_build_library(INTERFACE hello_world_lib
|
||||
COMPONENTS hello_world_example)
|
||||
target_link_libraries(hello_world_example PRIVATE hello_world_lib)
|
||||
|
||||
endfunction()
|
||||
|
||||
# Run tests
|
||||
test_component_priority()
|
||||
test_idf_version()
|
||||
test_python()
|
||||
test_toolchain()
|
||||
@@ -221,6 +233,10 @@ test_kconfig()
|
||||
test_project_properties()
|
||||
test_include_component()
|
||||
test_executable()
|
||||
|
||||
# Call this test last because it replaces the ESP system component.
|
||||
test_component_priority()
|
||||
|
||||
__dump_all_properties()
|
||||
__generate_project_info()
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "fatfs_getting_started_main.c"
|
||||
PRIV_REQUIRES vfs fatfs lwip
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "esp_vfs.h"
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
static const char *TAG = "example";
|
||||
|
||||
// Mount path for the partition
|
||||
const char *base_path = "/spiflash";
|
||||
|
||||
// Handle of the wear levelling library instance
|
||||
static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Mounting FAT filesystem");
|
||||
// To mount device we need name of device partition, define base_path
|
||||
// and allow format partition in case if it is new one and was not formatted before
|
||||
const esp_vfs_fat_mount_config_t mount_config = {
|
||||
.max_files = 4, // Number of files that can be open at a time
|
||||
.format_if_mount_failed = true, // If true, try to format the partition if mount fails
|
||||
.allocation_unit_size = CONFIG_WL_SECTOR_SIZE, // Size of allocation unit, cluster size.
|
||||
.use_one_fat = false, // Use only one FAT table (reduce memory usage), but decrease reliability of file system in case of power failure.
|
||||
};
|
||||
|
||||
// Mount FATFS filesystem located on "storage" partition in read-write mode
|
||||
esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(base_path, "storage", &mount_config, &s_wl_handle);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Filesystem mounted");
|
||||
|
||||
ESP_LOGI(TAG, "Opening file");
|
||||
|
||||
const char *filename = "/spiflash/example.txt";
|
||||
|
||||
FILE *f = fopen(filename, "wb");
|
||||
if (f == NULL) {
|
||||
perror("fopen"); // Print reason why fopen failed
|
||||
ESP_LOGE(TAG, "Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(f, "Hello World!\n");
|
||||
fclose(f);
|
||||
|
||||
ESP_LOGI(TAG, "File written");
|
||||
|
||||
// Open file for reading
|
||||
ESP_LOGI(TAG, "Reading file");
|
||||
|
||||
f = fopen(filename, "r");
|
||||
if (f == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to open file for reading");
|
||||
return;
|
||||
}
|
||||
|
||||
char line[128];
|
||||
|
||||
fgets(line, sizeof(line), f);
|
||||
fclose(f);
|
||||
|
||||
// strip newline
|
||||
char *pos = strchr(line, '\n');
|
||||
if (pos) {
|
||||
*pos = '\0';
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Read from file: '%s'", line);
|
||||
|
||||
// Unmount FATFS
|
||||
ESP_LOGI(TAG, "Unmounting FAT filesystem");
|
||||
|
||||
ESP_ERROR_CHECK(esp_vfs_fat_spiflash_unmount_rw_wl(base_path, s_wl_handle));
|
||||
|
||||
ESP_LOGI(TAG, "Done");
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "hello_world_main.c"
|
||||
PRIV_REQUIRES spi_flash
|
||||
INCLUDE_DIRS "")
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_chip_info.h"
|
||||
#include "esp_flash.h"
|
||||
#include "esp_system.h"
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
printf("Hello world!\n");
|
||||
|
||||
/* Print chip information */
|
||||
esp_chip_info_t chip_info;
|
||||
uint32_t flash_size;
|
||||
esp_chip_info(&chip_info);
|
||||
printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
|
||||
CONFIG_IDF_TARGET,
|
||||
chip_info.cores,
|
||||
(chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
|
||||
(chip_info.features & CHIP_FEATURE_BT) ? "BT" : "",
|
||||
(chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "",
|
||||
(chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : "");
|
||||
|
||||
unsigned major_rev = chip_info.revision / 100;
|
||||
unsigned minor_rev = chip_info.revision % 100;
|
||||
printf("silicon revision v%d.%d, ", major_rev, minor_rev);
|
||||
if (esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
|
||||
printf("Get flash size failed");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
|
||||
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
|
||||
|
||||
printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
|
||||
|
||||
for (int i = 10; i >= 0; i--) {
|
||||
printf("Restarting in %d seconds...\n", i);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
printf("Restarting now.\n");
|
||||
fflush(stdout);
|
||||
esp_restart();
|
||||
}
|
||||
Reference in New Issue
Block a user