1 /*
2  * Copyright (c) 2020, Teslabs Engineering S.L.
3  * Copyright (c) 2022, Basalte bv
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/linker/devicetree_regions.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/ztest.h>
11 
12 /** Buffer size. */
13 #define BUF_SIZE 64U
14 #define BUF_DEF(label) static uint32_t buf_##label[BUF_SIZE]			\
15 	Z_GENERIC_SECTION(LINKER_DT_NODE_REGION_NAME(DT_NODELABEL(label)))
16 
17 /**
18  * @brief Helper function to test RAM r/w.
19  *
20  * @param mem RAM memory location to be tested.
21  */
test_ram_rw(uint32_t * mem,size_t size)22 static void test_ram_rw(uint32_t *mem, size_t size)
23 {
24 	/* fill memory with number range (0, BUF_SIZE - 1) */
25 	for (size_t i = 0U; i < size / sizeof(uint32_t); i++) {
26 		mem[i] = i;
27 	}
28 
29 	/* check that memory contains written range */
30 	for (size_t i = 0U; i < size / sizeof(uint32_t); i++) {
31 		zassert_equal(mem[i], i, "Unexpected content on byte %ld", i);
32 	}
33 }
34 
35 #if DT_NODE_HAS_STATUS(DT_NODELABEL(sdram1), okay)
36 BUF_DEF(sdram1);
37 #endif
38 #if DT_NODE_HAS_STATUS(DT_NODELABEL(sdram2), okay)
39 BUF_DEF(sdram2);
40 #endif
41 #if DT_NODE_HAS_STATUS(DT_NODELABEL(sram1), okay)
42 BUF_DEF(sram1);
43 #endif
44 #if DT_NODE_HAS_STATUS(DT_NODELABEL(sram2), okay)
45 BUF_DEF(sram2);
46 #endif
47 #if DT_NODE_HAS_STATUS(DT_NODELABEL(memc), okay)
48 BUF_DEF(psram);
49 #endif
50 
51 #if DT_NODE_HAS_STATUS(DT_NODELABEL(ram0), okay)
52 #define RAM_SIZE DT_REG_SIZE(DT_NODELABEL(ram0))
53 static uint32_t *buf_ram0 = (uint32_t *)DT_REG_ADDR(DT_NODELABEL(ram0));
54 #endif
55 
56 ZTEST_SUITE(test_ram, NULL, NULL, NULL, NULL, NULL);
57 
ZTEST(test_ram,test_sdram1)58 ZTEST(test_ram, test_sdram1)
59 {
60 #if DT_NODE_HAS_STATUS(DT_NODELABEL(sdram1), okay)
61 	test_ram_rw(buf_sdram1, BUF_SIZE);
62 #else
63 	ztest_test_skip();
64 #endif
65 }
66 
ZTEST(test_ram,test_ram0)67 ZTEST(test_ram, test_ram0)
68 {
69 #if DT_NODE_HAS_STATUS(DT_NODELABEL(ram0), okay)
70 	test_ram_rw(buf_ram0, RAM_SIZE);
71 #else
72 	ztest_test_skip();
73 #endif
74 }
75 
ZTEST(test_ram,test_sdram2)76 ZTEST(test_ram, test_sdram2)
77 {
78 #if DT_NODE_HAS_STATUS(DT_NODELABEL(sdram2), okay)
79 	test_ram_rw(buf_sdram2, BUF_SIZE);
80 #else
81 	ztest_test_skip();
82 #endif
83 }
84 
ZTEST(test_ram,test_sram1)85 ZTEST(test_ram, test_sram1)
86 {
87 #if DT_NODE_HAS_STATUS(DT_NODELABEL(sram1), okay)
88 	test_ram_rw(buf_sram1, BUF_SIZE);
89 #else
90 	ztest_test_skip();
91 #endif
92 }
93 
ZTEST(test_ram,test_sram2)94 ZTEST(test_ram, test_sram2)
95 {
96 #if DT_NODE_HAS_STATUS(DT_NODELABEL(sram2), okay)
97 	test_ram_rw(buf_sram2, BUF_SIZE);
98 #else
99 	ztest_test_skip();
100 #endif
101 }
102 
ZTEST(test_ram,test_psram)103 ZTEST(test_ram, test_psram)
104 {
105 #if DT_NODE_HAS_STATUS(DT_NODELABEL(memc), okay)
106 	test_ram_rw(buf_psram, BUF_SIZE);
107 #else
108 	ztest_test_skip();
109 #endif
110 }
111