<p>This chapter describes the programming interface of the debug console driver.</p>
<p>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. The below picture shows the laylout of debug console.</p>
<p>To initialize the debug console, call the <spanstyle="color:red"><aclass="el"href="a00281.html#ga12e50ee0450679fd8ca950a89338d366"title="Initializes the peripheral used for debug messages. ">DbgConsole_Init()</a></span> function with these parameters. This function automatically enables the module and the clock.</p>
</div><!-- fragment --><p>After the initialization is successful, stdout and stdin are connected to the selected peripheral.</p>
<p>This example shows how to call the <aclass="el"href="a00281.html#ga12e50ee0450679fd8ca950a89338d366"title="Initializes the peripheral used for debug messages. ">DbgConsole_Init()</a> given the user configuration structure.</p>
</div><!-- fragment --><h2><aclass="anchor"id="DbgConsoleAdvFeature"></a>
Advanced Feature</h2>
<p>The debug console provides input and output functions to scan and print formatted data.</p>
<ul>
<li>Support a format specifier for PRINTF following this prototype <span>" %[flags][width][.precision][length]specifier"</span>, which is explained below</li>
</ul>
<divstyle="width:800px;"><tableclass="doxtable">
<tr>
<th>flags </th><th>Description </th></tr>
<tr>
<td>- </td><td>Left-justified within the given field width. Right-justified is the default. </td></tr>
<tr>
<td>+ </td><td>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. </td></tr>
<tr>
<td>(space) </td><td>If no sign is written, a blank space is inserted before the value. </td></tr>
<tr>
<td># </td><td>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. </td></tr>
<tr>
<td>0 </td><td>Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier). </td></tr>
<td>(number) </td><td>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. </td></tr>
<tr>
<td>* </td><td>The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. </td></tr>
<td>.number </td><td>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. </td></tr>
<tr>
<td>.* </td><td>The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. </td></tr>
</table>
</div><tableclass="doxtable">
<tr>
<th>length </th><th>Description </th></tr>
<tr>
<tdcolspan="2">Do not support </td></tr>
</table>
<tableclass="doxtable">
<tr>
<th>specifier </th><th>Description </th></tr>
<tr>
<td>d or i </td><td>Signed decimal integer </td></tr>
<tr>
<td>f </td><td>Decimal floating point </td></tr>
<tr>
<td>F </td><td>Decimal floating point capital letters </td></tr>
<li>Support a format specifier for SCANF following this prototype <spanstyle="color:red">" %[*][width][length]specifier"</span>, which is explained below</li>
</ul>
<divstyle="width:800px;"><tableclass="doxtable">
<tr>
<th>* </th><th>Description </th></tr>
<tr>
<tdcolspan="2">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. </td></tr>
<td>hh </td><td>The argument is interpreted as a signed character or unsigned character (only applies to integer specifiers: i, d, o, u, x, and X). </td></tr>
<tr>
<td>h </td><td>The argument is interpreted as a short integer or unsigned short integer (only applies to integer specifiers: i, d, o, u, x, and X). </td></tr>
<tr>
<td>l </td><td>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. </td></tr>
<tr>
<td>ll </td><td>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. </td></tr>
<tr>
<td>L </td><td>The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f, g, and G). </td></tr>
<th>specifier </th><th>Qualifying Input </th><th>Type of argument </th></tr>
<tr>
<td>c </td><td>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. </td><td>char * </td></tr>
<tr>
<td>i </td><td>Integer: : Number optionally preceded with a + or - sign </td><td>int * </td></tr>
<tr>
<td>d </td><td>Decimal integer: Number optionally preceded with a + or - sign </td><td>int * </td></tr>
<tr>
<td>a, A, e, E, f, F, g, G </td><td>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 </td><td>float * </td></tr>
<td>s </td><td>String of characters. This reads subsequent characters until a white space is found (white space characters are considered to be blank, newline, and tab). </td><td>char * </td></tr>
<tr>
<td>u </td><td>Unsigned decimal integer. </td><td>unsigned int * </td></tr>
</table>
</div><p>The debug console has its own printf/scanf/putchar/getchar functions which are defined in the header file.</p>
</div><!-- fragment --><h2><aclass="anchor"id="DbgConsoleFrontendAndBackend"></a>
SDK_DEBUGCONSOLE and SDK_DEBUGCONSOLE_UART</h2>
<p>There are two macros SDK_DEBUGCONSOLE and SDK_DEBUGCONSOLE_UART added to configure PRINTF and low level output perihperal.</p>
<ul>
<li>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.</li>
<li>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 similar to UART, like as USB CDC, UART, SWO, etc. So if the macro SDK_DEBUGCONSOLE_UART is not defined when tool-chain printf is calling, the semihosting will be used.</li>
</ul>
<p>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. </p>
</div><!-- fragment --><h2>Some examples use the SCANF function</h2>
<divclass="fragment"><divclass="line"><aclass="code"href="a00281.html#gae1649fc947ca37a86917a08354f48d1a">PRINTF</a>(<spanclass="stringliteral">"Enter a decimal number: "</span>);</div>
<divclass="line"><aclass="code"href="a00281.html#gae1649fc947ca37a86917a08354f48d1a">PRINTF</a>(<spanclass="stringliteral">"\r\nYou have entered %d.\r\n"</span>, i, i);</div>
<divclass="line"><aclass="code"href="a00281.html#gae1649fc947ca37a86917a08354f48d1a">PRINTF</a>(<spanclass="stringliteral">"Enter a hexadecimal number: "</span>);</div>
<divclass="line"><aclass="code"href="a00281.html#gae1649fc947ca37a86917a08354f48d1a">PRINTF</a>(<spanclass="stringliteral">"\r\nYou have entered 0x%X (%d).\r\n"</span>, i, i);</div>
</div><!-- fragment --><h2>Print out failure messages using MCUXpresso SDK __assert_func:</h2>
<p>To use 'printf' and 'scanf' for GNUC Base, add file <b>'fsl_sbrk.c'</b> in path: <b>..\{package}\devices\{subset}\utilities\fsl_sbrk.c </b> to your project.</p>
<trclass="memdesc:ga7fdd594efdc8374ecd8684ed758d6cec"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Definition to select sdk or toolchain printf, scanf. <ahref="#ga7fdd594efdc8374ecd8684ed758d6cec">More...</a><br/></td></tr>
<trclass="memdesc:gae1649fc947ca37a86917a08354f48d1a"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Definition to select redirect toolchain printf, scanf to uart or not. <ahref="#gae1649fc947ca37a86917a08354f48d1a">More...</a><br/></td></tr>
<trclass="memdesc:ga12e50ee0450679fd8ca950a89338d366"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Initializes the peripheral used for debug messages. <ahref="#ga12e50ee0450679fd8ca950a89338d366">More...</a><br/></td></tr>
<trclass="memdesc:gad80e7aa70bbb3fce1a9168621372833e"><tdclass="mdescLeft"> </td><tdclass="mdescRight">De-initializes the peripheral used for debug messages. <ahref="#gad80e7aa70bbb3fce1a9168621372833e">More...</a><br/></td></tr>
<trclass="memdesc:ga9ce272e795c2b235265d3dfb50669bee"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Prepares to enter low power consumption. <ahref="#ga9ce272e795c2b235265d3dfb50669bee">More...</a><br/></td></tr>
<trclass="memdesc:ga21831f5ee970f3a1f13ff375405f3592"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Restores from low power consumption. <ahref="#ga21831f5ee970f3a1f13ff375405f3592">More...</a><br/></td></tr>
<trclass="memdesc:ga7f9e0678f4c708ed5640b0823c07dc35"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Writes formatted output to the standard output stream. <ahref="#ga7f9e0678f4c708ed5640b0823c07dc35">More...</a><br/></td></tr>
<trclass="memdesc:ga48560c409b88fbe195e140aa20c5307b"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Writes formatted output to the standard output stream. <ahref="#ga48560c409b88fbe195e140aa20c5307b">More...</a><br/></td></tr>
<trclass="memdesc:gada572d86a06f028b5b1a5d0440f683e3"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Writes a character to stdout. <ahref="#gada572d86a06f028b5b1a5d0440f683e3">More...</a><br/></td></tr>
<trclass="memdesc:ga53b115907016172dcf58fcffab144a6d"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Reads formatted data from the standard input stream. <ahref="#ga53b115907016172dcf58fcffab144a6d">More...</a><br/></td></tr>
<trclass="memdesc:ga11898c5015274863741c4f3f4d9edc08"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Reads a character from standard input. <ahref="#ga11898c5015274863741c4f3f4d9edc08">More...</a><br/></td></tr>
<trclass="memdesc:ga23e7c243b07d594a0a1016dcab28d3a3"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Writes formatted output to the standard output stream with the blocking mode. <ahref="#ga23e7c243b07d594a0a1016dcab28d3a3">More...</a><br/></td></tr>
<trclass="memdesc:gaed722925a966ae462d5d44cd33f836c3"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Writes formatted output to the standard output stream with the blocking mode. <ahref="#gaed722925a966ae462d5d44cd33f836c3">More...</a><br/></td></tr>
<trclass="memdesc:ga867a9778cd1401d3336ae5599851c1fd"><tdclass="mdescLeft"> </td><tdclass="mdescRight">Debug console try to get char This function provides a API which will not block current task, if character is available return it, otherwise return fail. <ahref="#ga867a9778cd1401d3336ae5599851c1fd">More...</a><br/></td></tr>
<p>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. </p>
<p>Call this function to enable debug log messages to be output via the specified peripheral initialized by the serial manager module. After this function has returned, stdout and stdin are connected to the selected peripheral.</p>
<tr><tdclass="paramname">instance</td><td>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. </td></tr>
<tr><tdclass="paramname">baudRate</td><td>The desired baud rate in bits per second. </td></tr>
<tr><tdclass="paramname">device</td><td>Low level device type for the debug console, can be one of the following. <ul>
<li>kSerialPort_Uart, </li>
<li>kSerialPort_UsbCdc </li>
</ul>
</td></tr>
<tr><tdclass="paramname">clkSrcFreq</td><td>Frequency of peripheral source clock.</td></tr>
</table>
</dd>
</dl>
<dlclass="section return"><dt>Returns</dt><dd>Indicates whether initialization was successful or not. </dd></dl>
<p>Call this function to read formatted data from the standard input stream.</p>
<dlclass="section note"><dt>Note</dt><dd>Due the limitation in the BM OSA environment (CPU is blocked in the function, other tasks will not be scheduled), the function cannot be used when the DEBUG_CONSOLE_TRANSFER_NON_BLOCKING is set in the BM OSA environment. And an error is returned when the function called in this case. The suggestion is that polling the non-blocking function DbgConsole_TryGetchar to get the input char.</dd></dl>
<p>Call this function to read a character from standard input.</p>
<dlclass="section note"><dt>Note</dt><dd>Due the limitation in the BM OSA environment (CPU is blocked in the function, other tasks will not be scheduled), the function cannot be used when the DEBUG_CONSOLE_TRANSFER_NON_BLOCKING is set in the BM OSA environment. And an error is returned when the function called in this case. The suggestion is that polling the non-blocking function DbgConsole_TryGetchar to get the input char.</dd></dl>
<dlclass="section return"><dt>Returns</dt><dd>Returns the character read. </dd></dl>
<p>Call this function to write a formatted output to the standard output stream with the blocking mode. The function will send data with blocking mode no matter the DEBUG_CONSOLE_TRANSFER_NON_BLOCKING set or not. The function could be used in system ISR mode with DEBUG_CONSOLE_TRANSFER_NON_BLOCKING set.</p>
<p>Call this function to write a formatted output to the standard output stream with the blocking mode. The function will send data with blocking mode no matter the DEBUG_CONSOLE_TRANSFER_NON_BLOCKING set or not. The function could be used in system ISR mode with DEBUG_CONSOLE_TRANSFER_NON_BLOCKING set.</p>
<p>Call this function to wait the tx buffer empty. 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 </p>
<dlclass="section return"><dt>Returns</dt><dd>Indicates whether wait idle was successful or not. </dd></dl>