Skip to main content
skon.1
Associate III
July 23, 2020
Question

Function for printing values

  • July 23, 2020
  • 7 replies
  • 4975 views

Hello,

I'm working with the NUCLEO-F722ZE EVB on STM32CubeIDE.

I want to use the main USB connection to the PC as a UART.

The default CubeMX pin configuration already has PD8 and PD9 configured as a UART, so I proceeded with code generation (the auto generated main.c file is attached).

What's the recommended function I to print characters to the PC terminal over the UART ?

For example, if I want to send: "Hello World".

What would be the code for it ?

    This topic has been closed for replies.

    7 replies

    Tesla DeLorean
    Guru
    July 23, 2020

    You can plumb it so printf()/puts() works, and it would pull in extra library code.

    You can write your own StringOut() type function to interact with the USART registers,

    or use HAL functions

    void StringOut(char *s)

    {

    HAL_UART_Transmit(&huart3, (void *)s, strlen(s), 1000);

    }

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    skon.1
    skon.1Author
    Associate III
    July 24, 2020

    @Community member​ ,

    Can you please show an example of what you mean by:

    "plumb it so printf()/puts() works, and it would pull in extra library code."

    Tesla DeLorean
    Guru
    July 24, 2020

    Understand that forums are a land of repetitive questions, a search might yield the specific implementation details for the tools you have chosen to use.

    UART_HandleTypeDef UartHandle = {0};
     
    //****************************************************************************
    // Hosting of stdio functionality through USART (Keil)
    //****************************************************************************
     
    /* Implementation of putchar (also used by printf function to output data) */
    int SendChar(int ch) /* Write character to Serial Port */
    {
     HAL_UART_Transmit(&UartHandle, (void *)&ch, 1, 1000);
     return(ch);
    }
     
    //****************************************************************************
     
    #include <rt_misc.h>
     
    int fputc(int ch, FILE *f) { return (SendChar(ch)); }
     
    int ferror(FILE *f)
    {
     /* Your implementation of ferror */
     return EOF;
    }
     
    void _ttywrch(int ch) { SendChar(ch); }
     
    void _sys_exit(int return_code)
    {
    label: goto label; /* endless loop */
    }
     
     
    //****************************************************************************
     
    void USART2_Init(uint32_t BaudRate) // USART2 TX1/RX1
    {
     GPIO_InitTypeDef GPIO_InitStruct = {0};
     
     __USART2_CLK_ENABLE();
     __GPIOA_CLK_ENABLE();
     
     /* UART RX/TX GPIO pin configuration */
     GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
     GPIO_InitStruct.Pull = GPIO_PULLUP;
     GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
     GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
     
     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
     
     /*## Configure the UART peripheral ######################################*/
     /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
     /* UART configured as follow:
     - Word Length = 8 Bits
     - Stop Bit = One Stop bit
     - Parity = NO parity
     - BaudRate = 115200 baud
     - Hardware flow control disabled (RTS and CTS signals) */
     UartHandle.Instance = USART2;
     
     UartHandle.Init.BaudRate = BaudRate;
     UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
     UartHandle.Init.StopBits = UART_STOPBITS_1;
     UartHandle.Init.Parity = UART_PARITY_NONE;
     UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
     UartHandle.Init.Mode = UART_MODE_TX_RX;
     
     UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
    #ifdef UART_ONE_BIT_SAMPLE_DISABLE
     UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
    #endif
     
     if (HAL_UART_Init(&UartHandle) != HAL_OK)
     {
     /* Initialization Error */
     Error_Handler();
     }
    }
     
    //****************************************************************************

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    skon.1
    skon.1Author
    Associate III
    July 23, 2020

    Thanks,

    This is what I used:

    char buffer[] = "Hello World";
    HAL_UART_Transmit(&huart3, buffer, sizeof(buffer), 0xFFFF);

    I turned on "Putty" configured it to 115200 (the same way as the CubeMx is).

    Instead of seeing "Hello World" printed on the screen I see garbage (attached).

    Generally I'd guess it's a baud rate mismatch - but as I said the setting in the CubeMx matches the Putty.

    S.Ma
    Principal
    July 24, 2020

    if you use the usb to usart service from stlink, tune on pc side the baudrate which will control stlink uart. your target should match. for debug, i build my own printf.

    skon.1
    skon.1Author
    Associate III
    July 24, 2020

    It is tuned...

    The baudrate on my PC is the same as the configuration at the MXCube.

    S.Ma
    Principal
    July 25, 2020

    Personnaly, LL is more practical for USART than HAL especially for single byte chunks

    Do send your output message every 1 second to monitor the output.

    If you have a scope, check the duration of the start bit and compare it with the baud rate.

    I don't know putty and if it uses unicode or ASCII, can you try with Teraterm?

    skon.1
    skon.1Author
    Associate III
    July 25, 2020
    1. What's "LL" ?
    2. I found out that his problem only happens when I use the board defaults. As I explained in this post: https://community.st.com/s/question/0D53W00000DKSfVSAX/uart-problem-when-using-default-initialization
    S.Ma
    Principal
    July 25, 2020

    if the baudrate is wrong at least should your get something else than the same char repeteadly... and it's even not a wrong ASCII code...

    S.Ma
    Principal
    July 25, 2020

    otherwise, try for run to get an HC-06 BT dongle (few bucks) and plug it on a free usart. It's 3.3 or 5V powered, 4 wires, then you can work wirelessly even when STlink is removed or your app is reloaded. The default speed is 9600bps and with the well documented AT+BAUD8 you can reprogram it to 115200bps which works reliably (don't try higher baud rate if sending continuously, BT packetize+timeout+buffer_when_out_of_range_momentaritly)

    And you can even use it with remote display on open plateforms such as android (look at Bluetooth Electronics in Play Store)

    RMcCa
    Senior II
    July 25, 2020

    I don't use hal, but I'm pretty sure it's not gonna work if you don't pass the address of the buffer to the hal transmit function. Clive's code passes the address of the buffer to his tansmit function, so there is (void *) cast in the hal function call to keep ​the compiler quiet. You need the & operator to dereference ( get the address of ) the buffer.