]> git.sur5r.net Git - freertos/blob
afd5c7af7c81febf9f26bc3a6d004dac531fa93a
[freertos] /
1 /*\r
2  * Copyright 2018 NXP\r
3  * All rights reserved.\r
4  *\r
5  *\r
6  * SPDX-License-Identifier: BSD-3-Clause\r
7  */\r
8 \r
9 #include "fsl_common.h"\r
10 #include "serial_manager.h"\r
11 \r
12 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))\r
13 #include "uart.h"\r
14 \r
15 #include "serial_port_uart.h"\r
16 \r
17 /*******************************************************************************\r
18  * Definitions\r
19  ******************************************************************************/\r
20 #ifndef NDEBUG\r
21 #if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))\r
22 #undef assert\r
23 #define assert(n)\r
24 #endif\r
25 #endif\r
26 \r
27 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))\r
28 #define SERIAL_PORT_UART_RECEIVE_DATA_LENGTH 1U\r
29 \r
30 typedef struct _serial_uart_send_state\r
31 {\r
32     serial_manager_callback_t callback;\r
33     void *callbackParam;\r
34     uint8_t *buffer;\r
35     uint32_t length;\r
36     volatile uint8_t busy;\r
37 } serial_uart_send_state_t;\r
38 \r
39 typedef struct _serial_uart_recv_state\r
40 {\r
41     serial_manager_callback_t callback;\r
42     void *callbackParam;\r
43     volatile uint8_t busy;\r
44     uint8_t readBuffer[SERIAL_PORT_UART_RECEIVE_DATA_LENGTH];\r
45 } serial_uart_recv_state_t;\r
46 #endif\r
47 \r
48 typedef struct _serial_uart_state\r
49 {\r
50 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))\r
51     serial_uart_send_state_t tx;\r
52     serial_uart_recv_state_t rx;\r
53 #endif\r
54     uint8_t usartHandleBuffer[HAL_UART_HANDLE_SIZE];\r
55 } serial_uart_state_t;\r
56 \r
57 /*******************************************************************************\r
58  * Prototypes\r
59  ******************************************************************************/\r
60 \r
61 serial_manager_status_t Serial_UartDeinit(serial_handle_t serialHandle);\r
62 \r
63 /*******************************************************************************\r
64  * Code\r
65  ******************************************************************************/\r
66 \r
67 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))\r
68 /* UART user callback */\r
69 static void Serial_UartCallback(hal_uart_handle_t handle, hal_uart_status_t status, void *userData)\r
70 {\r
71     serial_uart_state_t *serialUartHandle;\r
72     serial_manager_callback_message_t msg;\r
73 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))\r
74     hal_uart_transfer_t transfer;\r
75 #endif\r
76 \r
77     if (NULL == userData)\r
78     {\r
79         return;\r
80     }\r
81 \r
82     serialUartHandle = (serial_uart_state_t *)userData;\r
83 \r
84     if (kStatus_HAL_UartRxIdle == status)\r
85     {\r
86         if ((NULL != serialUartHandle->rx.callback))\r
87         {\r
88             msg.buffer = &serialUartHandle->rx.readBuffer[0];\r
89             msg.length = sizeof(serialUartHandle->rx.readBuffer);\r
90             serialUartHandle->rx.callback(serialUartHandle->rx.callbackParam, &msg, kStatus_SerialManager_Success);\r
91         }\r
92 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))\r
93         transfer.data = &serialUartHandle->rx.readBuffer[0];\r
94         transfer.dataSize = sizeof(serialUartHandle->rx.readBuffer);\r
95         if (kStatus_HAL_UartSuccess ==\r
96             HAL_UartTransferReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer))\r
97 #else\r
98         if (kStatus_HAL_UartSuccess ==\r
99             HAL_UartReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),\r
100                                        &serialUartHandle->rx.readBuffer[0], sizeof(serialUartHandle->rx.readBuffer)))\r
101 #endif\r
102         {\r
103             serialUartHandle->rx.busy = 1U;\r
104         }\r
105         else\r
106         {\r
107             serialUartHandle->rx.busy = 0U;\r
108         }\r
109     }\r
110     else if (kStatus_HAL_UartTxIdle == status)\r
111     {\r
112         if (serialUartHandle->tx.busy)\r
113         {\r
114             serialUartHandle->tx.busy = 0U;\r
115             if ((NULL != serialUartHandle->tx.callback))\r
116             {\r
117                 msg.buffer = serialUartHandle->tx.buffer;\r
118                 msg.length = serialUartHandle->tx.length;\r
119                 serialUartHandle->tx.callback(serialUartHandle->tx.callbackParam, &msg, kStatus_SerialManager_Success);\r
120             }\r
121         }\r
122     }\r
123     else\r
124     {\r
125     }\r
126 }\r
127 #endif\r
128 \r
129 serial_manager_status_t Serial_UartInit(serial_handle_t serialHandle, void *serialConfig)\r
130 {\r
131     serial_uart_state_t *serialUartHandle;\r
132     serial_port_uart_config_t *uartConfig;\r
133     hal_uart_config_t config;\r
134 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))\r
135     hal_uart_transfer_t transfer;\r
136 #endif\r
137 \r
138     assert(serialConfig);\r
139     assert(serialHandle);\r
140     if (SERIAL_PORT_UART_HANDLE_SIZE < sizeof(serial_uart_state_t))\r
141     {\r
142         return kStatus_SerialManager_Error;\r
143     }\r
144 \r
145     uartConfig = (serial_port_uart_config_t *)serialConfig;\r
146     serialUartHandle = (serial_uart_state_t *)serialHandle;\r
147 \r
148     config.baudRate_Bps = uartConfig->baudRate;\r
149     config.parityMode = (hal_uart_parity_mode_t)uartConfig->parityMode;\r
150     config.stopBitCount = (hal_uart_stop_bit_count_t)uartConfig->stopBitCount;\r
151     config.enableRx = uartConfig->enableRx;\r
152     config.enableTx = uartConfig->enableTx;\r
153     config.srcClock_Hz = uartConfig->clockRate;\r
154     config.instance = uartConfig->instance;\r
155 \r
156     if (kStatus_HAL_UartSuccess != HAL_UartInit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &config))\r
157     {\r
158         return kStatus_SerialManager_Error;\r
159     }\r
160 \r
161 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))\r
162 \r
163 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))\r
164     if (kStatus_HAL_UartSuccess !=\r
165         HAL_UartTransferInstallCallback(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),\r
166                                         Serial_UartCallback, serialUartHandle))\r
167 #else\r
168     if (kStatus_HAL_UartSuccess != HAL_UartInstallCallback(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),\r
169                                                            Serial_UartCallback, serialUartHandle))\r
170 #endif\r
171     {\r
172         return kStatus_SerialManager_Error;\r
173     }\r
174 \r
175     if (uartConfig->enableRx)\r
176     {\r
177         serialUartHandle->rx.busy = 1U;\r
178 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))\r
179         transfer.data = &serialUartHandle->rx.readBuffer[0];\r
180         transfer.dataSize = sizeof(serialUartHandle->rx.readBuffer);\r
181         if (kStatus_HAL_UartSuccess !=\r
182             HAL_UartTransferReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer))\r
183 #else\r
184         if (kStatus_HAL_UartSuccess !=\r
185             HAL_UartReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),\r
186                                        &serialUartHandle->rx.readBuffer[0], sizeof(serialUartHandle->rx.readBuffer)))\r
187 #endif\r
188         {\r
189             serialUartHandle->rx.busy = 0U;\r
190             return kStatus_SerialManager_Error;\r
191         }\r
192     }\r
193 #endif\r
194 \r
195     return kStatus_SerialManager_Success;\r
196 }\r
197 \r
198 serial_manager_status_t Serial_UartDeinit(serial_handle_t serialHandle)\r
199 {\r
200     serial_uart_state_t *serialUartHandle;\r
201 \r
202     assert(serialHandle);\r
203 \r
204     serialUartHandle = (serial_uart_state_t *)serialHandle;\r
205 \r
206 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))\r
207 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))\r
208     HAL_UartTransferAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));\r
209 #else\r
210     HAL_UartAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));\r
211 #endif\r
212 #endif\r
213     HAL_UartDeinit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));\r
214 \r
215 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))\r
216     serialUartHandle->tx.busy = 0U;\r
217     serialUartHandle->rx.busy = 0U;\r
218 #endif\r
219 \r
220     return kStatus_SerialManager_Success;\r
221 }\r
222 \r
223 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))\r
224 \r
225 serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)\r
226 {\r
227     serial_uart_state_t *serialUartHandle;\r
228 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))\r
229     hal_uart_transfer_t transfer;\r
230 #endif\r
231 \r
232     assert(serialHandle);\r
233     assert(buffer);\r
234     assert(length);\r
235 \r
236     serialUartHandle = (serial_uart_state_t *)serialHandle;\r
237 \r
238     if (serialUartHandle->tx.busy)\r
239     {\r
240         return kStatus_SerialManager_Busy;\r
241     }\r
242     serialUartHandle->tx.busy = 1U;\r
243 \r
244     serialUartHandle->tx.buffer = buffer;\r
245     serialUartHandle->tx.length = length;\r
246 \r
247 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))\r
248     transfer.data = buffer;\r
249     transfer.dataSize = length;\r
250     if (kStatus_HAL_UartSuccess !=\r
251         HAL_UartTransferSendNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer))\r
252 #else\r
253     if (kStatus_HAL_UartSuccess !=\r
254         HAL_UartSendNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length))\r
255 #endif\r
256     {\r
257         serialUartHandle->tx.busy = 0U;\r
258         return kStatus_SerialManager_Error;\r
259     }\r
260     return kStatus_SerialManager_Success;\r
261 }\r
262 \r
263 #else\r
264 \r
265 serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)\r
266 {\r
267     serial_uart_state_t *serialUartHandle;\r
268 \r
269     assert(serialHandle);\r
270     assert(buffer);\r
271     assert(length);\r
272 \r
273     serialUartHandle = (serial_uart_state_t *)serialHandle;\r
274 \r
275     return (serial_manager_status_t)HAL_UartSendBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),\r
276                                                          buffer, length);\r
277 }\r
278 \r
279 serial_manager_status_t Serial_UartRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)\r
280 {\r
281     serial_uart_state_t *serialUartHandle;\r
282 \r
283     assert(serialHandle);\r
284     assert(buffer);\r
285     assert(length);\r
286 \r
287     serialUartHandle = (serial_uart_state_t *)serialHandle;\r
288 \r
289     return (serial_manager_status_t)HAL_UartReceiveBlocking(\r
290         ((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length);\r
291 }\r
292 \r
293 #endif\r
294 \r
295 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))\r
296 serial_manager_status_t Serial_UartCancelWrite(serial_handle_t serialHandle)\r
297 {\r
298     serial_uart_state_t *serialUartHandle;\r
299     serial_manager_callback_message_t msg;\r
300     uint32_t primask;\r
301     uint8_t isBusy = 0U;\r
302 \r
303     assert(serialHandle);\r
304 \r
305     serialUartHandle = (serial_uart_state_t *)serialHandle;\r
306 \r
307     primask = DisableGlobalIRQ();\r
308     isBusy = serialUartHandle->tx.busy;\r
309     serialUartHandle->tx.busy = 0U;\r
310     EnableGlobalIRQ(primask);\r
311 \r
312 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))\r
313     HAL_UartTransferAbortSend(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));\r
314 #else\r
315     HAL_UartAbortSend(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));\r
316 #endif\r
317     if (isBusy)\r
318     {\r
319         if ((NULL != serialUartHandle->tx.callback))\r
320         {\r
321             msg.buffer = serialUartHandle->tx.buffer;\r
322             msg.length = serialUartHandle->tx.length;\r
323             serialUartHandle->tx.callback(serialUartHandle->tx.callbackParam, &msg, kStatus_SerialManager_Canceled);\r
324         }\r
325     }\r
326     return kStatus_SerialManager_Success;\r
327 }\r
328 \r
329 serial_manager_status_t Serial_UartInstallTxCallback(serial_handle_t serialHandle,\r
330                                                      serial_manager_callback_t callback,\r
331                                                      void *callbackParam)\r
332 {\r
333     serial_uart_state_t *serialUartHandle;\r
334 \r
335     assert(serialHandle);\r
336 \r
337     serialUartHandle = (serial_uart_state_t *)serialHandle;\r
338 \r
339     serialUartHandle->tx.callback = callback;\r
340     serialUartHandle->tx.callbackParam = callbackParam;\r
341 \r
342     return kStatus_SerialManager_Success;\r
343 }\r
344 \r
345 serial_manager_status_t Serial_UartInstallRxCallback(serial_handle_t serialHandle,\r
346                                                      serial_manager_callback_t callback,\r
347                                                      void *callbackParam)\r
348 {\r
349     serial_uart_state_t *serialUartHandle;\r
350 \r
351     assert(serialHandle);\r
352 \r
353     serialUartHandle = (serial_uart_state_t *)serialHandle;\r
354 \r
355     serialUartHandle->rx.callback = callback;\r
356     serialUartHandle->rx.callbackParam = callbackParam;\r
357 \r
358     return kStatus_SerialManager_Success;\r
359 }\r
360 \r
361 void Serial_UartIsrFunction(serial_handle_t serialHandle)\r
362 {\r
363     serial_uart_state_t *serialUartHandle;\r
364 \r
365     assert(serialHandle);\r
366 \r
367     serialUartHandle = (serial_uart_state_t *)serialHandle;\r
368 \r
369     HAL_UartIsrFunction(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));\r
370 }\r
371 #endif\r
372 \r
373 #endif\r