Skip to main content
Associate II
July 25, 2024
Solved

Can't get Printf to work on CubeIDE

  • July 25, 2024
  • 3 replies
  • 7208 views

Currently using a STM32L4R9I-DISCO and I've been struggling to get the SWV ITNM Data Console to work so I can do some debugging. I'm quite new to this, but I've followed every tutorial online to enable UART and how to set it all up, to no avail. I've managed to get the Trace Log to work, but I am looking for values of the variables in loops as the code is executed. Is there any other way to do this or is someone capable of helping me set this up?

Best answer by Andrew Neil

@Hafez1 wrote:

 help me set printf up so I can print the values of variables to the console? 


ST Knowledgebase article on redirecting printf to a UART:

https://community.st.com/t5/stm32-mcus/how-to-redirect-the-printf-function-to-a-uart-for-debug-messages/ta-p/49865

 

And a 3rd-party article on the same:

https://shawnhymel.com/1873/ 

 

EDIT:

So sending printf to SWV is just a matter of putting "send-a-char-to-swv" instead of "send-a-char-to-uart" ...

3 replies

Pavel A.
July 25, 2024

Let's begin with the ITM. Just call ITM_SendChar and ensure it goes out to the CubeIDE window. Send LF (\n) after some text characters to flush the line. If this won't work, something is wrong at ITM level (wrong clock value, something grabs the SWO pin, something not enabled in the GUI...).

Every ST evaluation board comes with a hidden gift to the buyer: a free debugging puzzle and IQ test )) Enjoy!

 

Hafez1Author
Associate II
July 25, 2024

I've got it to work and I can see it in my SWV ITM. Could you please help me set printf up so I can print the values of variables to the console? 

Andrew Neil
Andrew NeilBest answer
Super User
July 25, 2024

@Hafez1 wrote:

 help me set printf up so I can print the values of variables to the console? 


ST Knowledgebase article on redirecting printf to a UART:

https://community.st.com/t5/stm32-mcus/how-to-redirect-the-printf-function-to-a-uart-for-debug-messages/ta-p/49865

 

And a 3rd-party article on the same:

https://shawnhymel.com/1873/ 

 

EDIT:

So sending printf to SWV is just a matter of putting "send-a-char-to-swv" instead of "send-a-char-to-uart" ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Andrew Neil
Super User
July 25, 2024

@Hafez1 wrote:

I'm quite new to this


New to which - just STM32? embedded in general? C programming in general?

 


@Hafez1 wrote:

 struggling to get the SWV ITNM Data Console to work ... I've followed every tutorial online to enable UART and how to set it all up


You realise that SWV and UART are different things?

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Hafez1Author
Associate II
July 25, 2024

New to Embdedded in general. Sorry my wording's a bit off I'm both tired and new to embedded. I meant that I've tried setting up SWV, and UART through PuttyGen for debugging and both don't seem to give me what I want. Thanks for the input.

Andrew Neil
Super User
July 25, 2024

@Hafez1 wrote:

New to Embdedded in general. 


:thumbs_up:

So familiar with C programming in general ?

 

As @Pavel A. suggested, start with just the basic sending a character - whether SWV or UART.

Get that working before trying to add the complications of printf.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Pavel A.
July 25, 2024

+1 and for printf, don't forget the stdout is line-buffered. So end every line with \n.

Or use stderr: fprintf(stderr...)  - it should print immediately.

 

Andrew Neil
Super User
July 25, 2024

@Pavel A. wrote:

 stdout is line-buffered. 


I had noticed that!

So, rather than just a 'send-a-char', your could use a 'send-a-buffer' ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.