• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Beken Corporation
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 
17 #include "cli.h"
18 #include <components/system.h>
19 #include <os/str.h>
20 #include "icu_hal.h"
21 #include "pwm_hal.h"
22 #include "timer_hal.h"
23 #include "gpio_hal.h"
24 #include "dma_hal.h"
25 #include "uart_hal.h"
26 #include "wdt_hal.h"
27 #include "trng_hal.h"
28 #include "efuse_hal.h"
29 #include "adc_hal.h"
30 #include "spi_hal.h"
31 #include "i2c_hal.h"
32 
33 #if CONFIG_QSPI
34 #include "qspi_hal.h"
35 #endif
36 #if CONFIG_AON_RTC
37 #include "aon_rtc_hal.h"
38 #endif
39 #if CONFIG_CALENDAR
40 #include "calendar_hal.h"
41 #endif
42 #if CONFIG_FLASH
43 #include "flash_hal.h"
44 #endif
45 #if CONFIG_SDIO_HOST
46 #include "sdio_host_hal.h"
47 #endif
48 
hex2num(char c)49 static int hex2num(char c)
50 {
51 	if (c >= '0' && c <= '9')
52 		return c - '0';
53 	if (c >= 'a' && c <= 'f')
54 		return c - 'a' + 10;
55 	if (c >= 'A' && c <= 'F')
56 		return c - 'A' + 10;
57 	return -1;
58 }
59 
hex2byte(const char * hex)60 static int hex2byte(const char *hex)
61 {
62 	int a, b;
63 	a = hex2num(*hex++);
64 	if (a < 0)
65 		return -1;
66 	b = hex2num(*hex++);
67 	if (b < 0)
68 		return -1;
69 	return (a << 4) | b;
70 }
71 
72 /**
73  * hexstr2bin - Convert ASCII hex string into binary data
74  * @hex: ASCII hex string (e.g., "01ab")
75  * @buf: Buffer for the binary data
76  * @len: Length of the text to convert in bytes (of buf); hex will be double
77  * this size
78  * Returns: 0 on success, -1 on failure (invalid hex string)
79  */
cli_hexstr2bin(const char * hex,u8 * buf,size_t len)80 static int cli_hexstr2bin(const char *hex, u8 *buf, size_t len)
81 {
82 	size_t i;
83 	int a;
84 	const char *ipos = hex;
85 	u8 *opos = buf;
86 
87 	for (i = 0; i < len; i++) {
88 		a = hex2byte(ipos);
89 		if (a < 0)
90 			return -1;
91 		*opos++ = a;
92 		ipos += 2;
93 	}
94 	return 0;
95 }
96 
97 #if CONFIG_JPEG_ENCODE
98 #include "jpeg_hal.h"
99 #endif
100 
cli_reg_write_read_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)101 static void cli_reg_write_read_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
102 {
103 	UINT32 reg_addr = 0, reg_value = 0;
104 	UINT8 optr_len = 0, optr_tab[9];
105 
106 	os_memset(optr_tab, 0, 9);
107 	os_memset(optr_tab, 0x30, 8);
108 
109 	if (os_strncmp(argv[1], "-r", 2) == 0) {
110 		if (argc != 3) {
111 			os_printf("regshow -r addr\r\n");
112 			return;
113 		}
114 
115 		optr_len = os_strlen(argv[2]);
116 		if (optr_len > 8) {
117 			os_printf("addr 0-FFFFFFFF\r\n");
118 			return;
119 		}
120 		optr_len = 8 - optr_len;
121 		os_memcpy(&optr_tab[optr_len], argv[2], os_strlen(argv[2]));
122 
123 		cli_hexstr2bin((char *)optr_tab, (u8 *)&reg_addr, 4);
124 		reg_addr = ntohl(reg_addr);
125 		os_printf("regshow R: addr:0x%08x, value:0x%08x\r\n", reg_addr, REG_READ(reg_addr));
126 	} else if (os_strncmp(argv[1], "-w", 2) == 0) {
127 		if (argc != 4) {
128 			os_printf("regshow -w addr value\r\n");
129 			return;
130 		}
131 
132 		optr_len = os_strlen(argv[2]);
133 		if (optr_len > 8) {
134 			os_printf("addr 0-FFFFFFFF\r\n");
135 			return;
136 		}
137 		optr_len = 8 - optr_len;
138 		os_memcpy(&optr_tab[optr_len], argv[2], os_strlen(argv[2]));
139 
140 		cli_hexstr2bin((char *)optr_tab, (u8 *)&reg_addr, 4);
141 		reg_addr = ntohl(reg_addr);
142 
143 
144 		os_memset(optr_tab, 0x30, 8);
145 		optr_len = os_strlen(argv[3]);
146 		if (optr_len > 8) {
147 			os_printf("value 0-FFFFFFFF\r\n");
148 			return;
149 		}
150 		optr_len = 8 - optr_len;
151 		os_memcpy(&optr_tab[optr_len], argv[3], os_strlen(argv[3]));
152 		cli_hexstr2bin((char *)optr_tab, (u8 *)&reg_value, 4);
153 		reg_value = ntohl(reg_value);
154 
155 		REG_WRITE(reg_addr, reg_value);
156 
157 #if (CLI_CFG_WIFI == 1)
158 		extern INT32 rwnx_cal_save_trx_rcbekn_reg_val(void);
159 		// when write trx and rc beken regs, updata registers save.
160 		if ((reg_addr & 0xfff0000) == 0x1050000)
161 			rwnx_cal_save_trx_rcbekn_reg_val();
162 #endif
163 
164 		os_printf("regshow W: addr:0x%08x, value:0x%08x - check:0x%08x\r\n",
165 				  reg_addr, reg_value, REG_READ(reg_addr));
166 	} else
167 		os_printf("regshow -w/r addr [value]\r\n");
168 }
169 
cli_reg_dump_help(void)170 static void cli_reg_dump_help(void)
171 {
172 	CLI_LOGI("regdump icu\n");
173 	CLI_LOGI("        pwm\n");
174 	CLI_LOGI("        gpio [index]\n");
175 	CLI_LOGI("        timer\n");
176 	CLI_LOGI("        dma [channel]\n");
177 	CLI_LOGI("        uart id\n");
178 	CLI_LOGI("        wdt\n");
179 	CLI_LOGI("        trng\n");
180 	CLI_LOGI("        efuse\n");
181 	CLI_LOGI("        adc\n");
182 	CLI_LOGI("        spi\n");
183 }
184 
cli_reg_dump_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)185 static void cli_reg_dump_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
186 {
187 	uint8_t index = os_strtoul(argv[2], NULL, 10);
188 
189 	if (argc == 1) {
190 		cli_reg_dump_help();
191 		return;
192 	}
193 
194 	if (os_strcmp(argv[1], "icu") == 0) {
195 #if (CONFIG_ICU)
196 		icu_struct_dump();
197 #endif
198 	} else if (os_strcmp(argv[1], "pwm") == 0) {
199 		pwm_struct_dump();
200 	} else if (os_strcmp(argv[1], "timer") == 0) {
201 		timer_struct_dump();
202 	} else if (os_strcmp(argv[1], "gpio") == 0) {
203 		gpio_struct_dump(index);
204 	} else if (os_strcmp(argv[1], "dma") == 0) {
205 		dma_struct_dump(index);
206 	} else if (os_strcmp(argv[1], "uart") == 0) {
207 		uart_struct_dump(index);
208 	} else if (os_strcmp(argv[1], "wdt") == 0) {
209 		wdt_struct_dump();
210 	} else if (os_strcmp(argv[1], "trng") == 0) {
211 #if (CONFIG_TRNG_SUPPORT)
212 		trng_struct_dump();
213 #endif
214 	} else if (os_strcmp(argv[1], "efuse") == 0) {
215 #if (CONFIG_EFUSE)
216 		efuse_struct_dump();
217 #endif
218 	} else if (os_strcmp(argv[1], "adc") == 0) {
219 		adc_struct_dump();
220 	} else if (os_strcmp(argv[1], "spi") == 0) {
221 		spi_struct_dump(index);
222 	} else if (os_strcmp(argv[1], "i2c") == 0) {
223 		i2c_struct_dump(index);
224 	}
225 #if CONFIG_QSPI
226 	else if (os_strcmp(argv[1], "qspi") == 0) {
227 		qspi_struct_dump();
228 	}
229 #endif
230 #if CONFIG_AON_RTC
231 	else if (os_strcmp(argv[1], "aon_rtc") == 0) {
232 		aon_rtc_struct_dump();
233 	}
234 #endif
235 #if CONFIG_JPEG_ENCODE
236 	else if (os_strcmp(argv[1], "jpeg") == 0) {
237 		jpeg_struct_dump();
238 	}
239 #endif
240 #if CONFIG_CALENDAR
241 	else if (os_strcmp(argv[1], "calendar") == 0) {
242 		calendar_struct_dump();
243 	}
244 #endif
245 #if CONFIG_FLASH
246 	else if (os_strcmp(argv[1], "flash") == 0) {
247 		flash_struct_dump();
248 	}
249 #endif
250 #if CONFIG_SDIO_HOST
251 	else if (os_strcmp(argv[1], "sdio_host") == 0) {
252 		sdio_host_struct_dump();
253 	}
254 #endif
255 	else {
256 		cli_reg_dump_help();
257 		return;
258 	}
259 }
260 
261 #define REG_CMD_CNT (sizeof(s_reg_commands) / sizeof(struct cli_command))
262 static const struct cli_command s_reg_commands[] = {
263 	{"regshow", "regshow -w/r addr [value]", cli_reg_write_read_cmd},
264 	{"regdump", "regdump {module}", cli_reg_dump_cmd},
265 };
266 
cli_reg_init(void)267 int cli_reg_init(void)
268 {
269 	return cli_register_commands(s_reg_commands, REG_CMD_CNT);
270 }
271