MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Debug Console

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.It is consist of log, str, io. Log layer is used to handle the formatted log, push log to buffer or flush log to IO. STR layer is used to format the printf and scanf log. IO layer is a warpper of various uart peripheral.

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.

/*
* @brief Initializes the peripheral used to debug messages.
*
* @param baseAddr Indicates which address of the peripheral is used to send debug messages.
* @param baudRate The desired baud rate in bits per second.
* @param device Low level device type for the debug console, can be one of:
* @arg DEBUG_CONSOLE_DEVICE_TYPE_UART,
* @arg DEBUG_CONSOLE_DEVICE_TYPE_LPUART,
* @arg DEBUG_CONSOLE_DEVICE_TYPE_LPSCI,
* @arg DEBUG_CONSOLE_DEVICE_TYPE_USBCDC.
* @param clkSrcFreq Frequency of peripheral source clock.
*
* @return Whether initialization was successful or not.
*/
status_t DbgConsole_Init(uint32_t baseAddr, uint32_t baudRate, uint8_t device, uint32_t clkSrcFreq)

Selects the supported debug console hardware device type, such as

After the initialization is successful, stdout and stdin are connected to the selected peripheral.

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

uint32_t uartClkSrcFreq = CLOCK_GetFreq(BOARD_DEBUG_UART_CLKSRC);
DbgConsole_Init(BOARD_DEBUG_UART_BASEADDR, BOARD_DEBUG_UART_BAUDRATE, DEBUG_CONSOLE_DEVICE_TYPE_UART, uartClkSrcFreq);

Advanced Features

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

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
Not supported
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
* 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(const char *fmt_ptr, ...);
int DbgConsole_Getchar(void);

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

#if SDK_DEBUGCONSOLE /* Select printf, scanf, putchar, getchar of SDK version. */
#define PRINTF DbgConsole_Printf
#define SCANF DbgConsole_Scanf
#define PUTCHAR DbgConsole_Putchar
#define GETCHAR DbgConsole_Getchar
#else /* Select printf, scanf, putchar, getchar of toolchain. */
#define PRINTF printf
#define SCANF scanf
#define PUTCHAR putchar
#define GETCHAR getchar
#endif /* SDK_DEBUGCONSOLE */

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
 

Data Structures

struct  io_state_t
 State structure storing io. More...
 

Macros

#define SDK_DEBUGCONSOLE   1U
 Definition to select sdk or toolchain printf, scanf. More...
 

Typedefs

typedef void(* notify )(size_t *size, bool rx, bool tx)
 define a notify callback for IO More...
 
typedef void(* printfCb )(char *buf, int32_t *indicator, char val, int len)
 A function pointer which is used when format printf log.
 

Functions

void IO_Init (io_state_t *io, uint32_t baudRate, uint32_t clkSrcFreq, uint8_t *ringBuffer)
 io init function. More...
 
status_t IO_Deinit (void)
 Deinit IO. More...
 
status_t IO_Transfer (uint8_t *ch, size_t size, bool tx)
 io transfer function. More...
 
status_t IO_WaitIdle (void)
 io wait idle. More...
 
status_t LOG_Init (uint32_t baseAddr, uint8_t device, uint32_t baudRate, uint32_t clkSrcFreq)
 Initializes. More...
 
void LOG_Deinit (void)
 De-Initializes. More...
 
int LOG_Push (uint8_t *buf, size_t size)
 log push interface More...
 
int LOG_ReadLine (uint8_t *buf, size_t size)
 log read one line function More...
 
int LOG_ReadCharacter (uint8_t *ch)
 log read one character function More...
 
status_t LOG_WaitIdle (void)
 wait log and io idle More...
 
int LOG_Pop (uint8_t *buf, size_t size)
 log pop function More...
 
int StrFormatPrintf (const char *fmt, va_list ap, char *buf, printfCb cb)
 This function outputs its parameters according to a formatted string. More...
 
int StrFormatScanf (const char *line_ptr, char *format, va_list args_ptr)
 Converts an input line of ASCII characters based upon a provided string format. More...
 

Initialization

status_t DbgConsole_Init (uint32_t baseAddr, uint32_t baudRate, uint8_t device, uint32_t clkSrcFreq)
 Initializes the 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_Putchar (int ch)
 Writes a character to stdout. More...
 
int DbgConsole_Scanf (char *fmt_ptr,...)
 Reads formatted data from the standard input stream. More...
 
int DbgConsole_Getchar (void)
 Reads a character from standard input. More...
 
status_t DbgConsole_Flush (void)
 Debug console flush log. More...
 

Data Structure Documentation

struct io_state_t

Data Fields

void * ioBase
 Base of the IP register. More...
 
uint8_t ioType
 device type
 

Field Documentation

void* io_state_t::ioBase

Macro Definition Documentation

#define SDK_DEBUGCONSOLE   1U

Typedef Documentation

typedef void(* notify)(size_t *size, bool rx, bool tx)
Parameters
size,transferdata size.
rx,indicatea rx transfer is success.
tx,indicatea tx transfer is success.

Function Documentation

status_t DbgConsole_Init ( uint32_t  baseAddr,
uint32_t  baudRate,
uint8_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
baseAddrIndicates the address of the peripheral used to send debug messages.
baudRateThe desired baud rate in bits per second.
deviceLow level device type for the debug console, can be one of the following.
  • DEBUG_CONSOLE_DEVICE_TYPE_UART,
  • DEBUG_CONSOLE_DEVICE_TYPE_LPUART,
  • DEBUG_CONSOLE_DEVICE_TYPE_LPSCI,
  • DEBUG_CONSOLE_DEVICE_TYPE_USBCDC.
clkSrcFreqFrequency of peripheral source clock.
Returns
Indicates whether initialization was successful or not.
Return values
kStatus_SuccessExecution successfully
kStatus_FailExecution failure
kStatus_InvalidArgumentInvalid argument existed
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_sFormat control string.
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
chCharacter to be written.
Returns
Returns the character written.
int DbgConsole_Scanf ( char *  fmt_ptr,
  ... 
)

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

Parameters
fmt_ptrFormat 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.
status_t DbgConsole_Flush ( void  )

Call this function to wait the buffer empty and io idle before. If interrupt transfer is using, make sure the global IRQ is enable before call this function This function should be called when 1, before enter power down mode 2, log is required to print to terminal immediately

Returns
Indicates whether wait idle was successful or not.
void IO_Init ( io_state_t io,
uint32_t  baudRate,
uint32_t  clkSrcFreq,
uint8_t *  ringBuffer 
)

Call this function to init IO.

Parameters
ioconfiguration pointer
baudRatebaud rate
clkSrcFreqclock freq
ringbufferused to receive character
status_t IO_Deinit ( void  )

Call this function to Deinit IO.

Returns
deinit status
status_t IO_Transfer ( uint8_t *  ch,
size_t  size,
bool  tx 
)

Call this function to transfer log. Print log:

* IO_Transfer(ch, size, true);
*

Scanf log:

* IO_Transfer(ch, size, false);
*
Parameters
chtransfer buffer pointer
sizetransfer size
txindicate the transfer is TX or RX
status_t IO_WaitIdle ( void  )

Call this function to wait the io idle

Returns
Indicates whether wait idle was successful or not.
status_t LOG_Init ( uint32_t  baseAddr,
uint8_t  device,
uint32_t  baudRate,
uint32_t  clkSrcFreq 
)

Call this function to init the buffer

Parameters
baseAddr,devicebase address
device,devicetype
baudRate,devicecommunicate baudrate
clkSrcFreq,devicesource clock freq
Returns
Indicates whether initialization was successful or not.
Return values
kStatus_SuccessExecution successfully
kStatus_FailExecution failure
void LOG_Deinit ( void  )

Call this function to deinit the buffer

Returns
Indicates whether Deinit was successful or not.
int LOG_Push ( uint8_t *  buf,
size_t  size 
)

Call this function to print log

Parameters
fmt,bufferpointer
size,avaliablesize
Returns
indicate the push size
Return values
indicate buffer is full or transfer fail.
sizereturn the push log size.
int LOG_ReadLine ( uint8_t *  buf,
size_t  size 
)

Call this function to print log

Parameters
fmt,bufferpointer
size,avaliablesize the number of the recieved character
int LOG_ReadCharacter ( uint8_t *  ch)

Call this function to GETCHAR

Parameters
chreceive address the number of the recieved character
status_t LOG_WaitIdle ( void  )

Call this function to wait log buffer empty and io idle before enter low power mode.

Returns
Indicates whether wait idle was successful or not.
int LOG_Pop ( uint8_t *  buf,
size_t  size 
)

Call this function to pop log from buffer.

Parameters
bufbuffer address to pop
sizelog size to pop
Returns
pop log size.
int StrFormatPrintf ( const char *  fmt,
va_list  ap,
char *  buf,
printfCb  cb 
)
Note
I/O is performed by calling given function pointer using following (*func_ptr)(c);
Parameters
[in]fmt_ptrFormat string for printf.
[in]args_ptrArguments to printf.
[in]bufpointer to the buffer
cbprint callbck function pointer
Returns
Number of characters to be print
int StrFormatScanf ( const char *  line_ptr,
char *  format,
va_list  args_ptr 
)
Parameters
[in]line_ptrThe input line of ASCII data.
[in]formatFormat first points to the format string.
[in]args_ptrThe list of parameters.
Returns
Number of input items converted and assigned.
Return values
IO_EOFWhen line_ptr is empty string "".