1 /*
2 * Copyright 2022 NXP
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8
9 #if DT_HAS_COMPAT_STATUS_OKAY(nxp_imx_flexspi)
10 /* FlexSPI memory mapped region is second register property of parent dev */
11 #define MEMC_BASE DT_REG_ADDR_BY_IDX(DT_PARENT(DT_ALIAS(sram_ext)), 1)
12 #define MEMC_SIZE (DT_PROP(DT_ALIAS(sram_ext), size) / 8)
13 #endif
14
dump_memory(uint8_t * p,uint32_t size)15 void dump_memory(uint8_t *p, uint32_t size)
16 {
17 uint32_t i, j;
18
19 for (i = 0, j = 0; j < (size / 16); i += 16, j++) {
20 printk("%02x %02x %02x %02x %02x %02x %02x %02x ",
21 p[i], p[i+1], p[i+2], p[i+3],
22 p[i+4], p[i+5], p[i+6], p[i+7]);
23 printk("%02x %02x %02x %02x %02x %02x %02x %02x\n",
24 p[i+8], p[i+9], p[i+10], p[i+11],
25 p[i+12], p[i+13], p[i+14], p[i+15]);
26 /* Split dump at 256B boundaries */
27 if (((i + 16) & 0xFF) == 0) {
28 printk("\n");
29 }
30 }
31 /* Dump any remaining data after 16 byte blocks */
32 for (; i < size; i++) {
33 printk("%02x ", p[i]);
34 }
35 printk("\n");
36 }
37
38 #define BUF_SIZE 1024
39
40 uint8_t memc_write_buffer[BUF_SIZE];
41 uint8_t memc_read_buffer[BUF_SIZE];
42
main(void)43 int main(void)
44 {
45 uint8_t *memc = (uint8_t *)MEMC_BASE;
46 uint32_t i, j;
47
48 /* Initialize write buffer */
49 for (i = 0; i < BUF_SIZE; i++) {
50 memc_write_buffer[i] = (uint8_t)i;
51 }
52 printk("Writing to memory region with base 0x%0x, size 0x%0x\n\n",
53 MEMC_BASE, MEMC_SIZE);
54 /* Copy write buffer into memc region */
55 for (i = 0, j = 0; j < (MEMC_SIZE / BUF_SIZE); i += BUF_SIZE, j++) {
56 memcpy(memc + i, memc_write_buffer, BUF_SIZE);
57 }
58 /* Copy any remaining space bytewise */
59 for (; i < MEMC_SIZE; i++) {
60 memc[i] = memc_write_buffer[i];
61 }
62 /* Read from memc region into buffer */
63 for (i = 0, j = 0; j < (MEMC_SIZE / BUF_SIZE); i += BUF_SIZE, j++) {
64 memcpy(memc_read_buffer, memc + i, BUF_SIZE);
65 /* Compare memory */
66 if (memcmp(memc_read_buffer, memc_write_buffer, BUF_SIZE)) {
67 printk("Error: read data differs in range [0x%x- 0x%x]\n",
68 i, i + (BUF_SIZE - 1));
69 return 0;
70 }
71 }
72 /* Copy any remaining space bytewise */
73 for (; i < MEMC_SIZE; i++) {
74 memc_read_buffer[i] = memc[i];
75 if (memc_write_buffer[i] != memc_read_buffer[i]) {
76 printk("Error: read data differs at offset 0x%x\n", i);
77 return 0;
78 }
79 }
80 printk("First 1KB of Data in memory:\n");
81 printk("===========================\n");
82 dump_memory(memc, MIN(MEMC_SIZE, KB(1)));
83 printk("Read data matches written data\n");
84 return 0;
85 }
86