From 542a61b6cb8e7d2110c3c0d25466c7daf823cdcb Mon Sep 17 00:00:00 2001 From: gaoxu Date: Tue, 23 Aug 2022 12:31:53 +0800 Subject: [PATCH 1/2] support SPI_FLASH_ENABLE_COUNTERS feature on esp_flash driver and rename the functions to esp_flash_xx --- .../esp_partition/test/test_partitions.c | 4 +- components/spi_flash/Kconfig | 8 +- components/spi_flash/esp_flash_api.c | 91 ++++++++++++++++++- components/spi_flash/esp_flash_spi_init.c | 2 +- components/spi_flash/flash_ops.c | 49 ---------- .../include/esp_spi_flash_counters.h | 43 +++++---- .../spi_flash/spi_flash_idf_vs_rom.rst | 2 +- .../release-5.x/5.1/storage.rst | 8 ++ tools/ci/check_copyright_ignore.txt | 1 - 9 files changed, 126 insertions(+), 82 deletions(-) diff --git a/components/esp_partition/test/test_partitions.c b/components/esp_partition/test/test_partitions.c index cf9fd2ac63..47d07cab0a 100644 --- a/components/esp_partition/test/test_partitions.c +++ b/components/esp_partition/test/test_partitions.c @@ -26,14 +26,14 @@ TEST_CASE("Test erase partition", "[spi_flash][esp_flash]") const esp_partition_t *part = get_test_data_partition(); #if CONFIG_SPI_FLASH_ENABLE_COUNTERS - spi_flash_reset_counters(); + esp_flash_reset_counters(); #endif // erase whole partition ESP_ERROR_CHECK( esp_partition_erase_range(part, 0, part->size) ); #if CONFIG_SPI_FLASH_ENABLE_COUNTERS - spi_flash_dump_counters(); + esp_flash_dump_counters(stdout); #endif // put some dummy data on sector boundaries diff --git a/components/spi_flash/Kconfig b/components/spi_flash/Kconfig index c10a7b46d0..4f00ec23ae 100644 --- a/components/spi_flash/Kconfig +++ b/components/spi_flash/Kconfig @@ -35,13 +35,13 @@ menu "SPI Flash driver" config SPI_FLASH_ENABLE_COUNTERS bool "Enable operation counters" - default 0 + default n help This option enables the following APIs: - - spi_flash_reset_counters - - spi_flash_dump_counters - - spi_flash_get_counters + - esp_flash_reset_counters + - esp_flash_dump_counters + - esp_flash_get_counters These APIs may be used to collect performance data for spi_flash APIs and to help understand behaviour of libraries which use SPI flash. diff --git a/components/spi_flash/esp_flash_api.c b/components/spi_flash/esp_flash_api.c index 3cd3920016..a3a932cf0e 100644 --- a/components/spi_flash/esp_flash_api.c +++ b/components/spi_flash/esp_flash_api.c @@ -19,6 +19,9 @@ #include "spi_flash_mmap.h" #include "esp_rom_caps.h" #include "esp_rom_spiflash.h" +#include "esp_private/esp_clk.h" +#include "esp_spi_flash_counters.h" + #if CONFIG_IDF_TARGET_ESP32S2 #include "esp_crypto_lock.h" // for locking flash encryption peripheral #endif //CONFIG_IDF_TARGET_ESP32S2 @@ -65,6 +68,66 @@ DRAM_ATTR static const char TAG[] = "spi_flash"; } \ } while (0) + +#if CONFIG_SPI_FLASH_ENABLE_COUNTERS +static esp_flash_counters_t esp_flash_stats; + +#define COUNTER_START() uint32_t ts_begin = esp_cpu_get_cycle_count() +#define COUNTER_STOP(counter) \ + do{ \ + esp_flash_stats.counter.count++; \ + esp_flash_stats.counter.time += (esp_cpu_get_cycle_count() - ts_begin) / (esp_clk_cpu_freq() / 1000000); \ + } while(0) + +#define COUNTER_ADD_BYTES(counter, size) \ + do { \ + esp_flash_stats.counter.bytes += size; \ + } while (0) + + + +const esp_flash_counters_t *esp_flash_get_counters(void) +{ + return &esp_flash_stats; +} + +void esp_flash_reset_counters(void) +{ + memset(&esp_flash_stats, 0, sizeof(esp_flash_stats)); +} + +void esp_flash_dump_counters(FILE* stream) +{ + if (stream != NULL) { + fprintf(stream, " read: count=%8ld time=%8ldus bytes=%8ld\n", esp_flash_stats.read.count, esp_flash_stats.read.time, esp_flash_stats.read.bytes); + fprintf(stream, "write: count=%8ld time=%8ldus bytes=%8ld\n", esp_flash_stats.write.count, esp_flash_stats.write.time, esp_flash_stats.write.bytes); + fprintf(stream, "erase: count=%8ld time=%8ldus bytes=%8ld\n", esp_flash_stats.erase.count, esp_flash_stats.erase.time, esp_flash_stats.erase.bytes); + } +} + + +const spi_flash_counters_t *spi_flash_get_counters(void) +{ + return (spi_flash_counters_t *)esp_flash_get_counters(); +} + +void spi_flash_reset_counters(void) +{ + esp_flash_reset_counters(); +} + +void spi_flash_dump_counters(void) +{ + esp_flash_dump_counters(stdout); +} + +#else +#define COUNTER_START() +#define COUNTER_STOP(counter) +#define COUNTER_ADD_BYTES(counter, size) + +#endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS + #define IO_STR_LEN 10 static const char io_mode_str[][IO_STR_LEN] = { @@ -534,6 +597,8 @@ esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, ui uint32_t block_erase_size = chip->chip_drv->erase_block == NULL ? 0 : chip->chip_drv->block_erase_size; uint32_t sector_size = chip->chip_drv->sector_size; + COUNTER_START(); + if (sector_size == 0 || (block_erase_size % sector_size) != 0) { return ESP_ERR_FLASH_NOT_INITIALISED; } @@ -600,6 +665,7 @@ esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, ui err = chip->chip_drv->erase_block(chip, erase_addr); erase_addr += block_erase_size; len_remain -= block_erase_size; + COUNTER_ADD_BYTES(erase, block_erase_size); } else #endif { @@ -607,6 +673,7 @@ esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, ui err = chip->chip_drv->erase_sector(chip, erase_addr); erase_addr += sector_size; len_remain -= sector_size; + COUNTER_ADD_BYTES(erase, sector_size); } assert(len_remain < len); @@ -626,6 +693,8 @@ esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, ui bus_acquired = false; } + COUNTER_STOP(erase); + return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, start, len); } @@ -811,6 +880,8 @@ esp_err_t IRAM_ATTR esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t add } } + COUNTER_START(); + err = ESP_OK; do { err = rom_spiflash_api_funcs->start(chip); @@ -840,11 +911,14 @@ esp_err_t IRAM_ATTR esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t add address += length_to_read; length -= length_to_read; buffer = (void*)((intptr_t)buffer + length_to_read); + COUNTER_ADD_BYTES(read, length_to_read); } while (err == ESP_OK && length > 0); if (chip->os_func->release_temp_buffer != NULL) { chip->os_func->release_temp_buffer(chip->os_func_data, temp_buffer); } + + COUNTER_STOP(read); return err; } @@ -953,6 +1027,9 @@ esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint3 // Indicate whether the bus is acquired by the driver, needs to be released before return bool bus_acquired = false; err = ESP_OK; + + COUNTER_START(); + /* Write output in chunks, either by buffering on stack or by artificially cutting into MAX_WRITE_CHUNK parts (in an OS environment, this prevents writing from causing interrupt or higher priority task @@ -997,6 +1074,7 @@ esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint3 err = chip->chip_drv->write(chip, write_buf, write_addr, write_len); len_remain -= write_len; assert(len_remain < length); + COUNTER_ADD_BYTES(write, write_len); if (err != ESP_OK) { //Error happens, we end flash operation. Re-enable cache and flush it @@ -1028,12 +1106,13 @@ esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint3 buffer = (void *)((intptr_t)buffer + write_len); } + COUNTER_STOP(write); err = rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length); return err; restore_cache: - + COUNTER_STOP(write); ret = rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length); if (ret != ESP_OK) { ESP_DRAM_LOGE(TAG, "restore cache fail\n"); @@ -1078,6 +1157,8 @@ esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t addres const uint8_t *ssrc = (const uint8_t *)buffer; + COUNTER_START(); + /* On ESP32, write_encrypted encrypts data in RAM as it writes, so copy to a temporary buffer - 32 bytes at a time. @@ -1168,6 +1249,7 @@ esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t addres goto restore_cache; } err = rom_spiflash_api_funcs->end(chip, ESP_OK); + COUNTER_ADD_BYTES(write, encrypt_byte); #if CONFIG_IDF_TARGET_ESP32S2 esp_crypto_dma_lock_release(); #endif //CONFIG_IDF_TARGET_ESP32S2 @@ -1187,12 +1269,13 @@ esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t addres #endif //CONFIG_SPI_FLASH_VERIFY_WRITE } + COUNTER_STOP(write); err = rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length); return err; restore_cache: - + COUNTER_STOP(write); ret = rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length); if (ret != ESP_OK) { ESP_DRAM_LOGE(TAG, "restore cache fail\n"); @@ -1222,6 +1305,7 @@ esp_err_t IRAM_ATTR esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address return ESP_ERR_INVALID_ARG; } + COUNTER_START(); const uint8_t *map; spi_flash_mmap_handle_t map_handle; size_t map_src = address & ~(SPI_FLASH_MMU_PAGE_SIZE - 1); @@ -1233,6 +1317,9 @@ esp_err_t IRAM_ATTR esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address } memcpy(out_buffer, map + (address - map_src), length); spi_flash_munmap(map_handle); + + COUNTER_ADD_BYTES(read, length); + COUNTER_STOP(read); return err; } diff --git a/components/spi_flash/esp_flash_spi_init.c b/components/spi_flash/esp_flash_spi_init.c index 136472fd0a..a8a6d8ab70 100644 --- a/components/spi_flash/esp_flash_spi_init.c +++ b/components/spi_flash/esp_flash_spi_init.c @@ -439,7 +439,7 @@ esp_err_t esp_flash_app_init(void) spi_flash_init_lock(); spi_flash_guard_set(&g_flash_guard_default_ops); #if CONFIG_SPI_FLASH_ENABLE_COUNTERS - spi_flash_reset_counters(); + esp_flash_reset_counters(); #endif #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS err = esp_flash_init_main_bus_lock(); diff --git a/components/spi_flash/flash_ops.c b/components/spi_flash/flash_ops.c index 8aa98777c0..fa6c945936 100644 --- a/components/spi_flash/flash_ops.c +++ b/components/spi_flash/flash_ops.c @@ -77,28 +77,6 @@ static const char *TAG __attribute__((unused)) = "spi_flash"; -#if CONFIG_SPI_FLASH_ENABLE_COUNTERS -static spi_flash_counters_t s_flash_stats; - -#define COUNTER_START() uint32_t ts_begin = esp_cpu_get_cycle_count() -#define COUNTER_STOP(counter) \ - do{ \ - s_flash_stats.counter.count++; \ - s_flash_stats.counter.time += (esp_cpu_get_cycle_count() - ts_begin) / (esp_clk_cpu_freq() / 1000000); \ - } while(0) - -#define COUNTER_ADD_BYTES(counter, size) \ - do { \ - s_flash_stats.counter.bytes += size; \ - } while (0) - -#else -#define COUNTER_START() -#define COUNTER_STOP(counter) -#define COUNTER_ADD_BYTES(counter, size) - -#endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS - const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_default_ops = { .start = spi_flash_disable_interrupts_caches_and_other_cpu, .end = spi_flash_enable_interrupts_caches_and_other_cpu, @@ -197,33 +175,6 @@ esp_err_t IRAM_ATTR spi_flash_init_chip_state(void) } } -#if CONFIG_SPI_FLASH_ENABLE_COUNTERS - -static inline void dump_counter(spi_flash_counter_t *counter, const char *name) -{ - ESP_LOGI(TAG, "%s count=%8d time=%8dus bytes=%8d\n", name, - counter->count, counter->time, counter->bytes); -} - -const spi_flash_counters_t *spi_flash_get_counters(void) -{ - return &s_flash_stats; -} - -void spi_flash_reset_counters(void) -{ - memset(&s_flash_stats, 0, sizeof(s_flash_stats)); -} - -void spi_flash_dump_counters(void) -{ - dump_counter(&s_flash_stats.read, "read "); - dump_counter(&s_flash_stats.write, "write"); - dump_counter(&s_flash_stats.erase, "erase"); -} - -#endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS - void IRAM_ATTR spi_flash_set_rom_required_regs(void) { #if SOC_SPI_MEM_SUPPORT_OPI_MODE diff --git a/components/spi_flash/include/esp_spi_flash_counters.h b/components/spi_flash/include/esp_spi_flash_counters.h index ab8157c256..3355ee16bc 100644 --- a/components/spi_flash/include/esp_spi_flash_counters.h +++ b/components/spi_flash/include/esp_spi_flash_counters.h @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once @@ -32,31 +24,38 @@ typedef struct { uint32_t count; // number of times operation was executed uint32_t time; // total time taken, in microseconds uint32_t bytes; // total number of bytes -} spi_flash_counter_t; +} esp_flash_counter_t; typedef struct { - spi_flash_counter_t read; - spi_flash_counter_t write; - spi_flash_counter_t erase; -} spi_flash_counters_t; + esp_flash_counter_t read; + esp_flash_counter_t write; + esp_flash_counter_t erase; +} esp_flash_counters_t; + +// for deprecate old api +typedef esp_flash_counter_t spi_flash_counter_t; +typedef esp_flash_counters_t spi_flash_counters_t; /** * @brief Reset SPI flash operation counters */ -void spi_flash_reset_counters(void); +void esp_flash_reset_counters(void); +void spi_flash_reset_counters(void) __attribute__((deprecated("Please use 'esp_flash_reset_counters' instead"))); /** * @brief Print SPI flash operation counters */ -void spi_flash_dump_counters(void); +void esp_flash_dump_counters(FILE* stream); +void spi_flash_dump_counters(void) __attribute__((deprecated("Please use 'esp_flash_dump_counters' instead"))); /** * @brief Return current SPI flash operation counters * - * @return pointer to the spi_flash_counters_t structure holding values + * @return pointer to the esp_flash_counters_t structure holding values * of the operation counters */ -const spi_flash_counters_t* spi_flash_get_counters(void); +const esp_flash_counters_t* esp_flash_get_counters(void); +const spi_flash_counters_t* spi_flash_get_counters(void) __attribute__((deprecated("Please use 'esp_flash_get_counters' instead"))); #ifdef __cplusplus } diff --git a/docs/en/api-reference/peripherals/spi_flash/spi_flash_idf_vs_rom.rst b/docs/en/api-reference/peripherals/spi_flash/spi_flash_idf_vs_rom.rst index 6b23d7f140..1830dfb205 100644 --- a/docs/en/api-reference/peripherals/spi_flash/spi_flash_idf_vs_rom.rst +++ b/docs/en/api-reference/peripherals/spi_flash/spi_flash_idf_vs_rom.rst @@ -20,7 +20,7 @@ Feature Supported by ESP-IDF but not in Chip-ROM - :ref:`CONFIG_SPI_FLASH_LOG_FAILED_WRITE`, enabling this option will print the bad writing. - :ref:`CONFIG_SPI_FLASH_WARN_SETTING_ZERO_TO_ONE`, enabling this option will check if you're writing zero to one. - :ref:`CONFIG_SPI_FLASH_DANGEROUS_WRITE`, enabling this option will check for flash programming to certain protected regions like bootloader, partition table or application itself. - + - :ref:`CONFIG_SPI_FLASH_ENABLE_COUNTERS`, enabling this option to collect performance data for ESP-IDF SPI Flash driver APIs. Bugfixes Introduced in ESP-IDF but not in Chip-ROM -------------------------------------------------- diff --git a/docs/en/migration-guides/release-5.x/5.1/storage.rst b/docs/en/migration-guides/release-5.x/5.1/storage.rst index 40f24f34ac..efe8b075a5 100644 --- a/docs/en/migration-guides/release-5.x/5.1/storage.rst +++ b/docs/en/migration-guides/release-5.x/5.1/storage.rst @@ -7,3 +7,11 @@ FatFs ----- ``esp_vfs_fat_sdmmc_unmount()`` is now deprecated, you can use :cpp:func:`esp_vfs_fat_sdcard_unmount()` instead. This API is deprecated in previous IDF versions, but without deprecation warning and migration guide. Since IDF v5.1, calling this ``esp_vfs_fat_sdmmc_unmount()`` API will generate deprecation warning. + + +SPI_FLASH +--------- + +- :cpp:func:`spi_flash_get_counters` is deprecated, please use :cpp:func:`esp_flash_get_counters` instead. +- :cpp:func:`spi_flash_dump_counters` is deprecated, please use :cpp:func:`esp_flash_dump_counters` instead. +- :cpp:func:`spi_flash_reset_counters` is deprecated, please use :cpp:func:`esp_flash_reset_counters` instead. diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 34e224f733..21b27e8952 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -1017,7 +1017,6 @@ components/soc/include/soc/uhci_periph.h components/soc/lldesc.c components/soc/soc_include_legacy_warn.c components/spi_flash/cache_utils.h -components/spi_flash/include/esp_spi_flash_counters.h components/spi_flash/include/spi_flash_chip_boya.h components/spi_flash/include/spi_flash_chip_driver.h components/spi_flash/include/spi_flash_chip_gd.h From 500aab66188c04f81ae81c566016719ed5c26749 Mon Sep 17 00:00:00 2001 From: wanlei Date: Thu, 30 Mar 2023 20:09:06 +0800 Subject: [PATCH 2/2] add a test to verify SPI_FLASH_ENABLE_COUNTERS feature --- .../test_apps/esp_flash/main/test_app_main.c | 12 +-- .../esp_flash/main/test_esp_flash_drv.c | 81 ++++++++++++++++++- .../test_apps/esp_flash/pytest_esp_flash.py | 10 +-- .../test_apps/esp_flash/sdkconfig.ci.release | 1 + 4 files changed, 85 insertions(+), 19 deletions(-) diff --git a/components/spi_flash/test_apps/esp_flash/main/test_app_main.c b/components/spi_flash/test_apps/esp_flash/main/test_app_main.c index 3f6f58e601..01255c9084 100644 --- a/components/spi_flash/test_apps/esp_flash/main/test_app_main.c +++ b/components/spi_flash/test_apps/esp_flash/main/test_app_main.c @@ -11,22 +11,14 @@ // Some resources are lazy allocated in flash encryption, the threadhold is left for that case #define TEST_MEMORY_LEAK_THRESHOLD (400) -static size_t before_free_8bit; -static size_t before_free_32bit; - - void setUp(void) { - before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); - before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); + unity_utils_record_free_mem(); } void tearDown(void) { - size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); - size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); - unity_utils_check_leak(before_free_8bit, after_free_8bit, "8BIT", TEST_MEMORY_LEAK_THRESHOLD); - unity_utils_check_leak(before_free_32bit, after_free_32bit, "32BIT", TEST_MEMORY_LEAK_THRESHOLD); + unity_utils_evaluate_leaks_direct(TEST_MEMORY_LEAK_THRESHOLD); } void app_main(void) diff --git a/components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c b/components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c index 231615f564..a104830c50 100644 --- a/components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c +++ b/components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c @@ -916,6 +916,83 @@ static void test_flash_read_large_psram_buffer_low_internal_mem(const esp_partit } TEST_CASE_FLASH("esp_flash_read large PSRAM buffer low memory", test_flash_read_large_psram_buffer_low_internal_mem); - - #endif + + +#if CONFIG_SPI_FLASH_ENABLE_COUNTERS +#define TEST_CNT_RW_TIMES 4 +#define TEST_CNT_RW_LEN 64 +#define TEST_CNT_ERASE_LEN 8192 +void test_flash_counter(const esp_partition_t* part) +{ + esp_flash_t* chip = part->flash_chip; + uint32_t offs = part->address; + esp_flash_counters_t flash_counter; + static uint8_t write_buf[TEST_CNT_RW_LEN * TEST_CNT_RW_TIMES]; + static uint8_t read_buf[TEST_CNT_RW_LEN * TEST_CNT_RW_TIMES]; + + for(int i = 0;i < TEST_CNT_RW_LEN * TEST_CNT_RW_TIMES; i ++){ + write_buf[i] = i; + } + + esp_flash_reset_counters(); + flash_counter = *esp_flash_get_counters(); + // check for resset_counter and get_counter API + TEST_ASSERT_EACH_EQUAL_HEX8(0, &flash_counter, sizeof(esp_flash_counters_t)); + + TEST_ASSERT_EQUAL(ESP_OK, esp_flash_erase_region(chip, offs, TEST_CNT_ERASE_LEN)); + flash_counter = *esp_flash_get_counters(); + // check for erase_counter API + TEST_ASSERT_EQUAL_UINT32(0, flash_counter.read.count); + TEST_ASSERT_EQUAL_UINT32(0, flash_counter.write.count); + TEST_ASSERT_EQUAL_UINT32(1, flash_counter.erase.count); + TEST_ASSERT_EQUAL_UINT32(0 * TEST_CNT_RW_LEN, flash_counter.read.bytes); + TEST_ASSERT_EQUAL_UINT32(0 * TEST_CNT_RW_LEN, flash_counter.write.bytes); + TEST_ASSERT_EQUAL_UINT32(TEST_CNT_ERASE_LEN, flash_counter.erase.bytes); + + int count; + for(count = 0; count < TEST_CNT_RW_TIMES; count ++) { + // check counter on write option + TEST_ASSERT_EQUAL(ESP_OK, esp_flash_write(chip, write_buf + TEST_CNT_RW_LEN * count, offs + TEST_CNT_RW_LEN * count, TEST_CNT_RW_LEN) ); + flash_counter = *esp_flash_get_counters(); + TEST_ASSERT_EQUAL_UINT32((count + 1), flash_counter.write.count); + TEST_ASSERT_EQUAL_UINT32((count + 1) * TEST_CNT_RW_LEN, flash_counter.write.bytes); + + // check counter on read option + TEST_ASSERT_EQUAL(ESP_OK, esp_flash_read(chip, read_buf + TEST_CNT_RW_LEN * count, offs + TEST_CNT_RW_LEN * count, TEST_CNT_RW_LEN) ); + flash_counter = *esp_flash_get_counters(); + TEST_ASSERT_EQUAL_UINT32((count + 1), flash_counter.read.count); + TEST_ASSERT_EQUAL_UINT32((count + 1) * TEST_CNT_RW_LEN, flash_counter.read.bytes); + + esp_flash_dump_counters(stdout); + } + TEST_ASSERT_EQUAL_HEX8_ARRAY(write_buf, read_buf, TEST_CNT_RW_LEN * TEST_CNT_RW_TIMES); + + // check remain value + flash_counter = *esp_flash_get_counters(); + TEST_ASSERT_EQUAL_UINT32(1, flash_counter.erase.count); + TEST_ASSERT_EQUAL_UINT32(TEST_CNT_ERASE_LEN, flash_counter.erase.bytes); + + esp_flash_reset_counters(); + flash_counter = *esp_flash_get_counters(); + // check for resset_counter after used + TEST_ASSERT_EACH_EQUAL_HEX8(0, &flash_counter, sizeof(esp_flash_counters_t)); + + TEST_ASSERT_EQUAL(ESP_OK, esp_flash_write_encrypted(chip, offs, write_buf, TEST_CNT_RW_LEN) ); + TEST_ASSERT_EQUAL(ESP_OK, esp_flash_read_encrypted(chip, offs, read_buf, TEST_CNT_RW_LEN) ); + + printf("\ntest for encrypted write/read\n"); + esp_flash_dump_counters(stdout); + + flash_counter = *esp_flash_get_counters(); + // As we do one time of encrypted RW, no erase option + TEST_ASSERT_EQUAL_UINT32(1, flash_counter.read.count); + TEST_ASSERT_EQUAL_UINT32(1, flash_counter.write.count); + TEST_ASSERT_EQUAL_UINT32(0, flash_counter.erase.count); + TEST_ASSERT_EQUAL_UINT32(1 * TEST_CNT_RW_LEN, flash_counter.read.bytes); + TEST_ASSERT_EQUAL_UINT32(1 * TEST_CNT_RW_LEN, flash_counter.write.bytes); + TEST_ASSERT_EQUAL_UINT32(0, flash_counter.erase.bytes); +} + +TEST_CASE_FLASH("SPI flash counter test", test_flash_counter); +#endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS diff --git a/components/spi_flash/test_apps/esp_flash/pytest_esp_flash.py b/components/spi_flash/test_apps/esp_flash/pytest_esp_flash.py index 179b424b56..4785ffcf0a 100644 --- a/components/spi_flash/test_apps/esp_flash/pytest_esp_flash.py +++ b/components/spi_flash/test_apps/esp_flash/pytest_esp_flash.py @@ -16,14 +16,12 @@ from pytest_embedded import Dut [ 'release', 'flash_qio', - 'verify' + 'verify', ], indirect=True, ) def test_esp_flash(dut: Dut) -> None: - dut.expect_exact('Press ENTER to see the list of tests') - dut.write('[esp_flash]') - dut.expect_unity_test_output() + dut.run_all_single_board_cases(group='esp_flash') @pytest.mark.esp32s3 @@ -38,9 +36,7 @@ def test_esp_flash(dut: Dut) -> None: indirect=True, ) def test_esp_flash_rom(dut: Dut) -> None: - dut.expect_exact('Press ENTER to see the list of tests') - dut.write('[esp_flash]') - dut.expect_unity_test_output() + dut.run_all_single_board_cases(group='esp_flash') @pytest.mark.esp32 diff --git a/components/spi_flash/test_apps/esp_flash/sdkconfig.ci.release b/components/spi_flash/test_apps/esp_flash/sdkconfig.ci.release index 7eb62a2852..e20ceb05da 100644 --- a/components/spi_flash/test_apps/esp_flash/sdkconfig.ci.release +++ b/components/spi_flash/test_apps/esp_flash/sdkconfig.ci.release @@ -3,3 +3,4 @@ CONFIG_FREERTOS_USE_TICKLESS_IDLE=y CONFIG_COMPILER_OPTIMIZATION_SIZE=y CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y +CONFIG_SPI_FLASH_ENABLE_COUNTERS=y