All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Debug Console Lite

## Overview

This chapter describes the programming interface of the debug console driver.

The debug console enables debug log messages to be output via the specified peripheral with frequency of the peripheral source clock and base address at the specified baud rate. Additionally, it provides input and output functions to scan and print formatted data.

# Function groups

## Initialization

To initialize the debug console, call the DbgConsole_Init() function with these parameters. This function automatically enables the module and the clock.

status_t DbgConsole_Init(uint8_t instance, uint32_t baudRate, serial_port_type_t device, uint32_t clkSrcFreq);

Selects the supported debug console hardware device type, such as

typedef enum _serial_port_type
{
kSerialPort_None = 0U,
kSerialPort_Uart = 1U,
} serial_port_type_t;

After the initialization is successful, stdout and stdin are connected to the selected peripheral. The debug console state is stored in the debug_console_state_t structure, such as shown here.

typedef struct DebugConsoleState
{
uint8_t uartHandleBuffer[HAL_UART_HANDLE_SIZE];
hal_uart_status_t (*putChar)(hal_uart_handle_t handle, const uint8_t *data, size_t length);
hal_uart_status_t (*getChar)(hal_uart_handle_t handle, uint8_t *data, size_t length);
serial_port_type_t type;
} debug_console_state_t;

This example shows how to call the DbgConsole_Init() given the user configuration structure.

DbgConsole_Init(BOARD_DEBUG_USART_INSTANCE, BOARD_DEBUG_USART_BAUDRATE, BOARD_DEBUG_USART_TYPE,
BOARD_DEBUG_USART_CLK_FREQ);

## Advanced Feature

The debug console provides input and output functions to scan and print formatted data.

- Support a format specifier for PRINTF following this prototype " %[flags][width][.precision][length]specifier", which is explained below

flags | Description
--- | ---
- | Left-justified within the given field width. Right-justified is the default.
+ | Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.
(space) | If no sign is written, a blank space is inserted before the value.
# | Used with o, x, or X specifiers the value is preceded with 0, 0x, or 0X respectively for values other than zero. Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written. Used with g or G the result is the same as with e or E but trailing zeros are not removed.
0 | Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier).

Width | Description
--- | ---
(number) | A minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
* | The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

.precision | Description
--- | ---

.number

For integer specifiers (d, i, o, u, x, X) − precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For e, E, and f specifiers − this is the number of digits to be printed after the decimal point. For g and G specifiers − This is the maximum number of significant digits to be printed. For s − this is the maximum number of characters to be printed. By default, all characters are printed until the ending null character is encountered. For c type − it has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.

.* | The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

length | Description
--- | ---
Do not support

specifier | Description
--- | ---
d or i | Signed decimal integer
f | Decimal floating point
F | Decimal floating point capital letters
x | Unsigned hexadecimal integer
X | Unsigned hexadecimal integer capital letters
o | Signed octal
b | Binary value
p | Pointer address
u | Unsigned decimal integer
c | Character
s | String of characters
n | Nothing printed

- Support a format specifier for SCANF following this prototype " %[*][width][length]specifier", which is explained below

* | Description
--- | ---
An optional starting asterisk indicates that the data is to be read from the stream but ignored. In other words, it is not stored in the corresponding argument.

width | Description
--- | ---
This specifies the maximum number of characters to be read in the current reading operation.

length | Description
--- | ---
hh | The argument is interpreted as a signed character or unsigned character (only applies to integer specifiers: i, d, o, u, x, and X).
h | The argument is interpreted as a short integer or unsigned short integer (only applies to integer specifiers: i, d, o, u, x, and X).
l | The argument is interpreted as a long integer or unsigned long integer for integer specifiers (i, d, o, u, x, and X) and as a wide character or wide character string for specifiers c and s.
ll | The argument is interpreted as a long long integer or unsigned long long integer for integer specifiers (i, d, o, u, x, and X) and as a wide character or wide character string for specifiers c and s.
L | The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f, g, and G).
j or z or t | Not supported

specifier | Qualifying Input | Type of argument
--- | --- | ---
c | Single character: Reads the next character. If a width different from 1 is specified, the function reads width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end. | char *
i | Integer: : Number optionally preceded with a + or - sign | int *
d | Decimal integer: Number optionally preceded with a + or - sign | int *
a, A, e, E, f, F, g, G | Floating point: Decimal number containing a decimal point, optionally preceded by a + or - sign and optionally followed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4 | float *
o | Octal Integer: | int *
s | String of characters. This reads subsequent characters until a white space is found (white space characters are considered to be blank, newline, and tab). | char *
u | Unsigned decimal integer. | unsigned int *

The debug console has its own printf/scanf/putchar/getchar functions which are defined in the header file.

int DbgConsole_Printf(const char *fmt_s, ...);
int DbgConsole_Putchar(int ch);
int DbgConsole_Scanf(char *fmt_ptr, ...);
int DbgConsole_Getchar(void);

This utility supports selecting toolchain's printf/scanf or the MCUXpresso SDK printf/scanf.

#if SDK_DEBUGCONSOLE == DEBUGCONSOLE_DISABLE /* Disable debug console */
#define PRINTF
#define SCANF
#define PUTCHAR
#define GETCHAR
#elif SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK /* Select printf, scanf, putchar, getchar of SDK version. */
#define PRINTF DbgConsole_Printf
#define SCANF DbgConsole_Scanf
#define PUTCHAR DbgConsole_Putchar
#define GETCHAR DbgConsole_Getchar
#elif SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN /* Select printf, scanf, putchar, getchar of toolchain. */
#define PRINTF printf
#define SCANF scanf
#define PUTCHAR putchar
#define GETCHAR getchar
#endif /* SDK_DEBUGCONSOLE */

## SDK_DEBUGCONSOLE and SDK_DEBUGCONSOLE_UART

There are two macros SDK_DEBUGCONSOLE and SDK_DEBUGCONSOLE_UART added to configure PRINTF and low level output perihperal.

- The macro SDK_DEBUGCONSOLE is used for forntend. Whether debug console redirect to toolchain or SDK or disabled, it decides which is the frontend of the debug console, Tool chain or SDK. The fucntion can be set by the macro SDK_DEBUGCONSOLE.
- The macro SDK_DEBUGCONSOLE_UART is used for backend. It is use to decide whether provide low level IO implementation to toolchain printf and scanf. For example, within MCUXpresso, if the macro SDK_DEBUGCONSOLE_UART is defined, __sys_write and __sys_readc will be used when __REDLIB__ is defined; _write and _read will be used in other cases.The macro does not specifically refer to the perihpheral "UART". It refers to the external perihperal UART. So if the macro SDK_DEBUGCONSOLE_UART is not defined when tool-chain printf is calling, the semihosting will be used.

The following the matrix show the effects of SDK_DEBUGCONSOLE and SDK_DEBUGCONSOLE_UART on PRINTF and printf. The green mark is the default setting of the debug console.

SDK_DEBUGCONSOLE | SDK_DEBUGCONSOLE_UART | PRINTF | printf
--- | --- | --- | ---
DEBUGCONSOLE_REDIRECT_TO_SDK | defined | UART | UART
DEBUGCONSOLE_REDIRECT_TO_SDK | undefined | UART | semihost
DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN | defined | UART | UART
DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN | undefined | semihost | semihost
DEBUGCONSOLE_DISABLE | defined | No ouput | UART
DEBUGCONSOLE_DISABLE | undefined | No ouput | semihost

# Typical use case

## Some examples use the PUTCHAR & GETCHAR function

ch = GETCHAR();
PUTCHAR(ch);

## Some examples use the PRINTF function

Statement prints the string format.

PRINTF("%s %s\r\n", "Hello", "world!");

Statement prints the hexadecimal format/

PRINTF("0x%02X hexadecimal number equivalents 255", 255);

Statement prints the decimal floating point and unsigned decimal.

PRINTF("Execution timer: %s\n\rTime: %u ticks %2.5f milliseconds\n\rDONE\n\r", "1 day", 86400, 86.4);

## Some examples use the SCANF function

PRINTF("Enter a decimal number: ");
SCANF("%d", &i);
PRINTF("\r\nYou have entered %d.\r\n", i, i);
PRINTF("Enter a hexadecimal number: ");
SCANF("%x", &i);
PRINTF("\r\nYou have entered 0x%X (%d).\r\n", i, i);

## Print out failure messages using MCUXpresso SDK __assert_func:

void __assert_func(const char *file, int line, const char *func, const char *failedExpr)
{
PRINTF("ASSERT ERROR \" %s \": file \"%s\" Line \"%d\" function name \"%s\" \n", failedExpr, file , line, func);
for (;;)
{}
}

## Note:

To use 'printf' and 'scanf' for GNUC Base, add file 'fsl_sbrk.c' in path: ..\{package}\devices\{subset}\utilities\fsl_sbrk.c to your project.

## Modules

| Semihosting

## Macros

#define | DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN 0U
| Definition select redirect toolchain printf, scanf to uart or not. More...
#define | DEBUGCONSOLE_REDIRECT_TO_SDK 1U
| Select SDK version printf, scanf. More...
#define | DEBUGCONSOLE_DISABLE 2U
| Disable debugconsole function. More...
#define | SDK_DEBUGCONSOLE DEBUGCONSOLE_REDIRECT_TO_SDK
| Definition to select sdk or toolchain printf, scanf. More...
#define | PRINTF_FLOAT_ENABLE 0U
| Definition to printf the float number. More...
#define | SCANF_FLOAT_ENABLE 0U
| Definition to scanf the float number. More...
#define | PRINTF_ADVANCED_ENABLE 0U
| Definition to support advanced format specifier for printf. More...
#define | SCANF_ADVANCED_ENABLE 0U
| Definition to support advanced format specifier for scanf. More...
#define | PRINTF DbgConsole_Printf
| Definition to select redirect toolchain printf, scanf to uart or not. More...

## Initialization

status_t | DbgConsole_Init (uint8_t instance, uint32_t baudRate, serial_port_type_t device, uint32_t clkSrcFreq)
| Initializes the peripheral used for debug messages. More...
status_t | DbgConsole_Deinit (void)
| De-initializes the peripheral used for debug messages. More...
int | DbgConsole_Printf (const char *fmt_s,...)
| Writes formatted output to the standard output stream. More...
int | DbgConsole_Vprintf (const char *fmt_s, va_list formatStringArg)
| Writes formatted output to the standard output stream. More...
int | DbgConsole_Putchar (int ch)
| Writes a character to stdout. More...
int | DbgConsole_Scanf (char *fmt_s,...)
| Reads formatted data from the standard input stream. More...
int | DbgConsole_Getchar (void)
| Reads a character from standard input. More...

## Macro Definition Documentation

#define DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN 0U

Select toolchain printf and scanf.

#define DEBUGCONSOLE_REDIRECT_TO_SDK 1U

#define DEBUGCONSOLE_DISABLE 2U

#define SDK_DEBUGCONSOLE DEBUGCONSOLE_REDIRECT_TO_SDK

#define PRINTF_FLOAT_ENABLE 0U

#define SCANF_FLOAT_ENABLE 0U

#define PRINTF_ADVANCED_ENABLE 0U

#define SCANF_ADVANCED_ENABLE 0U

#define PRINTF DbgConsole_Printf

if SDK_DEBUGCONSOLE defined to 0,it represents select toolchain printf, scanf. if SDK_DEBUGCONSOLE defined to 1,it represents select SDK version printf, scanf. if SDK_DEBUGCONSOLE defined to 2,it represents disable debugconsole function.

## Function Documentation

status_t DbgConsole_Init | ( | uint8_t | instance,
|  | uint32_t | baudRate,
|  | serial_port_type_t | device,
|  | uint32_t | clkSrcFreq
| ) |  |

Call this function to enable debug log messages to be output via the specified peripheral, frequency of peripheral source clock, and base address at the specified baud rate. After this function has returned, stdout and stdin are connected to the selected peripheral.

Parameters

  instance | The instance of the module.If the device is kSerialPort_Uart, the instance is UART peripheral instance. The UART hardware peripheral type is determined by UART adapter. For example, if the instance is 1, if the lpuart_adapter.c is added to the current project, the UART periheral is LPUART1. If the uart_adapter.c is added to the current project, the UART periheral is UART1.
  baudRate | The desired baud rate in bits per second.

  device

  Low level device type for the debug console, can be one of the following.
  - kSerialPort_Uart.

  clkSrcFreq | Frequency of peripheral source clock.

Returns
  Indicates whether initialization was successful or not.

Return values

  kStatus_Success | Execution successfully
  kStatus_Fail | Execution failure

status_t DbgConsole_Deinit | ( | void |  | ) |

Call this function to disable debug log messages to be output via the specified peripheral base address and at the specified baud rate.

Returns
  Indicates whether de-initialization was successful or not.

int DbgConsole_Printf | ( | const char * | fmt_s,
|  |  | ...
| ) |  |

Call this function to write a formatted output to the standard output stream.

Parameters

  fmt_s | Format control string.

Returns
  Returns the number of characters printed or a negative value if an error occurs.

int DbgConsole_Vprintf | ( | const char * | fmt_s,
|  | va_list | formatStringArg
| ) |  |

Call this function to write a formatted output to the standard output stream.

Parameters

  fmt_s | Format control string.
  formatStringArg | Format arguments.

Returns
  Returns the number of characters printed or a negative value if an error occurs.

int DbgConsole_Putchar | ( | int | ch | ) |

Call this function to write a character to stdout.

Parameters

  ch | Character to be written.

Returns
  Returns the character written.

int DbgConsole_Scanf | ( | char * | fmt_s,
|  |  | ...
| ) |  |

Call this function to read formatted data from the standard input stream.

Parameters

  fmt_s | Format control string.

Returns
  Returns the number of fields successfully converted and assigned.

int DbgConsole_Getchar | ( | void |  | ) |

Call this function to read a character from standard input.

Returns
  Returns the character read.