app_main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * In this example, we show you a way to make an ISR-based callback work during Flash operations, when the ISR-based
  8. * callback is put in Flash.
  9. *
  10. * Please read the README.md to know more details about this feature!
  11. */
  12. #include <stdbool.h>
  13. #include <stdio.h>
  14. #include "sdkconfig.h"
  15. #include "esp_log.h"
  16. #include "esp_attr.h"
  17. #include "esp_cpu.h"
  18. #include "esp_partition.h"
  19. #include "driver/gptimer.h"
  20. #define TIMER_RESOLUTION_HZ (1 * 1000 * 1000) // 1MHz resolution
  21. #define TIMER_ALARM_PERIOD_S 1 // Alarm period 1s
  22. #define RECORD_TIME_PREPARE() uint32_t __t1, __t2
  23. #define RECORD_TIME_START() do {__t1 = esp_cpu_get_cycle_count();} while(0)
  24. #define RECORD_TIME_END(p_time) do{__t2 = esp_cpu_get_cycle_count(); p_time = (__t2 - __t1);} while(0)
  25. #define GET_US_BY_CCOUNT(t) ((double)(t)/CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ)
  26. const static char *TAG = "Example";
  27. DRAM_ATTR static uint32_t s_t1;
  28. DRAM_ATTR static uint32_t s_flash_func_t2;
  29. DRAM_ATTR static uint32_t s_iram_func_t2;
  30. static NOINLINE_ATTR void s_function_in_flash(void)
  31. {
  32. /**
  33. * - Here we will have few instructions in .flash.text
  34. * - Cache will read from Flash to get the instructions synchronized.
  35. * - CPU will execute around 400 times.
  36. */
  37. for (int i = 0; i < 100; i++) {
  38. asm volatile("nop");
  39. }
  40. s_flash_func_t2 = esp_cpu_get_cycle_count();
  41. }
  42. static IRAM_ATTR NOINLINE_ATTR void s_funtion_in_iram(void)
  43. {
  44. /**
  45. * - Here we will have few instructions in .iram0.text
  46. * - CPU will execute around 400 times.
  47. */
  48. for (int i = 0; i < 100; i++) {
  49. asm volatile("nop");
  50. }
  51. s_iram_func_t2 = esp_cpu_get_cycle_count();
  52. }
  53. static bool IRAM_ATTR on_gptimer_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
  54. {
  55. bool is_flash = *(bool *)user_ctx;
  56. s_t1 = esp_cpu_get_cycle_count();
  57. if (is_flash) {
  58. s_function_in_flash();
  59. } else {
  60. s_funtion_in_iram();
  61. }
  62. return false;
  63. }
  64. static const esp_partition_t *s_get_partition(void)
  65. {
  66. //Find the "storage1" partition defined in `partitions.csv`
  67. const esp_partition_t *result = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage1");
  68. if (!result) {
  69. ESP_LOGE(TAG, "Can't find the partition, please define it correctly in `partitions.csv`");
  70. abort();
  71. }
  72. return result;
  73. }
  74. void app_main(void)
  75. {
  76. //Get the partition used for SPI1 erase operation
  77. const esp_partition_t *part = s_get_partition();
  78. ESP_LOGI(TAG, "found partition '%s' at offset 0x%x with size 0x%x", part->label, part->address, part->size);
  79. //Erase whole region
  80. ESP_ERROR_CHECK(esp_flash_erase_region(part->flash_chip, part->address, part->size));
  81. gptimer_handle_t gptimer = NULL;
  82. gptimer_config_t timer_config = {
  83. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  84. .direction = GPTIMER_COUNT_UP,
  85. .resolution_hz = TIMER_RESOLUTION_HZ,
  86. };
  87. ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer));
  88. gptimer_alarm_config_t alarm_config = {
  89. .reload_count = 0,
  90. .alarm_count = 1 * 1000 * 1000,
  91. .flags.auto_reload_on_alarm = false,
  92. };
  93. ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_config));
  94. gptimer_event_callbacks_t cbs = {
  95. .on_alarm = on_gptimer_alarm_cb,
  96. };
  97. bool is_flash = true;
  98. ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer, &cbs, &is_flash));
  99. ESP_ERROR_CHECK(gptimer_enable(gptimer));
  100. ESP_ERROR_CHECK(gptimer_start(gptimer));
  101. uint32_t erase_time = 0;
  102. RECORD_TIME_PREPARE();
  103. RECORD_TIME_START();
  104. ESP_ERROR_CHECK(esp_flash_erase_region(part->flash_chip, part->address, part->size));
  105. RECORD_TIME_END(erase_time);
  106. ESP_ERROR_CHECK(gptimer_stop(gptimer));
  107. ESP_LOGI(TAG, "Flash Driver Erase Operation finishes, duration:\n\t\t%0.2f us", GET_US_BY_CCOUNT(erase_time));
  108. ESP_LOGI(TAG, "During Erase, ISR callback function(in flash) response time:\n\t\t%0.2f us", GET_US_BY_CCOUNT(s_flash_func_t2 - s_t1));
  109. //Let the timer alarm callback to run code reside in .iram0.text
  110. is_flash = false;
  111. ESP_ERROR_CHECK(gptimer_set_raw_count(gptimer, 0));
  112. ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_config));
  113. ESP_ERROR_CHECK(gptimer_start(gptimer));
  114. RECORD_TIME_START();
  115. //Erase whole region
  116. ESP_ERROR_CHECK(esp_flash_erase_region(part->flash_chip, part->address, part->size));
  117. RECORD_TIME_END(erase_time);
  118. ESP_ERROR_CHECK(gptimer_stop(gptimer));
  119. ESP_LOGI(TAG, "Flash Driver Erase Operation finishes, duration:\n\t\t%0.2f us", GET_US_BY_CCOUNT(erase_time));
  120. ESP_LOGI(TAG, "During Erase, ISR callback function(in iram) response time:\n\t\t%0.2f us", GET_US_BY_CCOUNT(s_iram_func_t2 - s_t1));
  121. ESP_LOGI(TAG, "Finish");
  122. ESP_ERROR_CHECK(gptimer_disable(gptimer));
  123. ESP_ERROR_CHECK(gptimer_del_timer(gptimer));
  124. }