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