40 #if defined(__CC_ARM) || defined(__ARMCC_VERSION)
44 #ifdef FSL_RTOS_FREE_RTOS
62 #if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
69 #define DEBUG_CONSOLE_FUNCTION_PREFIX
71 #define DEBUG_CONSOLE_FUNCTION_PREFIX static
75 #define DEBUG_CONSOLE_BACKSPACE 127U
78 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
80 static SemaphoreHandle_t s_debugConsoleReadSemaphore;
81 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
85 #elif (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_BM)
87 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
97 #define IS_RUNNING_IN_ISR() SystemGetIRQNestingLevel()
99 #define IS_RUNNING_IN_ISR() __get_IPSR()
103 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
107 #define DEBUG_CONSOLE_CREATE_MUTEX_SEMAPHORE(mutex) ((mutex) = xSemaphoreCreateMutex())
108 #define DEBUG_CONSOLE_DESTROY_MUTEX_SEMAPHORE(mutex) \
113 vSemaphoreDelete(mutex); \
118 #define DEBUG_CONSOLE_GIVE_MUTEX_SEMAPHORE(mutex) \
120 if (IS_RUNNING_IN_ISR() == 0U) \
122 (void)xSemaphoreGive(mutex); \
126 #define DEBUG_CONSOLE_TAKE_MUTEX_SEMAPHORE_BLOCKING(mutex) \
128 if (IS_RUNNING_IN_ISR() == 0U) \
130 (void)xSemaphoreTake(mutex, portMAX_DELAY); \
134 #define DEBUG_CONSOLE_TAKE_MUTEX_SEMAPHORE_NONBLOCKING(mutex, result) \
136 if (IS_RUNNING_IN_ISR() == 0U) \
138 result = xSemaphoreTake(mutex, 0U); \
147 #define DEBUG_CONSOLE_CREATE_BINARY_SEMAPHORE(binary) ((binary) = xSemaphoreCreateBinary())
148 #define DEBUG_CONSOLE_DESTROY_BINARY_SEMAPHORE(binary) \
153 vSemaphoreDelete(binary); \
157 #define DEBUG_CONSOLE_TAKE_BINARY_SEMAPHORE_BLOCKING(binary) ((void)xSemaphoreTake(binary, portMAX_DELAY))
158 #define DEBUG_CONSOLE_GIVE_BINARY_SEMAPHORE_FROM_ISR(binary) ((void)xSemaphoreGiveFromISR(binary, NULL))
160 #elif (DEBUG_CONSOLE_SYNCHRONIZATION_BM == DEBUG_CONSOLE_SYNCHRONIZATION_MODE)
162 #define DEBUG_CONSOLE_CREATE_MUTEX_SEMAPHORE(mutex)
163 #define DEBUG_CONSOLE_DESTROY_MUTEX_SEMAPHORE(mutex)
164 #define DEBUG_CONSOLE_TAKE_MUTEX_SEMAPHORE_BLOCKING(mutex)
165 #define DEBUG_CONSOLE_GIVE_MUTEX_SEMAPHORE(mutex)
166 #define DEBUG_CONSOLE_TAKE_MUTEX_SEMAPHORE_NONBLOCKING(mutex, result) (result = 1U)
168 #define DEBUG_CONSOLE_CREATE_BINARY_SEMAPHORE(binary)
169 #define DEBUG_CONSOLE_DESTROY_BINARY_SEMAPHORE(binary)
170 #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
171 #define DEBUG_CONSOLE_TAKE_BINARY_SEMAPHORE_BLOCKING(binary) \
178 #define DEBUG_CONSOLE_GIVE_BINARY_SEMAPHORE_FROM_ISR(binary) (binary = true)
180 #define DEBUG_CONSOLE_TAKE_BINARY_SEMAPHORE_BLOCKING(binary)
181 #define DEBUG_CONSOLE_GIVE_BINARY_SEMAPHORE_FROM_ISR(binary)
192 #error RTOS type is not defined by DEBUG_CONSOLE_SYNCHRONIZATION_MODE.
196 #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
198 typedef struct _debug_console_write_ring_buffer
200 uint32_t ringBufferSize;
201 volatile uint32_t ringHead;
202 volatile uint32_t ringTail;
203 uint8_t ringBuffer[DEBUG_CONSOLE_TRANSMIT_BUFFER_LEN];
204 } debug_console_write_ring_buffer_t;
211 #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
212 debug_console_write_ring_buffer_t writeRingBuffer;
213 uint8_t readRingBuffer[DEBUG_CONSOLE_RECEIVE_BUFFER_LEN];
224 #if (defined(DATA_SECTION_IS_CACHEABLE) && (DATA_SECTION_IS_CACHEABLE > 0))
254 #if ((SDK_DEBUGCONSOLE == 0U) && defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING) && \
255 (defined(DEBUG_CONSOLE_TX_RELIABLE_ENABLE) && (DEBUG_CONSOLE_TX_RELIABLE_ENABLE > 0U)))
262 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
264 static void DbgConsole_SerialManagerTxCallback(
void *callbackParam,
269 uint32_t sendDataLength;
271 if ((
NULL == callbackParam) || (
NULL == message))
278 ioState->writeRingBuffer.ringTail += message->
length;
279 if (ioState->writeRingBuffer.ringTail >= ioState->writeRingBuffer.ringBufferSize)
281 ioState->writeRingBuffer.ringTail = 0U;
286 if (ioState->writeRingBuffer.ringTail != ioState->writeRingBuffer.ringHead)
288 if (ioState->writeRingBuffer.ringHead > ioState->writeRingBuffer.ringTail)
290 sendDataLength = ioState->writeRingBuffer.ringHead - ioState->writeRingBuffer.ringTail;
294 sendDataLength = ioState->writeRingBuffer.ringBufferSize - ioState->writeRingBuffer.ringTail;
297 (void)SerialManager_WriteNonBlocking(
299 &ioState->writeRingBuffer.ringBuffer[ioState->writeRingBuffer.ringTail], sendDataLength);
304 ioState->writeRingBuffer.ringTail = 0U;
305 ioState->writeRingBuffer.ringHead = 0U;
313 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
315 static void DbgConsole_SerialManagerRxCallback(
void *callbackParam,
319 if ((
NULL == callbackParam) || (
NULL == message))
343 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
345 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING) && \
346 (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_BM) && defined(OSA_USED)
353 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
380 #if DEBUG_CONSOLE_ENABLE_ECHO_FUNCTION
381 static status_t DbgConsole_EchoCharacter(uint8_t *ch,
bool isGetChar,
int *index)
384 if (((*ch != (uint8_t)
'\r') && (*ch != (uint8_t)
'\n')) || (isGetChar))
393 if ((!isGetChar) && (index !=
NULL))
415 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
416 uint32_t sendDataLength;
422 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
437 if (sendDataLength < size)
442 for (
int i = 0; i < (int)size; i++)
466 status = (
status_t)SerialManager_WriteNonBlocking(
481 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
482 #if (defined(DEBUG_CONSOLE_TX_RELIABLE_ENABLE) && (DEBUG_CONSOLE_TX_RELIABLE_ENABLE > 0U))
484 uint32_t sendDataLength;
485 uint32_t totalLength = size;
500 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
502 #if (defined(DEBUG_CONSOLE_TX_RELIABLE_ENABLE) && (DEBUG_CONSOLE_TX_RELIABLE_ENABLE > 0U))
519 if (sendDataLength > 0U)
521 if (sendDataLength > totalLength)
523 sendDataLength = totalLength;
529 totalLength = totalLength - (uint32_t)sentLength;
534 if (totalLength != 0U)
542 }
while (totalLength != 0U);
543 return ((
int)size - (int)totalLength);
579 #if DEBUG_CONSOLE_ENABLE_ECHO_FUNCTION
580 (void)DbgConsole_EchoCharacter(&buf[i],
false, &i);
583 if (((uint8_t)
'\r' == buf[i]) || ((uint8_t)
'\n' == buf[i]))
588 buf[i] = (uint8_t)
'\0';
597 }
while (i < (
int)size);
602 buf[i] = (uint8_t)
'\0';
606 buf[i + 1] = (uint8_t)
'\0';
632 #if DEBUG_CONSOLE_ENABLE_ECHO_FUNCTION
633 (void)DbgConsole_EchoCharacter(ch,
true,
NULL);
652 for (i = 0; i < len; i++)
660 buf[*indicator] = dbgVal;
668 #if ((SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK) || defined(SDK_DEBUGCONSOLE_UART))
675 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
678 .clockRate = clkSrcFreq,
679 .baudRate = baudRate,
687 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
688 serial_port_usb_cdc_config_t usbCdcConfig = {
689 .controllerIndex = (serial_port_usb_cdc_controller_index_t)instance,
693 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
694 serial_port_swo_config_t swoConfig = {
695 .clockRate = clkSrcFreq,
696 .baudRate = baudRate,
698 .protocol = kSerialManager_SwoProtocolNrz,
702 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
703 serial_port_usb_cdc_virtual_config_t usbCdcVirtualConfig = {
704 .controllerIndex = (serial_port_usb_cdc_virtual_controller_index_t)instance,
709 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
711 serialConfig.ringBufferSize = DEBUG_CONSOLE_RECEIVE_BUFFER_LEN;
716 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
724 #if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
732 #if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
740 #if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
741 serialConfig.
portConfig = &usbCdcVirtualConfig;
755 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
765 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
774 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
775 (void)SerialManager_InstallTxCallback(
781 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
787 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
788 (void)SerialManager_InstallRxCallback(
810 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
826 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
835 #if ((SDK_DEBUGCONSOLE > 0U) || \
836 ((SDK_DEBUGCONSOLE == 0U) && defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING) && \
837 (defined(DEBUG_CONSOLE_TX_RELIABLE_ENABLE) && (DEBUG_CONSOLE_TX_RELIABLE_ENABLE > 0U))))
840 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
842 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_BM) && defined(OSA_USED)
853 #if (DEBUG_CONSOLE_SYNCHRONIZATION_MODE == DEBUG_CONSOLE_SYNCHRONIZATION_FREERTOS)
856 if (taskSCHEDULER_RUNNING == xTaskGetSchedulerState())
880 int logLength = 0, dbgResult = 0;
885 va_start(ap, formatString);
913 va_start(ap, formatString);
926 int logLength = 0, dbgResult = 0;
934 va_start(ap, formatString);
938 #if defined(DEBUG_CONSOLE_TRANSFER_NON_BLOCKING)
944 (uint8_t *)printBuf, (
size_t)logLength);
951 #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
952 status_t DbgConsole_TryGetchar(
char *ch)
954 #if (defined(DEBUG_CONSOLE_RX_ENABLE) && (DEBUG_CONSOLE_RX_ENABLE > 0U))
974 #if DEBUG_CONSOLE_ENABLE_ECHO_FUNCTION
975 (void)DbgConsole_EchoCharacter((uint8_t *)ch,
true,
NULL);
1004 #if (defined(__ICCARM__))
1005 #if defined(SDK_DEBUGCONSOLE_UART)
1006 #pragma weak __write
1007 size_t __write(
int handle,
const unsigned char *buffer,
size_t size);
1008 size_t __write(
int handle,
const unsigned char *buffer,
size_t size)
1019 else if ((handle != 1) && (handle != 2))
1023 ret = (uint32_t)0xff;
1029 (void)memcpy(buff, buffer, size);
1038 size_t __read(
int handle,
unsigned char *buffer,
size_t size);
1039 size_t __read(
int handle,
unsigned char *buffer,
size_t size)
1052 for (; size > 0U; size--)
1064 return (
size_t)actualSize;
1069 #elif (defined(__REDLIB__))
1071 #if (defined(SDK_DEBUGCONSOLE_UART))
1072 int __attribute__((weak)) __sys_write(
int handle,
char *buffer,
int size)
1081 if ((handle != 1) && (handle != 2))
1104 #elif defined(__CC_ARM) || defined(__ARMCC_VERSION)
1105 #if defined(SDK_DEBUGCONSOLE_UART)
1106 #if defined(__CC_ARM)
1118 #pragma weak __stdout
1119 #pragma weak __stdin
1124 int fputc(
int ch, FILE *f)
1145 void _sys_exit(
int returncode)
1156 void _ttywrch(
int ch)
1162 char *_sys_command_string(
char *cmd,
int len)
1169 #elif (defined(__GNUC__))
1171 #if ((defined(__GNUC__) && (!defined(__MCUXPRESSO)) && (defined(SDK_DEBUGCONSOLE_UART))) || \
1172 (defined(__MCUXPRESSO) && (defined(SDK_DEBUGCONSOLE_UART))))
1183 if ((handle != 1) && (handle != 2))
1207 for (; size > 0; size--)
1214 *buffer++ = (char)ch;
1217 if ((ch == 0U) || (ch == (uint8_t)
'\n') || (ch == (uint8_t)
'\r'))
1223 return (actualSize > 0) ? actualSize : -1;