1 #include <platform_stdlib.h>
2 #include "FreeRTOS.h"
3 #include "task.h"
4 #include "serial_api.h"
5 #include "semphr.h"
6 #include "log_service.h"
7 #include <stdbool.h>
8 #include "hci_uart.h"
9 #include "osdep_service.h"
10 
11 #define KEY_NL			0xa // '\n'
12 #define KEY_ENTER		0xd // '\r'
13 
14 extern char log_buf[LOG_SERVICE_BUFLEN];
15 extern xSemaphoreHandle log_rx_interrupt_sema;
16 
17 #ifdef CONFIG_PLATFORM_8710C
18 extern hal_uart_adapter_t log_uart;
19 #endif
20 
21 static uint32_t baud_0 = 0;
22 //static uint32_t baud_1 = 0;
23 static uint8_t uart_parity = 0;
24 static uint8_t check_byte_num =0;
25 
26 #ifdef CONFIG_PLATFORM_8721D
27 	static void * UartBkFunc=NULL;
28 #endif
29 
bt_uart_tx(uint8_t tx_data)30 void bt_uart_tx(uint8_t tx_data)
31 {
32 #ifdef CONFIG_PLATFORM_8710C
33 	while (!hal_uart_writeable(&log_uart));
34 	hal_uart_putc(&log_uart, (u8)tx_data);
35 #endif
36 
37 
38 #ifdef CONFIG_PLATFORM_8721D
39 	while (!UART_Writable(UART2_DEV));
40 	UART_CharPut(UART2_DEV, (u8)tx_data);
41 #endif
42 
43 }
44 
bt_uart_bridge_getc(void)45 char bt_uart_bridge_getc(void)
46 {
47 #ifdef CONFIG_PLATFORM_8710C
48 	while(!hal_uart_readable(&log_uart));
49 	return (char)(hal_uart_getc (&log_uart));
50 #endif
51 #ifdef CONFIG_PLATFORM_8721D
52 	u8 Byte;
53 	while(!UART_Readable(UART2_DEV));
54 	UART_CharGet(UART2_DEV, &Byte);
55 	return Byte;
56 #endif
57 
58 }
59 
60 #ifdef CONFIG_PLATFORM_8710C
61 static int log_flag = 0;
62 extern uint32_t btc_set_single_tone_tx(uint8_t bStart);
_bt_uart_bridge_irq(u32 id,u32 event)63 static void _bt_uart_bridge_irq(u32 id,u32 event)
64 {
65 	unsigned char rc = 0;
66 	char const close_cmd_buf[]="ATM2=bridge,close";
67 	if(event==RxIrq){
68 		rc = bt_uart_bridge_getc();
69 
70 		//if(log_flag)
71 			//printf("~~~~~~~~~~~set = %02x, check_byte_num = %d\r\n",rc, check_byte_num);
72 		//bt_uart_tx((u8)rc);//Test UART rx
73 		if(check_byte_num!=17){
74 			switch (rc)
75 			{
76 				case 'A':
77 						check_byte_num=1;
78 					break;
79 				case 'T':
80 					if(check_byte_num==1)
81 						check_byte_num=2;
82 					else
83 						check_byte_num=0;
84 					break;
85 				case 'M':
86 					if(check_byte_num==2)
87 						check_byte_num=3;
88 					else
89 						check_byte_num=0;
90 					break;
91 			 	case '2':
92 					if(check_byte_num==3)
93 						check_byte_num=4;
94 					else
95 						check_byte_num=0;
96 					break;
97 				case '=':
98 					if(check_byte_num==4)
99 						check_byte_num=5;
100 					else
101 						check_byte_num=0;
102 					break;
103 				case 'b':
104 					if(check_byte_num==5)
105 						check_byte_num=6;
106 					else
107 						check_byte_num=0;
108 					break;
109 				case 'r':
110 					if(check_byte_num==6)
111 						check_byte_num=7;
112 					else
113 						check_byte_num=0;
114 					break;
115 				case 'i':
116 					if(check_byte_num==7)
117 						check_byte_num=8;
118 					else
119 						check_byte_num=0;
120 					break;
121 				case 'd':
122 					if(check_byte_num==8)
123 						check_byte_num=9;
124 					else
125 						check_byte_num=0;
126 					break;
127 				case 'g':
128 					if(check_byte_num==9)
129 						check_byte_num=10;
130 					else
131 						check_byte_num=0;
132 					break;
133 				case 'e':
134 					if(check_byte_num==10)
135 						check_byte_num=11;
136 					else if(check_byte_num==16)
137 						check_byte_num=17;
138 					else
139 						check_byte_num=0;
140 					break;
141 				case ',':
142 					if(check_byte_num==11)
143 						check_byte_num=12;
144 					else
145 						check_byte_num=0;
146 					break;
147 				case 'c':
148 					if(check_byte_num==12)
149 						check_byte_num=13;
150 					else
151 						check_byte_num=0;
152 					break;
153 				case 'l':
154 					if(check_byte_num==13)
155 						check_byte_num=14;
156 					else
157 						check_byte_num=0;
158 					break;
159 				case 'o':
160 					if(check_byte_num==14)
161 						check_byte_num=15;
162 					else
163 						check_byte_num=0;
164 					break;
165 				case 's':
166 					if(check_byte_num==15)
167 						check_byte_num=16;
168 					else
169 						check_byte_num=0;
170 					break;
171                 case 0x00: //single tone command 00
172                     if(check_byte_num==23)
173 						check_byte_num=25;
174 					else
175 						check_byte_num=0;
176                     break;
177                 case 0x01: //single tone command 01
178                     if(check_byte_num==23)
179 						check_byte_num=24;
180                     else if(check_byte_num==0)
181                         check_byte_num=20;
182 					else
183 						check_byte_num=0;
184                     break;
185                 case 0xfc: //single tone command fc
186                     if(check_byte_num==21)
187 						check_byte_num=22;
188 					else
189 						check_byte_num=0;
190                     break;
191                 case 'x': //single tone command 78
192                     if(check_byte_num==20)
193 						check_byte_num=21;
194 					else
195 						check_byte_num=0;
196 					break;
197                 case 0x04: //single tone command 04
198 					if(check_byte_num==22)
199 						check_byte_num=23;
200 					else
201 						check_byte_num=0;
202                     break;
203                 case 'L':
204 					if(check_byte_num==5)
205 						check_byte_num=30;
206 					else
207 						check_byte_num=0;
208 					break;
209                 case 'O':
210 					if(check_byte_num==30)
211 						check_byte_num=31;
212 					else
213 						check_byte_num=0;
214 					break;
215                 case 'G':
216 					if(check_byte_num==31)
217 						check_byte_num=32;
218 					else
219 						check_byte_num=0;
220 					break;
221 				default:
222 					check_byte_num=0;
223 					break;
224 			}
225 		}
226 		if(check_byte_num==17){
227 
228 			if(rc==KEY_ENTER){
229                 log_flag=0;
230 				memset(log_buf,'\0',LOG_SERVICE_BUFLEN);
231 				strncpy(log_buf,close_cmd_buf,strlen(close_cmd_buf));
232 				check_byte_num=0;
233 				set_hci_uart_out(false);
234 				rtw_up_sema_from_isr((_sema*)&log_rx_interrupt_sema);
235 			}
236 
237 		}
238 		else if(check_byte_num==32){
239 			log_flag += 1;
240 		}
241 		else if(check_byte_num==24){
242 			btc_set_single_tone_tx(1);
243 		}
244 		else if(check_byte_num==25){
245 			btc_set_single_tone_tx(0);
246 		}
247 		else
248 		{
249 			hci_uart_tx_bridge(rc);
250 		}
251 	}
252 }
253 #endif
254 
255 #ifdef CONFIG_PLATFORM_8721D
_bt_uart_bridge_irq(void * data)256 static void _bt_uart_bridge_irq(void * data)
257 {
258 	(void)data;
259 	volatile u8 reg_iir;
260 	u8 IntId;
261 	u32 RegValue;
262 	UART_TypeDef * pLOG_UART = UART2_DEV;
263 	unsigned char rc = 0;
264 	const char close_cmd_buf[]="ATM2=bridge,close";
265 
266 	reg_iir = UART_IntStatus(pLOG_UART);
267 	if ((reg_iir & RUART_IIR_INT_PEND) != 0) {
268 		// No pending IRQ
269 		return ;
270 	}
271 
272 	IntId = (reg_iir & RUART_IIR_INT_ID) >> 1;
273 
274 	if(IntId==RUART_RECEIVER_DATA_AVAILABLE || IntId==RUART_TIME_OUT_INDICATION) {
275 		RegValue = (UART_LineStatusGet(pLOG_UART));
276 		(void)RegValue;
277 		rc = bt_uart_bridge_getc();
278 		if(check_byte_num!=4){
279 			switch (rc)
280 			{
281 				case 'A':
282 						check_byte_num=1;
283 					break;
284 				case 'T':
285 					if(check_byte_num==1)
286 						check_byte_num=2;
287 					else
288 						check_byte_num=0;
289 					break;
290 				case 'M':
291 					if(check_byte_num==2)
292 						check_byte_num=3;
293 					else
294 						check_byte_num=0;
295 					break;
296 			 	case '2':
297 					if(check_byte_num==3)
298 						check_byte_num=4;
299 					else
300 						check_byte_num=0;
301 					break;
302 				default:
303 					check_byte_num=0;
304 					break;
305 			}
306 		}
307 		if(check_byte_num==4){
308 
309 			if(rc==KEY_ENTER){
310 				memset(log_buf,'\0',LOG_SERVICE_BUFLEN);
311 				strncpy(log_buf,close_cmd_buf,strlen(close_cmd_buf));
312 				check_byte_num=0;
313 				set_hci_uart_out(false);
314 				UserIrqFunTable[UART_LOG_IRQ_LP] = (IRQ_FUN)UartBkFunc;
315 				rtw_up_sema_from_isr((_sema*)&log_rx_interrupt_sema);
316 			}
317 		}
318 		else
319 		{
320 			hci_uart_tx_bridge(rc);
321 		}
322 	}
323 
324 	return ;
325 }
326 #endif
327 
328 
bt_uart_bridge_set(uint32_t baudrate_0,uint32_t baudrate_1,uint8_t parity,uint8_t debug)329 void bt_uart_bridge_set(uint32_t baudrate_0, uint32_t baudrate_1, uint8_t parity, uint8_t debug)
330 {
331 	(void) baudrate_1;
332 	(void) debug;
333 	baud_0 = baudrate_0;
334 	//baud_1 = baudrate_1;
335 	uart_parity = parity;
336 }
337 
bt_uart_bridge_close(void)338 void bt_uart_bridge_close(void)
339 {
340 	baud_0 = 0;
341 	//baud_1 = 0;
342 	uart_parity = 0;
343 #ifdef CONFIG_PLATFORM_8721D
344 	ConfigDebugClose=0;
345 #endif
346 
347 }
348 
bt_uart_bridge_open(PinName tx,PinName rx)349 void bt_uart_bridge_open(PinName tx, PinName rx)
350 {
351 	(void)tx;
352 	(void)rx;
353 #ifdef CONFIG_PLATFORM_8710C
354 	//serial_init(&uart_0_sobj, tx, rx);
355 	hal_uart_reset_rx_fifo(&log_uart);
356 
357 	if(baud_0 != 0)
358 		hal_uart_set_baudrate(&log_uart, baud_0);
359 	if(uart_parity == 1)
360 		hal_uart_set_format(&log_uart, 8, ParityOdd, 1);
361 	else if(uart_parity == 2)
362 		hal_uart_set_format(&log_uart, 8, ParityEven, 1);
363 
364 	hal_uart_rxind_hook(&log_uart,_bt_uart_bridge_irq,0,0);
365 #endif
366 
367 #ifdef CONFIG_PLATFORM_8721D
368 	#if 1
369 	UART_TypeDef * LOG_UART=UART2_DEV;
370 
371 	/*reset rx fifo*/
372 	UART_ClearRxFifo(UART2_DEV);
373 
374 	if(baud_0 != 0)
375 		UART_SetBaud(UART2_DEV, baud_0);
376 	if(uart_parity==0) {
377 		LOG_UART->LCR &= ~ BIT(3);
378 	}else if(uart_parity == 1) {
379 		LOG_UART->LCR |=  BIT(3);
380 		LOG_UART->LCR &= ~ BIT(4);
381 	}else if(uart_parity == 2) {
382 		LOG_UART->LCR |=  BIT(3);
383 		LOG_UART->LCR |= BIT(4);
384 	}
385 
386 	UartBkFunc = (void *)UserIrqFunTable[UART_LOG_IRQ_LP];
387 	ConfigDebugClose=1;
388 	/* Register Log Uart Callback function */
389 	InterruptRegister((IRQ_FUN) _bt_uart_bridge_irq, UART_LOG_IRQ_LP, (u32)NULL, 10);
390 	InterruptEn(UART_LOG_IRQ_LP, 10);
391 	#endif
392 //#error //add UART init and hook _bt_uart_bridge_irq function here, use same API:_bt_uart_bridge_irq
393 #endif
394 	set_hci_uart_out(true);
395 }
396 
397