1. Introduction
In embedded system development, debugging and log output are critical for ensuring code correctness and optimizing performance. By monitoring system status in real time, capturing abnormal behavior, and recording key data, developers can quickly locate issues and verify functionality. In resource-constrained embedded environments, choosing the right debugging tools and methods significantly impacts development efficiency.
MCXN947, a high-performance microcontroller launched by NXP, offers multiple debugging and logging solutions, among which ITM (Instrumentation Trace Macrocell) and UART printing are the most commonly used. ITM leverages the Cortex-M core’s debugging capabilities to transmit data at high speed via the debug interface, making it suitable for scenarios with strict real-time requirements. UART printing, on the other hand, outputs logs through the UART peripheral, offering strong versatility and suitability for production environments.
This article explains how to use these two output methods in MCUXpresso IDE to improve development efficiency and system reliability.
2. Principles and Features of ITM and UART Printing
2.1 ITM – Principles and Features
Working Principle: ITM (Instrumentation Trace Macrocell) is a debugging feature provided by the Cortex-M core. It transmits data in real time through the debug interface (e.g., SWD/JTAG) using SWO. Developers can use the ITM_SendChar() function to send debugging information to the ITM port, which is then captured by the debugger and displayed in the IDE’s debug console.
Advantages:
High-speed transmission: ITM uses the bandwidth of the debug interface, achieving speeds far higher than UART (typically over 10 Mbps).
Low CPU overhead: ITM is handled by hardware, consuming almost no CPU resources.
No extra peripherals: Does not rely on UART or other peripherals, saving hardware resources.
Disadvantages:
Debugger dependency: Requires a connected debugger (e.g., MCU-Link, J-Link) and cannot be used directly in production environments.
Complex configuration: Proper ITM port and debugger settings are needed, which may be challenging for beginners.
2.2 UART Printing – Principles and Features
Working Principle: UART printing sends debugging information to a serial terminal via the UART peripheral. Developers typically redirect the printf function to UART, enabling standard library functions for log output. Data is transmitted through TX/RX pins and can be viewed on a PC using serial tools (e.g., PuTTY, Tera Term). MCUXpresso IDE also integrates a terminal for UART debugging.
Advantages:
Highly versatile: Almost all embedded boards support UART, making it widely applicable.
Production-ready: Does not require a debugger and can be used directly in production environments.
Easy to implement: Simple configuration, ideal for beginners and rapid prototyping.
Disadvantages:
Lower speed: Limited by UART baud rate, resulting in lower transmission efficiency.
Peripheral resource usage: Occupies UART peripherals and pins, which may affect other functions.
Poor real-time performance: Due to interrupt delays and baud rate limitations, unsuitable for high real-time scenarios.
2.3 Summary of Comparison
The following table summarizes the main characteristics of ITM versus UART printing:
Feature
ITM
UART Printing
Transmission Speed
High (10 Mbps or more)
Low (typically 115200 bps)
CPU Overhead
Low
Higher
Peripheral Dependency
None
Requires UART peripheral & pins
Debugger Dependency
Required
Not required
Production Suitability
Not suitable
Suitable
Configuration Complexity
Complex
Simple
Real-time Performance
High
Low
Usage Scenarios:
ITM: Ideal for development-phase debugging with high real-time requirements (e.g., motor control, signal processing).
UART Printing: Suitable for production logging, beginner-friendly setups, and general debugging needs.
3. Implementation Steps in MCUXpresso IDE
3.1 Using ITM Printing
Hardware Requirement: Ensure the chip’s SWO pin is connected to the debugger’s SWO interface.
Software Configuration:
1) When creating a new project, select “Redirect printf/scanf to ITM” to redirect printf/scanf to ITM.
2) Configure the Trace clock:
/*!< Switch TRACE to TRACE_DIV */
CLOCK_AttachClk(kTRACE_DIV_to_TRACE);
/*!< Set up dividers */
/*!< Set TRACECLKDIV divider to value 3 */
CLOCK_SetClkDiv(kCLOCK_DivTraceClk, 3U);
3)In MCUXpresso IDE, open the SWO ITM Console and configure Core Clock and Trace Clock settings.
4)View the output results in the ITM console.
3.2 Using UART Printing
1)When creating a new project, select SDK Debug Console to UART to set DebugConsole to use UART.
2)Use MCUXpresso IDE’s peripheral tools to configure DebugConsole-UART.
3)Configure UART TX/RX pins using the Pins tool.
4)Connect the serial hardware and view the output results in a terminal.
4. Conclusion
ITM and UART printing each have their advantages. Developers should choose the appropriate method based on project requirements. ITM is better suited for high real-time debugging during development, while UART printing is ideal for production environments and general debugging.
記事全体を表示