cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 CubeMX ST-LinkV2.1 : how to redirect printf

David PICARD
Associate II
Posted on February 02, 2017 at 09:31

I found several posts like

http://www.openstm32.org/forumthread339

, using syscalls.c, but I don't have this file in my project... I generate the code with CubeMX v4.18.0.

#qt-creator #cubemx #stm32 #arm-none-eabi
2 REPLIES 2
Sirma Siang
ST Employee
Posted on February 02, 2017 at 09:47

Hello David,

To get printf working, you have to do the following.

In the main.c, add this code in the relevant sections:

  • /* USER CODE BEGIN Includes */

    #include

    'stdio.h'

    /* USER CODE END Includes */

  • /* USER CODE BEGIN PFP */

    /* Private function prototypes -----------------------------------------------*/

    #ifdef

    __GNUC__

    /* With GCC/RAISONANCE, small

    printf

    (option LD Linker->Libraries->Small

    printf

    set to 'Yes') calls __io_putchar() */

    #define

    PUTCHAR_PROTOTYPE

    int

    __io_putchar(

    int

    ch)

    #else

    #define

    PUTCHAR_PROTOTYPE

    int

    fputc(

    int

    ch, FILE *f)

    #endif

    /* __GNUC__ */

    /* USER CODE END PFP */

  • /* USER CODE BEGIN 4 */

    /**

    * @brief

    Retargets

    the C library

    printf

    function to the USART.

    * @

    param

    None

    * @

    retval

    None

    */

    PUTCHAR_PROTOTYPE

    {

    /* Place your implementation of

    fputc

    here */

    /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */

    // HAL_UART_Transmit(&UartHandle, (uint8_t *)

    &ch

    , 1, 0xFFFF);

    HAL_UART_Transmit(&huart2, (

    uint8_t

    *)&ch, 1, 0xFFFF);

     

    return

    ch;

    }

    /* USER CODE END 4 */

In combination with the syscall.c, you should get your printf getting out on the UART.

Kind regards

Sirma

Posted on February 02, 2017 at 09:50

Addapt the code to the uart handler you are using :)

With cube, I generate mine with UART2. So in the code huart2 has been used.

You can refer to some projects examples in the firmware library directory.

Under the family you are using, you will find them.

Kind regards

Sirma