1 /*
2  *  Routines to access hardware
3  *
4  *  Copyright (c) 2013 Realtek Semiconductor Corp.
5  *
6  *  This module is a confidential and proprietary property of RealTek and
7  *  possession or use of this module requires written permission of RealTek.
8  */
9 
10 #include "ameba_soc.h"
11 #include "osdep_service.h"
12 #include <stdarg.h>
13 #include "strproc.h"
14 
15 #if defined ( __ICCARM__ )
16 #pragma section=".cmd.table.data"
17 
18 SECTION(".data") u8* __cmd_table_start__;
19 SECTION(".data") u8* __cmd_table_end__;
20 #endif
21 
22 extern volatile UART_LOG_CTL		shell_ctl;
23 extern UART_LOG_BUF				shell_buf;
24 #ifdef CONFIG_UART_LOG_HISTORY
25 extern u8							shell_history_cmd[UART_LOG_HISTORY_LEN][UART_LOG_CMD_BUFLEN];
26 #endif
27 
28 extern COMMAND_TABLE    shell_cmd_table[];
29 
30 _sema shell_sema;
31 
32 #if defined(CONFIG_WIFI_NORMAL)
33 #if SUPPORT_LOG_SERVICE
34 extern char log_buf[LOG_SERVICE_BUFLEN];
35 extern _sema log_rx_interrupt_sema;
36 extern void log_service_init(void);
37 #endif
38 #endif
39 
shell_get_cmd(char * argv)40 static monitor_cmd_handler shell_get_cmd(char* argv)
41 {
42 	PCOMMAND_TABLE  pCmdTbl = shell_ctl.pCmdTbl;
43 	u32 CmdCnt = 0;
44 	u32 CmdNum = shell_ctl.CmdTblSz;
45 	monitor_cmd_handler cmd_handler = NULL;
46 
47 	for (CmdCnt = 0; CmdCnt< CmdNum; CmdCnt++) {
48 		if ((_stricmp(argv, (const char *)pCmdTbl[CmdCnt].cmd))==0){
49 			cmd_handler = pCmdTbl[CmdCnt].func;
50 			break;
51 		}
52 	}
53 
54 	return cmd_handler;
55 }
56 
shell_give_sema(void)57 static void shell_give_sema(void)
58 {
59 	if (shell_ctl.shell_task_rdy) {
60 		portBASE_TYPE taskWoken = pdFALSE;
61 		rtw_up_sema_from_isr(&shell_sema);
62 	}
63 }
64 //======================================================
shell_cmd_exec(u8 argc,u8 ** argv)65 u32 shell_cmd_exec(u8  argc, u8  **argv)
66 {
67 	/* To avoid gcc warnings */
68 	( void ) argc;
69 	( void ) *argv;
70 
71 #if defined(CONFIG_WIFI_NORMAL)
72 	if(argc == 0) {
73 		return FALSE;
74 	}
75 
76 #if SUPPORT_LOG_SERVICE
77 	rtw_up_sema((_sema *)&log_rx_interrupt_sema);
78 #endif
79 	shell_array_init(argv[0], sizeof(argv[0]) ,0);
80 
81 #endif
82 	return TRUE;
83 }
84 
85 //======================================================
shell_cmd_exec_ram(u8 argc,u8 ** argv)86 static u32 shell_cmd_exec_ram(u8  argc, u8  **argv)
87 {
88 	monitor_cmd_handler cmd_handler = NULL;
89 
90 	if (argc > 0){
91 		cmd_handler = shell_get_cmd((char *)argv[0]);
92 
93 		if (cmd_handler != NULL) {
94 			_strupr(argv[0]);
95 			cmd_handler((argc-1) , (argv+1));
96 			shell_array_init(argv[0], sizeof(argv[0]) ,0);
97 			return TRUE;
98 		}
99 	}
100 
101 	//(*pUartLogBuf).BufCount = 0;
102 	//shell_array_init(&(*pUartLogBuf).UARTLogBuf[0], UART_LOG_CMD_BUFLEN, '\0');
103 
104 	return FALSE;
105 }
106 
107 
shell_task_ram(VOID * Data)108 static VOID shell_task_ram(VOID *Data)
109 {
110 	/* To avoid gcc warnings */
111 	( void ) Data;
112 
113 	u32 ret = TRUE;
114 
115 	//4 Set this for UartLog check cmd history
116 	shell_ctl.shell_task_rdy = 1;
117 	shell_ctl.BootRdy = 1;
118 
119 	do{
120 		rtw_down_sema(&shell_sema);
121 
122 		if (shell_ctl.ExecuteCmd) {
123 			u8  argc = 0;
124 			u8  **argv;
125 			PUART_LOG_BUF   pUartLogBuf = shell_ctl.pTmpLogBuf;
126 #if defined(CONFIG_WIFI_NORMAL)
127 #if SUPPORT_LOG_SERVICE
128 			_strncpy(log_buf, (const char*)&(*pUartLogBuf).UARTLogBuf[0], LOG_SERVICE_BUFLEN-1);
129 #endif
130 #endif
131 
132 			argc = shell_get_argc((const u8*)&((*pUartLogBuf).UARTLogBuf[0]));
133 			argv = (u8**)shell_get_argv((const u8*)&((*pUartLogBuf).UARTLogBuf[0])); /* UARTLogBuf will be changed */
134 
135 			if (argc > 0) {
136 				/* FPGA Verification */
137 				ret = shell_cmd_exec_ram(argc, argv);
138 
139 				/* normal for LOG service */
140 				if (ret == FALSE) {
141 					ret = shell_cmd_exec(argc, argv);
142 				}
143 
144 				(*pUartLogBuf).BufCount = 0;
145 				shell_array_init(&(*pUartLogBuf).UARTLogBuf[0], UART_LOG_CMD_BUFLEN, '\0');
146 			} else {
147 				/*In some exception case, even if argc parsed is 0(when the first character value in log buffer is '\0'),
148 				log buffer may not be empty and log buffer counter may not be zero. If not clean log buffer and counter
149 				, some error will happen. Therefore, clean log buffer and initialize buffer counter when it occurs.*/
150 				if((*pUartLogBuf).BufCount != 0) {
151 					(*pUartLogBuf).BufCount = 0;
152 					shell_array_init(&(*pUartLogBuf).UARTLogBuf[0], UART_LOG_CMD_BUFLEN, '\0');
153 				}
154 				CONSOLE_AMEBA();
155 			}
156 			shell_ctl.ExecuteCmd = _FALSE;
157 
158 			pmu_set_sysactive_time(10000);
159 		}
160 	}while(1);
161 }
162 
shell_init_ram(VOID)163 VOID shell_init_ram(VOID)
164 {
165 #if defined(CONFIG_WIFI_NORMAL)
166 #if SUPPORT_LOG_SERVICE
167 //	log_service_init();
168 #endif
169 #endif
170 
171 #ifdef AMEBAD_TODO
172 	LOGUART_SetBaud_FromFlash();
173 #endif
174 
175 #if defined ( __ICCARM__ )
176 	__cmd_table_start__ = (u8*)__section_begin(".cmd.table.data");
177 	__cmd_table_end__ = (u8*)__section_end(".cmd.table.data");
178 #endif
179 
180 	shell_ctl.pCmdTbl = (PCOMMAND_TABLE)__cmd_table_start__;
181 	shell_ctl.CmdTblSz = ((__cmd_table_end__ - __cmd_table_start__) / sizeof(COMMAND_TABLE));
182 
183 	shell_ctl.ExecuteCmd = _FALSE;
184 	shell_ctl.ExecuteEsc= _TRUE;//don't check Esc anymore
185 	shell_ctl.GiveSema = shell_give_sema;
186 
187 
188 	/* Create a Semaphone */
189 	rtw_init_sema(&shell_sema, 0);
190 	rtw_down_timeout_sema(&shell_sema, 1/portTICK_RATE_MS);
191 
192 
193 	aos_task_new("LOGUART_TASK", shell_task_ram, NULL, 1024*4);
194 #if 0
195 	if (pdTRUE != xTaskCreate( shell_task_ram, (const char * const)"LOGUART_TASK", 1024*2,
196 		NULL, 5 , NULL))
197 	{
198 		DiagPrintf("Create Log UART Task Err!!\n");
199 	}
200 #endif
201 
202 	//CONSOLE_AMEBA();
203 }
204 
shell_switch_ipc_int(VOID * Data,u32 IrqStatus,u32 ChanNum)205 void shell_switch_ipc_int(VOID *Data, u32 IrqStatus, u32 ChanNum)
206 {
207 	/* To avoid gcc warnings */
208 	( void ) Data;
209 	( void ) IrqStatus;
210 	( void ) ChanNum;
211 
212 	u8 CpuId = IPCM0_DEV->IPCx_CPUID;
213 
214 	if (CpuId == 1) {
215 		DBG_8195A("KM4 shell\n");
216 		InterruptEn(UART_LOG_IRQ, 10);
217 		pmu_set_sysactive_time(1000);
218 	} else {
219 		DBG_8195A("KM0 shell\n");
220 		InterruptEn(UART_LOG_IRQ_LP, 10);
221 	}
222 }
223