1 /*
2 * This is a modified version of the file printf.c, which was distributed
3 * by Motorola as part of the M5407C3BOOT.zip package used to initialize
4 * the M5407C3 evaluation board.
5 *
6 * Copyright:
7 * 1999-2000 MOTOROLA, INC. All Rights Reserved.
8 * You are hereby granted a copyright license to use, modify, and
9 * distribute the SOFTWARE so long as this entire notice is
10 * retained without alteration in any modified and/or redistributed
11 * versions, and that such modified versions are clearly identified
12 * as such. No licenses are granted by implication, estoppel or
13 * otherwise under any patents or trademarks of Motorola, Inc. This
14 * software is provided on an "AS IS" basis and without warranty.
15 *
16 * To the maximum extent permitted by applicable law, MOTOROLA
17 * DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED, INCLUDING
18 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
19 * PURPOSE AND ANY WARRANTY AGAINST INFRINGEMENT WITH REGARD TO THE
20 * SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF) AND ANY
21 * ACCOMPANYING WRITTEN MATERIALS.
22 *
23 * To the maximum extent permitted by applicable law, IN NO EVENT
24 * SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER (INCLUDING
25 * WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS
26 * INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY
27 * LOSS) ARISING OF THE USE OR INABILITY TO USE THE SOFTWARE.
28 *
29 * Motorola assumes no responsibility for the maintenance and support
30 * of this software
31
32 * Copyright (c) 2015, Freescale Semiconductor, Inc.
33 * Copyright 2016-2020 NXP
34 *
35 * SPDX-License-Identifier: BSD-3-Clause
36 */
37
38 #include <stdarg.h>
39 #include <stdlib.h>
40 #if defined(__CC_ARM) || defined(__ARMCC_VERSION)
41 #include <stdio.h>
42 #endif
43
44 #ifdef SDK_OS_FREE_RTOS
45 #include "FreeRTOS.h"
46 #include "semphr.h"
47 #include "task.h"
48 #endif
49
50 #include "fsl_debug_console_conf.h"
51 #include "fsl_str.h"
52
53 #include "fsl_common.h"
54 #include "fsl_component_serial_manager.h"
55
56 #include "fsl_debug_console.h"
57
58 /*******************************************************************************
59 * Definitions
60 ******************************************************************************/
61 #ifndef NDEBUG
62 #if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
63 #undef assert
64 #define assert(n)
65 #else
66 /* MISRA C-2012 Rule 17.2 */
67 #undef assert
68 #define assert(n) \
69 while (!(n)) \
70 { \
71 ; \
72 }
73 #endif
74 #endif
75
76 #if (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK))
77 #define DEBUG_CONSOLE_FUNCTION_PREFIX
78 #else
79 #define DEBUG_CONSOLE_FUNCTION_PREFIX static
80 #endif
81
82 /*! @brief character backspace ASCII value */
83 #define DEBUG_CONSOLE_BACKSPACE 127U
84
85 /* lock definition */
86 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
87
88 static SemaphoreHandle_t s_debugConsoleReadSemaphore;
89 #if configSUPPORT_STATIC_ALLOCATION
90 static StaticSemaphore_t s_debugConsoleReadSemaphoreStatic;
91 #endif
92 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
93 static SemaphoreHandle_t s_debugConsoleReadWaitSemaphore;
94 #if configSUPPORT_STATIC_ALLOCATION
95 static StaticSemaphore_t s_debugConsoleReadWaitSemaphoreStatic;
96 #endif
97 #endif
98
99 #elif (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_BM)
100
101 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
102 static volatile bool s_debugConsoleReadWaitSemaphore;
103 #endif
104
105 #else
106
107 #endif /* DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS */
108
109 /*! @brief get current runing environment is ISR or not */
110 #ifdef __CA7_REV
111 #define IS_RUNNING_IN_ISR() SystemGetIRQNestingLevel()
112 #else
113 #define IS_RUNNING_IN_ISR() __get_IPSR()
114 #endif /* __CA7_REV */
115
116 /* semaphore definition */
117 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
118
119 /* mutex semaphore */
120 /* clang-format off */
121 #if configSUPPORT_STATIC_ALLOCATION
122 #define DEBUG_CONSOLE_CREATE_MUTEX_SEMAPHORE(mutex, stack) ((mutex) = xSemaphoreCreateMutexStatic(stack))
123 #else
124 #define DEBUG_CONSOLE_CREATE_MUTEX_SEMAPHORE(mutex) ((mutex) = xSemaphoreCreateMutex())
125 #endif
126 #define DEBUG_CONSOLE_DESTROY_MUTEX_SEMAPHORE(mutex) \
127 do \
128 { \
129 if(NULL != (mutex)) \
130 { \
131 vSemaphoreDelete(mutex); \
132 (mutex) = NULL; \
133 } \
134 } while(false)
135
136 #define DEBUG_CONSOLE_GIVE_MUTEX_SEMAPHORE(mutex) \
137 { \
138 if (IS_RUNNING_IN_ISR() == 0U) \
139 { \
140 (void)xSemaphoreGive(mutex); \
141 } \
142 }
143
144 #define DEBUG_CONSOLE_TAKE_MUTEX_SEMAPHORE_BLOCKING(mutex) \
145 { \
146 if (IS_RUNNING_IN_ISR() == 0U) \
147 { \
148 (void)xSemaphoreTake(mutex, portMAX_DELAY); \
149 } \
150 }
151
152 #define DEBUG_CONSOLE_TAKE_MUTEX_SEMAPHORE_NONBLOCKING(mutex, result) \
153 { \
154 if (IS_RUNNING_IN_ISR() == 0U) \
155 { \
156 result = xSemaphoreTake(mutex, 0U); \
157 } \
158 else \
159 { \
160 result = 1U; \
161 } \
162 }
163
164 /* Binary semaphore */
165 #if configSUPPORT_STATIC_ALLOCATION
166 #define DEBUG_CONSOLE_CREATE_BINARY_SEMAPHORE(binary,stack) ((binary) = xSemaphoreCreateBinaryStatic(stack))
167 #else
168 #define DEBUG_CONSOLE_CREATE_BINARY_SEMAPHORE(binary) ((binary) = xSemaphoreCreateBinary())
169 #endif
170 #define DEBUG_CONSOLE_DESTROY_BINARY_SEMAPHORE(binary) \
171 do \
172 { \
173 if(NULL != (binary)) \
174 { \
175 vSemaphoreDelete((binary)); \
176 (binary) = NULL; \
177 } \
178 } while(false)
179 #define DEBUG_CONSOLE_TAKE_BINARY_SEMAPHORE_BLOCKING(binary) ((void)xSemaphoreTake((binary), portMAX_DELAY))
180 #define DEBUG_CONSOLE_GIVE_BINARY_SEMAPHORE_FROM_ISR(binary) ((void)xSemaphoreGiveFromISR((binary), NULL))
181
182 #elif (DEBUG_CONSOLE_SYNCHRONIZATION_BM == DEBUG_CONSOLE_SYNCHRONIZATION_MODE)
183
184 #define DEBUG_CONSOLE_CREATE_MUTEX_SEMAPHORE(mutex) (void)(mutex)
185 #define DEBUG_CONSOLE_DESTROY_MUTEX_SEMAPHORE(mutex) (void)(mutex)
186 #define DEBUG_CONSOLE_TAKE_MUTEX_SEMAPHORE_BLOCKING(mutex) (void)(mutex)
187 #define DEBUG_CONSOLE_GIVE_MUTEX_SEMAPHORE(mutex) (void)(mutex)
188 #define DEBUG_CONSOLE_TAKE_MUTEX_SEMAPHORE_NONBLOCKING(mutex, result) (result = 1U)
189
190 #define DEBUG_CONSOLE_CREATE_BINARY_SEMAPHORE(binary) (void)(binary)
191 #define DEBUG_CONSOLE_DESTROY_BINARY_SEMAPHORE(binary) (void)(binary)
192 #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
193 #define DEBUG_CONSOLE_TAKE_BINARY_SEMAPHORE_BLOCKING(binary) \
194 { \
195 while (!(binary)) \
196 { \
197 } \
198 (binary) = false; \
199 }
200 #define DEBUG_CONSOLE_GIVE_BINARY_SEMAPHORE_FROM_ISR(binary) \
201 do \
202 { \
203 (binary) = true; \
204 } while(false)
205 #else
206 #define DEBUG_CONSOLE_TAKE_BINARY_SEMAPHORE_BLOCKING(binary) (void)(binary)
207 #define DEBUG_CONSOLE_GIVE_BINARY_SEMAPHORE_FROM_ISR(binary) (void)(binary)
208 #endif /* DEBUG_CONSOLE_TRANSFER_NON_BLOCKING */
209 /* clang-format on */
210
211 /* add other implementation here
212 *such as :
213 * #elif(DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DDEBUG_CONSOLE_SYNCHRONIZATION_xxx)
214 */
215
216 #else
217
218 #error RTOS type is not defined by DEBUG_CONSOLE_SYNCHRONIZATION_MODE.
219
220 #endif /* DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS */
221
222 #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
223 /* receive state structure */
224 typedef struct _debug_console_write_ring_buffer
225 {
226 uint32_t ringBufferSize;
227 volatile uint32_t ringHead;
228 volatile uint32_t ringTail;
229 uint8_t ringBuffer[DEBUG_CONSOLE_TRANSMIT_BUFFER_LEN];
230 } debug_console_write_ring_buffer_t;
231 #endif
232
233 typedef struct _debug_console_state_struct
234 {
235 serial_handle_t serialHandle; /*!< serial manager handle */
236 #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
237 SERIAL_MANAGER_HANDLE_DEFINE(serialHandleBuffer);
238 debug_console_write_ring_buffer_t writeRingBuffer;
239 uint8_t readRingBuffer[DEBUG_CONSOLE_RECEIVE_BUFFER_LEN];
240 SERIAL_MANAGER_WRITE_HANDLE_DEFINE(serialWriteHandleBuffer);
241 SERIAL_MANAGER_WRITE_HANDLE_DEFINE(serialWriteHandleBuffer2);
242 SERIAL_MANAGER_READ_HANDLE_DEFINE(serialReadHandleBuffer);
243 #else
244 SERIAL_MANAGER_BLOCK_HANDLE_DEFINE(serialHandleBuffer);
245 SERIAL_MANAGER_WRITE_BLOCK_HANDLE_DEFINE(serialWriteHandleBuffer);
246 SERIAL_MANAGER_READ_BLOCK_HANDLE_DEFINE(serialReadHandleBuffer);
247 #endif
248 } debug_console_state_struct_t;
249
250 /*******************************************************************************
251 * Variables
252 ******************************************************************************/
253
254 /*! @brief Debug console state information. */
255 #if (defined(DATA_SECTION_IS_CACHEABLE) && (DATA_SECTION_IS_CACHEABLE > 0))
256 AT_NONCACHEABLE_SECTION(static debug_console_state_struct_t s_debugConsoleState);
257 #else
258 static debug_console_state_struct_t s_debugConsoleState;
259 #endif
260 serial_handle_t g_serialHandle; /*!< serial manager handle */
261
262 /*******************************************************************************
263 * Prototypes
264 ******************************************************************************/
265 /*!
266 * @brief This is a printf call back function which is used to relocate the log to buffer
267 * or print the log immediately when the local buffer is full.
268 *
269 * @param[in] buf Buffer to store log.
270 * @param[in] indicator Buffer index.
271 * @param[in] val Target character to store.
272 * @param[in] len length of the character
273 *
274 */
275 #if SDK_DEBUGCONSOLE
276 static void DbgConsole_PrintCallback(char *buf, int32_t *indicator, char dbgVal, int len);
277 #endif
278
279 status_t DbgConsole_ReadOneCharacter(uint8_t *ch);
280 int DbgConsole_SendData(uint8_t *ch, size_t size);
281 int DbgConsole_SendDataReliable(uint8_t *ch, size_t size);
282 int DbgConsole_ReadLine(uint8_t *buf, size_t size);
283 int DbgConsole_ReadCharacter(uint8_t *ch);
284
285 #if ((SDK_DEBUGCONSOLE != DEBUGCONSOLE_REDIRECT_TO_SDK) && defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING) && \
286 (defined(DEBUG_CONSOLE_TX_RELIABLE_ENABLE) && (DEBUG_CONSOLE_TX_RELIABLE_ENABLE > 0U)))
287 DEBUG_CONSOLE_FUNCTION_PREFIX status_t DbgConsole_Flush(void);
288 #endif
289 /*******************************************************************************
290 * Code
291 ******************************************************************************/
292
293 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
294
DbgConsole_SerialManagerPerformTransfer(debug_console_state_struct_t * ioState)295 static status_t DbgConsole_SerialManagerPerformTransfer(debug_console_state_struct_t *ioState)
296 {
297 serial_manager_status_t ret = kStatus_SerialManager_Error;
298 uint32_t sendDataLength;
299 uint32_t startIndex;
300 uint32_t regPrimask;
301
302 regPrimask = DisableGlobalIRQ();
303 if (ioState->writeRingBuffer.ringTail != ioState->writeRingBuffer.ringHead)
304 {
305 if (ioState->writeRingBuffer.ringHead > ioState->writeRingBuffer.ringTail)
306 {
307 sendDataLength = ioState->writeRingBuffer.ringHead - ioState->writeRingBuffer.ringTail;
308 startIndex = ioState->writeRingBuffer.ringTail;
309 }
310 else
311 {
312 sendDataLength = ioState->writeRingBuffer.ringBufferSize - ioState->writeRingBuffer.ringTail;
313 startIndex = ioState->writeRingBuffer.ringTail;
314 if (0U != ioState->writeRingBuffer.ringHead)
315 {
316 ret = SerialManager_WriteNonBlocking(((serial_write_handle_t)&ioState->serialWriteHandleBuffer2[0]),
317 &ioState->writeRingBuffer.ringBuffer[startIndex], sendDataLength);
318 sendDataLength = ioState->writeRingBuffer.ringHead - 0U;
319 startIndex = 0U;
320 }
321 }
322 ret = SerialManager_WriteNonBlocking(((serial_write_handle_t)&ioState->serialWriteHandleBuffer[0]),
323 &ioState->writeRingBuffer.ringBuffer[startIndex], sendDataLength);
324 }
325 EnableGlobalIRQ(regPrimask);
326 return (status_t)ret;
327 }
328
DbgConsole_SerialManagerTxCallback(void * callbackParam,serial_manager_callback_message_t * message,serial_manager_status_t status)329 static void DbgConsole_SerialManagerTxCallback(void *callbackParam,
330 serial_manager_callback_message_t *message,
331 serial_manager_status_t status)
332 {
333 debug_console_state_struct_t *ioState;
334
335 if ((NULL == callbackParam) || (NULL == message))
336 {
337 return;
338 }
339
340 ioState = (debug_console_state_struct_t *)callbackParam;
341
342 ioState->writeRingBuffer.ringTail += message->length;
343 if (ioState->writeRingBuffer.ringTail >= ioState->writeRingBuffer.ringBufferSize)
344 {
345 ioState->writeRingBuffer.ringTail = 0U;
346 }
347
348 if (kStatus_SerialManager_Success == status)
349 {
350 (void)DbgConsole_SerialManagerPerformTransfer(ioState);
351 }
352 else if (kStatus_SerialManager_Canceled == status)
353 {
354 ioState->writeRingBuffer.ringTail = 0U;
355 ioState->writeRingBuffer.ringHead = 0U;
356 }
357 else
358 {
359 /*MISRA rule 16.4*/
360 }
361 }
362
DbgConsole_SerialManagerTx2Callback(void * callbackParam,serial_manager_callback_message_t * message,serial_manager_status_t status)363 static void DbgConsole_SerialManagerTx2Callback(void *callbackParam,
364 serial_manager_callback_message_t *message,
365 serial_manager_status_t status)
366 {
367 debug_console_state_struct_t *ioState;
368
369 if ((NULL == callbackParam) || (NULL == message))
370 {
371 return;
372 }
373
374 ioState = (debug_console_state_struct_t *)callbackParam;
375
376 ioState->writeRingBuffer.ringTail += message->length;
377 if (ioState->writeRingBuffer.ringTail >= ioState->writeRingBuffer.ringBufferSize)
378 {
379 ioState->writeRingBuffer.ringTail = 0U;
380 }
381
382 if (kStatus_SerialManager_Success == status)
383 {
384 /* Empty block*/
385 }
386 else if (kStatus_SerialManager_Canceled == status)
387 {
388 /* Empty block*/
389 }
390 else
391 {
392 /*MISRA rule 16.4*/
393 }
394 }
395
396 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
397
DbgConsole_SerialManagerRxCallback(void * callbackParam,serial_manager_callback_message_t * message,serial_manager_status_t status)398 static void DbgConsole_SerialManagerRxCallback(void *callbackParam,
399 serial_manager_callback_message_t *message,
400 serial_manager_status_t status)
401 {
402 if ((NULL == callbackParam) || (NULL == message))
403 {
404 return;
405 }
406
407 if (kStatus_SerialManager_Notify == status)
408 {
409 }
410 else if (kStatus_SerialManager_Success == status)
411 {
412 /* release s_debugConsoleReadWaitSemaphore from RX callback */
413 DEBUG_CONSOLE_GIVE_BINARY_SEMAPHORE_FROM_ISR(s_debugConsoleReadWaitSemaphore);
414 }
415 else
416 {
417 /*MISRA rule 16.4*/
418 }
419 }
420 #endif
421
422 #endif
423
DbgConsole_ReadOneCharacter(uint8_t * ch)424 status_t DbgConsole_ReadOneCharacter(uint8_t *ch)
425 {
426 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
427
428 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING) && \
429 (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_BM) && defined(OSA_USED)
430 return (status_t)kStatus_Fail;
431 #else /*defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING) && (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == \
432 DEBUG_CONSOLE_SYNCHRONIZATION_BM) && defined(OSA_USED)*/
433 serial_manager_status_t status = kStatus_SerialManager_Error;
434
435 /* recieve one char every time */
436 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
437 status =
438 SerialManager_ReadNonBlocking(((serial_read_handle_t)&s_debugConsoleState.serialReadHandleBuffer[0]), ch, 1);
439 #else /*defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)*/
440 status = SerialManager_ReadBlocking(((serial_read_handle_t)&s_debugConsoleState.serialReadHandleBuffer[0]), ch, 1);
441 #endif /*defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)*/
442 if (kStatus_SerialManager_Success != status)
443 {
444 status = (serial_manager_status_t)kStatus_Fail;
445 }
446 else
447 {
448 /* wait s_debugConsoleReadWaitSemaphore from RX callback */
449 DEBUG_CONSOLE_TAKE_BINARY_SEMAPHORE_BLOCKING(s_debugConsoleReadWaitSemaphore);
450 status = (serial_manager_status_t)kStatus_Success;
451 }
452 return (status_t)status;
453 #endif /*defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING) && (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == \
454 DEBUG_CONSOLE_SYNCHRONIZATION_BM) && defined(OSA_USED)*/
455
456 #else /*(defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))*/
457
458 return (status_t)kStatus_Fail;
459
460 #endif /*(defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))*/
461 }
462
463 #if DEBUG_CONSOLE_ENABLE_ECHO_FUNCTION
DbgConsole_EchoCharacter(uint8_t * ch,bool isGetChar,int * index)464 static status_t DbgConsole_EchoCharacter(uint8_t *ch, bool isGetChar, int *index)
465 {
466 /* Due to scanf take \n and \r as end of string,should not echo */
467 if (((*ch != (uint8_t)'\r') && (*ch != (uint8_t)'\n')) || (isGetChar))
468 {
469 /* recieve one char every time */
470 if (1 != DbgConsole_SendDataReliable(ch, 1U))
471 {
472 return (status_t)kStatus_Fail;
473 }
474 }
475
476 if ((!isGetChar) && (index != NULL))
477 {
478 if (DEBUG_CONSOLE_BACKSPACE == *ch)
479 {
480 if ((*index >= 2))
481 {
482 *index -= 2;
483 }
484 else
485 {
486 *index = 0;
487 }
488 }
489 }
490
491 return (status_t)kStatus_Success;
492 }
493 #endif
494
DbgConsole_SendData(uint8_t * ch,size_t size)495 int DbgConsole_SendData(uint8_t *ch, size_t size)
496 {
497 status_t status;
498 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
499 uint32_t sendDataLength;
500 int txBusy = 0;
501 #endif
502 assert(NULL != ch);
503 assert(0U != size);
504
505 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
506 uint32_t regPrimask = DisableGlobalIRQ();
507 if (s_debugConsoleState.writeRingBuffer.ringHead != s_debugConsoleState.writeRingBuffer.ringTail)
508 {
509 txBusy = 1;
510 sendDataLength =
511 (s_debugConsoleState.writeRingBuffer.ringHead + s_debugConsoleState.writeRingBuffer.ringBufferSize -
512 s_debugConsoleState.writeRingBuffer.ringTail) %
513 s_debugConsoleState.writeRingBuffer.ringBufferSize;
514 }
515 else
516 {
517 sendDataLength = 0U;
518 }
519 sendDataLength = s_debugConsoleState.writeRingBuffer.ringBufferSize - sendDataLength - 1U;
520 if (sendDataLength < size)
521 {
522 EnableGlobalIRQ(regPrimask);
523 return -1;
524 }
525 for (int i = 0; i < (int)size; i++)
526 {
527 s_debugConsoleState.writeRingBuffer.ringBuffer[s_debugConsoleState.writeRingBuffer.ringHead++] = ch[i];
528 if (s_debugConsoleState.writeRingBuffer.ringHead >= s_debugConsoleState.writeRingBuffer.ringBufferSize)
529 {
530 s_debugConsoleState.writeRingBuffer.ringHead = 0U;
531 }
532 }
533
534 status = (status_t)kStatus_SerialManager_Success;
535
536 if (txBusy == 0)
537 {
538 status = DbgConsole_SerialManagerPerformTransfer(&s_debugConsoleState);
539 }
540 EnableGlobalIRQ(regPrimask);
541 #else
542 status = (status_t)SerialManager_WriteBlocking(
543 ((serial_write_handle_t)&s_debugConsoleState.serialWriteHandleBuffer[0]), ch, size);
544 #endif
545 return (((status_t)kStatus_Success == status) ? (int)size : -1);
546 }
547
DbgConsole_SendDataReliable(uint8_t * ch,size_t size)548 int DbgConsole_SendDataReliable(uint8_t *ch, size_t size)
549 {
550 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
551 #if (defined(DEBUG_CONSOLE_TX_RELIABLE_ENABLE) && (DEBUG_CONSOLE_TX_RELIABLE_ENABLE > 0U))
552 serial_manager_status_t status = kStatus_SerialManager_Error;
553 uint32_t sendDataLength;
554 uint32_t totalLength = size;
555 int sentLength;
556 #endif /* DEBUG_CONSOLE_TX_RELIABLE_ENABLE */
557 #else /* DEBUG_CONSOLE_TRANSFER_NON_BLOCKING */
558 serial_manager_status_t status;
559 #endif /* DEBUG_CONSOLE_TRANSFER_NON_BLOCKING */
560
561 assert(NULL != ch);
562
563 if (0U == size)
564 {
565 return 0;
566 }
567
568 if (NULL == g_serialHandle)
569 {
570 return 0;
571 }
572
573 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
574
575 #if (defined(DEBUG_CONSOLE_TX_RELIABLE_ENABLE) && (DEBUG_CONSOLE_TX_RELIABLE_ENABLE > 0U))
576 do
577 {
578 uint32_t regPrimask = DisableGlobalIRQ();
579 if (s_debugConsoleState.writeRingBuffer.ringHead != s_debugConsoleState.writeRingBuffer.ringTail)
580 {
581 sendDataLength =
582 (s_debugConsoleState.writeRingBuffer.ringHead + s_debugConsoleState.writeRingBuffer.ringBufferSize -
583 s_debugConsoleState.writeRingBuffer.ringTail) %
584 s_debugConsoleState.writeRingBuffer.ringBufferSize;
585 }
586 else
587 {
588 sendDataLength = 0U;
589 }
590 sendDataLength = s_debugConsoleState.writeRingBuffer.ringBufferSize - sendDataLength - 1U;
591
592 if ((sendDataLength > 0U) && ((sendDataLength >= totalLength) ||
593 (totalLength >= (s_debugConsoleState.writeRingBuffer.ringBufferSize - 1U))))
594 {
595 if (sendDataLength > totalLength)
596 {
597 sendDataLength = totalLength;
598 }
599
600 sentLength = DbgConsole_SendData(&ch[size - totalLength], (size_t)sendDataLength);
601 if (sentLength > 0)
602 {
603 totalLength = totalLength - (uint32_t)sentLength;
604 }
605 }
606 EnableGlobalIRQ(regPrimask);
607
608 if (totalLength != 0U)
609 {
610 status = (serial_manager_status_t)DbgConsole_Flush();
611 if (kStatus_SerialManager_Success != status)
612 {
613 break;
614 }
615 }
616 } while (totalLength != 0U);
617 return ((int)size - (int)totalLength);
618 #else /* DEBUG_CONSOLE_TX_RELIABLE_ENABLE */
619 return DbgConsole_SendData(ch, size);
620 #endif /* DEBUG_CONSOLE_TX_RELIABLE_ENABLE */
621
622 #else /* DEBUG_CONSOLE_TRANSFER_NON_BLOCKING */
623 status =
624 SerialManager_WriteBlocking(((serial_write_handle_t)&s_debugConsoleState.serialWriteHandleBuffer[0]), ch, size);
625 return ((kStatus_SerialManager_Success == status) ? (int)size : -1);
626 #endif /* DEBUG_CONSOLE_TRANSFER_NON_BLOCKING */
627 }
628
DbgConsole_ReadLine(uint8_t * buf,size_t size)629 int DbgConsole_ReadLine(uint8_t *buf, size_t size)
630 {
631 int i = 0;
632
633 assert(buf != NULL);
634
635 if (NULL == g_serialHandle)
636 {
637 return -1;
638 }
639
640 /* take mutex lock function */
641 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
642 DEBUG_CONSOLE_TAKE_MUTEX_SEMAPHORE_BLOCKING(s_debugConsoleReadSemaphore);
643 #endif
644
645 do
646 {
647 /* recieve one char every time */
648 if ((status_t)kStatus_Success != DbgConsole_ReadOneCharacter(&buf[i]))
649 {
650 /* release mutex lock function */
651 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
652 DEBUG_CONSOLE_GIVE_MUTEX_SEMAPHORE(s_debugConsoleReadSemaphore);
653 #endif
654 i = -1;
655 break;
656 }
657 #if DEBUG_CONSOLE_ENABLE_ECHO_FUNCTION
658 (void)DbgConsole_EchoCharacter(&buf[i], false, &i);
659 #endif
660 /* analysis data */
661 if (((uint8_t)'\r' == buf[i]) || ((uint8_t)'\n' == buf[i]))
662 {
663 /* End of Line. */
664 if (0 == i)
665 {
666 buf[i] = (uint8_t)'\0';
667 continue;
668 }
669 else
670 {
671 break;
672 }
673 }
674 i++;
675 } while (i < (int)size);
676
677 /* get char should not add '\0'*/
678 if (i == (int)size)
679 {
680 buf[i] = (uint8_t)'\0';
681 }
682 else
683 {
684 buf[i + 1] = (uint8_t)'\0';
685 }
686
687 /* release mutex lock function */
688 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
689 DEBUG_CONSOLE_GIVE_MUTEX_SEMAPHORE(s_debugConsoleReadSemaphore);
690 #endif
691
692 return i;
693 }
694
DbgConsole_ReadCharacter(uint8_t * ch)695 int DbgConsole_ReadCharacter(uint8_t *ch)
696 {
697 int ret;
698
699 assert(ch);
700
701 if (NULL == g_serialHandle)
702 {
703 return -1;
704 }
705
706 /* take mutex lock function */
707 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
708 DEBUG_CONSOLE_TAKE_MUTEX_SEMAPHORE_BLOCKING(s_debugConsoleReadSemaphore);
709 #endif
710 /* read one character */
711 if ((status_t)kStatus_Success == DbgConsole_ReadOneCharacter(ch))
712 {
713 ret = 1;
714 #if DEBUG_CONSOLE_ENABLE_ECHO_FUNCTION
715 (void)DbgConsole_EchoCharacter(ch, true, NULL);
716 #endif
717 }
718 else
719 {
720 ret = -1;
721 }
722
723 /* release mutex lock function */
724 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
725 DEBUG_CONSOLE_GIVE_MUTEX_SEMAPHORE(s_debugConsoleReadSemaphore);
726 #endif
727
728 return ret;
729 }
730
731 #if SDK_DEBUGCONSOLE
DbgConsole_PrintCallback(char * buf,int32_t * indicator,char dbgVal,int len)732 static void DbgConsole_PrintCallback(char *buf, int32_t *indicator, char dbgVal, int len)
733 {
734 int i = 0;
735
736 for (i = 0; i < len; i++)
737 {
738 if (((uint32_t)*indicator + 1UL) >= (uint32_t)DEBUG_CONSOLE_PRINTF_MAX_LOG_LEN)
739 {
740 (void)DbgConsole_SendDataReliable((uint8_t *)buf, (size_t)(*indicator));
741 *indicator = 0;
742 }
743
744 buf[*indicator] = dbgVal;
745 (*indicator)++;
746 }
747 }
748 #endif
749
750 /*************Code for DbgConsole Init, Deinit, Printf, Scanf *******************************/
751 #if ((SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK) || defined(SDK_DEBUGCONSOLE_UART))
752 #if (defined(SERIAL_USE_CONFIGURE_STRUCTURE) && (SERIAL_USE_CONFIGURE_STRUCTURE > 0U))
753 #include "board.h"
754 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
755 static const serial_port_uart_config_t uartConfig = {.instance = BOARD_DEBUG_UART_INSTANCE,
756 .clockRate = BOARD_DEBUG_UART_CLK_FREQ,
757 .baudRate = BOARD_DEBUG_UART_BAUDRATE,
758 .parityMode = kSerialManager_UartParityDisabled,
759 .stopBitCount = kSerialManager_UartOneStopBit,
760 .enableRx = 1U,
761 .enableTx = 1U,
762 .enableRxRTS = 0U,
763 .enableTxCTS = 0U,
764 #if (defined(HAL_UART_ADAPTER_FIFO) && (HAL_UART_ADAPTER_FIFO > 0u))
765 .txFifoWatermark = 0U,
766 .rxFifoWatermark = 0U
767 #endif
768 };
769 #endif
770 #endif
771 /* See fsl_debug_console.h for documentation of this function. */
DbgConsole_Init(uint8_t instance,uint32_t baudRate,serial_port_type_t device,uint32_t clkSrcFreq)772 status_t DbgConsole_Init(uint8_t instance, uint32_t baudRate, serial_port_type_t device, uint32_t clkSrcFreq)
773 {
774 serial_manager_config_t serialConfig = {0};
775 serial_manager_status_t status = kStatus_SerialManager_Success;
776
777 #if (defined(SERIAL_USE_CONFIGURE_STRUCTURE) && (SERIAL_USE_CONFIGURE_STRUCTURE == 0U))
778 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
779 serial_port_uart_config_t uartConfig = {
780 .instance = instance,
781 .clockRate = clkSrcFreq,
782 .baudRate = baudRate,
783 .parityMode = kSerialManager_UartParityDisabled,
784 .stopBitCount = kSerialManager_UartOneStopBit,
785 .enableRx = 1,
786 .enableTx = 1,
787 .enableRxRTS = 0U,
788 .enableTxCTS = 0U,
789 #if (defined(HAL_UART_ADAPTER_FIFO) && (HAL_UART_ADAPTER_FIFO > 0u))
790 .txFifoWatermark = 0U,
791 .rxFifoWatermark = 0U
792 #endif
793 };
794 #endif
795 #endif
796
797 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
798 serial_port_usb_cdc_config_t usbCdcConfig = {
799 .controllerIndex = (serial_port_usb_cdc_controller_index_t)instance,
800 };
801 #endif
802
803 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
804 serial_port_swo_config_t swoConfig = {
805 .clockRate = clkSrcFreq,
806 .baudRate = baudRate,
807 .port = instance,
808 .protocol = kSerialManager_SwoProtocolNrz,
809 };
810 #endif
811
812 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
813 serial_port_virtual_config_t serialPortVirtualConfig = {
814 .controllerIndex = (serial_port_virtual_controller_index_t)instance,
815 };
816 #endif
817
818 serialConfig.type = device;
819 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
820 serialConfig.ringBuffer = &s_debugConsoleState.readRingBuffer[0];
821 serialConfig.ringBufferSize = DEBUG_CONSOLE_RECEIVE_BUFFER_LEN;
822 serialConfig.blockType = kSerialManager_NonBlocking;
823 #else
824 serialConfig.blockType = kSerialManager_Blocking;
825 #endif
826
827 if (kSerialPort_Uart == device)
828 {
829 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
830 #if (defined(SERIAL_USE_CONFIGURE_STRUCTURE) && (SERIAL_USE_CONFIGURE_STRUCTURE > 0U))
831 serialConfig.portConfig = (void *)&uartConfig;
832 #else
833 serialConfig.portConfig = &uartConfig;
834 #endif
835 #else
836 status = kStatus_SerialManager_Error;
837 #endif
838 }
839 else if (kSerialPort_UsbCdc == device)
840 {
841 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
842 serialConfig.portConfig = &usbCdcConfig;
843 #else
844 status = kStatus_SerialManager_Error;
845 #endif
846 }
847 else if (kSerialPort_Swo == device)
848 {
849 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
850 serialConfig.portConfig = &swoConfig;
851 #else
852 status = kStatus_SerialManager_Error;
853 #endif
854 }
855 else if (kSerialPort_Virtual == device)
856 {
857 #if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
858 serialConfig.portConfig = &serialPortVirtualConfig;
859 #else
860 status = kStatus_SerialManager_Error;
861 #endif
862 }
863 else
864 {
865 status = kStatus_SerialManager_Error;
866 }
867
868 if (kStatus_SerialManager_Error != status)
869 {
870 (void)memset(&s_debugConsoleState, 0, sizeof(s_debugConsoleState));
871
872 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
873 s_debugConsoleState.writeRingBuffer.ringBufferSize = DEBUG_CONSOLE_TRANSMIT_BUFFER_LEN;
874 #endif
875
876 s_debugConsoleState.serialHandle = (serial_handle_t)&s_debugConsoleState.serialHandleBuffer[0];
877 status = SerialManager_Init(s_debugConsoleState.serialHandle, &serialConfig);
878
879 assert(kStatus_SerialManager_Success == status);
880
881 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
882 #if configSUPPORT_STATIC_ALLOCATION
883 DEBUG_CONSOLE_CREATE_MUTEX_SEMAPHORE(s_debugConsoleReadSemaphore, &s_debugConsoleReadSemaphoreStatic);
884 #else
885 DEBUG_CONSOLE_CREATE_MUTEX_SEMAPHORE(s_debugConsoleReadSemaphore);
886 #endif
887 #endif
888 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
889 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS) && configSUPPORT_STATIC_ALLOCATION
890 DEBUG_CONSOLE_CREATE_BINARY_SEMAPHORE(s_debugConsoleReadWaitSemaphore, &s_debugConsoleReadWaitSemaphoreStatic);
891 #else
892 DEBUG_CONSOLE_CREATE_BINARY_SEMAPHORE(s_debugConsoleReadWaitSemaphore);
893 #endif
894 #endif
895
896 {
897 status =
898 SerialManager_OpenWriteHandle(s_debugConsoleState.serialHandle,
899 ((serial_write_handle_t)&s_debugConsoleState.serialWriteHandleBuffer[0]));
900 assert(kStatus_SerialManager_Success == status);
901 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
902 (void)SerialManager_InstallTxCallback(
903 ((serial_write_handle_t)&s_debugConsoleState.serialWriteHandleBuffer[0]),
904 DbgConsole_SerialManagerTxCallback, &s_debugConsoleState);
905 status = SerialManager_OpenWriteHandle(
906 s_debugConsoleState.serialHandle,
907 ((serial_write_handle_t)&s_debugConsoleState.serialWriteHandleBuffer2[0]));
908 assert(kStatus_SerialManager_Success == status);
909 (void)SerialManager_InstallTxCallback(
910 ((serial_write_handle_t)&s_debugConsoleState.serialWriteHandleBuffer2[0]),
911 DbgConsole_SerialManagerTx2Callback, &s_debugConsoleState);
912 #endif
913 }
914
915 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
916 {
917 status =
918 SerialManager_OpenReadHandle(s_debugConsoleState.serialHandle,
919 ((serial_read_handle_t)&s_debugConsoleState.serialReadHandleBuffer[0]));
920 assert(kStatus_SerialManager_Success == status);
921 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
922 (void)SerialManager_InstallRxCallback(
923 ((serial_read_handle_t)&s_debugConsoleState.serialReadHandleBuffer[0]),
924 DbgConsole_SerialManagerRxCallback, &s_debugConsoleState);
925 #endif
926 }
927 #endif
928
929 g_serialHandle = s_debugConsoleState.serialHandle;
930 }
931 return (status_t)status;
932 }
933
934 /* See fsl_debug_console.h for documentation of this function. */
DbgConsole_EnterLowpower(void)935 status_t DbgConsole_EnterLowpower(void)
936 {
937 serial_manager_status_t status = kStatus_SerialManager_Error;
938 if (s_debugConsoleState.serialHandle != NULL)
939 {
940 status = SerialManager_EnterLowpower(s_debugConsoleState.serialHandle);
941 }
942 return (status_t)status;
943 }
944
945 /* See fsl_debug_console.h for documentation of this function. */
DbgConsole_ExitLowpower(void)946 status_t DbgConsole_ExitLowpower(void)
947 {
948 serial_manager_status_t status = kStatus_SerialManager_Error;
949
950 if (s_debugConsoleState.serialHandle != NULL)
951 {
952 status = SerialManager_ExitLowpower(s_debugConsoleState.serialHandle);
953 }
954 return (status_t)status;
955 }
956 /* See fsl_debug_console.h for documentation of this function. */
DbgConsole_Deinit(void)957 status_t DbgConsole_Deinit(void)
958 {
959 {
960 if (s_debugConsoleState.serialHandle != NULL)
961 {
962 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
963 (void)SerialManager_CloseWriteHandle(
964 ((serial_write_handle_t)&s_debugConsoleState.serialWriteHandleBuffer2[0]));
965 #endif
966 (void)SerialManager_CloseWriteHandle(
967 ((serial_write_handle_t)&s_debugConsoleState.serialWriteHandleBuffer[0]));
968 }
969 }
970 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
971 {
972 if (s_debugConsoleState.serialHandle != NULL)
973 {
974 (void)SerialManager_CloseReadHandle(((serial_read_handle_t)&s_debugConsoleState.serialReadHandleBuffer[0]));
975 }
976 }
977 #endif
978 if (NULL != s_debugConsoleState.serialHandle)
979 {
980 if (kStatus_SerialManager_Success == SerialManager_Deinit(s_debugConsoleState.serialHandle))
981 {
982 s_debugConsoleState.serialHandle = NULL;
983 g_serialHandle = NULL;
984 }
985 }
986 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
987 DEBUG_CONSOLE_DESTROY_BINARY_SEMAPHORE(s_debugConsoleReadWaitSemaphore);
988 #endif
989 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
990 DEBUG_CONSOLE_DESTROY_MUTEX_SEMAPHORE(s_debugConsoleReadSemaphore);
991 #endif
992
993 return (status_t)kStatus_Success;
994 }
995 #endif /* ((SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK) || defined(SDK_DEBUGCONSOLE_UART)) */
996
997 #if (((defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE > DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN))) || \
998 ((SDK_DEBUGCONSOLE == 0U) && defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING) && \
999 (defined(DEBUG_CONSOLE_TX_RELIABLE_ENABLE) && (DEBUG_CONSOLE_TX_RELIABLE_ENABLE > 0U))))
DbgConsole_Flush(void)1000 DEBUG_CONSOLE_FUNCTION_PREFIX status_t DbgConsole_Flush(void)
1001 {
1002 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
1003
1004 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_BM) && defined(OSA_USED)
1005
1006 if (s_debugConsoleState.writeRingBuffer.ringHead != s_debugConsoleState.writeRingBuffer.ringTail)
1007 {
1008 return (status_t)kStatus_Fail;
1009 }
1010
1011 #else
1012
1013 while (s_debugConsoleState.writeRingBuffer.ringHead != s_debugConsoleState.writeRingBuffer.ringTail)
1014 {
1015 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
1016 if (0U == IS_RUNNING_IN_ISR())
1017 {
1018 if (taskSCHEDULER_RUNNING == xTaskGetSchedulerState())
1019 {
1020 vTaskDelay(1);
1021 }
1022 }
1023 else
1024 {
1025 return (status_t)kStatus_Fail;
1026 }
1027 #endif
1028 }
1029
1030 #endif
1031
1032 #endif
1033 return (status_t)kStatus_Success;
1034 }
1035 #endif
1036
1037 #if (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK))
1038 /* See fsl_debug_console.h for documentation of this function. */
DbgConsole_Printf(const char * fmt_s,...)1039 int DbgConsole_Printf(const char *fmt_s, ...)
1040 {
1041 va_list ap;
1042 int result = 0;
1043
1044 va_start(ap, fmt_s);
1045 result = DbgConsole_Vprintf(fmt_s, ap);
1046 va_end(ap);
1047
1048 return result;
1049 }
1050
1051 /* See fsl_debug_console.h for documentation of this function. */
DbgConsole_Vprintf(const char * fmt_s,va_list formatStringArg)1052 int DbgConsole_Vprintf(const char *fmt_s, va_list formatStringArg)
1053 {
1054 int logLength = 0, result = 0;
1055 char printBuf[DEBUG_CONSOLE_PRINTF_MAX_LOG_LEN] = {'\0'};
1056
1057 if (NULL != g_serialHandle)
1058 {
1059 /* format print log first */
1060 logLength = StrFormatPrintf(fmt_s, formatStringArg, printBuf, DbgConsole_PrintCallback);
1061 /* print log */
1062 result = DbgConsole_SendDataReliable((uint8_t *)printBuf, (size_t)logLength);
1063 }
1064 return result;
1065 }
1066
1067 /* See fsl_debug_console.h for documentation of this function. */
DbgConsole_Putchar(int ch)1068 int DbgConsole_Putchar(int ch)
1069 {
1070 /* print char */
1071 return DbgConsole_SendDataReliable((uint8_t *)&ch, 1U);
1072 }
1073
1074 /* See fsl_debug_console.h for documentation of this function. */
DbgConsole_Scanf(char * fmt_s,...)1075 int DbgConsole_Scanf(char *fmt_s, ...)
1076 {
1077 va_list ap;
1078 int formatResult;
1079 char scanfBuf[DEBUG_CONSOLE_SCANF_MAX_LOG_LEN + 1U] = {'\0'};
1080
1081 /* scanf log */
1082 (void)DbgConsole_ReadLine((uint8_t *)scanfBuf, DEBUG_CONSOLE_SCANF_MAX_LOG_LEN);
1083 /* get va_list */
1084 va_start(ap, fmt_s);
1085 /* format scanf log */
1086 formatResult = StrFormatScanf(scanfBuf, fmt_s, ap);
1087
1088 va_end(ap);
1089
1090 return formatResult;
1091 }
1092
1093 /* See fsl_debug_console.h for documentation of this function. */
DbgConsole_BlockingPrintf(const char * fmt_s,...)1094 int DbgConsole_BlockingPrintf(const char *fmt_s, ...)
1095 {
1096 va_list ap;
1097 int result = 0;
1098
1099 va_start(ap, fmt_s);
1100 result = DbgConsole_BlockingVprintf(fmt_s, ap);
1101 va_end(ap);
1102
1103 return result;
1104 }
1105
1106 /* See fsl_debug_console.h for documentation of this function. */
DbgConsole_BlockingVprintf(const char * fmt_s,va_list formatStringArg)1107 int DbgConsole_BlockingVprintf(const char *fmt_s, va_list formatStringArg)
1108 {
1109 status_t status;
1110 int logLength = 0, result = 0;
1111 char printBuf[DEBUG_CONSOLE_PRINTF_MAX_LOG_LEN] = {'\0'};
1112
1113 if (NULL == g_serialHandle)
1114 {
1115 return 0;
1116 }
1117
1118 /* format print log first */
1119 logLength = StrFormatPrintf(fmt_s, formatStringArg, printBuf, DbgConsole_PrintCallback);
1120
1121 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
1122 (void)SerialManager_CancelWriting(((serial_write_handle_t)&s_debugConsoleState.serialWriteHandleBuffer[0]));
1123 #endif
1124 /* print log */
1125 status =
1126 (status_t)SerialManager_WriteBlocking(((serial_write_handle_t)&s_debugConsoleState.serialWriteHandleBuffer[0]),
1127 (uint8_t *)printBuf, (size_t)logLength);
1128 result = (((status_t)kStatus_Success == status) ? (int)logLength : -1);
1129
1130 return result;
1131 }
1132
1133 #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
DbgConsole_TryGetchar(char * ch)1134 status_t DbgConsole_TryGetchar(char *ch)
1135 {
1136 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
1137 uint32_t length = 0;
1138 status_t status = (status_t)kStatus_Fail;
1139
1140 assert(ch);
1141
1142 if (NULL == g_serialHandle)
1143 {
1144 return kStatus_Fail;
1145 }
1146
1147 /* take mutex lock function */
1148 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
1149 DEBUG_CONSOLE_TAKE_MUTEX_SEMAPHORE_BLOCKING(s_debugConsoleReadSemaphore);
1150 #endif
1151
1152 if (kStatus_SerialManager_Success ==
1153 SerialManager_TryRead(((serial_read_handle_t)&s_debugConsoleState.serialReadHandleBuffer[0]), (uint8_t *)ch, 1,
1154 &length))
1155 {
1156 if (length != 0U)
1157 {
1158 #if DEBUG_CONSOLE_ENABLE_ECHO_FUNCTION
1159 (void)DbgConsole_EchoCharacter((uint8_t *)ch, true, NULL);
1160 #endif
1161 status = (status_t)kStatus_Success;
1162 }
1163 }
1164 /* release mutex lock function */
1165 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
1166 DEBUG_CONSOLE_GIVE_MUTEX_SEMAPHORE(s_debugConsoleReadSemaphore);
1167 #endif
1168 return status;
1169 #else
1170 return (status_t)kStatus_Fail;
1171 #endif
1172 }
1173 #endif
1174
1175 /* See fsl_debug_console.h for documentation of this function. */
DbgConsole_Getchar(void)1176 int DbgConsole_Getchar(void)
1177 {
1178 int ret = -1;
1179 uint8_t ch = 0U;
1180
1181 /* Get char */
1182 if (DbgConsole_ReadCharacter(&ch) > 0)
1183 {
1184 ret = (int)ch;
1185 }
1186
1187 return ret;
1188 }
1189
1190 #endif /* SDK_DEBUGCONSOLE */
1191
1192 /*************Code to support toolchain's printf, scanf *******************************/
1193 /* These function __write and __read is used to support IAR toolchain to printf and scanf*/
1194 #if (defined(__ICCARM__))
1195 #if defined(SDK_DEBUGCONSOLE_UART)
1196 #pragma weak __write
1197 size_t __write(int handle, const unsigned char *buffer, size_t size);
__write(int handle,const unsigned char * buffer,size_t size)1198 size_t __write(int handle, const unsigned char *buffer, size_t size)
1199 {
1200 size_t ret;
1201 if (NULL == buffer)
1202 {
1203 /*
1204 * This means that we should flush internal buffers. Since we don't we just return.
1205 * (Remember, "handle" == -1 means that all handles should be flushed.)
1206 */
1207 ret = 0U;
1208 }
1209 else if ((handle != 1) && (handle != 2))
1210 {
1211 /* This function only writes to "standard out" and "standard err" for all other file handles it returns failure.
1212 */
1213 ret = (size_t)-1;
1214 }
1215 else
1216 {
1217 /* Send data. */
1218 uint8_t buff[512];
1219 (void)memcpy(buff, buffer, size);
1220 (void)DbgConsole_SendDataReliable((uint8_t *)buff, size);
1221
1222 ret = size;
1223 }
1224 return ret;
1225 }
1226
1227 #pragma weak __read
1228 size_t __read(int handle, unsigned char *buffer, size_t size);
__read(int handle,unsigned char * buffer,size_t size)1229 size_t __read(int handle, unsigned char *buffer, size_t size)
1230 {
1231 uint8_t ch = 0U;
1232 int actualSize = 0;
1233
1234 /* This function only reads from "standard in", for all other file handles it returns failure. */
1235 if (0 != handle)
1236 {
1237 actualSize = -1;
1238 }
1239 else
1240 {
1241 /* Receive data.*/
1242 for (; size > 0U; size--)
1243 {
1244 (void)DbgConsole_ReadCharacter(&ch);
1245 if (0U == ch)
1246 {
1247 break;
1248 }
1249
1250 *buffer++ = ch;
1251 actualSize++;
1252 }
1253 }
1254 return (size_t)actualSize;
1255 }
1256 #endif /* SDK_DEBUGCONSOLE_UART */
1257
1258 /* support LPC Xpresso with RedLib */
1259 #elif (defined(__REDLIB__))
1260
1261 #if (defined(SDK_DEBUGCONSOLE_UART))
__sys_write(int handle,char * buffer,int size)1262 int __attribute__((weak)) __sys_write(int handle, char *buffer, int size)
1263 {
1264 if (NULL == buffer)
1265 {
1266 /* return -1 if error. */
1267 return -1;
1268 }
1269
1270 /* This function only writes to "standard out" and "standard err" for all other file handles it returns failure. */
1271 if ((handle != 1) && (handle != 2))
1272 {
1273 return -1;
1274 }
1275
1276 /* Send data. */
1277 DbgConsole_SendDataReliable((uint8_t *)buffer, size);
1278
1279 return 0;
1280 }
1281
__sys_readc(void)1282 int __attribute__((weak)) __sys_readc(void)
1283 {
1284 char tmp;
1285
1286 /* Receive data. */
1287 DbgConsole_ReadCharacter((uint8_t *)&tmp);
1288
1289 return tmp;
1290 }
1291 #endif /* SDK_DEBUGCONSOLE_UART */
1292
1293 /* These function fputc and fgetc is used to support KEIL toolchain to printf and scanf*/
1294 #elif defined(__CC_ARM) || defined(__ARMCC_VERSION)
1295 #if defined(SDK_DEBUGCONSOLE_UART)
1296 #if defined(__CC_ARM)
1297 struct __FILE
1298 {
1299 int handle;
1300 /*
1301 * Whatever you require here. If the only file you are using is standard output using printf() for debugging,
1302 * no file handling is required.
1303 */
1304 };
1305 #endif
1306
1307 /* FILE is typedef in stdio.h. */
1308 #pragma weak __stdout
1309 #pragma weak __stdin
1310 FILE __stdout;
1311 FILE __stdin;
1312
1313 #pragma weak fputc
fputc(int ch,FILE * f)1314 int fputc(int ch, FILE *f)
1315 {
1316 /* Send data. */
1317 return DbgConsole_SendDataReliable((uint8_t *)(&ch), 1);
1318 }
1319
1320 #pragma weak fgetc
fgetc(FILE * f)1321 int fgetc(FILE *f)
1322 {
1323 char ch;
1324
1325 /* Receive data. */
1326 DbgConsole_ReadCharacter((uint8_t *)&ch);
1327
1328 return ch;
1329 }
1330
1331 /*
1332 * Terminate the program, passing a return code back to the user.
1333 * This function may not return.
1334 */
_sys_exit(int returncode)1335 void _sys_exit(int returncode)
1336 {
1337 while (1)
1338 {
1339 }
1340 }
1341
1342 /*
1343 * Writes a character to the output channel. This function is used
1344 * for last-resort error message output.
1345 */
_ttywrch(int ch)1346 void _ttywrch(int ch)
1347 {
1348 char ench = ch;
1349 DbgConsole_SendDataReliable((uint8_t *)(&ench), 1);
1350 }
1351
_sys_command_string(char * cmd,int len)1352 char *_sys_command_string(char *cmd, int len)
1353 {
1354 return (cmd);
1355 }
1356 #endif /* SDK_DEBUGCONSOLE_UART */
1357
1358 /* These function __write and __read is used to support ARM_GCC, KDS, Atollic toolchains to printf and scanf*/
1359 #elif (defined(__GNUC__))
1360
1361 #if ((defined(__GNUC__) && (!defined(__MCUXPRESSO)) && (defined(SDK_DEBUGCONSOLE_UART))) || \
1362 (defined(__MCUXPRESSO) && (defined(SDK_DEBUGCONSOLE_UART))))
1363 int __attribute__((weak)) _write(int handle, char *buffer, int size);
_write(int handle,char * buffer,int size)1364 int __attribute__((weak)) _write(int handle, char *buffer, int size)
1365 {
1366 if (NULL == buffer)
1367 {
1368 /* return -1 if error. */
1369 return -1;
1370 }
1371
1372 /* This function only writes to "standard out" and "standard err" for all other file handles it returns failure. */
1373 if ((handle != 1) && (handle != 2))
1374 {
1375 return -1;
1376 }
1377
1378 /* Send data. */
1379 (void)DbgConsole_SendDataReliable((uint8_t *)buffer, (size_t)size);
1380
1381 return size;
1382 }
1383
1384 int __attribute__((weak)) _read(int handle, char *buffer, int size);
_read(int handle,char * buffer,int size)1385 int __attribute__((weak)) _read(int handle, char *buffer, int size)
1386 {
1387 uint8_t ch = 0U;
1388 int actualSize = 0;
1389
1390 /* This function only reads from "standard in", for all other file handles it returns failure. */
1391 if (handle != 0)
1392 {
1393 return -1;
1394 }
1395
1396 /* Receive data. */
1397 for (; size > 0; size--)
1398 {
1399 if (DbgConsole_ReadCharacter(&ch) < 0)
1400 {
1401 break;
1402 }
1403
1404 *buffer++ = (char)ch;
1405 actualSize++;
1406
1407 if ((ch == 0U) || (ch == (uint8_t)'\n') || (ch == (uint8_t)'\r'))
1408 {
1409 break;
1410 }
1411 }
1412
1413 return (actualSize > 0) ? actualSize : -1;
1414 }
1415 #endif
1416
1417 #endif /* __ICCARM__ */
1418