printf() to UART Console

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

printf() to UART Console

Jump to solution
7,631 Views
zhiqunhu
Contributor III

Have been worked a few projects with KDS and MCUXprosso, and currently using MCUXpresso 11.1.1 and Kinetis SDK 2.7, I found myself still having problems on how to use printf() to send data to a UART console.

 

There are three ways to configure a project regarding printf() to UART console

  1. Create a new project from Quickstart Panel -> Create or import a project -> New project
  2. Change the configuration from Quickstart Panel -> Miscellaneous -> Quick Settings>>
  3. Make changes on Project -> Property -> C/C++ Build -> Settings -> Tool Settings -> Preprocessor

 

It is convenient to have multi-places for some key configurations, but it is confusing as well. For instance, you could have identical Preprocessor settings but projects behave in different ways.

 

Here is the procedure to create a project with printf() which I have had problem with. And I cannot find any solid information on how to use standard IO library printf for uart console in MCUXpresso configuration.

 

Step 1 – Create a new project from MCUXpresso Quick Panel

pastedImage_1.png

Step 2 - Choose right SDK software components

case 1:

pastedImage_2.png

case 2:

pastedImage_3.png

In my case, since I just use UART for connecting to Tera Term VT100 screen, so I simply do:

1. Select FreeRTOS

2. Select all Drivers

3. Select all CMSIS Drivers

4. On Utilities, I have done two ways:

(1). Select none

(2). Select Serial manager shown above

Step 3 - Advanced configurations (tried multi cases, got the same result)

case 1: use Redlib (nohost)

pastedImage_4.png

case 2: use NewlibNano (nohost)

pastedImage_7.png

Step 4 - Check the configurations from Project Property

pastedImage_6.png

It is confusing that the UART is not selected as I have done so in Step 2, but it can be reconfigured.

pastedImage_8.png

I checked the configuration from Quicksettings, and it is correct here.

 

Step 5 - Run the project and see the result

pastedImage_10.png

Result: printf() did not send anything to the UART (without set SERIAL_PORT_TYPE_UART=1).

"MCUXpresso & Kinetis K64 MCU" shown in the console captured in the screenshot below is done by UART_WriteBlocking() for checking UART configuration.

 

Step 6 - Modify Preprocessor by adding SERIAL_PORT_TYPE_UART=1, and run the project and see the same result

pastedImage_15.png

Result is the same.

The UART can be read or written properly with other functions, but printf() cannot route information to the UART, and I have tried with different configurations and I am unable to make it work.

The way I have made printf() work properly on UART/TeraTerm is via semihosting, which I not sure why I have to do that.

A project working properly should look like:

pastedImage_13.png

pastedImage_16.png

What did I miss here?

Thanks.

0 Kudos
1 Solution
7,511 Views
zhiqunhu
Contributor III

I have been using printf for UART console in a few projects with KDS and MCUXPresso IDE and SDK 2.6/2.7, but I did not document the procedure on how I did that and I could not replicate it recently in creating a new Kinetis project. After downloading MCUXpresso 11.2.0 and updated to the SDK 2.8, I decided to give it another try(I need to use printf/sprintf and other stdio functions instead of using PRINTF). This time I figured out how to do so and here are the steps.

To create a new project for a device in MCUXpresso:

1. Select FreeRTOS

2. Select all Drivers

3. Select all CMSIS Drivers

4. Instead of selecting proper Utilities, I went to next:  Advanced project settings first:

pastedImage_6.png

As can been from screenshot below, you are able to check pastedImage_9.pngwithout checking pastedImage_10.png, I checked pastedImage_11.pngas well since I need to use floating point version of printf (this can be done later in Quick Settings).

pastedImage_3.png

After that I go back to select Utilities:

pastedImage_4.png

Then, click Finish to create the new project.

In Settings ->Preprocessor, you should see the settings as below (If you see CR_INTEGER_PRINTF, remove it for floating point)

pastedImage_5.png

Then, you should be able to use all standard library functions in your application.

Hope this helps.

Regards.

View solution in original post

0 Kudos
3 Replies
7,512 Views
zhiqunhu
Contributor III

I have been using printf for UART console in a few projects with KDS and MCUXPresso IDE and SDK 2.6/2.7, but I did not document the procedure on how I did that and I could not replicate it recently in creating a new Kinetis project. After downloading MCUXpresso 11.2.0 and updated to the SDK 2.8, I decided to give it another try(I need to use printf/sprintf and other stdio functions instead of using PRINTF). This time I figured out how to do so and here are the steps.

To create a new project for a device in MCUXpresso:

1. Select FreeRTOS

2. Select all Drivers

3. Select all CMSIS Drivers

4. Instead of selecting proper Utilities, I went to next:  Advanced project settings first:

pastedImage_6.png

As can been from screenshot below, you are able to check pastedImage_9.pngwithout checking pastedImage_10.png, I checked pastedImage_11.pngas well since I need to use floating point version of printf (this can be done later in Quick Settings).

pastedImage_3.png

After that I go back to select Utilities:

pastedImage_4.png

Then, click Finish to create the new project.

In Settings ->Preprocessor, you should see the settings as below (If you see CR_INTEGER_PRINTF, remove it for floating point)

pastedImage_5.png

Then, you should be able to use all standard library functions in your application.

Hope this helps.

Regards.

0 Kudos
5,388 Views
stnslv
Contributor I

Hello zhiqunhu,

I'm trying to do the same initialization but I stuck at "Advanced project settings". I use MCUXpresso IDE v11.5.0 with 2.11.0 SDK Version (MK64FN1M0xxx12). I can't select 'Redirect print/scanf to UART' (it's not active) without checking 'Redirect SDK "PRINTF" to C library "printf"'. It would be nice to have possibility to use standard printf. I tried to set different library types and hosting variants but it didn't help.

stnslv_0-1645184209455.png

Do you have some idea? Did I miss come setting?

Best regards!

 

0 Kudos
7,511 Views
zhiqunhu
Contributor III

The way to make PRINTF() work is to follow configurations below:

1. Step 1 is the same as that in previous post.

2. Step 2 Configure the project:

(1). Select RTOS, Drivers and CMSIS Drivers the same way as previous post.

(2). Select Utilities as the screenshot below:

pastedImage_3.png

Please note to select uart_adapter, otherwise you will get the error like this of missing uart.h file:

pastedImage_5.png

Step 3 - Advanced project settings

pastedImage_6.png

To check on Redirect printf/scanf to UART, you have to check at Redirect SDK "PRINTF" to C library "printf"

If you check SDK Debug Console from QuickSettings, you will see UART is not selected. And if you take a look at the Preprocessor, you will see SDK_DEBUGCONSOLE=0.

pastedImage_7.png

I ran the project and got:

pastedImage_8.png

From assemble code, you can see PRINTF() is still using printf(), so that is why there is nothing to UART.

So you need to modify SDK_DEBUGCONSOLE=1, then build/run again and as can be seen from assembly code, right now PRINTF calls DbgConsole_Print(), so the string "Kinetis uProject: Ver" come to Tera Term console via UART.

pastedImage_1.png

Regards.

0 Kudos