3 * All rights reserved.
\r
6 * SPDX-License-Identifier: BSD-3-Clause
\r
9 #include "fsl_common.h"
\r
10 #include "serial_manager.h"
\r
12 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
\r
15 #include "serial_port_uart.h"
\r
17 /*******************************************************************************
\r
19 ******************************************************************************/
\r
21 #if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
\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
30 typedef struct _serial_uart_send_state
\r
32 serial_manager_callback_t callback;
\r
33 void *callbackParam;
\r
36 volatile uint8_t busy;
\r
37 } serial_uart_send_state_t;
\r
39 typedef struct _serial_uart_recv_state
\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
48 typedef struct _serial_uart_state
\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
54 uint8_t usartHandleBuffer[HAL_UART_HANDLE_SIZE];
\r
55 } serial_uart_state_t;
\r
57 /*******************************************************************************
\r
59 ******************************************************************************/
\r
61 serial_manager_status_t Serial_UartDeinit(serial_handle_t serialHandle);
\r
63 /*******************************************************************************
\r
65 ******************************************************************************/
\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
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
77 if (NULL == userData)
\r
82 serialUartHandle = (serial_uart_state_t *)userData;
\r
84 if (kStatus_HAL_UartRxIdle == status)
\r
86 if ((NULL != serialUartHandle->rx.callback))
\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
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
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
103 serialUartHandle->rx.busy = 1U;
\r
107 serialUartHandle->rx.busy = 0U;
\r
110 else if (kStatus_HAL_UartTxIdle == status)
\r
112 if (serialUartHandle->tx.busy)
\r
114 serialUartHandle->tx.busy = 0U;
\r
115 if ((NULL != serialUartHandle->tx.callback))
\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
129 serial_manager_status_t Serial_UartInit(serial_handle_t serialHandle, void *serialConfig)
\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
138 assert(serialConfig);
\r
139 assert(serialHandle);
\r
140 if (SERIAL_PORT_UART_HANDLE_SIZE < sizeof(serial_uart_state_t))
\r
142 return kStatus_SerialManager_Error;
\r
145 uartConfig = (serial_port_uart_config_t *)serialConfig;
\r
146 serialUartHandle = (serial_uart_state_t *)serialHandle;
\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
156 if (kStatus_HAL_UartSuccess != HAL_UartInit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &config))
\r
158 return kStatus_SerialManager_Error;
\r
161 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
\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
168 if (kStatus_HAL_UartSuccess != HAL_UartInstallCallback(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
\r
169 Serial_UartCallback, serialUartHandle))
\r
172 return kStatus_SerialManager_Error;
\r
175 if (uartConfig->enableRx)
\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
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
189 serialUartHandle->rx.busy = 0U;
\r
190 return kStatus_SerialManager_Error;
\r
195 return kStatus_SerialManager_Success;
\r
198 serial_manager_status_t Serial_UartDeinit(serial_handle_t serialHandle)
\r
200 serial_uart_state_t *serialUartHandle;
\r
202 assert(serialHandle);
\r
204 serialUartHandle = (serial_uart_state_t *)serialHandle;
\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
210 HAL_UartAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
\r
213 HAL_UartDeinit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
\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
220 return kStatus_SerialManager_Success;
\r
223 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
\r
225 serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
\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
232 assert(serialHandle);
\r
236 serialUartHandle = (serial_uart_state_t *)serialHandle;
\r
238 if (serialUartHandle->tx.busy)
\r
240 return kStatus_SerialManager_Busy;
\r
242 serialUartHandle->tx.busy = 1U;
\r
244 serialUartHandle->tx.buffer = buffer;
\r
245 serialUartHandle->tx.length = length;
\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
253 if (kStatus_HAL_UartSuccess !=
\r
254 HAL_UartSendNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length))
\r
257 serialUartHandle->tx.busy = 0U;
\r
258 return kStatus_SerialManager_Error;
\r
260 return kStatus_SerialManager_Success;
\r
265 serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
\r
267 serial_uart_state_t *serialUartHandle;
\r
269 assert(serialHandle);
\r
273 serialUartHandle = (serial_uart_state_t *)serialHandle;
\r
275 return (serial_manager_status_t)HAL_UartSendBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
\r
279 serial_manager_status_t Serial_UartRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
\r
281 serial_uart_state_t *serialUartHandle;
\r
283 assert(serialHandle);
\r
287 serialUartHandle = (serial_uart_state_t *)serialHandle;
\r
289 return (serial_manager_status_t)HAL_UartReceiveBlocking(
\r
290 ((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length);
\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
298 serial_uart_state_t *serialUartHandle;
\r
299 serial_manager_callback_message_t msg;
\r
301 uint8_t isBusy = 0U;
\r
303 assert(serialHandle);
\r
305 serialUartHandle = (serial_uart_state_t *)serialHandle;
\r
307 primask = DisableGlobalIRQ();
\r
308 isBusy = serialUartHandle->tx.busy;
\r
309 serialUartHandle->tx.busy = 0U;
\r
310 EnableGlobalIRQ(primask);
\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
315 HAL_UartAbortSend(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
\r
319 if ((NULL != serialUartHandle->tx.callback))
\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
326 return kStatus_SerialManager_Success;
\r
329 serial_manager_status_t Serial_UartInstallTxCallback(serial_handle_t serialHandle,
\r
330 serial_manager_callback_t callback,
\r
331 void *callbackParam)
\r
333 serial_uart_state_t *serialUartHandle;
\r
335 assert(serialHandle);
\r
337 serialUartHandle = (serial_uart_state_t *)serialHandle;
\r
339 serialUartHandle->tx.callback = callback;
\r
340 serialUartHandle->tx.callbackParam = callbackParam;
\r
342 return kStatus_SerialManager_Success;
\r
345 serial_manager_status_t Serial_UartInstallRxCallback(serial_handle_t serialHandle,
\r
346 serial_manager_callback_t callback,
\r
347 void *callbackParam)
\r
349 serial_uart_state_t *serialUartHandle;
\r
351 assert(serialHandle);
\r
353 serialUartHandle = (serial_uart_state_t *)serialHandle;
\r
355 serialUartHandle->rx.callback = callback;
\r
356 serialUartHandle->rx.callbackParam = callbackParam;
\r
358 return kStatus_SerialManager_Success;
\r
361 void Serial_UartIsrFunction(serial_handle_t serialHandle)
\r
363 serial_uart_state_t *serialUartHandle;
\r
365 assert(serialHandle);
\r
367 serialUartHandle = (serial_uart_state_t *)serialHandle;
\r
369 HAL_UartIsrFunction(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
\r