serial_manager.c
Go to the documentation of this file.
1 /*
2  * Copyright 2018-2020 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_common.h"
10 #include <string.h>
11 
12 #include "serial_manager.h"
13 #include "serial_port_internal.h"
14 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
15 
16 #include "generic_list.h"
17 
18 /*
19  * The OSA_USED macro can only be defined when the OSA component is used.
20  * If the source code of the OSA component does not exist, the OSA_USED cannot be defined.
21  * OR, If OSA component is not added into project event the OSA source code exists, the OSA_USED
22  * also cannot be defined.
23  * The source code path of the OSA component is <MCUXpresso_SDK>/components/osa.
24  *
25  */
26 #if defined(OSA_USED)
27 
28 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
29 #include "common_task.h"
30 #else
31 #include "fsl_os_abstraction.h"
32 #endif
33 
34 #endif
35 
36 #endif
37 
38 /*******************************************************************************
39  * Definitions
40  ******************************************************************************/
41 
42 #ifndef NDEBUG
43 #if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
44 #undef assert
45 #define assert(n)
46 #endif
47 #endif
48 
49 #define SERIAL_EVENT_DATA_RECEIVED (1U << 0)
50 #define SERIAL_EVENT_DATA_SENT (1U << 1)
51 
52 #define SERIAL_MANAGER_WRITE_TAG 0xAABB5754U
53 #define SERIAL_MANAGER_READ_TAG 0xBBAA5244U
54 
55 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
56 typedef enum _serial_manager_transmission_mode
57 {
58  kSerialManager_TransmissionBlocking = 0x0U,
59  kSerialManager_TransmissionNonBlocking = 0x1U,
60 } serial_manager_transmission_mode_t;
61 
62 /* TX transfer structure */
63 typedef struct _serial_manager_transfer
64 {
65  uint8_t *buffer;
66  volatile uint32_t length;
67  volatile uint32_t soFar;
68  serial_manager_transmission_mode_t mode;
70 } serial_manager_transfer_t;
71 #endif
72 
73 /* write handle structure */
75 {
76 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
77  list_element_t link;
78  serial_manager_transfer_t transfer;
79 #endif
81 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
83  void *callbackParam;
84  uint32_t tag;
85 #endif
87 
89 
90 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
91 /* receive state structure */
92 typedef struct _serial_manager_read_ring_buffer
93 {
94  uint8_t *ringBuffer;
95  uint32_t ringBufferSize;
96  volatile uint32_t ringHead;
97  volatile uint32_t ringTail;
98 } serial_manager_read_ring_buffer_t;
99 #endif
100 
101 #if defined(__CC_ARM)
102 #pragma anon_unions
103 #endif
104 /* The serial manager handle structure */
106 {
107 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
108  list_label_t runningWriteHandleHead;
109  list_label_t completedWriteHandleHead;
110 #endif
112  volatile uint32_t openedWriteHandleCount;
113  union
114  {
115  uint32_t lowLevelhandleBuffer[1];
116 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
117  uint8_t uartHandleBuffer[SERIAL_PORT_UART_HANDLE_SIZE];
118 #endif
119 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
120  uint8_t usbcdcHandleBuffer[SERIAL_PORT_USB_CDC_HANDLE_SIZE];
121 #endif
122 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
123  uint8_t swoHandleBuffer[SERIAL_PORT_SWO_HANDLE_SIZE];
124 #endif
125 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
126  uint8_t usbcdcVirtualHandleBuffer[SERIAL_PORT_USB_VIRTUAL_HANDLE_SIZE];
127 #endif
128  };
129 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
130  serial_manager_read_ring_buffer_t ringBuffer;
131 #endif
132 
133 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
134 
135 #if defined(OSA_USED)
136 
137 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
138  common_task_message_t commontaskMsg;
139 #else
140  OSA_EVENT_HANDLE_DEFINE(event);
141  OSA_TASK_HANDLE_DEFINE(taskId);
142 #endif
143 
144 #endif
145 
146 #endif
147 
150 
151 /*******************************************************************************
152  * Prototypes
153  ******************************************************************************/
154 
155 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
156 static void SerialManager_Task(void *param);
157 #endif
158 
159 /*******************************************************************************
160  * Variables
161  ******************************************************************************/
162 
163 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
164 
165 #if defined(OSA_USED)
166 
167 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
168 
169 #else
170  /*
171  * \brief Defines the serial manager task's stack
172  */
173 OSA_TASK_DEFINE(SerialManager_Task, SERIAL_MANAGER_TASK_PRIORITY, 1, SERIAL_MANAGER_TASK_STACK_SIZE, false);
174 #endif
175 
176 #endif
177 
178 #endif
179 
180 /*******************************************************************************
181  * Code
182  ******************************************************************************/
183 
184 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
185 static void SerialManager_AddTail(list_label_t *queue, serial_manager_write_handle_t *node)
186 {
187  (void)LIST_AddTail(queue, &node->link);
188 }
189 
190 static void SerialManager_RemoveHead(list_label_t *queue)
191 {
192  (void)LIST_RemoveHead(queue);
193 }
194 #endif
195 
196 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
197 
199 {
201  serial_manager_write_handle_t *writeHandle =
202  (serial_manager_write_handle_t *)(void *)LIST_GetHead(&handle->runningWriteHandleHead);
203 
204  if (writeHandle != NULL)
205  {
206  switch (handle->type)
207  {
208 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
209  case kSerialPort_Uart:
210  status = Serial_UartWrite(((serial_handle_t)&handle->lowLevelhandleBuffer[0]),
211  writeHandle->transfer.buffer, writeHandle->transfer.length);
212  break;
213 #endif
214 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
215  case kSerialPort_UsbCdc:
216  status = Serial_UsbCdcWrite(((serial_handle_t)&handle->lowLevelhandleBuffer[0]),
217  writeHandle->transfer.buffer, writeHandle->transfer.length);
218  break;
219 #endif
220 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
221  case kSerialPort_Swo:
222  status = Serial_SwoWrite(((serial_handle_t)&handle->lowLevelhandleBuffer[0]),
223  writeHandle->transfer.buffer, writeHandle->transfer.length);
224  break;
225 #endif
226 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
228  status = Serial_UsbCdcVirtualWrite(((serial_handle_t)&handle->lowLevelhandleBuffer[0]),
229  writeHandle->transfer.buffer, writeHandle->transfer.length);
230  break;
231 #endif
232  default:
234  break;
235  }
236  }
237  return status;
238 }
239 
241  serial_manager_read_handle_t *readHandle,
242  uint8_t *buffer,
243  uint32_t length)
244 {
246 
247  if (NULL != readHandle)
248  {
249 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
250  if (handle->type == kSerialPort_UsbCdc)
251  {
252  status = Serial_UsbCdcRead(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), buffer, length);
253  }
254 #endif
255 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
256  if (handle->type == kSerialPort_UsbCdcVirtual)
257  {
258  status = Serial_UsbCdcVirtualRead(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), buffer, length);
259  }
260 #endif
261  }
262  return status;
263 }
264 
265 #else /*SERIAL_MANAGER_NON_BLOCKING_MODE > 0U*/
266 
268  serial_manager_write_handle_t *writeHandle,
269  uint8_t *buffer,
270  uint32_t length)
271 {
273 
274 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
275  if (kSerialPort_Uart == handle->type) /* Serial port UART */
276  {
277  status = Serial_UartWrite(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), buffer, length);
278  }
279  else
280 #endif
281 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
282  if (kSerialPort_UsbCdc == handle->type) /* Serial port UsbCdc */
283  {
284  status = Serial_UsbCdcWrite(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), buffer, length);
285  }
286  else
287 #endif
288 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
289  if (kSerialPort_Swo == handle->type) /* Serial port SWO */
290  {
291  status = Serial_SwoWrite(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), buffer, length);
292  }
293  else
294 #endif
295 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
296  if (kSerialPort_UsbCdcVirtual == handle->type) /* Serial port UsbCdcVirtual */
297  {
298  status = Serial_UsbCdcVirtualWrite(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), buffer, length);
299  }
300  else
301 #endif
302  {
303  /*MISRA rule*/
304  }
305  return status;
306 }
307 
309  serial_manager_read_handle_t *readHandle,
310  uint8_t *buffer,
311  uint32_t length)
312 {
314 
315 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
316  if (kSerialPort_Uart == handle->type) /* Serial port UART */
317  {
318  status = Serial_UartRead(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), buffer, length);
319  }
320  else
321 #endif
322 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
323  if (kSerialPort_UsbCdc == handle->type) /* Serial port UsbCdc */
324  {
325  status = Serial_UsbCdcRead(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), buffer, length);
326  }
327  else
328 #endif
329 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
330  if (kSerialPort_Swo == handle->type) /* Serial port SWO */
331  {
332  status = Serial_SwoRead(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), buffer, length);
333  }
334  else
335 #endif
336 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
337  if (kSerialPort_UsbCdcVirtual == handle->type) /* Serial port UsbCdcVirtual */
338  {
339  status = Serial_UsbCdcVirtualRead(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), buffer, length);
340  }
341 #endif
342  {
343  /*MISRA rule*/
344  }
345  return status;
346 }
347 #endif /*SERIAL_MANAGER_NON_BLOCKING_MODE > 0U*/
348 
349 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
350 static void SerialManager_IsrFunction(serial_manager_handle_t *handle)
351 {
352  uint32_t regPrimask = DisableGlobalIRQ();
353  switch (handle->type)
354  {
355 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
356  case kSerialPort_Uart:
357  Serial_UartIsrFunction(((serial_handle_t)&handle->lowLevelhandleBuffer[0]));
358  break;
359 #endif
360 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
361  case kSerialPort_UsbCdc:
362  Serial_UsbCdcIsrFunction(((serial_handle_t)&handle->lowLevelhandleBuffer[0]));
363  break;
364 #endif
365 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
366  case kSerialPort_Swo:
367  Serial_SwoIsrFunction(((serial_handle_t)&handle->lowLevelhandleBuffer[0]));
368  break;
369 #endif
370 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
372  Serial_UsbCdcVirtualIsrFunction(((serial_handle_t)&handle->lowLevelhandleBuffer[0]));
373  break;
374 #endif
375  default:
376  /*MISRA rule 16.4*/
377  break;
378  }
379  EnableGlobalIRQ(regPrimask);
380 }
381 
382 static void SerialManager_Task(void *param)
383 {
385  serial_manager_write_handle_t *serialWriteHandle;
386  serial_manager_read_handle_t *serialReadHandle;
387  uint32_t primask;
389 
390  if (NULL != handle)
391  {
392 #if defined(OSA_USED)
393 
394 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
395 #else
396  osa_event_flags_t ev = 0;
397 
398  do
399  {
400  if (KOSA_StatusSuccess ==
401  OSA_EventWait((osa_event_handle_t)handle->event, osaEventFlagsAll_c, false, osaWaitForever_c, &ev))
402  {
403  if (ev & SERIAL_EVENT_DATA_SENT)
404 #endif
405 
406 #endif
407  {
408  serialWriteHandle =
409  (serial_manager_write_handle_t *)(void *)LIST_GetHead(&handle->completedWriteHandleHead);
410  while (NULL != serialWriteHandle)
411  {
412  SerialManager_RemoveHead(&handle->completedWriteHandleHead);
413  msg.buffer = serialWriteHandle->transfer.buffer;
414  msg.length = serialWriteHandle->transfer.soFar;
415  serialWriteHandle->transfer.buffer = NULL;
416  if (NULL != serialWriteHandle->callback)
417  {
418  serialWriteHandle->callback(serialWriteHandle->callbackParam, &msg,
419  serialWriteHandle->transfer.status);
420  }
421  serialWriteHandle =
422  (serial_manager_write_handle_t *)(void *)LIST_GetHead(&handle->completedWriteHandleHead);
423  }
424  }
425 #if defined(OSA_USED)
426 
427 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
428 #else
430 #endif
431 
432 #endif
433  {
434  primask = DisableGlobalIRQ();
435  serialReadHandle = handle->openedReadHandleHead;
436  EnableGlobalIRQ(primask);
437 
438  if (NULL != serialReadHandle)
439  {
440  if (NULL != serialReadHandle->transfer.buffer)
441  {
442  if (serialReadHandle->transfer.soFar >= serialReadHandle->transfer.length)
443  {
444  msg.buffer = serialReadHandle->transfer.buffer;
445  msg.length = serialReadHandle->transfer.soFar;
446  serialReadHandle->transfer.buffer = NULL;
447  if (NULL != serialReadHandle->callback)
448  {
449  serialReadHandle->callback(serialReadHandle->callbackParam, &msg,
450  serialReadHandle->transfer.status);
451  }
452  }
453  }
454  }
455  }
456 #if defined(OSA_USED)
457 
458 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
459 #else
460  }
461  } while (gUseRtos_c);
462 #endif
463 
464 #endif
465  }
466 }
467 #endif
468 
469 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
470 static void SerialManager_TxCallback(void *callbackParam,
473 {
474  serial_manager_handle_t *handle;
475  serial_manager_write_handle_t *writeHandle;
476 
477  assert(callbackParam);
478  assert(message);
479 
480  handle = (serial_manager_handle_t *)callbackParam;
481 
482  writeHandle = (serial_manager_write_handle_t *)(void *)LIST_GetHead(&handle->runningWriteHandleHead);
483 
484  if (NULL != writeHandle)
485  {
486  SerialManager_RemoveHead(&handle->runningWriteHandleHead);
487  (void)SerialManager_StartWriting(handle);
488  writeHandle->transfer.soFar = message->length;
489  writeHandle->transfer.status = status;
490  if (kSerialManager_TransmissionNonBlocking == writeHandle->transfer.mode)
491  {
492  SerialManager_AddTail(&handle->completedWriteHandleHead, writeHandle);
493 #if defined(OSA_USED)
494 
495 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
496  handle->commontaskMsg.callback = SerialManager_Task;
497  handle->commontaskMsg.callbackParam = handle;
498  COMMON_TASK_post_message(&handle->commontaskMsg);
499 #else
500  (void)OSA_EventSet((osa_event_handle_t)handle->event, SERIAL_EVENT_DATA_SENT);
501 #endif
502 
503 #else
504  SerialManager_Task(handle);
505 #endif
506  }
507  else
508  {
509  writeHandle->transfer.buffer = NULL;
510  }
511  }
512 }
513 
514 static void SerialManager_RxCallback(void *callbackParam,
517 {
518  serial_manager_handle_t *handle;
519  uint32_t ringBufferLength;
520  uint32_t primask;
521 
522  assert(callbackParam);
523  assert(message);
524 
525  handle = (serial_manager_handle_t *)callbackParam;
526 
528 
529  for (uint32_t i = 0; i < message->length; i++)
530  {
531  handle->ringBuffer.ringBuffer[handle->ringBuffer.ringHead++] = message->buffer[i];
532  if (handle->ringBuffer.ringHead >= handle->ringBuffer.ringBufferSize)
533  {
534  handle->ringBuffer.ringHead = 0U;
535  }
536  if (handle->ringBuffer.ringHead == handle->ringBuffer.ringTail)
537  {
539  handle->ringBuffer.ringTail++;
540  if (handle->ringBuffer.ringTail >= handle->ringBuffer.ringBufferSize)
541  {
542  handle->ringBuffer.ringTail = 0U;
543  }
544  }
545  }
546 
547  ringBufferLength = handle->ringBuffer.ringHead + handle->ringBuffer.ringBufferSize - handle->ringBuffer.ringTail;
548  ringBufferLength = ringBufferLength % handle->ringBuffer.ringBufferSize;
549 
550  primask = DisableGlobalIRQ();
551  if ((NULL != handle->openedReadHandleHead) && (NULL != handle->openedReadHandleHead->transfer.buffer))
552  {
553  if (handle->openedReadHandleHead->transfer.length > handle->openedReadHandleHead->transfer.soFar)
554  {
555  uint32_t remainLength =
556  handle->openedReadHandleHead->transfer.length - handle->openedReadHandleHead->transfer.soFar;
557  for (uint32_t i = 0; i < MIN(ringBufferLength, remainLength); i++)
558  {
559  handle->openedReadHandleHead->transfer.buffer[handle->openedReadHandleHead->transfer.soFar] =
560  handle->ringBuffer.ringBuffer[handle->ringBuffer.ringTail];
561  handle->ringBuffer.ringTail++;
562  handle->openedReadHandleHead->transfer.soFar++;
563  if (handle->ringBuffer.ringTail >= handle->ringBuffer.ringBufferSize)
564  {
565  handle->ringBuffer.ringTail = 0U;
566  }
567  }
568  ringBufferLength = ringBufferLength - MIN(ringBufferLength, remainLength);
569  }
570 
571  if (handle->openedReadHandleHead->transfer.length > handle->openedReadHandleHead->transfer.soFar)
572  {
573  }
574  else
575  {
576  if (kSerialManager_TransmissionBlocking == handle->openedReadHandleHead->transfer.mode)
577  {
578  handle->openedReadHandleHead->transfer.buffer = NULL;
579  }
580  else
581  {
582  handle->openedReadHandleHead->transfer.status = kStatus_SerialManager_Success;
583 
584 #if defined(OSA_USED)
585 
586 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
587  handle->commontaskMsg.callback = SerialManager_Task;
588  handle->commontaskMsg.callbackParam = handle;
589  COMMON_TASK_post_message(&handle->commontaskMsg);
590 #else
591  (void)OSA_EventSet((osa_event_handle_t)handle->event, SERIAL_EVENT_DATA_RECEIVED);
592 #endif
593 
594 #else
595  SerialManager_Task(handle);
596 #endif
597  }
598  }
599  }
600 
601  if (0U != ringBufferLength)
602  {
603  message->buffer = NULL;
604  message->length = ringBufferLength;
605  if ((NULL != handle->openedReadHandleHead) && (NULL != handle->openedReadHandleHead->callback))
606  {
607  handle->openedReadHandleHead->callback(handle->openedReadHandleHead->callbackParam, message, status);
608  }
609  }
610 
611  ringBufferLength = handle->ringBuffer.ringBufferSize - 1U - ringBufferLength;
612 
613  if (NULL != handle->openedReadHandleHead)
614  {
615  (void)SerialManager_StartReading(handle, handle->openedReadHandleHead, NULL, ringBufferLength);
616  }
617  EnableGlobalIRQ(primask);
618 }
619 
621  uint8_t *buffer,
622  uint32_t length,
623  serial_manager_transmission_mode_t mode)
624 {
625  serial_manager_write_handle_t *serialWriteHandle;
626  serial_manager_handle_t *handle;
628  uint32_t primask;
629  uint8_t isEmpty = 0U;
630 
631  assert(writeHandle);
632  assert(buffer);
633  assert(length);
634 
635  serialWriteHandle = (serial_manager_write_handle_t *)writeHandle;
636  handle = serialWriteHandle->serialManagerHandle;
637 
638  assert(handle);
639  assert(SERIAL_MANAGER_WRITE_TAG == serialWriteHandle->tag);
640  assert(!((kSerialManager_TransmissionNonBlocking == mode) && (NULL == serialWriteHandle->callback)));
641 
642  primask = DisableGlobalIRQ();
643  if (NULL != serialWriteHandle->transfer.buffer)
644  {
645  EnableGlobalIRQ(primask);
647  }
648  serialWriteHandle->transfer.buffer = buffer;
649  serialWriteHandle->transfer.length = length;
650  serialWriteHandle->transfer.soFar = 0U;
651  serialWriteHandle->transfer.mode = mode;
652 
653  if (NULL == LIST_GetHead(&handle->runningWriteHandleHead))
654  {
655  isEmpty = 1U;
656  }
657  SerialManager_AddTail(&handle->runningWriteHandleHead, serialWriteHandle);
658  EnableGlobalIRQ(primask);
659 
660  if (0U != isEmpty)
661  {
662  status = SerialManager_StartWriting(handle);
664  {
665 #if (USB_CDC_SERIAL_MANAGER_RUN_NO_HOST == 1)
667  {
668  SerialManager_RemoveHead(&handle->runningWriteHandleHead);
669  serialWriteHandle->transfer.buffer = 0;
670  serialWriteHandle->transfer.length = 0;
671  }
672 #endif /* USB_CDC_SERIAL_MANAGER_RUN_NO_HOST == 1 */
673  return status;
674  }
675  }
676 
677  if (kSerialManager_TransmissionBlocking == mode)
678  {
679  while (serialWriteHandle->transfer.length > serialWriteHandle->transfer.soFar)
680  {
681 #if defined(__GIC_PRIO_BITS)
682  if (0x13 == (__get_CPSR() & CPSR_M_Msk))
683 #else
684  if (0U != __get_IPSR())
685 #endif
686  {
687  SerialManager_IsrFunction(handle);
688  }
689  }
690  }
692 }
693 
695  uint8_t *buffer,
696  uint32_t length,
697  serial_manager_transmission_mode_t mode,
698  uint32_t *receivedLength)
699 {
700  serial_manager_read_handle_t *serialReadHandle;
701  serial_manager_handle_t *handle;
702  uint32_t dataLength;
703  uint32_t primask;
704 
705  assert(readHandle);
706  assert(buffer);
707  assert(length);
708 
709  serialReadHandle = (serial_manager_read_handle_t *)readHandle;
710  handle = serialReadHandle->serialManagerHandle;
711 
712  assert(handle);
713  assert(SERIAL_MANAGER_READ_TAG == serialReadHandle->tag);
714  assert(!((kSerialManager_TransmissionNonBlocking == mode) && (NULL == serialReadHandle->callback)));
715 
716  primask = DisableGlobalIRQ();
717  if (NULL != serialReadHandle->transfer.buffer)
718  {
719  EnableGlobalIRQ(primask);
721  }
722  serialReadHandle->transfer.buffer = buffer;
723  serialReadHandle->transfer.length = length;
724  serialReadHandle->transfer.soFar = 0U;
725  serialReadHandle->transfer.mode = mode;
726 
727  dataLength = handle->ringBuffer.ringHead + handle->ringBuffer.ringBufferSize - handle->ringBuffer.ringTail;
728  dataLength = dataLength % handle->ringBuffer.ringBufferSize;
729 
730  for (serialReadHandle->transfer.soFar = 0U; serialReadHandle->transfer.soFar < MIN(dataLength, length);
731  serialReadHandle->transfer.soFar++)
732  {
733  buffer[serialReadHandle->transfer.soFar] = handle->ringBuffer.ringBuffer[handle->ringBuffer.ringTail];
734  handle->ringBuffer.ringTail++;
735  if (handle->ringBuffer.ringTail >= handle->ringBuffer.ringBufferSize)
736  {
737  handle->ringBuffer.ringTail = 0U;
738  }
739  }
740 
741  dataLength = handle->ringBuffer.ringHead + handle->ringBuffer.ringBufferSize - handle->ringBuffer.ringTail;
742  dataLength = dataLength % handle->ringBuffer.ringBufferSize;
743  dataLength = handle->ringBuffer.ringBufferSize - 1U - dataLength;
744 
745  (void)SerialManager_StartReading(handle, readHandle, NULL, dataLength);
746 
747  if (NULL != receivedLength)
748  {
749  *receivedLength = serialReadHandle->transfer.soFar;
750  serialReadHandle->transfer.buffer = NULL;
751  EnableGlobalIRQ(primask);
752  }
753  else
754  {
755  if (serialReadHandle->transfer.soFar >= serialReadHandle->transfer.length)
756  {
757  serialReadHandle->transfer.buffer = NULL;
758  EnableGlobalIRQ(primask);
759  if (kSerialManager_TransmissionNonBlocking == mode)
760  {
761  if (NULL != serialReadHandle->callback)
762  {
764  msg.buffer = buffer;
765  msg.length = serialReadHandle->transfer.soFar;
766  serialReadHandle->callback(serialReadHandle->callbackParam, &msg, kStatus_SerialManager_Success);
767  }
768  }
769  }
770  else
771  {
772  EnableGlobalIRQ(primask);
773  }
774 
775  if (kSerialManager_TransmissionBlocking == mode)
776  {
777  while (serialReadHandle->transfer.length > serialReadHandle->transfer.soFar)
778  {
779  }
780  }
781  }
782 
784 }
785 
786 #else
787 
788 static serial_manager_status_t SerialManager_Write(serial_write_handle_t writeHandle, uint8_t *buffer, uint32_t length)
789 {
790  serial_manager_write_handle_t *serialWriteHandle;
791  serial_manager_handle_t *handle;
792 
793  assert(writeHandle);
794  assert(buffer);
795  assert(length);
796 
797  serialWriteHandle = (serial_manager_write_handle_t *)writeHandle;
798  handle = serialWriteHandle->serialManagerHandle;
799 
800  assert(handle);
801 
802  return SerialManager_StartWriting(handle, serialWriteHandle, buffer, length);
803 }
804 
805 static serial_manager_status_t SerialManager_Read(serial_read_handle_t readHandle, uint8_t *buffer, uint32_t length)
806 {
807  serial_manager_read_handle_t *serialReadHandle;
808  serial_manager_handle_t *handle;
809 
810  assert(readHandle);
811  assert(buffer);
812  assert(length);
813 
814  serialReadHandle = (serial_manager_read_handle_t *)readHandle;
815  handle = serialReadHandle->serialManagerHandle;
816 
817  assert(handle);
818 
819  return SerialManager_StartReading(handle, serialReadHandle, buffer, length);
820 }
821 #endif
822 
824 {
825  serial_manager_handle_t *handle;
827 
828  assert(config);
829 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
830  assert(config->ringBuffer);
831  assert(config->ringBufferSize);
832 #endif
833  assert(serialHandle);
835 
836  handle = (serial_manager_handle_t *)serialHandle;
837 
838  (void)memset(handle, 0, SERIAL_MANAGER_HANDLE_SIZE);
839 
840 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
841 
842 #if defined(OSA_USED)
843 
844 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
845 
846  COMMON_TASK_init();
847 
848 #else
849  if (KOSA_StatusSuccess != OSA_EventCreate((osa_event_handle_t)handle->event, true))
850  {
852  }
853 
854  if (KOSA_StatusSuccess != OSA_TaskCreate((osa_task_handle_t)handle->taskId, OSA_TASK(SerialManager_Task), handle))
855  {
857  }
858 #endif
859 
860 #endif
861 
862 #endif
863 
864  handle->type = config->type;
865 
866 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
867  handle->ringBuffer.ringBuffer = config->ringBuffer;
868  handle->ringBuffer.ringBufferSize = config->ringBufferSize;
869 #endif
870 
871  switch (config->type)
872  {
873 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
874  case kSerialPort_Uart:
875  status = Serial_UartInit(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), config->portConfig);
876 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
878  {
879  status = Serial_UartInstallTxCallback(((serial_handle_t)&handle->lowLevelhandleBuffer[0]),
880  SerialManager_TxCallback, handle);
882  {
883  status = Serial_UartInstallRxCallback(((serial_handle_t)&handle->lowLevelhandleBuffer[0]),
884  SerialManager_RxCallback, handle);
885  }
886  }
887 #endif
888  break;
889 #endif
890 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
891  case kSerialPort_UsbCdc:
892  status = Serial_UsbCdcInit(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), config->portConfig);
893 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
894  if (kStatus_SerialManager_Success == status)
895  {
896  status = Serial_UsbCdcInstallTxCallback(((serial_handle_t)&handle->lowLevelhandleBuffer[0]),
897  SerialManager_TxCallback, handle);
898  if (kStatus_SerialManager_Success == status)
899  {
900  status = Serial_UsbCdcInstallRxCallback(((serial_handle_t)&handle->lowLevelhandleBuffer[0]),
901  SerialManager_RxCallback, handle);
902  }
903  }
904 #endif
905  break;
906 #endif
907 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
908  case kSerialPort_Swo:
909  status = Serial_SwoInit(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), config->portConfig);
910 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
911  if (kStatus_SerialManager_Success == status)
912  {
913  status = Serial_SwoInstallTxCallback(((serial_handle_t)&handle->lowLevelhandleBuffer[0]),
914  SerialManager_TxCallback, handle);
915  }
916 #endif
917  break;
918 #endif
919 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
921  status = Serial_UsbCdcVirtualInit(((serial_handle_t)&handle->lowLevelhandleBuffer[0]), config->portConfig);
922 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
923  if (kStatus_SerialManager_Success == status)
924  {
925  status = Serial_UsbCdcVirtualInstallTxCallback(((serial_handle_t)&handle->lowLevelhandleBuffer[0]),
926  SerialManager_TxCallback, handle);
927  if (kStatus_SerialManager_Success == status)
928  {
929  status = Serial_UsbCdcVirtualInstallRxCallback(((serial_handle_t)&handle->lowLevelhandleBuffer[0]),
930  SerialManager_RxCallback, handle);
931  }
932  }
933 #endif
934  break;
935 #endif
936  default:
937  /*MISRA rule 16.4*/
938  break;
939  }
940 
941  return status;
942 }
943 
945 {
946  serial_manager_handle_t *handle;
947  uint32_t primask;
948 
949  assert(serialHandle);
950 
951  handle = (serial_manager_handle_t *)serialHandle;
952 
953  primask = DisableGlobalIRQ();
954  if ((NULL != handle->openedReadHandleHead) || (0U != handle->openedWriteHandleCount))
955  {
956  EnableGlobalIRQ(primask);
958  }
959  EnableGlobalIRQ(primask);
960 
961  switch (handle->type)
962  {
963 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
964  case kSerialPort_Uart:
965  (void)Serial_UartDeinit(((serial_handle_t)&handle->lowLevelhandleBuffer[0]));
966  break;
967 #endif
968 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
969  case kSerialPort_UsbCdc:
970  (void)Serial_UsbCdcDeinit(((serial_handle_t)&handle->lowLevelhandleBuffer[0]));
971  break;
972 #endif
973 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
974  case kSerialPort_Swo:
975  (void)Serial_SwoDeinit(((serial_handle_t)&handle->lowLevelhandleBuffer[0]));
976  break;
977 #endif
978 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
980  (void)Serial_UsbCdcVirtualDeinit(((serial_handle_t)&handle->lowLevelhandleBuffer[0]));
981  break;
982 #endif
983  default:
984  /*MISRA rule 16.4*/
985  break;
986  }
987 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
988 
989 #if defined(OSA_USED)
990 
991 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
992 #else
993  OSA_EventDestroy((osa_event_handle_t)handle->event);
994  OSA_TaskDestroy((osa_task_handle_t)handle->taskId);
995 #endif
996 
997 #endif
998 
999 #endif
1000 
1002 }
1003 
1005 {
1006  serial_manager_handle_t *handle;
1007  serial_manager_write_handle_t *serialWriteHandle;
1008  uint32_t primask;
1009 
1010  assert(serialHandle);
1011  assert(writeHandle);
1013 
1014  handle = (serial_manager_handle_t *)serialHandle;
1015  serialWriteHandle = (serial_manager_write_handle_t *)writeHandle;
1016 
1017  (void)memset(writeHandle, 0, SERIAL_MANAGER_WRITE_HANDLE_SIZE);
1018 
1019  primask = DisableGlobalIRQ();
1020  handle->openedWriteHandleCount++;
1021  EnableGlobalIRQ(primask);
1022 
1023  serialWriteHandle->serialManagerHandle = handle;
1024 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1025  serialWriteHandle->tag = SERIAL_MANAGER_WRITE_TAG;
1026 #endif
1027 
1029 }
1030 
1032 {
1033  serial_manager_handle_t *handle;
1034  serial_manager_write_handle_t *serialWriteHandle;
1035  uint32_t primask;
1036 
1037  assert(writeHandle);
1038 
1039  serialWriteHandle = (serial_manager_write_handle_t *)writeHandle;
1040  handle = (serial_manager_handle_t *)(void *)serialWriteHandle->serialManagerHandle;
1041 
1042  assert(handle);
1043 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1044  assert(SERIAL_MANAGER_WRITE_TAG == serialWriteHandle->tag);
1045 #endif
1046 
1047 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1048  (void)SerialManager_CancelWriting(writeHandle);
1049 #endif
1050  primask = DisableGlobalIRQ();
1051  if (handle->openedWriteHandleCount > 0U)
1052  {
1053  handle->openedWriteHandleCount--;
1054  }
1055  EnableGlobalIRQ(primask);
1056 
1057  (void)memset(writeHandle, 0, SERIAL_MANAGER_WRITE_HANDLE_SIZE);
1058 
1060 }
1061 
1063 {
1064  serial_manager_handle_t *handle;
1065  serial_manager_read_handle_t *serialReadHandle;
1066  uint32_t primask;
1067 
1068  assert(serialHandle);
1069  assert(readHandle);
1071 
1072  handle = (serial_manager_handle_t *)serialHandle;
1073  serialReadHandle = (serial_manager_read_handle_t *)readHandle;
1074 
1075  primask = DisableGlobalIRQ();
1076  if (handle->openedReadHandleHead != NULL)
1077  {
1078  EnableGlobalIRQ(primask);
1080  }
1081  handle->openedReadHandleHead = serialReadHandle;
1082  EnableGlobalIRQ(primask);
1083 
1084  (void)memset(readHandle, 0, SERIAL_MANAGER_READ_HANDLE_SIZE);
1085 
1086  serialReadHandle->serialManagerHandle = handle;
1087 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1088  serialReadHandle->tag = SERIAL_MANAGER_READ_TAG;
1089 #endif
1090 
1092 }
1093 
1095 {
1096  serial_manager_handle_t *handle;
1097  serial_manager_read_handle_t *serialReadHandle;
1098  uint32_t primask;
1099 
1100  assert(readHandle);
1101 
1102  serialReadHandle = (serial_manager_read_handle_t *)readHandle;
1103  handle = (serial_manager_handle_t *)(void *)serialReadHandle->serialManagerHandle;
1104 
1105  assert((NULL != handle) && (handle->openedReadHandleHead == serialReadHandle));
1106 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1107  assert(SERIAL_MANAGER_READ_TAG == serialReadHandle->tag);
1108 #endif
1109 
1110 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1111  (void)SerialManager_CancelReading(readHandle);
1112 #endif
1113 
1114  primask = DisableGlobalIRQ();
1115  handle->openedReadHandleHead = NULL;
1116  EnableGlobalIRQ(primask);
1117 
1118  (void)memset(readHandle, 0, SERIAL_MANAGER_READ_HANDLE_SIZE);
1119 
1121 }
1122 
1123 serial_manager_status_t SerialManager_WriteBlocking(serial_write_handle_t writeHandle, uint8_t *buffer, uint32_t length)
1124 {
1125 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1126  return SerialManager_Write(writeHandle, buffer, length, kSerialManager_TransmissionBlocking);
1127 #else
1128  return SerialManager_Write(writeHandle, buffer, length);
1129 #endif
1130 }
1131 
1132 serial_manager_status_t SerialManager_ReadBlocking(serial_read_handle_t readHandle, uint8_t *buffer, uint32_t length)
1133 {
1134 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1135  return SerialManager_Read(readHandle, buffer, length, kSerialManager_TransmissionBlocking, NULL);
1136 #else
1137  return SerialManager_Read(readHandle, buffer, length);
1138 #endif
1139 }
1140 
1141 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
1142 serial_manager_status_t SerialManager_WriteNonBlocking(serial_write_handle_t writeHandle,
1143  uint8_t *buffer,
1144  uint32_t length)
1145 {
1146  return SerialManager_Write(writeHandle, buffer, length, kSerialManager_TransmissionNonBlocking);
1147 }
1148 
1149 serial_manager_status_t SerialManager_ReadNonBlocking(serial_read_handle_t readHandle, uint8_t *buffer, uint32_t length)
1150 {
1151  return SerialManager_Read(readHandle, buffer, length, kSerialManager_TransmissionNonBlocking, NULL);
1152 }
1153 
1154 serial_manager_status_t SerialManager_CancelWriting(serial_write_handle_t writeHandle)
1155 {
1156  serial_manager_write_handle_t *serialWriteHandle;
1157  uint32_t primask;
1158  uint8_t isNotUsed = 0;
1159 
1160  assert(writeHandle);
1161 
1162  serialWriteHandle = (serial_manager_write_handle_t *)writeHandle;
1163 
1164  assert(serialWriteHandle->serialManagerHandle);
1165  assert(SERIAL_MANAGER_WRITE_TAG == serialWriteHandle->tag);
1166 
1167  if ((NULL != serialWriteHandle->transfer.buffer) &&
1168  (kSerialManager_TransmissionBlocking == serialWriteHandle->transfer.mode))
1169  {
1171  }
1172 
1173  primask = DisableGlobalIRQ();
1174  if (serialWriteHandle != (serial_manager_write_handle_t *)(void *)LIST_GetHead(
1175  &serialWriteHandle->serialManagerHandle->runningWriteHandleHead))
1176  {
1177  (void)LIST_RemoveElement(&serialWriteHandle->link);
1178  isNotUsed = 1;
1179  }
1180  EnableGlobalIRQ(primask);
1181 
1182  if (0U != isNotUsed)
1183  {
1184  serialWriteHandle->transfer.soFar = 0;
1185  serialWriteHandle->transfer.status = kStatus_SerialManager_Canceled;
1186 
1187  SerialManager_AddTail(&serialWriteHandle->serialManagerHandle->completedWriteHandleHead, serialWriteHandle);
1188 #if defined(OSA_USED)
1189 
1190 #if (defined(SERIAL_MANAGER_USE_COMMON_TASK) && (SERIAL_MANAGER_USE_COMMON_TASK > 0U))
1191  serialWriteHandle->serialManagerHandle->commontaskMsg.callback = SerialManager_Task;
1192  serialWriteHandle->serialManagerHandle->commontaskMsg.callbackParam = serialWriteHandle->serialManagerHandle;
1193  COMMON_TASK_post_message(&serialWriteHandle->serialManagerHandle->commontaskMsg);
1194 #else
1195  (void)OSA_EventSet((osa_event_handle_t)serialWriteHandle->serialManagerHandle->event, SERIAL_EVENT_DATA_SENT);
1196 #endif
1197 
1198 #else
1199  SerialManager_Task(serialWriteHandle->serialManagerHandle);
1200 #endif
1201  }
1202  else
1203  {
1204  switch (serialWriteHandle->serialManagerHandle->type)
1205  {
1206 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
1207  case kSerialPort_Uart:
1208  (void)Serial_UartCancelWrite(
1209  ((serial_handle_t)&serialWriteHandle->serialManagerHandle->lowLevelhandleBuffer[0]));
1210  break;
1211 #endif
1212 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
1213  case kSerialPort_UsbCdc:
1214  (void)Serial_UsbCdcCancelWrite(
1215  ((serial_handle_t)&serialWriteHandle->serialManagerHandle->lowLevelhandleBuffer[0]));
1216  break;
1217 #endif
1218 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
1219  case kSerialPort_Swo:
1220  (void)Serial_SwoCancelWrite(
1221  ((serial_handle_t)&serialWriteHandle->serialManagerHandle->lowLevelhandleBuffer[0]));
1222  break;
1223 #endif
1224 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
1226  (void)Serial_UsbCdcVirtualCancelWrite(
1227  ((serial_handle_t)&serialWriteHandle->serialManagerHandle->lowLevelhandleBuffer[0]));
1228  break;
1229 #endif
1230  default:
1231  /*MISRA rule 16.4*/
1232  break;
1233  }
1234  }
1235 
1236  (void)SerialManager_StartWriting(serialWriteHandle->serialManagerHandle);
1237 
1239 }
1240 
1241 serial_manager_status_t SerialManager_CancelReading(serial_read_handle_t readHandle)
1242 {
1243  serial_manager_read_handle_t *serialReadHandle;
1245  uint8_t *buffer;
1246  uint32_t primask;
1247 
1248  assert(readHandle);
1249 
1250  serialReadHandle = (serial_manager_read_handle_t *)readHandle;
1251 
1252  assert(SERIAL_MANAGER_READ_TAG == serialReadHandle->tag);
1253 
1254  if ((NULL != serialReadHandle->transfer.buffer) &&
1255  (kSerialManager_TransmissionBlocking == serialReadHandle->transfer.mode))
1256  {
1258  }
1259 
1260  primask = DisableGlobalIRQ();
1261  buffer = serialReadHandle->transfer.buffer;
1262  serialReadHandle->transfer.buffer = NULL;
1263  serialReadHandle->transfer.length = 0;
1264  msg.buffer = buffer;
1265  msg.length = serialReadHandle->transfer.soFar;
1266  EnableGlobalIRQ(primask);
1267 
1268  if (NULL != buffer)
1269  {
1270  if (NULL != serialReadHandle->callback)
1271  {
1272  serialReadHandle->callback(serialReadHandle->callbackParam, &msg, kStatus_SerialManager_Canceled);
1273  }
1274  }
1276 }
1277 
1278 serial_manager_status_t SerialManager_TryRead(serial_read_handle_t readHandle,
1279  uint8_t *buffer,
1280  uint32_t length,
1281  uint32_t *receivedLength)
1282 {
1283  assert(receivedLength);
1284 
1285  return SerialManager_Read(readHandle, buffer, length, kSerialManager_TransmissionBlocking, receivedLength);
1286 }
1287 
1288 serial_manager_status_t SerialManager_InstallTxCallback(serial_write_handle_t writeHandle,
1289  serial_manager_callback_t callback,
1290  void *callbackParam)
1291 {
1292  serial_manager_write_handle_t *serialWriteHandle;
1293 
1294  assert(writeHandle);
1295 
1296  serialWriteHandle = (serial_manager_write_handle_t *)writeHandle;
1297 
1298  assert(SERIAL_MANAGER_WRITE_TAG == serialWriteHandle->tag);
1299 
1300  serialWriteHandle->callbackParam = callbackParam;
1301  serialWriteHandle->callback = callback;
1302 
1304 }
1305 
1306 serial_manager_status_t SerialManager_InstallRxCallback(serial_read_handle_t readHandle,
1307  serial_manager_callback_t callback,
1308  void *callbackParam)
1309 {
1310  serial_manager_read_handle_t *serialReadHandle;
1311 
1312  assert(readHandle);
1313 
1314  serialReadHandle = (serial_manager_read_handle_t *)readHandle;
1315 
1316  assert(SERIAL_MANAGER_READ_TAG == serialReadHandle->tag);
1317 
1318  serialReadHandle->callbackParam = callbackParam;
1319  serialReadHandle->callback = callback;
1320 
1322 }
1323 #endif
1324 
1326 {
1327  serial_manager_handle_t *handle;
1329 
1330  assert(serialHandle);
1331 
1332  handle = (serial_manager_handle_t *)serialHandle;
1333 
1334  switch (handle->type)
1335  {
1336 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
1337  case kSerialPort_Uart:
1338  status = Serial_UartEnterLowpower(((serial_handle_t)&handle->lowLevelhandleBuffer[0]));
1339  break;
1340 #endif
1341 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
1342  case kSerialPort_UsbCdc:
1343  break;
1344 #endif
1345 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
1346  case kSerialPort_Swo:
1347  break;
1348 #endif
1349 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
1351  break;
1352 #endif
1353  default:
1354  /*MISRA rule 16.4*/
1355  break;
1356  }
1357  return status;
1358 }
1359 
1361 {
1362  serial_manager_handle_t *handle;
1364 
1365  assert(serialHandle);
1366 
1367  handle = (serial_manager_handle_t *)serialHandle;
1368 
1369  switch (handle->type)
1370  {
1371 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
1372  case kSerialPort_Uart:
1373  status = Serial_UartExitLowpower(((serial_handle_t)&handle->lowLevelhandleBuffer[0]));
1374  break;
1375 #endif
1376 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
1377  case kSerialPort_UsbCdc:
1378  break;
1379 #endif
1380 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
1381  case kSerialPort_Swo:
1382  break;
1383 #endif
1384 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
1386  break;
1387 #endif
1388  default:
1389  /*MISRA rule 16.4*/
1390  break;
1391  }
1392  return status;
1393 }
list_element_tag
The list element.
Definition: generic_list.h:65
fsl_common.h
SerialManager_Init
serial_manager_status_t SerialManager_Init(serial_handle_t serialHandle, serial_manager_config_t *config)
Initializes a serial manager module with the serial manager handle and the user configuration structu...
Definition: serial_manager.c:823
serial_manager_status_t
enum _serial_manager_status serial_manager_status_t
serial manager error code
_serial_manager_send_handle::serialManagerHandle
struct _serial_manager_handle * serialManagerHandle
Definition: serial_manager.c:80
_serial_manager_callback_message::length
uint32_t length
Definition: serial_manager.h:255
SERIAL_MANAGER_TASK_PRIORITY
#define SERIAL_MANAGER_TASK_PRIORITY
Macro to set serial manager task priority.
Definition: serial_manager.h:197
serial_port_type_t
enum _serial_port_type serial_port_type_t
serial port type
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
kSerialPort_UsbCdcVirtual
@ kSerialPort_UsbCdcVirtual
Definition: serial_manager.h:220
SERIAL_MANAGER_TASK_STACK_SIZE
#define SERIAL_MANAGER_TASK_STACK_SIZE
Macro to set serial manager task stack size.
Definition: serial_manager.h:202
kStatus_SerialManager_NotConnected
@ kStatus_SerialManager_NotConnected
Definition: serial_manager.h:248
_serial_manager_config
serial manager config structure
Definition: serial_manager.h:224
generic_list.h
SERIAL_EVENT_DATA_RECEIVED
#define SERIAL_EVENT_DATA_RECEIVED
Definition: serial_manager.c:49
_serial_manager_callback_message::buffer
uint8_t * buffer
Definition: serial_manager.h:254
kStatus_SerialManager_Canceled
@ kStatus_SerialManager_Canceled
Definition: serial_manager.h:243
serial_handle_t
void * serial_handle_t
The handle of the serial manager module.
Definition: serial_manager.h:206
serial_port_internal.h
kStatus_SerialManager_Error
@ kStatus_SerialManager_Error
Definition: serial_manager.h:240
SERIAL_MANAGER_HANDLE_SIZE
#define SERIAL_MANAGER_HANDLE_SIZE
SERIAL_PORT_UART_HANDLE_SIZE/SERIAL_PORT_USB_CDC_HANDLE_SIZE + serial manager dedicated size.
Definition: serial_manager.h:133
serial_manager_write_handle_t
struct _serial_manager_send_handle serial_manager_write_handle_t
SERIAL_MANAGER_READ_HANDLE_SIZE
#define SERIAL_MANAGER_READ_HANDLE_SIZE
Definition: serial_manager.h:57
serial_manager_handle_t
struct _serial_manager_handle serial_manager_handle_t
SerialManager_StartReading
static serial_manager_status_t SerialManager_StartReading(serial_manager_handle_t *handle, serial_manager_read_handle_t *readHandle, uint8_t *buffer, uint32_t length)
Definition: serial_manager.c:308
serial_manager.h
kStatus_SerialManager_RingBufferOverflow
@ kStatus_SerialManager_RingBufferOverflow
Definition: serial_manager.h:246
kStatus_SerialManager_Notify
@ kStatus_SerialManager_Notify
Definition: serial_manager.h:242
SerialManager_CloseWriteHandle
serial_manager_status_t SerialManager_CloseWriteHandle(serial_write_handle_t writeHandle)
Closes a writing handle for the serial manager module.
Definition: serial_manager.c:1031
kSerialPort_Swo
@ kSerialPort_Swo
Definition: serial_manager.h:219
LIST_GetHead
list_element_handle_t LIST_GetHead(list_handle_t list)
Gets head element ID.
Definition: generic_list.c:246
_serial_manager_handle::type
serial_port_type_t type
Definition: serial_manager.c:148
_serial_manager_handle::lowLevelhandleBuffer
uint32_t lowLevelhandleBuffer[1]
Definition: serial_manager.c:115
SERIAL_MANAGER_READ_TAG
#define SERIAL_MANAGER_READ_TAG
Definition: serial_manager.c:53
SerialManager_Write
static serial_manager_status_t SerialManager_Write(serial_write_handle_t writeHandle, uint8_t *buffer, uint32_t length)
Definition: serial_manager.c:788
kStatus_SerialManager_Success
@ kStatus_SerialManager_Success
Definition: serial_manager.h:239
serial_read_handle_t
void * serial_read_handle_t
The read handle of the serial manager module.
Definition: serial_manager.h:212
_serial_manager_callback_message
Callback message structure.
Definition: serial_manager.h:252
SerialManager_ReadBlocking
serial_manager_status_t SerialManager_ReadBlocking(serial_read_handle_t readHandle, uint8_t *buffer, uint32_t length)
Reads data with the blocking mode.
Definition: serial_manager.c:1132
serial_manager_read_handle_t
serial_manager_write_handle_t serial_manager_read_handle_t
Definition: serial_manager.c:88
LIST_RemoveElement
list_status_t LIST_RemoveElement(list_element_handle_t element)
Unlinks an element from its list.
Definition: generic_list.c:310
LIST_AddTail
list_status_t LIST_AddTail(list_handle_t list, list_element_handle_t element)
Links element to the tail of the list.
Definition: generic_list.c:106
SerialManager_OpenWriteHandle
serial_manager_status_t SerialManager_OpenWriteHandle(serial_handle_t serialHandle, serial_write_handle_t writeHandle)
Opens a writing handle for the serial manager module.
Definition: serial_manager.c:1004
kSerialPort_Uart
@ kSerialPort_Uart
Definition: serial_manager.h:217
SerialManager_EnterLowpower
serial_manager_status_t SerialManager_EnterLowpower(serial_handle_t serialHandle)
Prepares to enter low power consumption.
Definition: serial_manager.c:1325
SerialManager_OpenReadHandle
serial_manager_status_t SerialManager_OpenReadHandle(serial_handle_t serialHandle, serial_read_handle_t readHandle)
Opens a reading handle for the serial manager module.
Definition: serial_manager.c:1062
kStatus_SerialManager_Busy
@ kStatus_SerialManager_Busy
Definition: serial_manager.h:241
SERIAL_EVENT_DATA_SENT
#define SERIAL_EVENT_DATA_SENT
Definition: serial_manager.c:50
_serial_manager_handle
Definition: serial_manager.c:105
_serial_manager_handle::openedReadHandleHead
serial_manager_read_handle_t *volatile openedReadHandleHead
Definition: serial_manager.c:111
kSerialPort_UsbCdc
@ kSerialPort_UsbCdc
Definition: serial_manager.h:218
_serial_manager_handle::openedWriteHandleCount
volatile uint32_t openedWriteHandleCount
Definition: serial_manager.c:112
DisableGlobalIRQ
static uint32_t DisableGlobalIRQ(void)
Disable the global IRQ.
Definition: fsl_common.h:552
serial_write_handle_t
void * serial_write_handle_t
The write handle of the serial manager module.
Definition: serial_manager.h:209
SERIAL_PORT_UART_HANDLE_SIZE
#define SERIAL_PORT_UART_HANDLE_SIZE
serial port uart handle size
Definition: serial_port_uart.h:26
SERIAL_MANAGER_WRITE_HANDLE_SIZE
#define SERIAL_MANAGER_WRITE_HANDLE_SIZE
Set serial manager write handle size.
Definition: serial_manager.h:56
SERIAL_MANAGER_WRITE_TAG
#define SERIAL_MANAGER_WRITE_TAG
Definition: serial_manager.c:52
serial_manager_callback_t
void(* serial_manager_callback_t)(void *callbackParam, serial_manager_callback_message_t *message, serial_manager_status_t status)
callback function
Definition: serial_manager.h:259
__get_IPSR
__STATIC_INLINE uint32_t __get_IPSR(void)
Get IPSR Register.
Definition: imxrt1050/imxrt1050-evkb/CMSIS/cmsis_armcc.h:183
SerialManager_Deinit
serial_manager_status_t SerialManager_Deinit(serial_handle_t serialHandle)
De-initializes the serial manager module instance.
Definition: serial_manager.c:944
_serial_manager_send_handle
Definition: serial_manager.c:74
SerialManager_WriteBlocking
serial_manager_status_t SerialManager_WriteBlocking(serial_write_handle_t writeHandle, uint8_t *buffer, uint32_t length)
Transmits data with the blocking mode.
Definition: serial_manager.c:1123
SerialManager_CloseReadHandle
serial_manager_status_t SerialManager_CloseReadHandle(serial_read_handle_t readHandle)
Closes a reading for the serial manager module.
Definition: serial_manager.c:1094
config
static sai_transceiver_t config
Definition: imxrt1050/imxrt1050-evkb/source/pv_audio_rec.c:75
SerialManager_Read
static serial_manager_status_t SerialManager_Read(serial_read_handle_t readHandle, uint8_t *buffer, uint32_t length)
Definition: serial_manager.c:805
LIST_RemoveHead
list_element_handle_t LIST_RemoveHead(list_handle_t list)
Unlinks element from the head of the list.
Definition: generic_list.c:198
SerialManager_ExitLowpower
serial_manager_status_t SerialManager_ExitLowpower(serial_handle_t serialHandle)
Restores from low power consumption.
Definition: serial_manager.c:1360
list_label
The list structure.
Definition: generic_list.h:49
SerialManager_StartWriting
static serial_manager_status_t SerialManager_StartWriting(serial_manager_handle_t *handle, serial_manager_write_handle_t *writeHandle, uint8_t *buffer, uint32_t length)
Definition: serial_manager.c:267
EnableGlobalIRQ
static void EnableGlobalIRQ(uint32_t primask)
Enable the global IRQ.
Definition: fsl_common.h:583
MIN
#define MIN(a, b)
Definition: fsl_common.h:201


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50