2021-10-12 00:25:45 -04:00
|
|
|
/*
|
2022-05-20 21:14:41 -04:00
|
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2021-10-12 00:25:45 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2017-03-01 23:21:03 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
#include <freertos/task.h>
|
|
|
|
#include <freertos/semphr.h>
|
|
|
|
|
|
|
|
#include <unity.h>
|
2022-06-27 03:24:07 -04:00
|
|
|
#include <spi_flash_mmap.h>
|
2017-03-01 23:21:03 -05:00
|
|
|
#include <esp_attr.h>
|
|
|
|
#include <esp_flash_encrypt.h>
|
2022-07-21 07:14:41 -04:00
|
|
|
#include "esp_memory_utils.h"
|
2017-03-01 23:21:03 -05:00
|
|
|
|
2022-06-27 03:24:07 -04:00
|
|
|
#include "esp_private/cache_utils.h"
|
2017-03-01 23:21:03 -05:00
|
|
|
|
2023-01-10 00:59:46 -05:00
|
|
|
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C6, ESP32H2)
|
2017-03-01 23:21:03 -05:00
|
|
|
static QueueHandle_t result_queue;
|
|
|
|
|
|
|
|
static IRAM_ATTR void cache_test_task(void *arg)
|
|
|
|
{
|
|
|
|
bool do_disable = (bool)arg;
|
|
|
|
bool result;
|
|
|
|
if(do_disable) {
|
|
|
|
spi_flash_disable_interrupts_caches_and_other_cpu();
|
|
|
|
}
|
|
|
|
result = spi_flash_cache_enabled();
|
|
|
|
if (do_disable) {
|
|
|
|
spi_flash_enable_interrupts_caches_and_other_cpu();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ASSERT( xQueueSendToBack(result_queue, &result, 0) );
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2019-01-08 05:29:25 -05:00
|
|
|
TEST_CASE("spi_flash_cache_enabled() works on both CPUs", "[spi_flash][esp_flash]")
|
2017-03-01 23:21:03 -05:00
|
|
|
{
|
|
|
|
result_queue = xQueueCreate(1, sizeof(bool));
|
|
|
|
|
|
|
|
for(int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
|
|
|
|
for(int disable = 0; disable <= 1; disable++) {
|
|
|
|
bool do_disable = disable;
|
|
|
|
bool result;
|
|
|
|
printf("Testing cpu %d disabled %d\n", cpu, do_disable);
|
|
|
|
|
|
|
|
xTaskCreatePinnedToCore(cache_test_task, "cache_check_task",
|
|
|
|
2048, (void *)do_disable, configMAX_PRIORITIES-1, NULL, cpu);
|
|
|
|
|
|
|
|
TEST_ASSERT( xQueueReceive(result_queue, &result, 2) );
|
|
|
|
TEST_ASSERT_EQUAL(!do_disable, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vQueueDelete(result_queue);
|
|
|
|
}
|
|
|
|
|
2021-10-12 00:25:45 -04:00
|
|
|
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
|
|
|
|
|
|
|
|
// This needs to sufficiently large array, otherwise it may end up in
|
|
|
|
// DRAM (e.g. size <= 8 bytes && ARCH == RISCV)
|
|
|
|
static const uint32_t s_in_rodata[8] = { 0x12345678, 0xfedcba98 };
|
2017-04-12 05:50:42 -04:00
|
|
|
|
|
|
|
static void IRAM_ATTR cache_access_test_func(void* arg)
|
|
|
|
{
|
2021-10-12 00:25:45 -04:00
|
|
|
/* Assert that the array s_in_rodata is in DROM. If not, this test is
|
|
|
|
* invalid as disabling the cache wouldn't have any effect. */
|
|
|
|
TEST_ASSERT(esp_ptr_in_drom(s_in_rodata));
|
|
|
|
|
2017-04-12 05:50:42 -04:00
|
|
|
spi_flash_disable_interrupts_caches_and_other_cpu();
|
|
|
|
volatile uint32_t* src = (volatile uint32_t*) s_in_rodata;
|
|
|
|
uint32_t v1 = src[0];
|
|
|
|
uint32_t v2 = src[1];
|
|
|
|
bool cache_enabled = spi_flash_cache_enabled();
|
|
|
|
spi_flash_enable_interrupts_caches_and_other_cpu();
|
|
|
|
printf("%d %x %x\n", cache_enabled, v1, v2);
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2022-05-20 21:14:41 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
#define CACHE_ERROR_REASON "Cache disabled,SW_RESET"
|
2022-10-19 03:57:24 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32H4 || CONFIG_IDF_TARGET_ESP32C6
|
2021-10-12 00:25:45 -04:00
|
|
|
#define CACHE_ERROR_REASON "Cache error,RTC_SW_CPU_RST"
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
#define CACHE_ERROR_REASON "Cache disabled,RTC_SW_CPU_RST"
|
|
|
|
#endif
|
|
|
|
|
2017-04-12 05:50:42 -04:00
|
|
|
// These tests works properly if they resets the chip with the
|
|
|
|
// "Cache disabled but cached memory region accessed" reason and the correct CPU is logged.
|
2021-10-12 00:25:45 -04:00
|
|
|
TEST_CASE("invalid access to cache raises panic (PRO CPU)", "[spi_flash][reset="CACHE_ERROR_REASON"]")
|
2017-04-12 05:50:42 -04:00
|
|
|
{
|
|
|
|
xTaskCreatePinnedToCore(&cache_access_test_func, "ia", 2048, NULL, 5, NULL, 0);
|
|
|
|
vTaskDelay(1000/portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
|
2019-06-27 10:35:06 -04:00
|
|
|
#ifndef CONFIG_FREERTOS_UNICORE
|
|
|
|
|
2021-10-12 00:25:45 -04:00
|
|
|
TEST_CASE("invalid access to cache raises panic (APP CPU)", "[spi_flash][reset="CACHE_ERROR_REASON"]")
|
2017-04-12 05:50:42 -04:00
|
|
|
{
|
|
|
|
xTaskCreatePinnedToCore(&cache_access_test_func, "ia", 2048, NULL, 5, NULL, 1);
|
|
|
|
vTaskDelay(1000/portTICK_PERIOD_MS);
|
|
|
|
}
|
2019-06-27 10:35:06 -04:00
|
|
|
|
2021-10-12 00:25:45 -04:00
|
|
|
#endif // !CONFIG_FREERTOS_UNICORE
|
|
|
|
#endif // !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
|
2023-01-10 00:59:46 -05:00
|
|
|
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C6, ESP32H2)
|