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
16 #if defined(USE_FULL_LL_DRIVER)
17
18 #include "stm32f4xx_ll_usart.h"
19 #include "stm32f4xx_ll_rcc.h"
20 #include "stm32f4xx_ll_bus.h"
21 #include "uart.h"
22 #include "los_task.h"
23
24 #if defined (USART1) || defined (USART2) || defined (USART3) || \
25 defined (UART4) || defined (UART5) || defined (USART6)
26
27 #define UART_NUM_MAX 6
28 #define UART_IRQ_NUM 0
29 #define UART_NUM1 1
30 #define UART_NUM2 2
31 #define UART_NUM3 3
32 #define UART_NUM4 4
33 #define UART_NUM5 5
34 #define UART_NUM6 6
35
36 static EVENT_CB_S g_uartInputEvent;
37 static BOOL g_eventInited = FALSE;
38 static RingBuffer *g_uartRingBuf[UART_NUM_MAX] = {NULL};
39 #define RING_BUFFER_SIZE 128
40
41 typedef void (*UART_FUNC_CB)(void);
USART1_IRQ_Func(void)42 static void USART1_IRQ_Func(void)
43 {
44 if (LL_USART_IsActiveFlag_RXNE(USART1)) {
45 uint8_t value;
46 value = LL_USART_ReceiveData8(USART1);
47 LL_USART_ClearFlag_RXNE(USART1);
48 RingBufWrite(g_uartRingBuf[UART_NUM1], value);
49 }
50
51 return;
52 }
53
USART1_BLOCK_IRQ_Func(void)54 static void USART1_BLOCK_IRQ_Func(void)
55 {
56 uint8_t value = 0;
57 if (LL_USART_IsActiveFlag_RXNE(USART1)) {
58 value = LL_USART_ReceiveData8(USART1);
59 LL_USART_ClearFlag_RXNE(USART1);
60 RingBufWrite(g_uartRingBuf[UART_NUM1 - 1], value);
61 }
62
63 if (LL_USART_IsActiveFlag_IDLE(USART1)) {
64 LL_USART_ClearFlag_IDLE(USART1);
65 if (LOS_EventWrite(&g_uartInputEvent, UART_NUM1) != 0) {
66 return;
67 }
68 }
69
70 return;
71 }
72
USART2_IRQ_Func(void)73 static void USART2_IRQ_Func(void)
74 {
75 if (LL_USART_IsActiveFlag_RXNE(USART2)) {
76 uint8_t value;
77 value = LL_USART_ReceiveData8(USART2);
78 LL_USART_ClearFlag_RXNE(USART2);
79 RingBufWrite(g_uartRingBuf[UART_NUM2 - 1], value);
80 }
81
82 return;
83 }
84
USART2_BLOCK_IRQ_Func(void)85 static void USART2_BLOCK_IRQ_Func(void)
86 {
87 if (LL_USART_IsActiveFlag_RXNE(USART2)) {
88 uint8_t value;
89 value = LL_USART_ReceiveData8(USART2);
90 LL_USART_ClearFlag_RXNE(USART2);
91 RingBufWrite(g_uartRingBuf[UART_NUM2 - 1], value);
92 }
93
94 LL_USART_EnableIT_IDLE(USART2);
95 if (LL_USART_IsActiveFlag_IDLE(USART2)) {
96 LL_USART_DisableIT_IDLE(USART2);
97 if (LOS_EventWrite(&g_uartInputEvent, UART_NUM2) != 0) {
98 return;
99 }
100 }
101
102 return;
103 }
104
USART3_IRQ_Func(void)105 static void USART3_IRQ_Func(void)
106 {
107 if (LL_USART_IsActiveFlag_RXNE(USART3)) {
108 uint8_t value;
109 value = LL_USART_ReceiveData8(USART3);
110 LL_USART_ClearFlag_RXNE(USART3);
111 RingBufWrite(g_uartRingBuf[UART_NUM3], value);
112 }
113
114 return;
115 }
116
USART3_BLOCK_IRQ_Func(void)117 static void USART3_BLOCK_IRQ_Func(void)
118 {
119 if (LL_USART_IsActiveFlag_RXNE(USART3)) {
120 uint8_t value;
121 value = LL_USART_ReceiveData8(USART3);
122 LL_USART_ClearFlag_RXNE(USART3);
123 RingBufWrite(g_uartRingBuf[UART_NUM3 - 1], value);
124 }
125
126 LL_USART_EnableIT_IDLE(USART3);
127 if (LL_USART_IsActiveFlag_IDLE(USART3)) {
128 LL_USART_DisableIT_IDLE(USART3);
129 if (LOS_EventWrite(&g_uartInputEvent, UART_NUM3) != 0) {
130 return;
131 }
132 }
133
134 return;
135 }
136
USART4_IRQ_Func(void)137 static void USART4_IRQ_Func(void)
138 {
139 if (LL_USART_IsActiveFlag_RXNE(UART4)) {
140 uint8_t value;
141 value = LL_USART_ReceiveData8(UART4);
142 LL_USART_ClearFlag_RXNE(UART4);
143 RingBufWrite(g_uartRingBuf[UART_NUM4], value);
144 }
145
146 return;
147 }
148
USART4_BLOCK_IRQ_Func(void)149 static void USART4_BLOCK_IRQ_Func(void)
150 {
151 if (LL_USART_IsActiveFlag_RXNE(UART4)) {
152 uint8_t value;
153 value = LL_USART_ReceiveData8(UART4);
154 LL_USART_ClearFlag_RXNE(UART4);
155 RingBufWrite(g_uartRingBuf[UART_NUM4 - 1], value);
156 }
157
158 LL_USART_EnableIT_IDLE(UART4);
159 if (LL_USART_IsActiveFlag_IDLE(UART4)) {
160 LL_USART_DisableIT_IDLE(UART4);
161 if (LOS_EventWrite(&g_uartInputEvent, UART_NUM4) != 0) {
162 return;
163 }
164 }
165
166 return;
167 }
168
USART5_IRQ_Func(void)169 static void USART5_IRQ_Func(void)
170 {
171 if (LL_USART_IsActiveFlag_RXNE(UART5)) {
172 uint8_t value;
173 value = LL_USART_ReceiveData8(UART5);
174 LL_USART_ClearFlag_RXNE(UART5);
175 RingBufWrite(g_uartRingBuf[UART_NUM5], value);
176 }
177
178 return;
179 }
180
USART5_BLOCK_IRQ_Func(void)181 static void USART5_BLOCK_IRQ_Func(void)
182 {
183 if (LL_USART_IsActiveFlag_RXNE(UART5)) {
184 uint8_t value;
185 value = LL_USART_ReceiveData8(UART5);
186 LL_USART_ClearFlag_RXNE(UART5);
187 RingBufWrite(g_uartRingBuf[UART_NUM5 - 1], value);
188 }
189
190 LL_USART_EnableIT_IDLE(UART5);
191 if (LL_USART_IsActiveFlag_IDLE(UART5)) {
192 LL_USART_DisableIT_IDLE(UART5);
193 if (LOS_EventWrite(&g_uartInputEvent, UART_NUM5) != 0) {
194 return;
195 }
196 }
197
198 return;
199 }
200
USART6_IRQ_Func(void)201 static void USART6_IRQ_Func(void)
202 {
203 if (LL_USART_IsActiveFlag_RXNE(USART6)) {
204 uint8_t value;
205 value = LL_USART_ReceiveData8(USART6);
206 RingBufWrite(g_uartRingBuf[UART_NUM6], value);
207 }
208
209 return;
210 }
211
USART6_BLOCK_IRQ_Func(void)212 static void USART6_BLOCK_IRQ_Func(void)
213 {
214 if (LL_USART_IsActiveFlag_RXNE(USART6)) {
215 uint8_t value;
216 value = LL_USART_ReceiveData8(USART6);
217 LL_USART_ClearFlag_RXNE(USART6);
218 RingBufWrite(g_uartRingBuf[UART_NUM6 - 1], value);
219 }
220
221 LL_USART_EnableIT_IDLE(USART6);
222 if (LL_USART_IsActiveFlag_IDLE(USART6)) {
223 LL_USART_DisableIT_IDLE(USART6);
224 if (LOS_EventWrite(&g_uartInputEvent, UART_NUM6) != 0) {
225 return;
226 }
227 }
228
229 return;
230 }
231
232 static UART_FUNC_CB g_funcMap[UART_NUM_MAX] = {
233 USART1_IRQ_Func,
234 USART2_IRQ_Func,
235 USART3_IRQ_Func,
236 USART4_IRQ_Func,
237 USART5_IRQ_Func,
238 USART6_IRQ_Func,
239 };
240
241 static UART_FUNC_CB g_funcBlockMap[UART_NUM_MAX] = {
242 USART1_BLOCK_IRQ_Func,
243 USART2_BLOCK_IRQ_Func,
244 USART3_BLOCK_IRQ_Func,
245 USART4_BLOCK_IRQ_Func,
246 USART5_BLOCK_IRQ_Func,
247 USART6_BLOCK_IRQ_Func,
248 };
249
USART_TxData(USART_TypeDef * UART,uint8_t * p_data,uint32_t size)250 uint32_t USART_TxData(USART_TypeDef * UART, uint8_t *p_data, uint32_t size)
251 {
252 while (size) {
253 while (!LL_USART_IsActiveFlag_TXE(UART));
254 LL_USART_TransmitData8(UART, *p_data);
255 size--;
256 p_data++;
257 }
258 while (LL_USART_IsActiveFlag_TC(UART) == RESET);
259 return size;
260 }
261
UART_IRQ_INIT(USART_TypeDef * UART,uint8_t num,uint32_t irqNum,BOOL isBlock)262 void UART_IRQ_INIT(USART_TypeDef * UART, uint8_t num, uint32_t irqNum, BOOL isBlock)
263 {
264 g_uartRingBuf[num - 1] = RingBufInit(RING_BUFFER_SIZE);
265 if (g_uartRingBuf[num - 1] == NULL) {
266 printf("RingBufInit fail!\n");
267 return;
268 }
269 if (isBlock) {
270 if (!g_eventInited) {
271 int32_t ret = LOS_EventInit(&g_uartInputEvent);
272 if (ret != LOS_OK) {
273 printf("Init uartInputEvent failed! ERROR: 0x%x\n", ret);
274 return;
275 } else {
276 g_eventInited = TRUE;
277 }
278 }
279
280 LL_USART_EnableIT_RXNE(UART);
281 LL_USART_EnableIT_IDLE(UART);
282 ArchHwiCreate(irqNum, UART_IRQ_NUM, 1, g_funcBlockMap[num - 1], NULL);
283 } else {
284 LL_USART_EnableIT_RXNE(UART);
285 ArchHwiCreate(irqNum, UART_IRQ_NUM, 1, g_funcMap[num - 1], NULL);
286 }
287
288 return;
289 }
290
UART_IRQ_DEINIT(USART_TypeDef * UART,uint32_t irqNum)291 void UART_IRQ_DEINIT(USART_TypeDef * UART, uint32_t irqNum)
292 {
293 LL_USART_DisableIT_RXNE(UART);
294 ArchHwiDelete(irqNum, NULL);
295
296 return;
297 }
298
USART_RxData(uint8_t num,uint8_t * p_data,uint32_t size,BOOL isBlock)299 uint32_t USART_RxData(uint8_t num, uint8_t *p_data, uint32_t size, BOOL isBlock)
300 {
301 uint32_t readLen = 0;
302 unsigned char data;
303 if (isBlock) {
304 (VOID)LOS_EventRead(&g_uartInputEvent, num, LOS_WAITMODE_AND | LOS_WAITMODE_CLR, LOS_WAIT_FOREVER);
305 }
306
307 while (size--) {
308 if (RingBufRead(g_uartRingBuf[num - 1], &data) == 0) {
309 *p_data = data;
310 readLen++;
311 p_data++;
312 } else {
313 break;
314 }
315 }
316
317 return readLen;
318 }
319 #endif /* USART1 || USART2 || USART3 || USART6 || UART4 || UART5 */
320
321 #endif /* USE_FULL_LL_DRIVER */
322