test_4mpsram.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "esp_heap_caps.h"
  2. #include "unity.h"
  3. #include "esp_log.h"
  4. #include "driver/spi_common_internal.h"
  5. #include "sdkconfig.h"
  6. static const char TAG[] = "test_psram";
  7. #ifdef CONFIG_SPIRAM
  8. static void test_psram_content(void)
  9. {
  10. const int test_size = 2048;
  11. uint32_t *test_area = heap_caps_malloc(test_size, MALLOC_CAP_SPIRAM);
  12. size_t p;
  13. size_t s=test_size;
  14. int errct=0;
  15. int initial_err=-1;
  16. for (p=0; p<(s/sizeof(int)); p+=4) {
  17. test_area[p]=p^0xAAAAAAAA;
  18. }
  19. for (p=0; p<(s/sizeof(int)); p+=4) {
  20. if (test_area[p]!=(p^0xAAAAAAAA)) {
  21. errct++;
  22. if (errct==1) initial_err=p*4;
  23. }
  24. }
  25. if (errct) {
  26. ESP_LOGE(TAG, "SPI SRAM memory test fail. %d/%d writes failed, first @ %p\n", errct, s/32, initial_err+test_area);
  27. TEST_FAIL();
  28. } else {
  29. ESP_LOGI(TAG, "SPI SRAM memory test OK");
  30. }
  31. free(test_area);
  32. }
  33. #endif
  34. bool psram_is_32mbit_ver0(void);
  35. static void test_spi_bus_occupy(spi_host_device_t expected_occupied_host)
  36. {
  37. bool claim_hspi = spicommon_periph_claim(HSPI_HOST, "ut-hspi");
  38. if (claim_hspi) ESP_LOGI(TAG, "HSPI claimed.");
  39. bool claim_vspi = spicommon_periph_claim(VSPI_HOST, "ut-vspi");
  40. if (claim_vspi) ESP_LOGI(TAG, "VSPI claimed.");
  41. if (expected_occupied_host == HSPI_HOST) {
  42. TEST_ASSERT_FALSE(claim_hspi);
  43. TEST_ASSERT(claim_vspi);
  44. } else if (expected_occupied_host == VSPI_HOST) {
  45. TEST_ASSERT_FALSE(claim_vspi);
  46. TEST_ASSERT(claim_hspi);
  47. } else {
  48. TEST_ASSERT(claim_hspi);
  49. TEST_ASSERT(claim_vspi);
  50. }
  51. #ifdef CONFIG_SPIRAM
  52. test_psram_content();
  53. #endif
  54. }
  55. #if CONFIG_SPIRAM_OCCUPY_HSPI_HOST || CONFIG_SPIRAM_OCCUPY_VSPI_HOST
  56. TEST_CASE("some spi bus occpied by psram", "[psram_4m][test_env=UT_T1_PSRAMV0]")
  57. {
  58. // NOTE: this unit test rely on the config that PSRAM of 8MB is used only when CONFIG_SPIRAM_BNKSWITCH_ENABLE is set
  59. //currently all 8M psram don't need more SPI peripherals
  60. #if !CONFIG_SPIRAM || !CONFIG_SPIRAM_SPEED_80M || CONFIG_SPIRAM_BANKSWITCH_ENABLE
  61. #error unexpected test config, only psram 32MBit ver 0 at 80MHz will trigger the workaround
  62. #endif
  63. spi_host_device_t host;
  64. if (!psram_is_32mbit_ver0()) {
  65. TEST_FAIL_MESSAGE("unexpected psram version");
  66. }
  67. #if CONFIG_SPIRAM_OCCUPY_HSPI_HOST
  68. host = HSPI_HOST;
  69. #elif CONFIG_SPIRAM_OCCUPY_VSPI_HOST
  70. host = VSPI_HOST;
  71. #endif
  72. test_spi_bus_occupy(host);
  73. }
  74. #else
  75. TEST_CASE("can use spi when not being used by psram", "[psram_4m]")
  76. {
  77. #if CONFIG_SPIRAM && CONFIG_SPIRAM_SPEED_80M
  78. #error unexpected test config, some runners using the UT_T1_PSRAMV0 but still perform this test.\
  79. they will not pass this test at 80MHz.
  80. #endif
  81. test_spi_bus_occupy(-1);
  82. }
  83. #endif