1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define TEST_KERNEL_FLASH
17 #ifdef TEST_KERNEL_FLASH
18 #include <app_demo_flash.h>
19
20 hi_u32 g_test_flash_tb = 0;
21 hi_u32 g_tf_test_time = 60;
22 hi_u32 g_tf_test_mode = 0;
23
24 hi_u8 g_tf_pdata[TEST_SIZE];
25 hi_u8 g_tf_pdata_back[TEST_SIZE];
26
27 #define CODE_IN_FLASH
28 #ifdef CODE_IN_FLASH
test_flash_body2(hi_void * param)29 hi_void *test_flash_body2(hi_void* param)
30 #else
31 BSP_RAM_TEXT_SECTION hi_void *test_flash_body2(hi_void* param)
32 #endif
33 {
34 hi_u32 ret;
35 hi_u32 tick[2]; /* 2 */
36 hi_unref_param(param);
37 hi_watchdog_disable();
38 tick[0] = hi_get_tick();
39 for (;;) {
40 if (g_tf_test_mode == 2) { /* 2 */
41 for (int i = 0; i < TEST_SIZE; i++) {
42 g_tf_pdata_back[i] = (hi_u8)(*(hi_u16*)(0x401000 + i * 2)); /* flash addr:0x401000 2 */
43 }
44 } else {
45 ret = hi_flash_erase(TEST_FLASH_OFFSET, TEST_SIZE);
46 if (ret != HI_ERR_SUCCESS) {
47 printf("erase fail:%x\n", ret);
48 }
49 ret = hi_flash_write(TEST_FLASH_OFFSET, TEST_SIZE, g_tf_pdata, HI_TRUE);
50 if (ret != HI_ERR_SUCCESS) {
51 printf("write fail:%x\n", ret);
52 }
53 memset_s(g_tf_pdata_back, TEST_SIZE, 0x0, TEST_SIZE);
54 ret = hi_flash_read(TEST_FLASH_OFFSET, TEST_SIZE, g_tf_pdata_back);
55 if (ret != HI_ERR_SUCCESS) {
56 printf("read fail:%x\n", ret);
57 }
58 }
59 if (memcmp(g_tf_pdata, g_tf_pdata_back, TEST_SIZE)) {
60 printf("hi_flash_read fail !\r\n");
61 break;
62 }
63 tick[1] = hi_get_tick();
64 if ((tick[1] - tick[0]) >= (g_tf_test_time * 100)) { /* 100 */
65 printf("test over\r\n");
66 break;
67 }
68 }
69 return HI_NULL;
70 }
71
cmd_test_flash(hi_u32 test_time,hi_u32 test_mode)72 hi_void cmd_test_flash(hi_u32 test_time, hi_u32 test_mode)
73 {
74 hi_task_attr attr;
75 hi_u32 ret;
76 hi_u32 i;
77 g_tf_test_time = test_time;
78 g_tf_test_mode = test_mode;
79 printf("test mode %d ,test %d s,wait ..\r\n", g_tf_test_mode, g_tf_test_time);
80 if (g_tf_test_mode == 0) {
81 for (i = 0; i < TEST_SIZE; i++) {
82 g_tf_pdata[i] = (hi_u8)i;
83 }
84 } else if (g_tf_test_mode == 1) {
85 memset_s(g_tf_pdata, TEST_SIZE, 0xF0, TEST_SIZE);
86 printf("test F0\r\n");
87 } else if (g_tf_test_mode == 2) { /* 2 */
88 printf("test task in flash\r\n");
89 }
90 ret = hi_flash_write(TEST_FLASH_OFFSET, TEST_SIZE, g_tf_pdata, HI_TRUE);
91 if (ret != HI_ERR_SUCCESS) {
92 return;
93 }
94 ret = hi_flash_read(TEST_FLASH_OFFSET, TEST_SIZE, g_tf_pdata_back);
95 if (ret != HI_ERR_SUCCESS) {
96 return;
97 }
98 if (memcmp(g_tf_pdata, g_tf_pdata_back, TEST_SIZE)) {
99 printf("write or read fail !\r\n");
100 printf("testdata addr:0x%x\r\n", (hi_u32)g_tf_pdata_back);
101 return;
102 }
103 printf("testdata addr:0x%x\r\n", (hi_u32)g_tf_pdata_back);
104 attr.stack_size = 0x800; /* 800 */
105 attr.task_name = (hi_char*) "test_flash";
106 attr.task_prio = 28; /* 28 */
107 hi_task_create(&g_test_flash_tb, &attr, test_flash_body2, 0);
108 }
109
110 #endif
111