How do I get C++ iostream (std::cout) to USB serial port (UART)

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

How do I get C++ iostream (std::cout) to USB serial port (UART)

2,413 次查看
rshipman
Contributor V

IDE: MCUXpresso IDE v11.0.1 [Build 2563] [2019-09-01]
Platform: MIMXRT1020-evk eval board

What do I need to do to get std::cout and printf output to go down the USB serial UART connection please (instead of to the semihost MCUXpresso console tab)?

I created a fresh new C++ project (zipped and attached):

MCUXpresso
   File
      New
         Project...
            C/C++
               New C/C++ Project
                  evkmimxrt1020
                     Select C++ Project
                     Select UART
                     Leave everything else default.

Then in the main() function in source/MIMXRT1021xxxxx_Project.cpp:

Replace line:

    PRINTF("Hello World\n");

with:

    // In UART mode, PRINTF output is seen on the USB serial port (TeraTerm)
    // In semihost mode, PRINTF output is seen in the MCUXpresso Telnet Console tab.
    PRINTF("Hello World from PRINTF\r\n");

    // PROBLEM: In UART mode, printf and cout output is _not_ seen on the USB serial port (TeraTerm) or the MCUXpresso Telnet Console tab.
    // In semihost mode, printf and cout output is seen in the MCUXpresso Telnet Console tab.
    printf("Hello word from printf\r\n");
    std::cout << "Hello world from std::cout\r" << std::endl;

Then build and debug as normal, but:

MCUXpresso

   Quickstart Panel

      Quick Settings
         SDK Debug Console

            Try both Semihost console or UART console, which seem to just set SDK_DEBUGCONSOLE to 0 or 1 respectively.

I cannot see any output on the USB port for the printf/cout calls when in UART mode.

In semihost mode PRINTF/printf/cout output is seen in the MCUXpresso console tab.

FYI:
In fsl_debug_console.h:
* if SDK_DEBUGCONSOLE defined to 0,it represents select toolchain printf, scanf. <-- In this case PRINTF = printf
* if SDK_DEBUGCONSOLE defined to 1,it represents select SDK version printf, scanf. <-- In this case PRINTF = DbgConsole_Printf
* if SDK_DEBUGCONSOLE defined to 2,it represents disable debugconsole function.

So how do we get std::cout/printf calls to go out through the USB/UART please?
I am especially interested in std::cout although I suspect either both work or both don't.


Thanks!

标签 (1)
0 项奖励
1 回复

2,135 次查看
victorjimenez
NXP TechSupport
NXP TechSupport

Hello, 

It is not possible to see a printf statement when your project is in UART mode. If you see an example from the SDK, you will see that the PRINTF statement is calling the function "DbgConsole_Printf" which sends the information directly through UART to your PC. If you want to send the information through the UART on the evaluation board you must use PRINTF, which is using the functions from the LPUART drivers of the SDK.

The reason why printf works in semihosting mode is that semihosting works different. To learn more about this mode, you can refer sections 17.4 and 17.5 from the MCUXpresso IDE User Guide.

Regards, 

Victor 

0 项奖励