test_cache_disabled.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <freertos/FreeRTOS.h>
  10. #include <freertos/task.h>
  11. #include <freertos/semphr.h>
  12. #include <unity.h>
  13. #include <esp_spi_flash.h>
  14. #include <esp_attr.h>
  15. #include <esp_flash_encrypt.h>
  16. #include "../cache_utils.h"
  17. static QueueHandle_t result_queue;
  18. static IRAM_ATTR void cache_test_task(void *arg)
  19. {
  20. bool do_disable = (bool)arg;
  21. bool result;
  22. if(do_disable) {
  23. spi_flash_disable_interrupts_caches_and_other_cpu();
  24. }
  25. result = spi_flash_cache_enabled();
  26. if (do_disable) {
  27. spi_flash_enable_interrupts_caches_and_other_cpu();
  28. }
  29. TEST_ASSERT( xQueueSendToBack(result_queue, &result, 0) );
  30. vTaskDelete(NULL);
  31. }
  32. TEST_CASE("spi_flash_cache_enabled() works on both CPUs", "[spi_flash][esp_flash]")
  33. {
  34. result_queue = xQueueCreate(1, sizeof(bool));
  35. for(int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
  36. for(int disable = 0; disable <= 1; disable++) {
  37. bool do_disable = disable;
  38. bool result;
  39. printf("Testing cpu %d disabled %d\n", cpu, do_disable);
  40. xTaskCreatePinnedToCore(cache_test_task, "cache_check_task",
  41. 2048, (void *)do_disable, configMAX_PRIORITIES-1, NULL, cpu);
  42. TEST_ASSERT( xQueueReceive(result_queue, &result, 2) );
  43. TEST_ASSERT_EQUAL(!do_disable, result);
  44. }
  45. }
  46. vQueueDelete(result_queue);
  47. }
  48. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
  49. // This needs to sufficiently large array, otherwise it may end up in
  50. // DRAM (e.g. size <= 8 bytes && ARCH == RISCV)
  51. static const uint32_t s_in_rodata[8] = { 0x12345678, 0xfedcba98 };
  52. static void IRAM_ATTR cache_access_test_func(void* arg)
  53. {
  54. /* Assert that the array s_in_rodata is in DROM. If not, this test is
  55. * invalid as disabling the cache wouldn't have any effect. */
  56. TEST_ASSERT(esp_ptr_in_drom(s_in_rodata));
  57. spi_flash_disable_interrupts_caches_and_other_cpu();
  58. volatile uint32_t* src = (volatile uint32_t*) s_in_rodata;
  59. uint32_t v1 = src[0];
  60. uint32_t v2 = src[1];
  61. bool cache_enabled = spi_flash_cache_enabled();
  62. spi_flash_enable_interrupts_caches_and_other_cpu();
  63. printf("%d %x %x\n", cache_enabled, v1, v2);
  64. vTaskDelete(NULL);
  65. }
  66. #ifdef CONFIG_IDF_TARGET_ESP32C3
  67. #define CACHE_ERROR_REASON "Cache error,RTC_SW_CPU_RST"
  68. #elif CONFIG_IDF_TARGET_ESP32S3
  69. #define CACHE_ERROR_REASON "Cache disabled,RTC_SW_CPU_RST"
  70. #else
  71. #define CACHE_ERROR_REASON "Cache disabled,SW_RESET"
  72. #endif
  73. // These tests works properly if they resets the chip with the
  74. // "Cache disabled but cached memory region accessed" reason and the correct CPU is logged.
  75. TEST_CASE("invalid access to cache raises panic (PRO CPU)", "[spi_flash][reset="CACHE_ERROR_REASON"]")
  76. {
  77. xTaskCreatePinnedToCore(&cache_access_test_func, "ia", 2048, NULL, 5, NULL, 0);
  78. vTaskDelay(1000/portTICK_PERIOD_MS);
  79. }
  80. #ifndef CONFIG_FREERTOS_UNICORE
  81. TEST_CASE("invalid access to cache raises panic (APP CPU)", "[spi_flash][reset="CACHE_ERROR_REASON"]")
  82. {
  83. xTaskCreatePinnedToCore(&cache_access_test_func, "ia", 2048, NULL, 5, NULL, 1);
  84. vTaskDelay(1000/portTICK_PERIOD_MS);
  85. }
  86. #endif // !CONFIG_FREERTOS_UNICORE
  87. #endif // !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)