• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2022 Huawei Technologies Co., Ltd. All rights reserved.
3  *
4  * UniProton is licensed under Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *          http://license.coscl.org.cn/MulanPSL2
8  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
9  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
10  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
11  * See the Mulan PSL v2 for more details.
12  * Create: 2009-12-22
13  * Description: UniProton hi3093 demo
14  */
15 #include <stdarg.h>
16 #include "prt_typedef.h"
17 #include "cpu_config.h"
18 #include "securec.h"
19 #include "serial.h"
20 #include "prt_hwi.h"
21 
22 typedef U32 (*PrintFunc)(const char *format, va_list vaList);
23 #define OS_MAX_SHOW_LEN 0x200
24 
uart_recv(U8 * value)25 U32 uart_recv(U8 *value)
26 {
27     S32 ret = 0;
28     ret = serial_getc();
29     if (ret == -EAGAIN) {
30         return EAGAIN;
31     }
32 
33     *value = (U8)ret;
34 
35     return 0;
36 }
37 
uart_recv_hwi(void)38 void uart_recv_hwi(void)
39 {
40     U8 data;
41     while (uart_recv(&data) == 0) {
42         /*Do not printf in this task*/
43     }
44 }
45 
PRT_UartInit(void)46 U32 PRT_UartInit(void)
47 {
48 
49     U32 ret;
50     (void)PRT_HwiDelete(CCORE_SYS_UART4_INTID);
51 
52     ret = PRT_HwiSetAttr(CCORE_SYS_UART4_INTID, 12, OS_HWI_MODE_ENGROSS);
53     if (ret != OS_OK) {
54         return ret;
55     }
56 
57     ret = PRT_HwiCreate(CCORE_SYS_UART4_INTID, (HwiProcFunc)uart_recv_hwi, CCORE_SYS_UART4_INTID);
58     if (ret != OS_OK) {
59         return ret;
60     }
61 
62     ret = PRT_HwiEnable(CCORE_SYS_UART4_INTID);
63     if (ret != OS_OK) {
64         return ret;
65     }
66 
67     serial_init(&g_uart_cfg, &g_uart_ops);
68 
69     return OS_OK;
70 }
71 
uart_poll_send(unsigned char ch)72 void uart_poll_send(unsigned char ch)
73 {
74     serial_putc((char)ch);
75 }
76 
TestPutc(unsigned char ch)77 void TestPutc(unsigned char ch)
78 {
79     uart_poll_send(ch);
80     if (ch == '\n') {
81         uart_poll_send('\r');
82     }
83 }
84 
TestPrintf(const char * format,va_list vaList)85 int TestPrintf(const char *format, va_list vaList)
86 {
87     int len;
88     char buff[OS_MAX_SHOW_LEN] = {0};
89     char *str = buff;
90 
91     len = vsnprintf_s(buff, OS_MAX_SHOW_LEN, OS_MAX_SHOW_LEN, format, vaList);
92     if (len == -1) {
93         return len;
94     }
95 
96     while (*str != '\0') {
97         TestPutc(*str);
98         str++;
99     }
100 
101     return OS_OK;
102 }
103 
PRT_Printf(const char * format,...)104 U32 PRT_Printf(const char *format, ...)
105 {
106     va_list vaList;
107     S32 count;
108 
109     va_start(vaList, format);
110     count = TestPrintf(format, vaList);
111     va_end(vaList);
112 
113     return count;
114 }
115 
PRT_PrintfInit()116 U32 PRT_PrintfInit()
117 {
118     U32 ret;
119 
120     ret = PRT_UartInit();
121     if (ret != OS_OK) {
122         return ret;
123     }
124 
125     return OS_OK;
126 }
127