test_psram.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include <sys/param.h>
  8. #include <string.h>
  9. #include "esp_log.h"
  10. #include "esp_attr.h"
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "test_utils.h"
  14. #include "unity.h"
  15. #include "esp_heap_caps.h"
  16. #if CONFIG_SPIRAM
  17. #include "spiram.h"
  18. const static char *TAG = "PSRAM";
  19. #if CONFIG_SPIRAM_MODE_OCT
  20. #define TEST_ALLOC_SIZE (4 * 1024 * 1024)
  21. #else
  22. #define TEST_ALLOC_SIZE (1 * 1024 * 1024)
  23. #endif
  24. TEST_CASE("test psram heap allocable","[psram]")
  25. {
  26. uint32_t *ext_buffer = (uint32_t *)heap_caps_calloc(TEST_ALLOC_SIZE, 1, MALLOC_CAP_SPIRAM);
  27. TEST_ASSERT(ext_buffer);
  28. uintptr_t start = (uintptr_t)ext_buffer;
  29. uintptr_t end = (uintptr_t)ext_buffer + TEST_ALLOC_SIZE;
  30. ESP_LOGI(TAG, "test ext buffer start addr is %x, end addr is %x", start, end);
  31. TEST_ASSERT((start >= SOC_EXTRAM_DATA_LOW) && (end <= SOC_EXTRAM_DATA_HIGH));
  32. for (int i = 0; i < TEST_ALLOC_SIZE / sizeof(uint32_t); i++) {
  33. ext_buffer[i] = (i + 1) ^ 0xaaaaaaaa;
  34. }
  35. for (int i = 0; i < TEST_ALLOC_SIZE / sizeof(uint32_t); i++) {
  36. TEST_ASSERT(ext_buffer[i] == ((i + 1) ^ 0xaaaaaaaa));
  37. }
  38. free(ext_buffer);
  39. }
  40. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_RODATA
  41. #include "esp_timer.h"
  42. #include "esp32s3/rom/spi_flash.h"
  43. #define SECTOR_LEN 4096
  44. #define TEST_NUM 10
  45. #define TEST_BUF {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9}
  46. static uint32_t s_timer_cb_exe_times;
  47. static const uint8_t s_test_buf[TEST_NUM] = TEST_BUF;
  48. static void NOINLINE_ATTR s_test_printf(void *arg)
  49. {
  50. s_timer_cb_exe_times ++;
  51. uint8_t cmp_buf[TEST_NUM] = TEST_BUF;
  52. TEST_ASSERT(memcmp(cmp_buf, s_test_buf, TEST_NUM) == 0);
  53. }
  54. TEST_CASE("test spi1 flash operation after putting .text and .rodata into psram", "[psram]")
  55. {
  56. //Create flash partition for test
  57. const esp_partition_t *part = get_test_data_partition();
  58. size_t start = part->address;
  59. ESP_LOGI(TAG, "test data partition: 0x%x", start);
  60. esp_timer_handle_t timer;
  61. esp_timer_create_args_t timer_args = {
  62. .callback = &s_test_printf,
  63. };
  64. TEST_ESP_OK(esp_timer_create(&timer_args, &timer));
  65. esp_rom_spiflash_result_t ret;
  66. uint32_t sector_num = start / SECTOR_LEN;
  67. TEST_ESP_OK(esp_timer_start_periodic(timer, 1 * 10));
  68. ret = esp_rom_spiflash_erase_sector(sector_num);
  69. if (ret != ESP_ROM_SPIFLASH_RESULT_OK) {
  70. ESP_LOGE(TAG, "erase fail!");
  71. TEST_ASSERT(false);
  72. }
  73. TEST_ESP_OK(esp_timer_stop(timer));
  74. TEST_ASSERT(s_timer_cb_exe_times > 0);
  75. printf("timer callback runs %d times\n", s_timer_cb_exe_times);
  76. }
  77. #endif //CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_RODATA
  78. #endif //#if CONFIG_SPIRAM