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 #include "test_auxiliary.h"
17 #include <stdlib.h>
18 #include "panic.h"
19 #include "common_def.h"
20 #include "test_suite_log.h"
21 #include "test_suite.h"
22 #include "test_suite_errors.h"
23 #include "test_suite_functions_processor.h"
24
25 #define LOG_32ASHEX_VALUE_H_SHIFT 16
26 #define LOG_16ASHEX_VALUE_H_SHIFT 8
27 #define LOG_8ASHEX_VALUE_H_SHIFT 4
28 #define LOG_OUTPUT_NUM 16
29
log_uint_8_as_hex(uint8_t value)30 static void log_uint_8_as_hex(uint8_t value)
31 {
32 int8_t table[LOG_OUTPUT_NUM] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
33
34 uint8_t a = (value >> LOG_8ASHEX_VALUE_H_SHIFT) & 0x0F;
35 uint8_t b = value & 0x0F;
36
37 test_suite_log_char(table[a]);
38 test_suite_log_char(table[b]);
39 }
40
log_uint_16_as_hex(uint16_t value)41 static void log_uint_16_as_hex(uint16_t value)
42 {
43 log_uint_8_as_hex((uint8_t)((value & 0xFF00) >> LOG_16ASHEX_VALUE_H_SHIFT));
44 log_uint_8_as_hex((uint8_t)(value & 0x00FF));
45 }
46
log_uint_32_as_hex(uint32_t value)47 static void log_uint_32_as_hex(uint32_t value)
48 {
49 log_uint_16_as_hex((uint16_t)((value & 0xFFFF0000) >> LOG_32ASHEX_VALUE_H_SHIFT));
50 log_uint_16_as_hex((uint16_t)(value & 0x0000FFFF));
51 }
52
log_mem_32_as_hex(uint32_t * src_p,uint16_t length,uint32_t addr)53 static void log_mem_32_as_hex(uint32_t *src_p, uint16_t length, uint32_t addr)
54 {
55 uint16_t i = 0;
56
57 test_suite_log_stringf("%08x: ", addr);
58 while (i < length) {
59 log_uint_32_as_hex(src_p[i]);
60 i++;
61
62 if (i % 4 == 0) { /* line breaks if uart outputs 4 * 8 characters */
63 test_suite_log_char('\r');
64 test_suite_log_char('\n');
65
66 if (i < length) {
67 /* Print the address every 4 * 8 characters */
68 test_suite_log_stringf("%08x: ", addr + (i * 4));
69 }
70 } else {
71 test_suite_log_char(' ');
72 }
73 }
74
75 test_suite_log_char('\r');
76 test_suite_log_char('\n');
77 }
78
echo_parameter_function(int argc,char * argv[])79 static int echo_parameter_function(int argc, char *argv[])
80 {
81 for (int i = 0; i < argc; i++) {
82 if (i > 0) {
83 test_suite_log_string(" ");
84 }
85
86 test_suite_log_string(argv[i]);
87 }
88 test_suite_log_string("\r\n");
89 return TEST_SUITE_OK;
90 }
91
test_auxiliary_infinite_loop(int argc,char * argv[])92 static int test_auxiliary_infinite_loop(int argc, char *argv[])
93 {
94 unused(argc);
95 unused(argv);
96 while (1) { //lint !e716
97 panic(PANIC_TESTSUIT, __LINE__);
98 }; //lint !e527
99 return TEST_SUITE_OK; //lint !e527
100 }
101
test_auxiliary_read_mem32(int argc,char * argv[])102 static int test_auxiliary_read_mem32(int argc, char *argv[])
103 {
104 uint32_t *addr = NULL;
105 uint16_t ne;
106
107 if (argc < TEST_PARAM_ARGC_1) {
108 return TEST_SUITE_ERROR_BAD_PARAMS;
109 }
110 addr = (uint32_t *)strtol(argv[0], NULL, 0);
111 if (argc >= TEST_PARAM_ARGC_2) {
112 ne = (uint16_t)strtol(argv[1], NULL, 0);
113 } else {
114 ne = 0;
115 }
116
117 test_suite_log_stringf("Reading %d bytes from 0x%08x\r\n", ne, addr);
118 log_mem_32_as_hex(addr, ne, (uint32_t)addr);
119 return TEST_SUITE_OK;
120 }
121
test_auxiliary_write_mem32(int argc,char * argv[])122 static int test_auxiliary_write_mem32(int argc, char *argv[])
123 {
124 if (argc != TEST_PARAM_ARGC_2) {
125 return TEST_SUITE_ERROR_BAD_PARAMS;
126 }
127
128 uint32_t addr = (uint32_t)strtol(argv[0], NULL, 0);
129 uint32_t val = (uint32_t)strtol(argv[1], NULL, 0);
130
131 *(volatile uint32_t *)(uintptr_t)(addr) = (uint32_t)(val);
132 test_suite_log_stringf("Write addr %8#x to %8#x\r\n", addr, *(volatile uint32_t *)(uintptr_t)(addr));
133 return TEST_SUITE_OK;
134 }
135
add_auxiliary_functions(void)136 void add_auxiliary_functions(void)
137 {
138 uapi_test_suite_add_function("list_functions", "Lists all the known functions",
139 (test_suite_function_callback_t)test_suite_function_list_func);
140 uapi_test_suite_add_function(
141 "echo", "Echos back the parameter you pass into this function", echo_parameter_function);
142 uapi_test_suite_add_function("infinite_loop", "Goes to an infinite loop", test_auxiliary_infinite_loop);
143 uapi_test_suite_add_function(
144 "mem32", "Read 32-bit items. Params: <Addr>, <NumItems> (hex or decimal)", test_auxiliary_read_mem32);
145 uapi_test_suite_add_function(
146 "w4", "Write 32-bit items. Params: <Addr>, <Value> (hex or decimal)", test_auxiliary_write_mem32);
147 }
148