1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 #include "rec_uart.h"
5 #include "serial_api.h"
6 
7 #define UART_BUFF_LEN 1032
8 
9 static unsigned char uart_buf[UART_BUFF_LEN];
10 static unsigned int  uart_buf_write_index = 0;
11 static unsigned int  uart_buf_read_index  = 0;
12 
rec_uart_printf(const char * fmt,...)13 void rec_uart_printf(const char *fmt, ...)
14 {
15 	(void)VSprintf(0, fmt, ((const int *)&fmt)+1);
16     return;
17 }
18 
rec_loguart_irq()19 static void rec_loguart_irq()
20 {
21     unsigned char nc   = 0;
22     unsigned int IrqEn = DiagGetIsrEnReg();
23 
24     DiagSetIsrEnReg(0);
25 
26     nc = DiagGetChar(0);
27 
28     uart_buf[uart_buf_write_index] = nc;
29     uart_buf_write_index ++;
30     if(uart_buf_write_index == UART_BUFF_LEN) {
31         uart_buf_write_index = 0;
32     }
33 
34     DiagSetIsrEnReg(IrqEn);
35 }
36 
uart0_recv_byte(unsigned char * c)37 static int uart0_recv_byte(unsigned char *c)
38 {
39     unsigned int IrqEn = DiagGetIsrEnReg();
40 
41     DiagSetIsrEnReg(0);
42 
43     if(uart_buf_read_index != uart_buf_write_index) {
44         *c = uart_buf[uart_buf_read_index];
45         uart_buf_read_index ++;
46         if(uart_buf_read_index == UART_BUFF_LEN) {
47             uart_buf_read_index = 0;
48         }
49         DiagSetIsrEnReg(IrqEn);
50 
51         return 1;
52     }
53 
54     DiagSetIsrEnReg(IrqEn);
55 
56     return 0;
57 }
58 
uart_recv_byte(unsigned char * c)59 unsigned char uart_recv_byte(unsigned char *c)
60 {
61     unsigned char tc = 0;
62 
63     if(uart0_recv_byte(&tc)) {
64         *c = tc;
65         return 1;
66     }
67 
68     return 0;
69 }
70 
rec_uart_recv_byte(unsigned char * c)71 unsigned char rec_uart_recv_byte(unsigned char *c)
72 {
73     return uart_recv_byte(c);
74 }
75 
rec_uart_init()76 void rec_uart_init()
77 {
78     memset(uart_buf, 0xFF, UART_BUFF_LEN);
79     DIAG_UartReInit((IRQ_FUN) rec_loguart_irq);
80 }
81 
rec_uart_deinit()82 void rec_uart_deinit()
83 {
84 
85 }
86 
rec_uart_send_byte(unsigned char buff)87 void rec_uart_send_byte(unsigned char buff)
88 {
89     DiagPutChar(buff);
90 }
91 
rec_uart_send_string(char * buff)92 void rec_uart_send_string(char *buff)
93 {
94     int i;
95 
96     if(NULL == buff) {
97         return;
98     }
99 
100     rec_uart_printf(buff);
101 
102     return;
103 }
104 
105