diff --git a/components/spi_flash/Kconfig b/components/spi_flash/Kconfig new file mode 100644 index 000000000..c344a6b74 --- /dev/null +++ b/components/spi_flash/Kconfig @@ -0,0 +1,16 @@ +menu "SPI Flash driver" + +config SPI_FLASH_ENABLE_COUNTERS + bool "Enable operation counters" + default 0 + help + This option enables the following APIs: + spi_flash_reset_counters + spi_flash_dump_counters + spi_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. + +endmenu + + diff --git a/components/spi_flash/esp_spi_flash.c b/components/spi_flash/esp_spi_flash.c index 28623df1b..a9da316ab 100644 --- a/components/spi_flash/esp_spi_flash.c +++ b/components/spi_flash/esp_spi_flash.c @@ -28,7 +28,7 @@ #include "esp_ipc.h" #include "esp_attr.h" #include "esp_spi_flash.h" - +#include "esp_log.h" /* Driver for SPI flash read/write/erase operations @@ -74,21 +74,20 @@ static bool s_flash_op_can_start = false; static bool s_flash_op_complete = false; #endif //CONFIG_FREERTOS_UNICORE -typedef struct { - uint32_t count; - uint32_t time; -} counter_t; - -typedef struct { - counter_t read; - counter_t write; - counter_t erase; - counter_t read_inner; - counter_t write_inner; -} spi_flash_counters_t; - +#if CONFIG_SPI_FLASH_ENABLE_COUNTERS +static const char* TAG = "spi_flash"; static spi_flash_counters_t s_flash_stats; +#define COUNTER_START() uint32_t ts_begin = xthal_get_ccount() +#define COUNTER_STOP(counter) do{ s_flash_stats.counter.count++; s_flash_stats.counter.time += (xthal_get_ccount() - ts_begin) / (XT_CLOCK_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 + #ifndef CONFIG_FREERTOS_UNICORE static void IRAM_ATTR spi_flash_op_block_func(void* arg) @@ -112,6 +111,10 @@ static void IRAM_ATTR spi_flash_op_block_func(void* arg) void spi_flash_init() { s_flash_op_mutex = xSemaphoreCreateMutex(); + +#if CONFIG_SPI_FLASH_ENABLE_COUNTERS + spi_flash_reset_counters(); +#endif } static void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu() @@ -177,7 +180,9 @@ static void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu() void spi_flash_init() { - // No-op in single core mode +#if CONFIG_SPI_FLASH_ENABLE_COUNTERS + spi_flash_reset_counters(); +#endif } static void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu() @@ -194,9 +199,6 @@ static void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu() #endif // CONFIG_FREERTOS_UNICORE -#define COUNTER_START() uint32_t ts_begin = xthal_get_ccount() -#define COUNTER_STOP(counter) do{ (counter)->count++; (counter)->time += (xthal_get_ccount() - ts_begin) / (XT_CLOCK_FREQ / 1000000); } while(0) - SpiFlashOpResult IRAM_ATTR spi_flash_unlock() { @@ -221,7 +223,7 @@ esp_err_t IRAM_ATTR spi_flash_erase_sector(uint16_t sec) rc = SPIEraseSector(sec); } spi_flash_enable_interrupts_caches_and_other_cpu(); - COUNTER_STOP(&s_flash_stats.erase); + COUNTER_STOP(erase); return spi_flash_translate_rc(rc); } @@ -232,12 +234,11 @@ esp_err_t IRAM_ATTR spi_flash_write(uint32_t dest_addr, const uint32_t *src, uin SpiFlashOpResult rc; rc = spi_flash_unlock(); if (rc == SPI_FLASH_RESULT_OK) { - COUNTER_START(); rc = SPIWrite(dest_addr, src, (int32_t) size); - COUNTER_STOP(&s_flash_stats.write_inner); + COUNTER_ADD_BYTES(write, size); } spi_flash_enable_interrupts_caches_and_other_cpu(); - COUNTER_STOP(&s_flash_stats.write); + COUNTER_STOP(write); return spi_flash_translate_rc(rc); } @@ -247,12 +248,11 @@ esp_err_t IRAM_ATTR spi_flash_read(uint32_t src_addr, uint32_t *dest, uint32_t s spi_flash_disable_interrupts_caches_and_other_cpu(); SpiFlashOpResult rc; { - COUNTER_START(); rc = SPIRead(src_addr, dest, (int32_t) size); - COUNTER_STOP(&s_flash_stats.read_inner); + COUNTER_ADD_BYTES(read, size); } spi_flash_enable_interrupts_caches_and_other_cpu(); - COUNTER_STOP(&s_flash_stats.read); + COUNTER_STOP(read); return spi_flash_translate_rc(rc); } @@ -303,21 +303,29 @@ static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_sta } } -static void dump_counter(counter_t* counter, const char* name) +#if CONFIG_SPI_FLASH_ENABLE_COUNTERS + +static inline void dump_counter(spi_flash_counter_t* counter, const char* name) { - printf("%s count=%8d\ttime=%8dms\n", name, counter->count, counter->time/1000); + ESP_LOGI(TAG, "%s count=%8d time=%8dms bytes=%8d\n", name, + counter->count, counter->time, counter->bytes); } -void spi_flash_reset_stats() +const spi_flash_counters_t* spi_flash_get_counters() +{ + return &s_flash_stats; +} + +void spi_flash_reset_counters() { memset(&s_flash_stats, 0, sizeof(s_flash_stats)); } -void spi_flash_dump_stats() +void spi_flash_dump_counters() { - dump_counter(&s_flash_stats.read, "read "); - dump_counter(&s_flash_stats.write, "write "); - dump_counter(&s_flash_stats.read_inner, "read_inner "); - dump_counter(&s_flash_stats.write_inner, "write_inner"); - dump_counter(&s_flash_stats.erase, "erase "); + 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 diff --git a/components/spi_flash/include/esp_spi_flash.h b/components/spi_flash/include/esp_spi_flash.h index 7169555d3..6d635880e 100644 --- a/components/spi_flash/include/esp_spi_flash.h +++ b/components/spi_flash/include/esp_spi_flash.h @@ -17,6 +17,7 @@ #include #include "esp_err.h" +#include "sdkconfig.h" #ifdef __cplusplus extern "C" { @@ -69,10 +70,42 @@ esp_err_t spi_flash_write(uint32_t des_addr, const uint32_t *src_addr, uint32_t esp_err_t spi_flash_read(uint32_t src_addr, uint32_t *des_addr, uint32_t size); -void spi_flash_reset_stats(); +#if CONFIG_SPI_FLASH_ENABLE_COUNTERS -void spi_flash_dump_stats(); +/** + * Structure holding statistics for one type of operation + */ +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, for read and write operations +} spi_flash_counter_t; +typedef struct { + spi_flash_counter_t read; + spi_flash_counter_t write; + spi_flash_counter_t erase; +} spi_flash_counters_t; + +/** + * @brief Reset SPI flash operation counters + */ +void spi_flash_reset_counters(); + +/** + * @brief Print SPI flash operation counters + */ +void spi_flash_dump_counters(); + +/** + * @brief Return current SPI flash operation counters + * + * @return pointer to the spi_flash_counters_t structure holding values + * of the operation counters + */ +const spi_flash_counters_t* spi_flash_get_counters(); + +#endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS #ifdef __cplusplus }