1 /*
2  * Copyright 2018-2023 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include <string.h>
10 #include "fsl_component_serial_manager.h"
11 #include "fsl_component_serial_port_internal.h"
12 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
13 
14 #include "fsl_component_generic_list.h"
15 
16 /*
17  * The OSA_USED macro can only be defined when the OSA component is used.
18  * If the source code of the OSA component does not exist, the OSA_USED cannot be defined.
19  * OR, If OSA component is not added into project event the OSA source code exists, the OSA_USED
20  * also cannot be defined.
21  * The source code path of the OSA component is <MCUXpresso_SDK>/components/osa.
22  *
23  */
24 #if defined(OSA_USED)
25 #include "fsl_os_abstraction.h"
26 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
27 #include "fsl_component_common_task.h"
28 #else
29 
30 #endif
31 
32 #endif
33 
34 #endif
35 
36 /*******************************************************************************
37  * Definitions
38  ******************************************************************************/
39 
40 #ifndef NDEBUG
41 #if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
42 #undef assert
43 #define assert(n)
44 #else
45 /* MISRA C-2012 Rule 17.2 */
46 #undef assert
47 #define assert(n) \
48     while (!(n))  \
49     {             \
50         ;         \
51     }
52 #endif
53 #endif
54 
55 /* Weak function. */
56 #if defined(__GNUC__)
57 #define __WEAK_FUNC __attribute__((weak))
58 #elif defined(__ICCARM__)
59 #define __WEAK_FUNC __weak
60 #elif defined(__CC_ARM) || defined(__ARMCC_VERSION)
61 #define __WEAK_FUNC __attribute__((weak))
62 #elif defined(__DSC__) || defined(__CW__)
63 #define __WEAK_FUNC __attribute__((weak))
64 #endif
65 
66 #define SERIAL_EVENT_DATA_RECEIVED   (0U)
67 #define SERIAL_EVENT_DATA_SENT       (1U)
68 #define SERIAL_EVENT_DATA_START_SEND (2U)
69 #define SERIAL_EVENT_DATA_RX_NOTIFY  (3U)
70 #define SERIAL_EVENT_DATA_NUMBER     (4U)
71 
72 #define SERIAL_MANAGER_WRITE_TAG 0xAABB5754U
73 #define SERIAL_MANAGER_READ_TAG  0xBBAA5244U
74 
75 #ifndef RINGBUFFER_WATERMARK_THRESHOLD
76 #define RINGBUFFER_WATERMARK_THRESHOLD 95U / 100U
77 #endif
78 
79 #ifndef gSerialManagerLpConstraint_c
80 #define gSerialManagerLpConstraint_c 0
81 #endif
82 
83 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
84 typedef enum _serial_manager_transmission_mode
85 {
86     kSerialManager_TransmissionBlocking    = 0x0U, /*!< Blocking transmission*/
87     kSerialManager_TransmissionNonBlocking = 0x1U, /*!< None blocking transmission*/
88 } serial_manager_transmission_mode_t;
89 
90 /* TX transfer structure */
91 typedef struct _serial_manager_transfer
92 {
93     uint8_t *buffer;
94     volatile uint32_t length;
95     volatile uint32_t soFar;
96     serial_manager_transmission_mode_t mode;
97     serial_manager_status_t status;
98 } serial_manager_transfer_t;
99 #endif
100 
101 /* write handle structure */
102 typedef struct _serial_manager_send_handle
103 {
104 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
105     list_element_t link; /*!< list element of the link */
106     serial_manager_transfer_t transfer;
107 #endif
108     struct _serial_manager_handle *serialManagerHandle;
109 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
110     serial_manager_callback_t callback;
111     void *callbackParam;
112     uint32_t tag;
113 #endif
114 } serial_manager_write_handle_t;
115 typedef struct _serial_manager_send_block_handle
116 {
117     struct _serial_manager_handle *serialManagerHandle;
118 
119 } serial_manager_write_block_handle_t;
120 
121 typedef serial_manager_write_handle_t serial_manager_read_handle_t;
122 typedef serial_manager_write_block_handle_t serial_manager_read_block_handle_t;
123 
124 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
125 /* receive state structure */
126 typedef struct _serial_manager_read_ring_buffer
127 {
128     uint8_t *ringBuffer;
129     uint32_t ringBufferSize;
130     volatile uint32_t ringHead;
131     volatile uint32_t ringTail;
132 } serial_manager_read_ring_buffer_t;
133 
134 #if defined(__CC_ARM)
135 #pragma anon_unions
136 #endif
137 typedef struct _serial_manager_block_handle
138 {
139     serial_manager_type_t handleType;
140     serial_port_type_t type;
141     serial_manager_read_handle_t *volatile openedReadHandleHead;
142     volatile uint32_t openedWriteHandleCount;
143     union
144     {
145         uint32_t lowLevelhandleBuffer[1];
146 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
147         uint8_t uartHandleBuffer[SERIAL_PORT_UART_BLOCK_HANDLE_SIZE];
148 #endif
149     };
150 
151 } serial_manager_block_handle_t;
152 #endif
153 
154 /* The serial manager handle structure */
155 typedef struct _serial_manager_handle
156 {
157 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
158     serial_manager_type_t handleType;
159 #endif
160     serial_port_type_t serialPortType;
161     serial_manager_read_handle_t *volatile openedReadHandleHead;
162     volatile uint32_t openedWriteHandleCount;
163     union
164     {
165         uint32_t lowLevelhandleBuffer[1];
166 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
167         uint8_t uartHandleBuffer[SERIAL_PORT_UART_HANDLE_SIZE];
168 #endif
169 #if (defined(SERIAL_PORT_TYPE_UART_DMA) && (SERIAL_PORT_TYPE_UART_DMA > 0U))
170         uint8_t uartDmaHandleBuffer[SERIAL_PORT_UART_DMA_HANDLE_SIZE];
171 #endif
172 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
173         uint8_t usbcdcHandleBuffer[SERIAL_PORT_USB_CDC_HANDLE_SIZE];
174 #endif
175 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
176         uint8_t swoHandleBuffer[SERIAL_PORT_SWO_HANDLE_SIZE];
177 #endif
178 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
179         uint8_t usbcdcVirtualHandleBuffer[SERIAL_PORT_VIRTUAL_HANDLE_SIZE];
180 #endif
181 #if (defined(SERIAL_PORT_TYPE_RPMSG) && (SERIAL_PORT_TYPE_RPMSG > 0U))
182         uint8_t rpmsgHandleBuffer[SERIAL_PORT_RPMSG_HANDLE_SIZE];
183 #endif
184 #if (defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U))
185         uint8_t spiMasterHandleBuffer[SERIAL_PORT_SPI_MASTER_HANDLE_SIZE];
186 #endif
187 #if (defined(SERIAL_PORT_TYPE_SPI_SLAVE) && (SERIAL_PORT_TYPE_SPI_SLAVE > 0U))
188         uint8_t spiSlaveHandleBuffer[SERIAL_PORT_SPI_SLAVE_HANDLE_SIZE];
189 #endif
190 #if (defined(SERIAL_PORT_TYPE_BLE_WU) && (SERIAL_PORT_TYPE_BLE_WU > 0U))
191         uint8_t bleWuHandleBuffer[SERIAL_PORT_BLE_WU_HANDLE_SIZE];
192 #endif
193     };
194 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
195     serial_manager_read_ring_buffer_t ringBuffer;
196 #endif
197 
198 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
199 
200 #if defined(OSA_USED)
201 
202 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
203     common_task_message_t commontaskMsg;
204 #else
205     OSA_SEMAPHORE_HANDLE_DEFINE(serSemaphore); /*!< Semaphore instance */
206     OSA_TASK_HANDLE_DEFINE(taskId);            /*!< Task handle */
207 #endif
208     uint8_t serialManagerState[SERIAL_EVENT_DATA_NUMBER]; /*!< Used to indicate the serial mnager state */
209 
210 #endif
211 
212 #endif
213 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
214     list_label_t runningWriteHandleHead;   /*!< The queue of running write handle */
215     list_label_t completedWriteHandleHead; /*!< The queue of completed write handle */
216 #endif
217 
218 } serial_manager_handle_t;
219 
220 /*******************************************************************************
221  * Prototypes
222  ******************************************************************************/
223 
224 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
225 static void SerialManager_Task(void *param);
226 #endif
227 
228 /*******************************************************************************
229  * Variables
230  ******************************************************************************/
231 
232 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
233 
234 #if defined(OSA_USED)
235 
236 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
237 
238 #else
239                                                /*
240                                                 * \brief Defines the serial manager task's stack
241                                                 */
242 static OSA_TASK_DEFINE(SerialManager_Task, SERIAL_MANAGER_TASK_PRIORITY, 1, SERIAL_MANAGER_TASK_STACK_SIZE, false);
243 #endif
244 
245 #endif
246 
247 #endif
248 static const serial_manager_lowpower_critical_CBs_t *s_pfserialLowpowerCriticalCallbacks = NULL;
249 /*******************************************************************************
250  * Code
251  ******************************************************************************/
252 
253 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
SerialManager_AddTail(list_label_t * queue,serial_manager_write_handle_t * node)254 static void SerialManager_AddTail(list_label_t *queue, serial_manager_write_handle_t *node)
255 {
256     (void)LIST_AddTail(queue, &node->link);
257 }
258 
SerialManager_RemoveHead(list_label_t * queue)259 static void SerialManager_RemoveHead(list_label_t *queue)
260 {
261     (void)LIST_RemoveHead(queue);
262 }
263 
SerialManager_SetLpConstraint(int32_t power_mode)264 static int32_t SerialManager_SetLpConstraint(int32_t power_mode)
265 {
266     int32_t status = -1;
267     if ((s_pfserialLowpowerCriticalCallbacks != NULL) &&
268         (s_pfserialLowpowerCriticalCallbacks->serialEnterLowpowerCriticalFunc != NULL))
269     {
270         status = s_pfserialLowpowerCriticalCallbacks->serialEnterLowpowerCriticalFunc(power_mode);
271     }
272     return status;
273 }
SerialManager_ReleaseLpConstraint(int32_t power_mode)274 static int32_t SerialManager_ReleaseLpConstraint(int32_t power_mode)
275 {
276     int32_t status = -1;
277     if ((s_pfserialLowpowerCriticalCallbacks != NULL) &&
278         (s_pfserialLowpowerCriticalCallbacks->serialExitLowpowerCriticalFunc != NULL))
279     {
280         status = s_pfserialLowpowerCriticalCallbacks->serialExitLowpowerCriticalFunc(power_mode);
281     }
282     return status;
283 }
284 #endif
285 
286 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
287 
SerialManager_StartWriting(serial_manager_handle_t * serHandle)288 static serial_manager_status_t SerialManager_StartWriting(serial_manager_handle_t *serHandle)
289 {
290     serial_manager_status_t status = kStatus_SerialManager_Error;
291     serial_manager_write_handle_t *writeHandle =
292         (serial_manager_write_handle_t *)(void *)LIST_GetHead(&serHandle->runningWriteHandleHead);
293 
294     if (writeHandle != NULL)
295     {
296         (void)SerialManager_SetLpConstraint(gSerialManagerLpConstraint_c);
297         switch (serHandle->serialPortType)
298         {
299 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
300             case kSerialPort_Uart:
301                 status = Serial_UartWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
302                                           writeHandle->transfer.buffer, writeHandle->transfer.length);
303                 break;
304 #endif
305 #if (defined(SERIAL_PORT_TYPE_UART_DMA) && (SERIAL_PORT_TYPE_UART_DMA > 0U))
306             case kSerialPort_UartDma:
307                 status = Serial_UartDmaWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
308                                              writeHandle->transfer.buffer, writeHandle->transfer.length);
309                 break;
310 #endif
311 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
312             case kSerialPort_UsbCdc:
313                 status = Serial_UsbCdcWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
314                                             writeHandle->transfer.buffer, writeHandle->transfer.length);
315                 break;
316 #endif
317 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
318             case kSerialPort_Swo:
319                 status = Serial_SwoWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
320                                          writeHandle->transfer.buffer, writeHandle->transfer.length);
321                 break;
322 #endif
323 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
324             case kSerialPort_Virtual:
325                 status = Serial_PortVirtualWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
326                                                  writeHandle->transfer.buffer, writeHandle->transfer.length);
327                 break;
328 #endif
329 #if (defined(SERIAL_PORT_TYPE_RPMSG) && (SERIAL_PORT_TYPE_RPMSG > 0U))
330             case kSerialPort_Rpmsg:
331                 status = Serial_RpmsgWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
332                                            writeHandle->transfer.buffer, writeHandle->transfer.length);
333                 break;
334 #endif
335 #if (defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U))
336             case kSerialPort_SpiMaster:
337                 status = Serial_SpiMasterWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
338                                                writeHandle->transfer.buffer, writeHandle->transfer.length);
339                 break;
340 #endif
341 #if (defined(SERIAL_PORT_TYPE_SPI_SLAVE) && (SERIAL_PORT_TYPE_SPI_SLAVE > 0U))
342             case kSerialPort_SpiSlave:
343                 status = Serial_SpiSlaveWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
344                                               writeHandle->transfer.buffer, writeHandle->transfer.length);
345                 break;
346 #endif
347 #if (defined(SERIAL_PORT_TYPE_BLE_WU) && (SERIAL_PORT_TYPE_BLE_WU > 0U))
348             case kSerialPort_BleWu:
349                 status = Serial_PortBleWuWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
350                                                writeHandle->transfer.buffer, writeHandle->transfer.length);
351                 break;
352 #endif
353 
354             default:
355                 status = kStatus_SerialManager_Error;
356                 break;
357         }
358         if (kStatus_SerialManager_Success != status)
359         {
360             (void)SerialManager_ReleaseLpConstraint(gSerialManagerLpConstraint_c);
361         }
362     }
363     return status;
364 }
365 
SerialManager_StartReading(serial_manager_handle_t * serHandle,serial_manager_read_handle_t * readHandle,uint8_t * buffer,uint32_t length)366 static serial_manager_status_t SerialManager_StartReading(serial_manager_handle_t *serHandle,
367                                                           serial_manager_read_handle_t *readHandle,
368                                                           uint8_t *buffer,
369                                                           uint32_t length)
370 {
371     serial_manager_status_t status = kStatus_SerialManager_Error;
372 
373     if (NULL != readHandle)
374     {
375 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
376         if (kSerialPort_Uart == serHandle->serialPortType) /* Serial port UART */
377         {
378             status = Serial_UartRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
379         }
380 #endif
381 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
382         if (serHandle->serialPortType == kSerialPort_UsbCdc)
383         {
384             status = Serial_UsbCdcRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
385         }
386 #endif
387 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
388         if (serHandle->serialPortType == kSerialPort_Virtual)
389         {
390             status = Serial_PortVirtualRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
391         }
392 #endif
393 #if (defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U))
394         if (serHandle->serialPortType == kSerialPort_SpiMaster)
395         {
396             status = Serial_SpiMasterRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
397         }
398 #endif
399 #if (defined(SERIAL_PORT_TYPE_SPI_SLAVE) && (SERIAL_PORT_TYPE_SPI_SLAVE > 0U))
400         if (serHandle->serialPortType == kSerialPort_SpiSlave)
401         {
402             status = Serial_SpiSlaveRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
403         }
404 #endif
405 
406 #if 0
407 #if (defined(SERIAL_PORT_TYPE_RPMSG) && (SERIAL_PORT_TYPE_RPMSG > 0U))
408         if (serHandle->serialPortType == kSerialPort_Rpmsg)
409         {
410             status = Serial_RpmsgRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
411         }
412 #endif
413 #endif
414 #if (defined(SERIAL_PORT_TYPE_BLE_WU) && (SERIAL_PORT_TYPE_BLE_WU > 0U))
415         if (serHandle->serialPortType == kSerialPort_BleWu)
416         {
417             status = Serial_PortBleWuRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
418         }
419 #endif
420     }
421     return status;
422 }
423 
424 #else /*SERIAL_MANAGER_NON_BLOCKING_MODE > 0U*/
425 
SerialManager_StartWriting(serial_manager_handle_t * serHandle,serial_manager_write_handle_t * writeHandle,uint8_t * buffer,uint32_t length)426 static serial_manager_status_t SerialManager_StartWriting(serial_manager_handle_t *serHandle,
427                                                           serial_manager_write_handle_t *writeHandle,
428                                                           uint8_t *buffer,
429                                                           uint32_t length)
430 {
431     serial_manager_status_t status = kStatus_SerialManager_Error;
432 
433 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
434     if (kSerialPort_Uart == serHandle->serialPortType) /* Serial port UART */
435     {
436         status = Serial_UartWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
437     }
438     else
439 #endif
440 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
441         if (kSerialPort_UsbCdc == serHandle->serialPortType) /* Serial port UsbCdc */
442     {
443         status = Serial_UsbCdcWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
444     }
445     else
446 #endif
447 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
448         if (kSerialPort_Swo == serHandle->serialPortType) /* Serial port SWO */
449     {
450         status = Serial_SwoWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
451     }
452     else
453 #endif
454 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
455         if (kSerialPort_Virtual == serHandle->serialPortType) /* Serial port UsbCdcVirtual */
456     {
457         status = Serial_PortVirtualWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
458     }
459     else
460 #endif
461 #if (defined(SERIAL_PORT_TYPE_RPMSG) && (SERIAL_PORT_TYPE_RPMSG > 0U))
462         if (kSerialPort_Rpmsg == serHandle->serialPortType) /* Serial port Rpmsg */
463     {
464         status = Serial_RpmsgWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
465     }
466     else
467 #endif
468 #if (defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U))
469         if (kSerialPort_SpiMaster == serHandle->serialPortType) /* Serial port Spi Master */
470     {
471         status = Serial_SpiMasterWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
472     }
473     else
474 #endif
475 #if (defined(SERIAL_PORT_TYPE_BLE_WU) && (SERIAL_PORT_TYPE_BLE_WU > 0U))
476         if (kSerialPort_BleWu == serHandle->serialPortType) /* Serial port BLE WU */
477     {
478         status = Serial_PortBleWuWrite(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
479     }
480     else
481 #endif
482     {
483         /*MISRA rule*/
484     }
485     return status;
486 }
487 
SerialManager_StartReading(serial_manager_handle_t * serHandle,serial_manager_read_handle_t * readHandle,uint8_t * buffer,uint32_t length)488 static serial_manager_status_t SerialManager_StartReading(serial_manager_handle_t *serHandle,
489                                                           serial_manager_read_handle_t *readHandle,
490                                                           uint8_t *buffer,
491                                                           uint32_t length)
492 {
493     serial_manager_status_t status = kStatus_SerialManager_Error;
494 
495 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
496     if (kSerialPort_Uart == serHandle->serialPortType) /* Serial port UART */
497     {
498         status = Serial_UartRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
499     }
500     else
501 #endif
502 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
503         if (kSerialPort_UsbCdc == serHandle->serialPortType) /* Serial port UsbCdc */
504     {
505         status = Serial_UsbCdcRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
506     }
507     else
508 #endif
509 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
510         if (kSerialPort_Swo == serHandle->serialPortType) /* Serial port SWO */
511     {
512         status = Serial_SwoRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
513     }
514     else
515 #endif
516 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
517         if (kSerialPort_Virtual == serHandle->serialPortType) /* Serial port UsbCdcVirtual */
518     {
519         status = Serial_PortVirtualRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
520     }
521     else
522 #endif
523 #if (defined(SERIAL_PORT_TYPE_RPMSG) && (SERIAL_PORT_TYPE_RPMSG > 0U))
524         if (kSerialPort_Rpmsg == serHandle->serialPortType) /* Serial port UsbCdcVirtual */
525     {
526         status = Serial_RpmsgRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
527     }
528     else
529 #endif
530 #if (defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U))
531         if (kSerialPort_SpiMaster == serHandle->serialPortType) /* Serial port Spi Master */
532     {
533         status = Serial_SpiMasterRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
534     }
535     else
536 #endif
537 #if (defined(SERIAL_PORT_TYPE_BLE_WU) && (SERIAL_PORT_TYPE_BLE_WU > 0U))
538         if (kSerialPort_BleWu == serHandle->serialPortType) /* Serial port BLE WU */
539     {
540         status = Serial_PortBleWuRead(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), buffer, length);
541     }
542     else
543 #endif
544     {
545         /*MISRA rule*/
546     }
547     return status;
548 }
549 #endif /*SERIAL_MANAGER_NON_BLOCKING_MODE > 0U*/
550 
551 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
SerialManager_IsrFunction(serial_manager_handle_t * serHandle)552 static void SerialManager_IsrFunction(serial_manager_handle_t *serHandle)
553 {
554     uint32_t regPrimask = DisableGlobalIRQ();
555     switch (serHandle->serialPortType)
556     {
557 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
558         case kSerialPort_Uart:
559             Serial_UartIsrFunction(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
560             break;
561 #endif
562 #if (defined(SERIAL_PORT_TYPE_UART_DMA) && (SERIAL_PORT_TYPE_UART_DMA > 0U))
563         case kSerialPort_UartDma:
564             Serial_UartIsrFunction(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
565             break;
566 #endif
567 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
568         case kSerialPort_UsbCdc:
569             Serial_UsbCdcIsrFunction(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
570             break;
571 #endif
572 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
573         case kSerialPort_Swo:
574             Serial_SwoIsrFunction(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
575             break;
576 #endif
577 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
578         case kSerialPort_Virtual:
579             Serial_PortVirtualIsrFunction(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
580             break;
581 #endif
582 #if (defined(SERIAL_PORT_TYPE_BLE_WU) && (SERIAL_PORT_TYPE_BLE_WU > 0U))
583         case kSerialPort_BleWu:
584             Serial_PortBleWuIsrFunction(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
585             break;
586 #endif
587         default:
588             /*MISRA rule 16.4*/
589             break;
590     }
591     EnableGlobalIRQ(regPrimask);
592 }
593 
SerialManager_Task(void * param)594 static void SerialManager_Task(void *param)
595 {
596     serial_manager_handle_t *serHandle = (serial_manager_handle_t *)param;
597     serial_manager_write_handle_t *serialWriteHandle;
598     serial_manager_read_handle_t *serialReadHandle;
599     uint32_t primask;
600     serial_manager_callback_message_t serialMsg;
601 #if (defined(SERIAL_MANAGER_TASK_HANDLE_RX_AVAILABLE_NOTIFY) && (SERIAL_MANAGER_TASK_HANDLE_RX_AVAILABLE_NOTIFY > 0U))
602     uint32_t ringBufferLength;
603 #endif /* SERIAL_MANAGER_TASK_HANDLE_RX_AVAILABLE_NOTIFY */
604 
605     if (NULL != serHandle)
606     {
607 #if defined(OSA_USED)
608 
609 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
610 #else
611 
612         do
613         {
614             if (KOSA_StatusSuccess ==
615                 OSA_SemaphoreWait((osa_semaphore_handle_t)serHandle->serSemaphore, osaWaitForever_c))
616             {
617 #endif
618 #endif
619 #if defined(OSA_USED)
620 
621 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
622 #else
623                 primask     = DisableGlobalIRQ();
624                 uint8_t *ev = serHandle->serialManagerState;
625                 EnableGlobalIRQ(primask);
626                 if (0U != (ev[SERIAL_EVENT_DATA_START_SEND]))
627 #endif
628 #endif
629         {
630             (void)SerialManager_StartWriting(serHandle);
631 #if defined(OSA_USED)
632 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
633 #else
634                     primask = DisableGlobalIRQ();
635                     serHandle->serialManagerState[SERIAL_EVENT_DATA_START_SEND]--;
636                     EnableGlobalIRQ(primask);
637 #endif
638 #endif
639         }
640 #if defined(OSA_USED)
641 
642 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
643 #else
644                 if (0U != (ev[SERIAL_EVENT_DATA_SENT]))
645 #endif
646 
647 #endif
648         {
649             serialWriteHandle =
650                 (serial_manager_write_handle_t *)(void *)LIST_GetHead(&serHandle->completedWriteHandleHead);
651             while (NULL != serialWriteHandle)
652             {
653                 SerialManager_RemoveHead(&serHandle->completedWriteHandleHead);
654                 serialMsg.buffer                   = serialWriteHandle->transfer.buffer;
655                 serialMsg.length                   = serialWriteHandle->transfer.soFar;
656                 serialWriteHandle->transfer.buffer = NULL;
657                 if (NULL != serialWriteHandle->callback)
658                 {
659                     serialWriteHandle->callback(serialWriteHandle->callbackParam, &serialMsg,
660                                                 serialWriteHandle->transfer.status);
661                 }
662                 serialWriteHandle =
663                     (serial_manager_write_handle_t *)(void *)LIST_GetHead(&serHandle->completedWriteHandleHead);
664                 (void)SerialManager_ReleaseLpConstraint(gSerialManagerLpConstraint_c);
665             }
666 #if defined(OSA_USED)
667 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
668 #else
669                     primask = DisableGlobalIRQ();
670                     serHandle->serialManagerState[SERIAL_EVENT_DATA_SENT]--;
671                     EnableGlobalIRQ(primask);
672 #endif
673 #endif
674         }
675 #if defined(OSA_USED)
676 
677 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
678 #else
679                 if (0U != (ev[SERIAL_EVENT_DATA_RECEIVED]))
680 #endif
681 
682 #endif
683         {
684             primask          = DisableGlobalIRQ();
685             serialReadHandle = serHandle->openedReadHandleHead;
686             EnableGlobalIRQ(primask);
687 
688             if (NULL != serialReadHandle)
689             {
690                 if (NULL != serialReadHandle->transfer.buffer)
691                 {
692                     if (serialReadHandle->transfer.soFar >= serialReadHandle->transfer.length)
693                     {
694                         serialMsg.buffer                  = serialReadHandle->transfer.buffer;
695                         serialMsg.length                  = serialReadHandle->transfer.soFar;
696                         serialReadHandle->transfer.buffer = NULL;
697                         if (NULL != serialReadHandle->callback)
698                         {
699                             serialReadHandle->callback(serialReadHandle->callbackParam, &serialMsg,
700                                                        serialReadHandle->transfer.status);
701                         }
702                     }
703                 }
704             }
705 #if defined(OSA_USED)
706 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
707 #else
708                     primask = DisableGlobalIRQ();
709                     serHandle->serialManagerState[SERIAL_EVENT_DATA_RECEIVED]--;
710                     EnableGlobalIRQ(primask);
711 #endif
712 #endif
713         }
714 
715 #if (defined(SERIAL_MANAGER_TASK_HANDLE_RX_AVAILABLE_NOTIFY) && (SERIAL_MANAGER_TASK_HANDLE_RX_AVAILABLE_NOTIFY > 0U))
716 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
717 #else
718                 if (0U != (ev[SERIAL_EVENT_DATA_RX_NOTIFY]))
719 #endif
720         {
721             primask                                                 = DisableGlobalIRQ();
722             serHandle->serialManagerState[SERIAL_EVENT_DATA_RX_NOTIFY] = 0;
723             ringBufferLength =
724                 serHandle->ringBuffer.ringHead + serHandle->ringBuffer.ringBufferSize - serHandle->ringBuffer.ringTail;
725             ringBufferLength = ringBufferLength % serHandle->ringBuffer.ringBufferSize;
726             EnableGlobalIRQ(primask);
727             /* Notify there are data in ringbuffer */
728             if (0U != ringBufferLength)
729             {
730                 serialMsg.buffer = NULL;
731                 serialMsg.length = ringBufferLength;
732                 if ((NULL != serHandle->openedReadHandleHead) && (NULL != serHandle->openedReadHandleHead->callback))
733                 {
734                     serHandle->openedReadHandleHead->callback(serHandle->openedReadHandleHead->callbackParam, &serialMsg,
735                                                            kStatus_SerialManager_Notify);
736                 }
737             }
738         }
739 #endif /* SERIAL_MANAGER_TASK_HANDLE_RX_AVAILABLE_NOTIFY */
740 
741 #if defined(OSA_USED)
742 
743 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
744 #else
745             }
746         } while (0U != gUseRtos_c);
747 #endif
748 
749 #endif
750     }
751 }
752 #endif
753 
754 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
755 static void SerialManager_TxCallback(void *callbackParam,
756                                      serial_manager_callback_message_t *message,
757                                      serial_manager_status_t status)
758 {
759     serial_manager_handle_t *serHandle;
760     serial_manager_write_handle_t *writeHandle;
761 #if (defined(OSA_USED))
762 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
763     /* Need to support common_task. */
764 #else /* SERIAL_MANAGER_USE_COMMON_TASK */
765     uint32_t primask;
766 #endif
767 #endif
768     assert(NULL != callbackParam);
769     assert(NULL != message);
770 
771     serHandle = (serial_manager_handle_t *)callbackParam;
772 
773     writeHandle = (serial_manager_write_handle_t *)(void *)LIST_GetHead(&serHandle->runningWriteHandleHead);
774 
775     if (NULL != writeHandle)
776     {
777         SerialManager_RemoveHead(&serHandle->runningWriteHandleHead);
778 
779 #if (defined(OSA_USED) && defined(SERIAL_MANAGER_TASK_HANDLE_TX) && (SERIAL_MANAGER_TASK_HANDLE_TX == 1))
780 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
781         /* Need to support common_task. */
782 #else  /* SERIAL_MANAGER_USE_COMMON_TASK */
783         primask = DisableGlobalIRQ();
784         serHandle->serialManagerState[SERIAL_EVENT_DATA_START_SEND]++;
785         EnableGlobalIRQ(primask);
786         (void)OSA_SemaphorePost((osa_semaphore_handle_t)serHandle->serSemaphore);
787 
788 #endif /* SERIAL_MANAGER_USE_COMMON_TASK */
789 #else  /* OSA_USED && SERIAL_MANAGER_TASK_HANDLE_TX */
790         if (kSerialManager_TransmissionBlocking == writeHandle->transfer.mode)
791         {
792             (void)SerialManager_StartWriting(serHandle);
793         }
794 #endif /* OSA_USED && SERIAL_MANAGER_TASK_HANDLE_TX */
795 
796         writeHandle->transfer.soFar  = message->length;
797         writeHandle->transfer.status = status;
798         if (kSerialManager_TransmissionNonBlocking == writeHandle->transfer.mode)
799         {
800             SerialManager_AddTail(&serHandle->completedWriteHandleHead, writeHandle);
801 #if defined(OSA_USED)
802 
803 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
804             serHandle->commontaskMsg.callback      = SerialManager_Task;
805             serHandle->commontaskMsg.callbackParam = serHandle;
806             COMMON_TASK_post_message(&serHandle->commontaskMsg);
807 #else
808             primask = DisableGlobalIRQ();
809             serHandle->serialManagerState[SERIAL_EVENT_DATA_SENT]++;
810             EnableGlobalIRQ(primask);
811             (void)OSA_SemaphorePost((osa_semaphore_handle_t)serHandle->serSemaphore);
812 #endif
813 
814 #else
815             SerialManager_Task(serHandle);
816 #endif
817         }
818         else
819         {
820             writeHandle->transfer.buffer = NULL;
821             (void)SerialManager_ReleaseLpConstraint(gSerialManagerLpConstraint_c);
822         }
823     }
824 }
825 
826 void SerialManager_RxCallback(void *callbackParam,
827                               serial_manager_callback_message_t *message,
828                               serial_manager_status_t status);
829 void SerialManager_RxCallback(void *callbackParam,
830                               serial_manager_callback_message_t *message,
831                               serial_manager_status_t status)
832 {
833     serial_manager_handle_t *serHandle;
834 #if (!((defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U))) && \
835      !((defined(SERIAL_PORT_TYPE_SPI_SLAVE) && (SERIAL_PORT_TYPE_SPI_SLAVE > 0U))))
836     uint32_t ringBufferLength = 0;
837     uint32_t primask;
838 #endif
839     assert(NULL != callbackParam);
840     assert(NULL != message);
841 
842     serHandle = (serial_manager_handle_t *)callbackParam;
843 #if ((defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U)) || \
844      (defined(SERIAL_PORT_TYPE_SPI_SLAVE) && (SERIAL_PORT_TYPE_SPI_SLAVE > 0U)))
845     serHandle->openedReadHandleHead->transfer.status = kStatus_SerialManager_Success;
846     serHandle->openedReadHandleHead->transfer.soFar  = message->length;
847     serHandle->openedReadHandleHead->transfer.length = message->length;
848     serHandle->openedReadHandleHead->transfer.buffer = message->buffer;
849 #if defined(OSA_USED)
850 
851 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
852     serHandle->commontaskMsg.callback      = SerialManager_Task;
853     serHandle->commontaskMsg.callbackParam = serHandle;
854     COMMON_TASK_post_message(&serHandle->commontaskMsg);
855 #else
856     primask = DisableGlobalIRQ();
857     serHandle->serialManagerState[SERIAL_EVENT_DATA_RECEIVED]++;
858     EnableGlobalIRQ(primask);
859     (void)OSA_SemaphorePost((osa_semaphore_handle_t)serHandle->serSemaphore);
860 #endif
861 
862 #else
863     SerialManager_Task(serHandle);
864 #endif
865 #else
866     status = kStatus_SerialManager_Notify;
867 
868     primask = DisableGlobalIRQ();
869 
870     /* If wrap around is expected copy byte one after the other. Note that this could also be done with 2 memcopy for
871      * better efficiency. */
872     if (serHandle->ringBuffer.ringHead + message->length >= serHandle->ringBuffer.ringBufferSize)
873     {
874         for (uint32_t i = 0; i < message->length; i++)
875         {
876             serHandle->ringBuffer.ringBuffer[serHandle->ringBuffer.ringHead++] = message->buffer[i];
877 
878             if (serHandle->ringBuffer.ringHead >= serHandle->ringBuffer.ringBufferSize)
879             {
880                 serHandle->ringBuffer.ringHead = 0U;
881             }
882             if (serHandle->ringBuffer.ringHead == serHandle->ringBuffer.ringTail)
883             {
884                 status = kStatus_SerialManager_RingBufferOverflow;
885                 serHandle->ringBuffer.ringTail++;
886                 if (serHandle->ringBuffer.ringTail >= serHandle->ringBuffer.ringBufferSize)
887                 {
888                     serHandle->ringBuffer.ringTail = 0U;
889                 }
890             }
891         }
892     }
893     else /*No wrap is expected so do a memcpy*/
894     {
895         (void)memcpy(&serHandle->ringBuffer.ringBuffer[serHandle->ringBuffer.ringHead], message->buffer,
896                      message->length);
897         serHandle->ringBuffer.ringHead += message->length;
898     }
899 
900     ringBufferLength =
901         serHandle->ringBuffer.ringHead + serHandle->ringBuffer.ringBufferSize - serHandle->ringBuffer.ringTail;
902     ringBufferLength = ringBufferLength % serHandle->ringBuffer.ringBufferSize;
903 
904     if ((NULL != serHandle->openedReadHandleHead) && (NULL != serHandle->openedReadHandleHead->transfer.buffer))
905     {
906         if (serHandle->openedReadHandleHead->transfer.length > serHandle->openedReadHandleHead->transfer.soFar)
907         {
908             uint32_t remainLength =
909                 serHandle->openedReadHandleHead->transfer.length - serHandle->openedReadHandleHead->transfer.soFar;
910             for (uint32_t i = 0; i < MIN(ringBufferLength, remainLength); i++)
911             {
912                 serHandle->openedReadHandleHead->transfer.buffer[serHandle->openedReadHandleHead->transfer.soFar] =
913                     serHandle->ringBuffer.ringBuffer[serHandle->ringBuffer.ringTail];
914                 serHandle->ringBuffer.ringTail++;
915                 serHandle->openedReadHandleHead->transfer.soFar++;
916                 if (serHandle->ringBuffer.ringTail >= serHandle->ringBuffer.ringBufferSize)
917                 {
918                     serHandle->ringBuffer.ringTail = 0U;
919                 }
920             }
921             ringBufferLength = ringBufferLength - MIN(ringBufferLength, remainLength);
922         }
923 
924         if (serHandle->openedReadHandleHead->transfer.length > serHandle->openedReadHandleHead->transfer.soFar)
925         {
926         }
927         else
928         {
929             if (kSerialManager_TransmissionBlocking == serHandle->openedReadHandleHead->transfer.mode)
930             {
931                 serHandle->openedReadHandleHead->transfer.buffer = NULL;
932             }
933             else
934             {
935                 serHandle->openedReadHandleHead->transfer.status = kStatus_SerialManager_Success;
936 
937 #if defined(OSA_USED)
938 
939 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
940                 serHandle->commontaskMsg.callback                = SerialManager_Task;
941                 serHandle->commontaskMsg.callbackParam           = serHandle;
942                 COMMON_TASK_post_message(&serHandle->commontaskMsg);
943 #else
944                 serHandle->serialManagerState[SERIAL_EVENT_DATA_RECEIVED]++;
945                 (void)OSA_SemaphorePost((osa_semaphore_handle_t)serHandle->serSemaphore);
946 #endif
947 
948 #else
949                 SerialManager_Task(serHandle);
950 #endif
951             }
952         }
953     }
954 #if (defined(SERIAL_MANAGER_RING_BUFFER_FLOWCONTROL) && (SERIAL_MANAGER_RING_BUFFER_FLOWCONTROL > 0U))
955     uint32_t ringBufferWaterMark =
956         serHandle->ringBuffer.ringHead + serHandle->ringBuffer.ringBufferSize - serHandle->ringBuffer.ringTail;
957     ringBufferWaterMark = ringBufferWaterMark % serHandle->ringBuffer.ringBufferSize;
958     if (ringBufferWaterMark < (uint32_t)(serHandle->ringBuffer.ringBufferSize * RINGBUFFER_WATERMARK_THRESHOLD))
959     {
960         (void)SerialManager_StartReading(serHandle, serHandle->openedReadHandleHead, NULL, ringBufferLength);
961     }
962 #else
963     (void)SerialManager_StartReading(serHandle, serHandle->openedReadHandleHead, NULL, ringBufferLength);
964 #endif
965     if (0U != ringBufferLength)
966     {
967 #if (defined(SERIAL_MANAGER_TASK_HANDLE_RX_AVAILABLE_NOTIFY) && (SERIAL_MANAGER_TASK_HANDLE_RX_AVAILABLE_NOTIFY > 0U))
968         if (serHandle->serialManagerState[SERIAL_EVENT_DATA_RX_NOTIFY] == 0)
969         {
970             serHandle->serialManagerState[SERIAL_EVENT_DATA_RX_NOTIFY]++;
971             (void)OSA_SemaphorePost((osa_semaphore_handle_t)serHandle->serSemaphore);
972         }
973 
974         (void)status; /* Fix "set but never used" warning. */
975 #else  /* !SERIAL_MANAGER_TASK_HANDLE_RX_AVAILABLE_NOTIFY */
976         message->buffer = NULL;
977         message->length = ringBufferLength;
978         if ((NULL != serHandle->openedReadHandleHead) && (NULL != serHandle->openedReadHandleHead->callback))
979         {
980             serHandle->openedReadHandleHead->callback(serHandle->openedReadHandleHead->callbackParam, message, status);
981         }
982 #endif /* SERIAL_MANAGER_TASK_HANDLE_RX_AVAILABLE_NOTIFY */
983     }
984 
985 #if (!((defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))) && \
986      !((defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))))
987     if (kSerialManager_Blocking ==
988         serHandle->handleType) /* No need to check for (NULL != serHandle->openedReadHandleHead) condition as it is
989                                already done in SerialManager_StartReading() */
990 #else
991     if (NULL != serHandle->openedReadHandleHead)
992 #endif
993     {
994         ringBufferLength = serHandle->ringBuffer.ringBufferSize - 1U - ringBufferLength;
995         (void)SerialManager_StartReading(serHandle, serHandle->openedReadHandleHead, NULL, ringBufferLength);
996     }
997     EnableGlobalIRQ(primask);
998 #endif
999 }
1000 
1001 /*
1002  * This function is used for perdiodic check if the transfer is complete, and will be called in blocking transfer at
1003  * non-blocking mode. The perdiodic unit is ms and default value is define by
1004  * SERIAL_MANAGER_WRITE_TIME_DELAY_DEFAULT_VALUE/SERIAL_MANAGER_READ_TIME_DELAY_DEFAULT_VALUE. The function
1005  * SerialManager_WriteTimeDelay()/SerialManager_ReadTimeDelay() is a weak function, so it could be re-implemented by
1006  * upper layer.
1007  */
1008 __WEAK_FUNC void SerialManager_WriteTimeDelay(uint32_t ms);
1009 __WEAK_FUNC void SerialManager_WriteTimeDelay(uint32_t ms)
1010 {
1011 #if defined(OSA_USED)
1012     OSA_TimeDelay(ms);
1013 #endif
1014 }
1015 
1016 __WEAK_FUNC void SerialManager_ReadTimeDelay(uint32_t ms);
1017 __WEAK_FUNC void SerialManager_ReadTimeDelay(uint32_t ms)
1018 {
1019 #if defined(OSA_USED)
1020     OSA_TimeDelay(ms);
1021 #endif
1022 }
1023 
1024 static serial_manager_status_t SerialManager_Write(serial_write_handle_t writeHandle,
1025                                                    uint8_t *buffer,
1026                                                    uint32_t length,
1027                                                    serial_manager_transmission_mode_t mode)
1028 {
1029     serial_manager_write_handle_t *serialWriteHandle;
1030     serial_manager_handle_t *serHandle;
1031 
1032 #if (defined(OSA_USED) && defined(SERIAL_MANAGER_TASK_HANDLE_TX) && (SERIAL_MANAGER_TASK_HANDLE_TX == 1))
1033 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
1034     /* Need to support common_task. */
1035 #else  /* SERIAL_MANAGER_USE_COMMON_TASK */
1036     /* Do nothing. */
1037 #endif /* SERIAL_MANAGER_USE_COMMON_TASK */
1038 #else  /* OSA_USED && SERIAL_MANAGER_TASK_HANDLE_TX */
1039     serial_manager_status_t status = kStatus_SerialManager_Success;
1040 #endif /* OSA_USED && SERIAL_MANAGER_TASK_HANDLE_TX */
1041 
1042     uint32_t primask;
1043     uint8_t isEmpty = 0U;
1044 
1045     assert(NULL != writeHandle);
1046     assert(NULL != buffer);
1047     assert(length > 0U);
1048 
1049     serialWriteHandle = (serial_manager_write_handle_t *)writeHandle;
1050     serHandle         = serialWriteHandle->serialManagerHandle;
1051     assert(NULL != serHandle);
1052 
1053     assert(SERIAL_MANAGER_WRITE_TAG == serialWriteHandle->tag);
1054     assert(!((kSerialManager_TransmissionNonBlocking == mode) && (NULL == serialWriteHandle->callback)));
1055 
1056     primask = DisableGlobalIRQ();
1057     if (NULL != serialWriteHandle->transfer.buffer)
1058     {
1059         EnableGlobalIRQ(primask);
1060         return kStatus_SerialManager_Busy;
1061     }
1062     serialWriteHandle->transfer.buffer = buffer;
1063     serialWriteHandle->transfer.length = length;
1064     serialWriteHandle->transfer.soFar  = 0U;
1065     serialWriteHandle->transfer.mode   = mode;
1066 
1067     if (NULL == LIST_GetHead(&serHandle->runningWriteHandleHead))
1068     {
1069         isEmpty = 1U;
1070     }
1071     SerialManager_AddTail(&serHandle->runningWriteHandleHead, serialWriteHandle);
1072     EnableGlobalIRQ(primask);
1073 
1074     if (0U != isEmpty)
1075     {
1076 #if (defined(OSA_USED) && defined(SERIAL_MANAGER_TASK_HANDLE_TX) && (SERIAL_MANAGER_TASK_HANDLE_TX == 1))
1077 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
1078         /* Need to support common_task. */
1079 #else  /* SERIAL_MANAGER_USE_COMMON_TASK */
1080         primask = DisableGlobalIRQ();
1081         serHandle->serialManagerState[SERIAL_EVENT_DATA_START_SEND]++;
1082         EnableGlobalIRQ(primask);
1083         (void)OSA_SemaphorePost((osa_semaphore_handle_t)serHandle->serSemaphore);
1084 
1085 #endif /* SERIAL_MANAGER_USE_COMMON_TASK */
1086 #else  /* OSA_USED && SERIAL_MANAGER_TASK_HANDLE_TX */
1087         status = SerialManager_StartWriting(serHandle);
1088         if ((serial_manager_status_t)kStatus_SerialManager_Success != status)
1089         {
1090 #if (defined(USB_CDC_SERIAL_MANAGER_RUN_NO_HOST) && (USB_CDC_SERIAL_MANAGER_RUN_NO_HOST == 1))
1091             if (status == kStatus_SerialManager_NotConnected)
1092             {
1093                 SerialManager_RemoveHead(&serHandle->runningWriteHandleHead);
1094                 serialWriteHandle->transfer.buffer = 0U;
1095                 serialWriteHandle->transfer.length = 0U;
1096             }
1097 #endif /* USB_CDC_SERIAL_MANAGER_RUN_NO_HOST == 1 */
1098             return status;
1099         }
1100 #endif /* OSA_USED && SERIAL_MANAGER_TASK_HANDLE_TX */
1101     }
1102 
1103     if (kSerialManager_TransmissionBlocking == mode)
1104     {
1105         while (serialWriteHandle->transfer.length > serialWriteHandle->transfer.soFar)
1106         {
1107             if (SerialManager_needPollingIsr())
1108             {
1109                 SerialManager_IsrFunction(serHandle);
1110             }
1111             else
1112             {
1113                 SerialManager_WriteTimeDelay(SERIAL_MANAGER_WRITE_TIME_DELAY_DEFAULT_VALUE);
1114             }
1115         }
1116     }
1117     return kStatus_SerialManager_Success;
1118 }
1119 
1120 static serial_manager_status_t SerialManager_Read(serial_read_handle_t readHandle,
1121                                                   uint8_t *buffer,
1122                                                   uint32_t length,
1123                                                   serial_manager_transmission_mode_t mode,
1124                                                   uint32_t *receivedLength)
1125 {
1126     serial_manager_read_handle_t *serialReadHandle;
1127     serial_manager_handle_t *serHandle;
1128     uint32_t dataLength;
1129     uint32_t primask;
1130 
1131     assert(NULL != readHandle);
1132     assert(NULL != buffer);
1133     assert(length > 0U);
1134 
1135     serialReadHandle = (serial_manager_read_handle_t *)readHandle;
1136 
1137     serHandle = serialReadHandle->serialManagerHandle;
1138     assert(NULL != serHandle);
1139 
1140     assert(SERIAL_MANAGER_READ_TAG == serialReadHandle->tag);
1141     assert(!((kSerialManager_TransmissionNonBlocking == mode) && (NULL == serialReadHandle->callback)));
1142 
1143     primask = DisableGlobalIRQ();
1144     if (NULL != serialReadHandle->transfer.buffer)
1145     {
1146         EnableGlobalIRQ(primask);
1147         return kStatus_SerialManager_Busy;
1148     }
1149     serialReadHandle->transfer.buffer = buffer;
1150     serialReadHandle->transfer.length = length;
1151     serialReadHandle->transfer.soFar  = 0U;
1152     serialReadHandle->transfer.mode   = mode;
1153 
1154     /* This code is reached if (serHandle->handleType != kSerialManager_Blocking)*/
1155 #if (!((defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))) && \
1156      !((defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))))
1157     if (length == 1U)
1158     {
1159         if (serHandle->ringBuffer.ringHead != serHandle->ringBuffer.ringTail)
1160         {
1161             buffer[serialReadHandle->transfer.soFar++] =
1162                 serHandle->ringBuffer.ringBuffer[serHandle->ringBuffer.ringTail];
1163             serHandle->ringBuffer.ringTail++;
1164             if (serHandle->ringBuffer.ringTail >= serHandle->ringBuffer.ringBufferSize)
1165             {
1166                 serHandle->ringBuffer.ringTail = 0U;
1167             }
1168         }
1169     }
1170     else
1171 #endif /*(!defined(SERIAL_PORT_TYPE_USBCDC) && !defined(SERIAL_PORT_TYPE_VIRTUAL))*/
1172     {
1173         dataLength =
1174             serHandle->ringBuffer.ringHead + serHandle->ringBuffer.ringBufferSize - serHandle->ringBuffer.ringTail;
1175         dataLength = dataLength % serHandle->ringBuffer.ringBufferSize;
1176 
1177         for (serialReadHandle->transfer.soFar = 0U; serialReadHandle->transfer.soFar < MIN(dataLength, length);
1178              serialReadHandle->transfer.soFar++)
1179         {
1180             buffer[serialReadHandle->transfer.soFar] = serHandle->ringBuffer.ringBuffer[serHandle->ringBuffer.ringTail];
1181             serHandle->ringBuffer.ringTail++;
1182             if (serHandle->ringBuffer.ringTail >= serHandle->ringBuffer.ringBufferSize)
1183             {
1184                 serHandle->ringBuffer.ringTail = 0U;
1185             }
1186         }
1187 
1188         dataLength =
1189             serHandle->ringBuffer.ringHead + serHandle->ringBuffer.ringBufferSize - serHandle->ringBuffer.ringTail;
1190         dataLength = dataLength % serHandle->ringBuffer.ringBufferSize;
1191         dataLength = serHandle->ringBuffer.ringBufferSize - 1U - dataLength;
1192 
1193         (void)SerialManager_StartReading(serHandle, readHandle, NULL, dataLength);
1194     }
1195 
1196     if (NULL != receivedLength)
1197     {
1198         *receivedLength                   = serialReadHandle->transfer.soFar;
1199         serialReadHandle->transfer.buffer = NULL;
1200         EnableGlobalIRQ(primask);
1201     }
1202     else
1203     {
1204         if (serialReadHandle->transfer.soFar >= serialReadHandle->transfer.length)
1205         {
1206             serialReadHandle->transfer.buffer = NULL;
1207             EnableGlobalIRQ(primask);
1208             if (kSerialManager_TransmissionNonBlocking == mode)
1209             {
1210                 if (NULL != serialReadHandle->callback)
1211                 {
1212                     serial_manager_callback_message_t serialMsg;
1213                     serialMsg.buffer = buffer;
1214                     serialMsg.length = serialReadHandle->transfer.soFar;
1215                     serialReadHandle->callback(serialReadHandle->callbackParam, &serialMsg,
1216                                                kStatus_SerialManager_Success);
1217                 }
1218             }
1219         }
1220         else
1221         {
1222             EnableGlobalIRQ(primask);
1223         }
1224 
1225         if (kSerialManager_TransmissionBlocking == mode)
1226         {
1227             while (serialReadHandle->transfer.length > serialReadHandle->transfer.soFar)
1228             {
1229                 SerialManager_ReadTimeDelay(SERIAL_MANAGER_READ_TIME_DELAY_DEFAULT_VALUE);
1230             }
1231         }
1232     }
1233 #if (defined(SERIAL_MANAGER_RING_BUFFER_FLOWCONTROL) && (SERIAL_MANAGER_RING_BUFFER_FLOWCONTROL > 0U))
1234     uint32_t ringBufferWaterMark =
1235         serHandle->ringBuffer.ringHead + serHandle->ringBuffer.ringBufferSize - serHandle->ringBuffer.ringTail;
1236     ringBufferWaterMark = ringBufferWaterMark % serHandle->ringBuffer.ringBufferSize;
1237     if (ringBufferWaterMark < (uint32_t)(serHandle->ringBuffer.ringBufferSize * RINGBUFFER_WATERMARK_THRESHOLD))
1238     {
1239         (void)SerialManager_StartReading(serHandle, serHandle->openedReadHandleHead, NULL,
1240                                          serialReadHandle->transfer.length);
1241     }
1242 #endif
1243     return kStatus_SerialManager_Success;
1244 }
1245 
1246 #else
1247 
1248 static serial_manager_status_t SerialManager_Write(serial_write_handle_t writeHandle, uint8_t *buffer, uint32_t length)
1249 {
1250     serial_manager_write_handle_t *serialWriteHandle;
1251     serial_manager_handle_t *serHandle;
1252 
1253     assert(writeHandle);
1254     assert(buffer);
1255     assert(length);
1256 
1257     serialWriteHandle = (serial_manager_write_handle_t *)writeHandle;
1258     serHandle         = serialWriteHandle->serialManagerHandle;
1259 
1260     assert(serHandle);
1261 
1262     return SerialManager_StartWriting(serHandle, serialWriteHandle, buffer, length);
1263 }
1264 
1265 static serial_manager_status_t SerialManager_Read(serial_read_handle_t readHandle, uint8_t *buffer, uint32_t length)
1266 {
1267     serial_manager_read_handle_t *serialReadHandle;
1268     serial_manager_handle_t *serHandle;
1269 
1270     assert(readHandle);
1271     assert(buffer);
1272     assert(length);
1273 
1274     serialReadHandle = (serial_manager_read_handle_t *)readHandle;
1275     serHandle        = serialReadHandle->serialManagerHandle;
1276 
1277     assert(serHandle);
1278 
1279     return SerialManager_StartReading(serHandle, serialReadHandle, buffer, length);
1280 }
1281 #endif
1282 
1283 serial_manager_status_t SerialManager_Init(serial_handle_t serialHandle, const serial_manager_config_t *serialConfig)
1284 {
1285     serial_manager_handle_t *serHandle;
1286     serial_manager_status_t status = kStatus_SerialManager_Error;
1287 
1288     assert(NULL != serialConfig);
1289 
1290     assert(NULL != serialHandle);
1291     assert(SERIAL_MANAGER_HANDLE_SIZE >= sizeof(serial_manager_handle_t));
1292 
1293     serHandle = (serial_manager_handle_t *)serialHandle;
1294 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1295 
1296     assert(NULL != serialConfig->ringBuffer);
1297     assert(serialConfig->ringBufferSize > 0U);
1298     (void)memset(serHandle, 0, SERIAL_MANAGER_HANDLE_SIZE);
1299     serHandle->handleType = serialConfig->blockType;
1300 #else
1301     (void)memset(serHandle, 0, SERIAL_MANAGER_HANDLE_SIZE);
1302 #endif
1303     serHandle->serialPortType = serialConfig->type;
1304 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1305     serHandle->ringBuffer.ringBuffer     = serialConfig->ringBuffer;
1306     serHandle->ringBuffer.ringBufferSize = serialConfig->ringBufferSize;
1307 #endif
1308 
1309 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1310 #if defined(OSA_USED)
1311 
1312 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
1313 
1314     COMMON_TASK_init();
1315 
1316 #else
1317     if (KOSA_StatusSuccess != OSA_SemaphoreCreate((osa_semaphore_handle_t)serHandle->serSemaphore, 1U))
1318     {
1319         return kStatus_SerialManager_Error;
1320     }
1321 
1322     if (KOSA_StatusSuccess != OSA_TaskCreate((osa_task_handle_t)serHandle->taskId, OSA_TASK(SerialManager_Task), serHandle))
1323     {
1324         return kStatus_SerialManager_Error;
1325     }
1326 #endif
1327 
1328 #endif
1329 
1330 #endif
1331 
1332     switch (serialConfig->type)
1333     {
1334 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
1335         case kSerialPort_Uart:
1336             status = Serial_UartInit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), serialConfig->portConfig);
1337 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1338             if ((serial_manager_status_t)kStatus_SerialManager_Success == status)
1339             {
1340                 (void)Serial_UartInstallTxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1341                                                    SerialManager_TxCallback, serHandle);
1342 
1343                 (void)Serial_UartInstallRxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1344                                                    SerialManager_RxCallback, serHandle);
1345             }
1346 #endif
1347             break;
1348 #endif
1349 #if (defined(SERIAL_PORT_TYPE_UART_DMA) && (SERIAL_PORT_TYPE_UART_DMA > 0U))
1350         case kSerialPort_UartDma:
1351             status = Serial_UartDmaInit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), serialConfig->portConfig);
1352 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1353             if ((serial_manager_status_t)kStatus_SerialManager_Success == status)
1354             {
1355                 (void)Serial_UartDmaInstallTxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1356                                                       SerialManager_TxCallback, serHandle);
1357 
1358                 (void)Serial_UartDmaInstallRxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1359                                                       SerialManager_RxCallback, serHandle);
1360             }
1361 #endif
1362             break;
1363 #endif
1364 
1365 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
1366         case kSerialPort_UsbCdc:
1367             status = Serial_UsbCdcInit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), serialConfig->portConfig);
1368 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1369 
1370             if (kStatus_SerialManager_Success == status)
1371             {
1372                 status = Serial_UsbCdcInstallTxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1373                                                         SerialManager_TxCallback, serHandle);
1374                 if (kStatus_SerialManager_Success == status)
1375                 {
1376                     status = Serial_UsbCdcInstallRxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1377                                                             SerialManager_RxCallback, serHandle);
1378                 }
1379             }
1380 #endif
1381             break;
1382 #endif
1383 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
1384         case kSerialPort_Swo:
1385             status = Serial_SwoInit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), serialConfig->portConfig);
1386 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1387             if (kStatus_SerialManager_Success == status)
1388             {
1389                 status = Serial_SwoInstallTxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1390                                                      SerialManager_TxCallback, serHandle);
1391             }
1392 #endif
1393             break;
1394 #endif
1395 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
1396         case kSerialPort_Virtual:
1397             status =
1398                 Serial_PortVirtualInit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), serialConfig->portConfig);
1399 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1400             if (kStatus_SerialManager_Success == status)
1401             {
1402                 status = Serial_PortVirtualInstallTxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1403                                                              SerialManager_TxCallback, serHandle);
1404                 if (kStatus_SerialManager_Success == status)
1405                 {
1406                     status = Serial_PortVirtualInstallRxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1407                                                                  SerialManager_RxCallback, serHandle);
1408                 }
1409             }
1410 #endif
1411             break;
1412 #endif
1413 #if (defined(SERIAL_PORT_TYPE_RPMSG) && (SERIAL_PORT_TYPE_RPMSG > 0U))
1414         case kSerialPort_Rpmsg:
1415             status = Serial_RpmsgInit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), (void *)serialConfig);
1416 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1417             if (kStatus_SerialManager_Success == status)
1418             {
1419                 status = Serial_RpmsgInstallTxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1420                                                        SerialManager_TxCallback, serHandle);
1421                 if (kStatus_SerialManager_Success == status)
1422                 {
1423                     status = Serial_RpmsgInstallRxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1424                                                            SerialManager_RxCallback, serHandle);
1425                 }
1426             }
1427 #endif
1428             break;
1429 #endif
1430 #if (defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U))
1431         case kSerialPort_SpiMaster:
1432             status =
1433                 Serial_SpiMasterInit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), serialConfig->portConfig);
1434 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1435             if (kStatus_SerialManager_Success == status)
1436             {
1437                 status = Serial_SpiMasterInstallTxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1438                                                            SerialManager_TxCallback, serHandle);
1439                 if (kStatus_SerialManager_Success == status)
1440                 {
1441                     status = Serial_SpiMasterInstallRxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1442                                                                SerialManager_RxCallback, serHandle);
1443                 }
1444             }
1445 #endif
1446             break;
1447 #endif
1448 #if (defined(SERIAL_PORT_TYPE_SPI_SLAVE) && (SERIAL_PORT_TYPE_SPI_SLAVE > 0U))
1449         case kSerialPort_SpiSlave:
1450             status = Serial_SpiSlaveInit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), serialConfig->portConfig);
1451 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1452             if (kStatus_SerialManager_Success == status)
1453             {
1454                 status = Serial_SpiSlaveInstallTxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1455                                                           SerialManager_TxCallback, serHandle);
1456                 if (kStatus_SerialManager_Success == status)
1457                 {
1458                     status = Serial_SpiSlaveInstallRxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1459                                                               SerialManager_RxCallback, serHandle);
1460                 }
1461             }
1462 #endif
1463             break;
1464 #endif
1465 #if (defined(SERIAL_PORT_TYPE_BLE_WU) && (SERIAL_PORT_TYPE_BLE_WU > 0U))
1466         case kSerialPort_BleWu:
1467             status =
1468                 Serial_PortBleWuInit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]), serialConfig->portConfig);
1469 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1470             if (kStatus_SerialManager_Success == status)
1471             {
1472                 status = Serial_PortBleWuInstallTxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1473                                                            SerialManager_TxCallback, serHandle);
1474                 if (kStatus_SerialManager_Success == status)
1475                 {
1476                     status = Serial_PortBleWuInstallRxCallback(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]),
1477                                                                SerialManager_RxCallback, serHandle);
1478                 }
1479             }
1480 #endif
1481             break;
1482 #endif
1483         default:
1484             /*MISRA rule 16.4*/
1485             break;
1486     }
1487 
1488     return status;
1489 }
1490 
1491 serial_manager_status_t SerialManager_Deinit(serial_handle_t serialHandle)
1492 {
1493     serial_manager_handle_t *serHandle;
1494 
1495     serial_manager_status_t serialManagerStatus = kStatus_SerialManager_Success;
1496 
1497     assert(NULL != serialHandle);
1498 
1499     serHandle = (serial_manager_handle_t *)serialHandle;
1500 
1501     if ((NULL != serHandle->openedReadHandleHead) || (0U != serHandle->openedWriteHandleCount))
1502     {
1503         serialManagerStatus = kStatus_SerialManager_Busy; /*Serial Manager Busy*/
1504     }
1505     else
1506     {
1507         switch (serHandle->serialPortType) /*serial port type*/
1508         {
1509 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
1510             case kSerialPort_Uart:
1511                 (void)Serial_UartDeinit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
1512                 break;
1513 #endif
1514 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
1515             case kSerialPort_UsbCdc:
1516                 (void)Serial_UsbCdcDeinit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
1517                 break;
1518 #endif
1519 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
1520             case kSerialPort_Swo:
1521                 (void)Serial_SwoDeinit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
1522                 break;
1523 #endif
1524 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
1525             case kSerialPort_Virtual:
1526                 (void)Serial_PortVirtualDeinit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
1527                 break;
1528 #endif
1529 #if (defined(SERIAL_PORT_TYPE_RPMSG) && (SERIAL_PORT_TYPE_RPMSG > 0U))
1530             case kSerialPort_Rpmsg:
1531                 (void)Serial_RpmsgDeinit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
1532                 break;
1533 #endif
1534 #if (defined(SERIAL_PORT_TYPE_SPI_SLAVE) && (SERIAL_PORT_TYPE_SPI_SLAVE > 0U))
1535             case kSerialPort_SpiSlave:
1536                 (void)Serial_SpiSlaveDeinit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
1537                 break;
1538 #endif
1539 #if (defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U))
1540             case kSerialPort_SpiMaster:
1541                 (void)Serial_SpiMasterDeinit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
1542                 break;
1543 #endif
1544 #if (defined(SERIAL_PORT_TYPE_BLE_WU) && (SERIAL_PORT_TYPE_BLE_WU > 0U))
1545             case kSerialPort_BleWu:
1546                 (void)Serial_PortBleWuDeinit(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
1547                 break;
1548 #endif
1549             default:
1550                 /*MISRA rule 16.4*/
1551                 break;
1552         }
1553 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1554 
1555 #if defined(OSA_USED)
1556 
1557 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
1558 #else
1559         (void)OSA_SemaphoreDestroy((osa_event_handle_t)serHandle->serSemaphore);
1560         (void)OSA_TaskDestroy((osa_task_handle_t)serHandle->taskId);
1561 #endif
1562 
1563 #endif
1564 
1565 #endif
1566     }
1567     return serialManagerStatus;
1568 }
1569 
1570 serial_manager_status_t SerialManager_OpenWriteHandle(serial_handle_t serialHandle, serial_write_handle_t writeHandle)
1571 {
1572     serial_manager_handle_t *serHandle;
1573     serial_manager_write_handle_t *serialWriteHandle;
1574     uint32_t primask;
1575 
1576     assert(NULL != serialHandle);
1577     assert(NULL != writeHandle);
1578     assert(SERIAL_MANAGER_WRITE_HANDLE_SIZE >= sizeof(serial_manager_write_handle_t));
1579 
1580     serHandle         = (serial_manager_handle_t *)serialHandle;
1581     serialWriteHandle = (serial_manager_write_handle_t *)writeHandle;
1582 
1583     primask = DisableGlobalIRQ();
1584     serHandle->openedWriteHandleCount++;
1585     EnableGlobalIRQ(primask);
1586 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1587     if (serHandle->handleType == kSerialManager_Blocking)
1588     {
1589         serialWriteHandle->serialManagerHandle = serHandle;
1590         return kStatus_SerialManager_Success;
1591     }
1592     else
1593 #endif
1594     {
1595         (void)memset(writeHandle, 0, SERIAL_MANAGER_WRITE_HANDLE_SIZE);
1596     }
1597 
1598     serialWriteHandle->serialManagerHandle = serHandle;
1599 
1600 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1601     serialWriteHandle->tag = SERIAL_MANAGER_WRITE_TAG;
1602 #endif
1603 
1604     return kStatus_SerialManager_Success;
1605 }
1606 
1607 serial_manager_status_t SerialManager_CloseWriteHandle(serial_write_handle_t writeHandle)
1608 {
1609     serial_manager_handle_t *serialHandle;
1610     serial_manager_write_handle_t *serialWriteHandle;
1611     uint32_t primask;
1612 
1613     assert(NULL != writeHandle);
1614 
1615     serialWriteHandle = (serial_manager_write_handle_t *)writeHandle;
1616     serialHandle      = (serial_manager_handle_t *)(void *)serialWriteHandle->serialManagerHandle;
1617 
1618     assert(NULL != serialHandle);
1619 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1620     assert(SERIAL_MANAGER_WRITE_TAG == serialWriteHandle->tag);
1621 #endif
1622 
1623 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1624     (void)SerialManager_CancelWriting(writeHandle);
1625 #endif
1626     primask = DisableGlobalIRQ();
1627     if (serialHandle->openedWriteHandleCount > 0U)
1628     {
1629         serialHandle->openedWriteHandleCount--;
1630     }
1631     EnableGlobalIRQ(primask);
1632 #if (defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING) && (DEBUG_CONSOLE_TRANSFER_NON_BLOCKING > 0U))
1633     (void)memset(writeHandle, 0, SERIAL_MANAGER_WRITE_HANDLE_SIZE);
1634 #else
1635     (void)memset(writeHandle, 0, SERIAL_MANAGER_WRITE_BLOCK_HANDLE_SIZE);
1636 #endif
1637 
1638     return kStatus_SerialManager_Success;
1639 }
1640 
1641 serial_manager_status_t SerialManager_OpenReadHandle(serial_handle_t serialHandle, serial_read_handle_t readHandle)
1642 {
1643     serial_manager_handle_t *serHandle;
1644     serial_manager_read_handle_t *serialReadHandle; /* read handle structure */
1645     serial_manager_status_t serialManagerStatus = kStatus_SerialManager_Success;
1646     uint32_t primask;
1647 
1648     assert(NULL != serialHandle);
1649     assert(NULL != readHandle);
1650     assert(SERIAL_MANAGER_READ_HANDLE_SIZE >= sizeof(serial_manager_read_handle_t));
1651 
1652     serHandle        = (serial_manager_handle_t *)serialHandle;
1653     serialReadHandle = (serial_manager_read_handle_t *)readHandle;
1654 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1655     if (serHandle->handleType == kSerialManager_Blocking)
1656     {
1657         serialReadHandle->serialManagerHandle = serHandle;
1658         return kStatus_SerialManager_Success;
1659     }
1660 #endif
1661     primask = DisableGlobalIRQ();
1662     if (serHandle->openedReadHandleHead != NULL)
1663     {
1664         serialManagerStatus = kStatus_SerialManager_Busy;
1665     }
1666     else
1667     {
1668         serHandle->openedReadHandleHead = serialReadHandle;
1669 
1670         (void)memset(readHandle, 0, SERIAL_MANAGER_READ_HANDLE_SIZE);
1671 
1672         serialReadHandle->serialManagerHandle = serHandle;
1673 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1674         serialReadHandle->tag = SERIAL_MANAGER_READ_TAG;
1675 #endif
1676     }
1677     EnableGlobalIRQ(primask);
1678     return serialManagerStatus;
1679 }
1680 
1681 serial_manager_status_t SerialManager_CloseReadHandle(serial_read_handle_t readHandle)
1682 {
1683     serial_manager_handle_t *serialHandle;
1684     serial_manager_read_handle_t *serialReadHandle;
1685     uint32_t primask;
1686 
1687     assert(NULL != readHandle);
1688 
1689     serialReadHandle = (serial_manager_read_handle_t *)readHandle;
1690     serialHandle     = (serial_manager_handle_t *)(void *)serialReadHandle->serialManagerHandle;
1691 
1692     assert((NULL != serialHandle) && (serialHandle->openedReadHandleHead == serialReadHandle));
1693 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1694     assert(SERIAL_MANAGER_READ_TAG == serialReadHandle->tag);
1695 #endif
1696 
1697 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1698     (void)SerialManager_CancelReading(readHandle);
1699 #endif
1700 
1701     primask                            = DisableGlobalIRQ();
1702     serialHandle->openedReadHandleHead = NULL;
1703     EnableGlobalIRQ(primask);
1704 #if (defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING) && (DEBUG_CONSOLE_TRANSFER_NON_BLOCKING > 0U))
1705     (void)memset(readHandle, 0, SERIAL_MANAGER_READ_HANDLE_SIZE);
1706 #else
1707     (void)memset(readHandle, 0, SERIAL_MANAGER_READ_BLOCK_HANDLE_SIZE);
1708 #endif
1709 
1710     return kStatus_SerialManager_Success;
1711 }
1712 
1713 serial_manager_status_t SerialManager_WriteBlocking(serial_write_handle_t writeHandle, uint8_t *buffer, uint32_t length)
1714 {
1715 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1716     return SerialManager_Write(writeHandle, buffer, length, kSerialManager_TransmissionBlocking);
1717 #else
1718     return SerialManager_Write(writeHandle, buffer, length);
1719 #endif
1720 }
1721 
1722 serial_manager_status_t SerialManager_ReadBlocking(serial_read_handle_t readHandle, uint8_t *buffer, uint32_t length)
1723 {
1724 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1725     return SerialManager_Read(readHandle, buffer, length, kSerialManager_TransmissionBlocking, NULL);
1726 #else
1727     return SerialManager_Read(readHandle, buffer, length);
1728 #endif
1729 }
1730 
1731 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1732 serial_manager_status_t SerialManager_WriteNonBlocking(serial_write_handle_t writeHandle,
1733                                                        uint8_t *buffer,
1734                                                        uint32_t length)
1735 {
1736     return SerialManager_Write(writeHandle, buffer, length, kSerialManager_TransmissionNonBlocking);
1737 }
1738 
1739 serial_manager_status_t SerialManager_ReadNonBlocking(serial_read_handle_t readHandle, uint8_t *buffer, uint32_t length)
1740 {
1741 #if ((defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U)) || \
1742      (defined(SERIAL_PORT_TYPE_SPI_SLAVE) && (SERIAL_PORT_TYPE_SPI_SLAVE > 0U)))
1743 
1744     serial_manager_read_handle_t *serialReadHandle;
1745     serialReadHandle = (serial_manager_read_handle_t *)readHandle;
1746 
1747     return (serial_manager_status_t)SerialManager_StartReading(serialReadHandle->serialManagerHandle, readHandle,
1748                                                                buffer, length);
1749 #else
1750     return SerialManager_Read(readHandle, buffer, length, kSerialManager_TransmissionNonBlocking, NULL);
1751 #endif
1752 }
1753 
1754 serial_manager_status_t SerialManager_CancelWriting(serial_write_handle_t writeHandle)
1755 {
1756     serial_manager_write_handle_t *serialWriteHandle;
1757     uint32_t primask;
1758     uint8_t isNotUsed        = 0U;
1759     uint8_t isNotNeed2Cancel = 0U;
1760 
1761     assert(NULL != writeHandle);
1762 
1763     serialWriteHandle = (serial_manager_write_handle_t *)writeHandle;
1764 
1765     assert(NULL != serialWriteHandle->serialManagerHandle);
1766     assert(SERIAL_MANAGER_WRITE_TAG == serialWriteHandle->tag);
1767 
1768     if ((NULL != serialWriteHandle->transfer.buffer) &&
1769         (kSerialManager_TransmissionBlocking == serialWriteHandle->transfer.mode))
1770     {
1771         return kStatus_SerialManager_Error;
1772     }
1773 
1774     primask = DisableGlobalIRQ();
1775     if (serialWriteHandle != (serial_manager_write_handle_t *)(void *)LIST_GetHead(
1776                                  &serialWriteHandle->serialManagerHandle->runningWriteHandleHead))
1777     {
1778         if (kLIST_Ok == LIST_RemoveElement(&serialWriteHandle->link))
1779         {
1780             isNotUsed = 1U;
1781         }
1782         else
1783         {
1784             isNotNeed2Cancel = 1U;
1785         }
1786     }
1787     EnableGlobalIRQ(primask);
1788 
1789     if (0U == isNotNeed2Cancel)
1790     {
1791         if (0U != isNotUsed)
1792         {
1793             serialWriteHandle->transfer.soFar  = 0;
1794             serialWriteHandle->transfer.status = kStatus_SerialManager_Canceled;
1795 
1796             SerialManager_AddTail(&serialWriteHandle->serialManagerHandle->completedWriteHandleHead, serialWriteHandle);
1797 #if defined(OSA_USED)
1798 
1799 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
1800             serialWriteHandle->serialManagerHandle->commontaskMsg.callback = SerialManager_Task;
1801             serialWriteHandle->serialManagerHandle->commontaskMsg.callbackParam =
1802                 serialWriteHandle->serialManagerHandle;
1803             COMMON_TASK_post_message(&serialWriteHandle->serialManagerHandle->commontaskMsg);
1804 #else
1805             primask = DisableGlobalIRQ();
1806             serialWriteHandle->serialManagerHandle->serialManagerState[SERIAL_EVENT_DATA_SENT]++;
1807             EnableGlobalIRQ(primask);
1808             (void)OSA_SemaphorePost((osa_semaphore_handle_t)serialWriteHandle->serialManagerHandle->serSemaphore);
1809 #endif
1810 
1811 #else
1812             SerialManager_Task(serialWriteHandle->serialManagerHandle);
1813 #endif
1814         }
1815         else
1816         {
1817             switch (serialWriteHandle->serialManagerHandle->serialPortType)
1818             {
1819 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
1820                 case kSerialPort_Uart:
1821                     (void)Serial_UartCancelWrite(
1822                         ((serial_handle_t)&serialWriteHandle->serialManagerHandle->lowLevelhandleBuffer[0]));
1823                     break;
1824 #endif
1825 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
1826                 case kSerialPort_UsbCdc:
1827                     (void)Serial_UsbCdcCancelWrite(
1828                         ((serial_handle_t)&serialWriteHandle->serialManagerHandle->lowLevelhandleBuffer[0]));
1829                     break;
1830 #endif
1831 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
1832                 case kSerialPort_Swo:
1833                     (void)Serial_SwoCancelWrite(
1834                         ((serial_handle_t)&serialWriteHandle->serialManagerHandle->lowLevelhandleBuffer[0]));
1835                     break;
1836 #endif
1837 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
1838                 case kSerialPort_Virtual:
1839                     (void)Serial_PortVirtualCancelWrite(
1840                         ((serial_handle_t)&serialWriteHandle->serialManagerHandle->lowLevelhandleBuffer[0]));
1841                     break;
1842 #endif
1843 #if (defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U))
1844                 case kSerialPort_SpiMaster:
1845                     (void)Serial_SpiMasterCancelWrite(
1846                         ((serial_handle_t)&serialWriteHandle->serialManagerHandle->lowLevelhandleBuffer[0]));
1847                     break;
1848 #endif
1849 #if (defined(SERIAL_PORT_TYPE_SPI_SLAVE) && (SERIAL_PORT_TYPE_SPI_SLAVE > 0U))
1850                 case kSerialPort_SpiSlave:
1851                     (void)Serial_SpiSlaveCancelWrite(
1852                         ((serial_handle_t)&serialWriteHandle->serialManagerHandle->lowLevelhandleBuffer[0]));
1853                     break;
1854 #endif
1855 #if (defined(SERIAL_PORT_TYPE_BLE_WU) && (SERIAL_PORT_TYPE_BLE_WU > 0U))
1856                 case kSerialPort_BleWu:
1857                     (void)Serial_PortBleWuCancelWrite(
1858                         ((serial_handle_t)&serialWriteHandle->serialManagerHandle->lowLevelhandleBuffer[0]));
1859                     break;
1860 #endif
1861                 default:
1862                     /*MISRA rule 16.4*/
1863                     break;
1864             }
1865         }
1866 
1867 #if (defined(OSA_USED) && defined(SERIAL_MANAGER_TASK_HANDLE_TX) && (SERIAL_MANAGER_TASK_HANDLE_TX == 1))
1868 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
1869         /* Need to support common_task. */
1870 #else  /* SERIAL_MANAGER_USE_COMMON_TASK */
1871         primask = DisableGlobalIRQ();
1872         serialWriteHandle->serialManagerHandle->serialManagerState[SERIAL_EVENT_DATA_START_SEND]++;
1873         EnableGlobalIRQ(primask);
1874         (void)OSA_SemaphorePost((osa_semaphore_handle_t)serialWriteHandle->serialManagerHandle->serSemaphore);
1875 
1876 #endif /* SERIAL_MANAGER_USE_COMMON_TASK */
1877 #else  /* OSA_USED && SERIAL_MANAGER_TASK_HANDLE_TX */
1878         (void)SerialManager_StartWriting(serialWriteHandle->serialManagerHandle);
1879 #endif /* OSA_USED && SERIAL_MANAGER_TASK_HANDLE_TX */
1880     }
1881 
1882     return kStatus_SerialManager_Success;
1883 }
1884 
1885 serial_manager_status_t SerialManager_CancelReading(serial_read_handle_t readHandle)
1886 {
1887     serial_manager_read_handle_t *serialReadHandle;
1888     serial_manager_callback_message_t serialMsg;
1889     uint8_t *buffer;
1890     uint32_t primask;
1891 
1892     assert(NULL != readHandle);
1893 
1894     serialReadHandle = (serial_manager_read_handle_t *)readHandle;
1895 
1896     assert(SERIAL_MANAGER_READ_TAG == serialReadHandle->tag);
1897 
1898     if ((NULL != serialReadHandle->transfer.buffer) &&
1899         (kSerialManager_TransmissionBlocking == serialReadHandle->transfer.mode))
1900     {
1901         return kStatus_SerialManager_Error;
1902     }
1903 
1904     primask                           = DisableGlobalIRQ();
1905     buffer                            = serialReadHandle->transfer.buffer;
1906     serialReadHandle->transfer.buffer = NULL;
1907     serialReadHandle->transfer.length = 0;
1908     serialMsg.buffer                  = buffer;
1909     serialMsg.length                  = serialReadHandle->transfer.soFar;
1910     EnableGlobalIRQ(primask);
1911 
1912     if (NULL != buffer)
1913     {
1914         if (NULL != serialReadHandle->callback)
1915         {
1916             serialReadHandle->callback(serialReadHandle->callbackParam, &serialMsg, kStatus_SerialManager_Canceled);
1917         }
1918     }
1919     return kStatus_SerialManager_Success;
1920 }
1921 
1922 serial_manager_status_t SerialManager_TryRead(serial_read_handle_t readHandle,
1923                                               uint8_t *buffer,
1924                                               uint32_t length,
1925                                               uint32_t *receivedLength)
1926 {
1927     assert(NULL != receivedLength);
1928 
1929     return SerialManager_Read(readHandle, buffer, length, kSerialManager_TransmissionBlocking, receivedLength);
1930 }
1931 
1932 serial_manager_status_t SerialManager_InstallTxCallback(serial_write_handle_t writeHandle,
1933                                                         serial_manager_callback_t callback,
1934                                                         void *callbackParam)
1935 {
1936     serial_manager_write_handle_t *serialWriteHandle;
1937 
1938     assert(NULL != writeHandle);
1939 
1940     serialWriteHandle = (serial_manager_write_handle_t *)writeHandle;
1941 
1942     assert(SERIAL_MANAGER_WRITE_TAG == serialWriteHandle->tag);
1943 
1944     serialWriteHandle->callbackParam = callbackParam;
1945     serialWriteHandle->callback      = callback;
1946 
1947     return kStatus_SerialManager_Success;
1948 }
1949 
1950 serial_manager_status_t SerialManager_InstallRxCallback(serial_read_handle_t readHandle,
1951                                                         serial_manager_callback_t callback,
1952                                                         void *callbackParam)
1953 {
1954     serial_manager_read_handle_t *serialReadHandle;
1955 
1956     assert(NULL != readHandle);
1957 
1958     serialReadHandle = (serial_manager_read_handle_t *)readHandle;
1959 
1960     assert(SERIAL_MANAGER_READ_TAG == serialReadHandle->tag);
1961 
1962     serialReadHandle->callbackParam = callbackParam;
1963     serialReadHandle->callback      = callback;
1964 
1965     return kStatus_SerialManager_Success;
1966 }
1967 #endif
1968 
1969 serial_manager_status_t SerialManager_EnterLowpower(serial_handle_t serialHandle)
1970 {
1971     serial_manager_handle_t *serHandle;
1972     serial_manager_status_t status = kStatus_SerialManager_Error;
1973 
1974     assert(NULL != serialHandle);
1975 
1976     serHandle = (serial_manager_handle_t *)serialHandle;
1977 
1978     switch (serHandle->serialPortType)
1979     {
1980 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
1981         case kSerialPort_Uart:
1982             status = Serial_UartEnterLowpower(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
1983             break;
1984 #endif
1985 #if (defined(SERIAL_PORT_TYPE_UART_DMA) && (SERIAL_PORT_TYPE_UART_DMA > 0U))
1986         case kSerialPort_UartDma:
1987             status = Serial_UartDmaEnterLowpower(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
1988             break;
1989 #endif
1990 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
1991         case kSerialPort_UsbCdc:
1992             break;
1993 #endif
1994 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
1995         case kSerialPort_Swo:
1996             break;
1997 #endif
1998 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
1999         case kSerialPort_Virtual:
2000             break;
2001 #endif
2002 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
2003         case kSerialPort_Rpmsg:
2004             break;
2005 #endif
2006 #if (defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U))
2007         case kSerialPort_SpiMaster:
2008             break;
2009 #endif
2010 #if (defined(SERIAL_PORT_TYPE_SPI_SLAVE) && (SERIAL_PORT_TYPE_SPI_SLAVE > 0U))
2011         case kSerialPort_SpiSlave:
2012             break;
2013 #endif
2014 #if (defined(SERIAL_PORT_TYPE_BLE_WU) && (SERIAL_PORT_TYPE_BLE_WU > 0U))
2015         case kSerialPort_BleWu:
2016             break;
2017 #endif
2018         default:
2019             /*MISRA rule 16.4*/
2020             break;
2021     }
2022     return status;
2023 }
2024 
2025 serial_manager_status_t SerialManager_ExitLowpower(serial_handle_t serialHandle)
2026 {
2027     serial_manager_handle_t *serHandle;
2028     serial_manager_status_t status = kStatus_SerialManager_Error;
2029 
2030     assert(NULL != serialHandle);
2031 
2032     serHandle = (serial_manager_handle_t *)serialHandle;
2033 
2034     switch (serHandle->serialPortType)
2035     {
2036 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
2037         case kSerialPort_Uart:
2038             status = Serial_UartExitLowpower(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
2039             break;
2040 #endif
2041 
2042 #if (defined(SERIAL_PORT_TYPE_UART_DMA) && (SERIAL_PORT_TYPE_UART_DMA > 0U))
2043         case kSerialPort_UartDma:
2044             status = Serial_UartDmaExitLowpower(((serial_handle_t)&serHandle->lowLevelhandleBuffer[0]));
2045             break;
2046 #endif
2047 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
2048         case kSerialPort_UsbCdc:
2049             break;
2050 #endif
2051 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
2052         case kSerialPort_Swo:
2053             break;
2054 #endif
2055 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
2056         case kSerialPort_Virtual:
2057             break;
2058 #endif
2059 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
2060         case kSerialPort_Rpmsg:
2061             break;
2062 #endif
2063 #if (defined(SERIAL_PORT_TYPE_SPI_MASTER) && (SERIAL_PORT_TYPE_SPI_MASTER > 0U))
2064         case kSerialPort_SpiMaster:
2065             break;
2066 #endif
2067 #if (defined(SERIAL_PORT_TYPE_SPI_SLAVE) && (SERIAL_PORT_TYPE_SPI_SLAVE > 0U))
2068         case kSerialPort_SpiSlave:
2069             break;
2070 #endif
2071 #if (defined(SERIAL_PORT_TYPE_BLE_WU) && (SERIAL_PORT_TYPE_BLE_WU > 0U))
2072         case kSerialPort_BleWu:
2073             break;
2074 #endif
2075         default:
2076             /*MISRA rule 16.4*/
2077             break;
2078     }
2079     return status;
2080 }
2081 /*!
2082  * @brief This function performs initialization of the callbacks structure used to disable lowpower
2083  *          when serial manager is active.
2084  *
2085  *
2086  * @param  pfCallback Pointer to the function structure used to allow/disable lowpower.
2087  *
2088  */
2089 void SerialManager_SetLowpowerCriticalCb(const serial_manager_lowpower_critical_CBs_t *pfCallback)
2090 {
2091     s_pfserialLowpowerCriticalCallbacks = pfCallback;
2092     (void)s_pfserialLowpowerCriticalCallbacks;
2093 }
2094