1 /*
2 * Copyright 2020 Google LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /** @file
8 * @brief Interactive shell test suite for 'flash' command
9 *
10 */
11
12 #include <zephyr/kernel.h>
13 #include <zephyr/ztest.h>
14 #include <zephyr/device.h>
15
16 #include <zephyr/drivers/flash.h>
17 #include <zephyr/shell/shell.h>
18 #include <zephyr/shell/shell_dummy.h>
19
20 /* configuration derived from DT */
21 #ifdef CONFIG_ARCH_POSIX
22 #define SOC_NV_FLASH_NODE DT_CHILD(DT_INST(0, zephyr_sim_flash), flash_0)
23 #else
24 #define SOC_NV_FLASH_NODE DT_CHILD(DT_INST(0, zephyr_sim_flash), flash_sim_0)
25 #endif /* CONFIG_ARCH_POSIX */
26 #define FLASH_SIMULATOR_BASE_OFFSET DT_REG_ADDR(SOC_NV_FLASH_NODE)
27
28 /* Test 'flash read' shell command */
ZTEST(shell_flash,test_flash_read)29 ZTEST(shell_flash, test_flash_read)
30 {
31 /* To keep the test simple, just compare against known data */
32 char *const lines[] = {
33 "00000000: 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 |ABCDEFGH IJKLMNOP|",
34 "00000010: 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 |QRSTUVWX YZ[\\]^_`|",
35 "00000020: 61 62 63 |abc |",
36 };
37 const struct shell *sh = shell_backend_dummy_get_ptr();
38 const struct device *const flash_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
39 const char *buf;
40 const int test_base = FLASH_SIMULATOR_BASE_OFFSET;
41 const int test_size = 0x24; /* 32-alignment required */
42 uint8_t data[test_size];
43 size_t size;
44 int ret;
45 int i;
46
47 for (i = 0; i < test_size; i++) {
48 data[i] = 'A' + i;
49 }
50
51 zassert_true(device_is_ready(flash_dev),
52 "Simulated flash driver not ready");
53
54 ret = flash_write(flash_dev, test_base, data, test_size);
55 zassert_equal(0, ret, "flash_write() failed: %d", ret);
56
57 ret = shell_execute_cmd(NULL, "flash read 0 23");
58 zassert_equal(0, ret, "flash read failed: %d", ret);
59
60 buf = shell_backend_dummy_get_output(sh, &size);
61 for (i = 0; i < ARRAY_SIZE(lines); i++) {
62 /* buf contains all the bytes that goes through the shell
63 * backend interface including escape codes, NL and CR.
64 * Function strstr finds place in the buffer where interesting
65 * data is located.
66 */
67 zassert_true(strstr(buf, lines[i]), "Line: %d not found", i);
68 }
69 }
70
shell_setup(void)71 static void *shell_setup(void)
72 {
73 /* Let the shell backend initialize. */
74 k_usleep(10);
75 return NULL;
76 }
77
78 ZTEST_SUITE(shell_flash, NULL, shell_setup, NULL, NULL, NULL);
79