• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Talkweb Co., Ltd.
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 #ifdef LOSCFG_DRIVERS_HDF_PLATFORM_UART
16 #include <hdf_log.h>
17 #include <uart_if.h>
18 #include "los_task.h"
19 #define HDF_SHELL_STACK_SIZE 0x1000
20 #define HDF_SHELL_TASK_NAME "hdf_shell_task"
21 #define HDF_SHELL_TASK_PRIORITY 26
22 #define MAX_BUF_SIZE 1024
23 uint8_t rbuf[MAX_BUF_SIZE] = {0};
24 DevHandle handle = NULL;
25 #define UART_DEBUG_SHELL_PORT 1
26 #endif
27 
28 #include "stdio.h"
29 #include "los_config.h"
30 #include "los_reg.h"
31 #include "los_interrupt.h"
32 #include "los_event.h"
33 #include "stm32f4xx_hal_uart.h"
34 #include "uart.h"
35 
36 #ifdef LOSCFG_DRIVERS_HDF_PLATFORM_UART
UartPutc(INT32 ch,VOID * file)37 INT32 UartPutc(INT32 ch, VOID *file)
38 {
39     char RL = '\r';
40     if (handle == NULL) {
41         if (ch =='\n') {
42             HAL_UART_Transmit(&huart1, &RL, 1, 0xFFFF);
43         }
44         return HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
45     } else {
46         if (ch == '\n') {
47             UartWrite(handle, &RL, 1);
48         }
49         return UartWrite(handle, (uint8_t *)&ch, 1);
50     }
51 }
52 #else
UartPutc(INT32 ch,VOID * file)53 INT32 UartPutc(INT32 ch, VOID *file)
54 {
55     char RL = '\r';
56     if (ch =='\n') {
57         HAL_UART_Transmit(&huart1, &RL, 1, 0xFFFF);
58     }
59 
60     return HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
61 }
62 #endif
63 
64 #define CN_RCV_RING_BUFLEN  128
65 static RingBuffer *g_debugRingBuf;
66 /**
67  * @brief: use this function to read data from ring buffer
68  * @input: ringBuf, the ring buf to be read
69  * @input: data, data to be read storaged
70  * @return: 0 success while -1 failed
71  */
RingBufRead(RingBuffer * buf,unsigned char * data)72 int RingBufRead(RingBuffer *buf, unsigned char *data)
73 {
74     if (buf->dataLen == 0) {
75         return -1;
76     } else {
77         *data = buf->buf[buf->posR];
78         buf->posR = (buf->posR + 1) % buf->size;
79         buf->dataLen--;
80         return 0;
81     }
82 }
83 
84 /**
85  * @brief: use this function to write data to ring buffer
86  * @input: ringBuf, the ring buf to be read
87  * @input: data, data to be written
88  * @return: 0 success while -1 failed
89  */
RingBufWrite(RingBuffer * buf,unsigned char data)90 int RingBufWrite(RingBuffer *buf, unsigned char data)
91 {
92     if (buf->dataLen == buf->size) {
93         return -1;
94     } else {
95         buf->buf[buf->posW] = data;
96         buf->posW = (buf->posW + 1) % buf->size;
97         buf->dataLen++;
98         return 0;
99     }
100 }
101 
RingBufWriteMore(RingBuffer * buf,unsigned char * data,uint32_t size)102 int RingBufWriteMore(RingBuffer *buf, unsigned char* data, uint32_t size)
103 {
104     for (uint32_t i = 0; i < size; i++) {
105         if (RingBufWrite(buf, data[i]) !=0) {
106             break;
107         }
108     }
109 
110     return 0;
111 }
112 
113 /**
114  * @brief: use this function to make a ringbuffer
115  * @return: the ring buffer maked, NULL failed while others success
116  */
RingBufInit(int size)117 RingBuffer* RingBufInit(int size)
118 {
119     RingBuffer *buf;
120     if (size <= 0) {
121         return NULL;
122     }
123     buf = (RingBuffer*)malloc(size + sizeof(RingBuffer));
124     if (buf != NULL) {
125         buf->buf = (unsigned char*) buf + sizeof(RingBuffer);
126         buf->dataLen = 0;
127         buf->posR = 0;
128         buf->posW = 0;
129         buf->size = size;
130     }
131     return buf;
132 }
133 
UartGetc(void)134 uint8_t UartGetc(void)
135 {
136     unsigned char data;
137     if (RingBufRead(g_debugRingBuf, &data) == 0) {
138         return (int) data;
139     } else {
140         return 0;
141     }
142 }
143 
144 #ifdef LOSCFG_DRIVERS_HDF_PLATFORM_UART
InitDebugShellUart(uint32_t port)145 static INT32 InitDebugShellUart(uint32_t port)
146 {
147     handle = UartOpen(port);
148     if (handle == NULL) {
149         return 1;
150     }
151 
152     return 0;
153 }
HdfShellTaskEntry(void)154 static void HdfShellTaskEntry(void)
155 {
156     while (1) {
157         memset_s(rbuf, MAX_BUF_SIZE, 0, MAX_BUF_SIZE);
158         int32_t readLen = UartRead(handle, rbuf, MAX_BUF_SIZE);
159         if (readLen < 0) {
160             return;
161         } else {
162             for (int i = 0; i < readLen; i++) {
163                 (void)RingBufWrite(g_debugRingBuf, rbuf[i]);
164                 (void)LOS_EventWrite(&g_shellInputEvent, 0x1);
165             }
166         }
167     }
168 
169     UartClose(handle);
170 
171     return;
172 }
173 
StartUartShell(void)174 void StartUartShell(void)
175 {
176     UINT32 uwRet;
177     UINT32 taskID;
178     TSK_INIT_PARAM_S stTask = {0};
179 
180     stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)HdfShellTaskEntry;
181     stTask.uwStackSize = HDF_SHELL_STACK_SIZE;
182     stTask.pcName = HDF_SHELL_TASK_NAME;
183     stTask.usTaskPrio = HDF_SHELL_TASK_PRIORITY; /* Os task priority is 26 */
184     uwRet = LOS_TaskCreate(&taskID, &stTask);
185     if (uwRet != LOS_OK) {
186         HDF_LOGE("Task1 create failed\n");
187     }
188 }
189 
ShellUartInit(VOID)190 VOID ShellUartInit(VOID)
191 {
192     LosShellInit();
193     g_debugRingBuf = RingBufInit(CN_RCV_RING_BUFLEN);
194     if (g_debugRingBuf == NULL) {
195         return;
196     }
197 
198     handle = UartOpen(UART_DEBUG_SHELL_PORT);
199     if (handle == NULL) {
200         HDF_LOGE("UartOpen: failed!\n");
201         return;
202     }
203     StartUartShell();
204 }
205 #else
huart1_irq(void)206 static void huart1_irq(void)
207 {
208     if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET) {
209         unsigned char value;
210         value = (uint8_t) (huart1.Instance->DR & 0x00FF);
211         RingBufWrite(g_debugRingBuf, value);
212     } else if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET) {
213         __HAL_UART_CLEAR_IDLEFLAG(&huart1);
214         (void)LOS_EventWrite(&g_shellInputEvent, 0x1);
215     }
216 }
217 
ShellUartInit(VOID)218 VOID ShellUartInit(VOID)
219 {
220     LosShellInit();
221     g_debugRingBuf = RingBufInit(CN_RCV_RING_BUFLEN);
222     if (g_debugRingBuf == NULL) {
223         printf("RingBufInit fail!\n");
224         return;
225     }
226     LOS_HwiCreate(USART1_IRQn, 0, 1, (HWI_PROC_FUNC)huart1_irq, 0);
227     __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
228     __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
229 }
230 #endif
231