Skip to main content
AVerm.4
Associate III
September 17, 2024
Solved

Retarget printf on STM32G070RBxx Cortex M0+

  • September 17, 2024
  • 6 replies
  • 4894 views

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? 

    Best answer by Andrew Neil

    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 

     

    6 replies

    KDJEM.1
    Technical Moderator
    September 17, 2024

    Hello @AVerm.4 ,

     

    I recommend you to refer to this FAQ: How to redirect the printf function to a UART for debug messages and follow the steps.

    This article describes how to redirect the printf output to the STM32 UART peripheral that is connected to the UART pins on the embedded ST-Link that will be transmitted to the host computer and displayed via a windows terminal program, Tera Term using Nucleo-G070RB.

     

    I hope this help you!

    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.
    AVerm.4
    AVerm.4Author
    Associate III
    September 17, 2024

    Hi @KDJEM.1 ,

    I appreciate your comment. But the link provides the way to retarget HAL library UART, which i see is working fine. 

    But here I'm using CMSIS library to write my UART driver. And here it is not working.

    Andrew Neil
    Super User
    September 17, 2024

    @AVerm.4 wrote:

    the link provides the way to retarget HAL library UART, which i see is working fine. 

    But here I'm using CMSIS library to write my UART driver. And here it is not working.


    It really shouldn't make any difference - they all just end up writing to the UART.

    If you're hitting a BKPT instruction, that suggests you're configured for semihosting - you don't want that.

    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
    September 17, 2024

    Please see this for how to capture a screenshot - far better than photographing the screen!

    https://www.techrepublic.com/article/how-to-take-screenshots-in-windows-10/

    For posting source code, please see the posting tips:

    https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

     

    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.
    AVerm.4
    AVerm.4Author
    Associate III
    September 17, 2024

    @Andrew Neil sorry for that. I'm not using a personal computer rather a corporate one were we have limited access to file sharing.!

    Andrew Neil
    Super User
    September 17, 2024

    @AVerm.4 wrote:

    i even tried semihosting


    Presumably, the screenshots you shared show your semihosting attempt - as this works via the BKPT instruction?

    https://interrupt.memfault.com/blog/arm-semihosting 

     

    Instead, follow the Knowledge Base article linked by @KDJEM.1 

    And here's a 3rd-party article on the same: https://shawnhymel.com/1873/ 

     

    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
    Andrew NeilBest answer
    Super User
    September 17, 2024

    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 

     

    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.
    AVerm.4
    AVerm.4Author
    Associate III
    September 17, 2024

    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

     

    Andrew Neil
    Super User
    September 17, 2024

    @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

    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.
    KDJEM.1
    Technical Moderator
    September 18, 2024

    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.
    Andrew Neil
    Super User
    September 18, 2024
    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.