cancel
Showing results for 
Search instead for 
Did you mean: 

Retarget printf on STM32G070RBxx Cortex M0+

AVerm.4
Associate III

Hi,

I'm trying retarget printf. My uart_write function is working correctly and i'm able to see character on Teraterm.

But when i try to use printf it is not working. i even tried semihosting to execute printf but it fails. i'm attaching the screenshots -

uart_code.jpg

Disassembly of the code stucks at "bkpt 0x00ab" -disassembly_debug.jpg

any solution for this problem? 

22 REPLIES 22
Andrew Neil
Evangelist III

Just noticed that you are doing this in C++

The linked articles are both just C, and I have just used C.

Could you try this in a plain C project to see if that makes a difference?

 

EDIT:

See: https://community.st.com/t5/stm32cubeide-mcus/stm32cubeide-c-redirected-printf-no-longer-works-how-to-use-cout/m-p/285002/highlight/true#M14943 

 

Tried with the C project as well it's not working.

It printed few lines then threw some garbage. I retried debugging but now nothing displayed on terminal.

1000018158.jpg

Do I need to increase baud rate from 9600 to 115200? Because it's taking time to print.

1000018159.jpg

 


@AVerm.4 wrote:

it's taking time to print.


What do you mean by that?

Remember that printf is line-buffered - it won't (usually) send its output until it gets a newline...

stderr isn't buffered:

https://community.st.com/t5/stm32cubeide-mcus/can-t-get-printf-to-work-on-cubeide/m-p/701740/highlight/true#M29337


@AVerm.4 wrote:

Sorry missed to mention i have disabled semihosting.


and that's stopped the BKPT problem?

@Andrew Neil 

Because I stopped semihosting in this new C project it is not getting stuck at BKPT.

Note - It's not getting stuck at BKPT even in C++ project if not using semihosting.

@Andrew Neil 

"Time to print" i mean to say, it's stuck in buffer before displaying in the terminal. Like it takes couple of minutes to display in terminal when I'm using printf. 

As noted, that's expected behaviour for stdout (used implicitly by printf)

From the other threads, you should be able to disable the buffering with:

setbuf( stdout, NULL );

https://en.cppreference.com/w/c/io/setbuf 

Or use stderr

Or just send a newline.

For C++ we need to add the extern "C"..

Thanks for the suggestions.!

@ST AME Support NF @STTwo-32 can the knowledge base article please be updated to note this?

 

I think the article also needs to explain about the line-buffering - as that is a frequent cause of problems!

KDJEM.1
ST Employee

Hello @Andrew Neil ,

 

Thank you for this proposal. I reported this request internally. 

Your feedback and request are welcome.

 

Thank you.

Kaouthar 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.