Skip to main content
Graduate
February 28, 2024
Solved

printf causes terminal to stay just empty.

  • February 28, 2024
  • 6 replies
  • 4913 views

Hey. I tried to setup `printf` using the following forum post. I am using the `NUCLEO-L4A6ZG`.

Here what I've tried so far. I connected the `USART2` to the Pins `PA2` (RX) and `PA3` (TX):

(By default LPUART1 was configured and connected to PG7/8, since the forum post used USART2 I used it as well.)

usart2.png

After the configuration was saved, and the code generated, I used the code from the previous mentioned forum post. Here a shorter and with comments removed version:

 

 

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdio.h"

// ...

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;

// ...

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USB_OTG_FS_PCD_Init(void);
static void MX_USART2_UART_Init(void);

/* USER CODE BEGIN PFP */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
/* USER CODE END PFP */

// ...
int main(void)
{
 HAL_Init();

 SystemClock_Config();

 MX_GPIO_Init();
 MX_USB_OTG_FS_PCD_Init();
 MX_USART2_UART_Init();

 while (1)
 {
 /* USER CODE END WHILE */
 printf("Hello, World!\n\r");
	 HAL_Delay(1000);
 /* USER CODE BEGIN 3 */
 }
}

// ...

static void MX_USART2_UART_Init(void)
{
 huart2.Instance = USART2;
 huart2.Init.BaudRate = 115200;
 huart2.Init.WordLength = UART_WORDLENGTH_7B;
 huart2.Init.StopBits = UART_STOPBITS_1;
 huart2.Init.Parity = UART_PARITY_NONE;
 huart2.Init.Mode = UART_MODE_TX_RX;
 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart2.Init.OverSampling = UART_OVERSAMPLING_16;
 huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
 huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
 if (HAL_UART_Init(&huart2) != HAL_OK)
 {
 Error_Handler();
 }
}

// ...

/* USER CODE BEGIN 4 */
PUTCHAR_PROTOTYPE {
	HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
	return ch;
}
/* USER CODE END 4 */

 

 

Then I build it, created the Debug-Profile and ran/resumed it via the Debug-Mode.

I wanted to connect via PuTTY. I turned Flow Control off, and set the data bits to 7. Here are the settings:

putty-settings.png

When I select open I can connect, but the terminal stays just empty.

putty.pngI not sure where the problem is. I would be very grateful if someone could help me.

    This topic has been closed for replies.
    Best answer by mƎALLEm

    You need to look at the NUCLEO-L4A6ZG schematics from this link.

    SofLit_0-1709116064368.png

    You need to connect USART2_Tx to STLK_RX and USART2_Rx to STLK_TX.

    By default LPUART1_TX is connected to STLK_RX and LPUART1_RX to STLK_TX.

    So your UART2 is not exposed to the virtual comport in STLink. 

    PS: make sure LPUART1_TX pin (PG7) is not configured. Keep it in analog input configuration.

    6 replies

    Technical Moderator
    February 28, 2024

    Hello,

    Try first to send data with HAL_UART_Transmit() without printf().

    Try with "8 Bits (including parity)":

     

    huart2.Init.WordLength = UART_WORDLENGTH_8B;

     

     

    stmaxAuthor
    Graduate
    February 28, 2024

    Thanks for your reply! I changed it to 8 bits (both in code and putty) and tried only using HAL_UART_Transmit(). Unfortunately the same result.

     

    #include "main.h"
    
    int main(void)
    {
     HAL_Init();
    
     SystemClock_Config();
    
     MX_GPIO_Init();
     MX_USB_OTG_FS_PCD_Init();
     MX_USART2_UART_Init();
    
     /* USER CODE BEGIN 2 */
     uint8_t message[] = "Hello, World!\n\r";
     HAL_UART_Transmit(&huart2, message, sizeof(message), 0xFFFF);
     /* USER CODE END 2 */
    
     while (1)
     {
    
     }
    }
    
    
    static void MX_USART2_UART_Init(void)
    {
    
     /* USER CODE BEGIN USART2_Init 0 */
    
     /* USER CODE END USART2_Init 0 */
    
     /* USER CODE BEGIN USART2_Init 1 */
    
     /* USER CODE END USART2_Init 1 */
     huart2.Instance = USART2;
     huart2.Init.BaudRate = 115200;
     huart2.Init.WordLength = UART_WORDLENGTH_8B;
     huart2.Init.StopBits = UART_STOPBITS_1;
     huart2.Init.Parity = UART_PARITY_NONE;
     huart2.Init.Mode = UART_MODE_TX_RX;
     huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
     huart2.Init.OverSampling = UART_OVERSAMPLING_16;
     huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
     huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
     if (HAL_UART_Init(&huart2) != HAL_OK)
     {
     Error_Handler();
     }
    
    }
    
    PUTCHAR_PROTOTYPE {
    	HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
    	return ch;
    }

     

     

    Technical Moderator
    February 28, 2024

    Ok you said "(By default LPUART1 was configured and connected to PG7/8, since the forum post used USART2 I used it as well.)"

    Did you connect USART2 to STLK_TX/STLK_RX lines of the STLINK block (replacement of LPUART1)?

    And what is the config of USART2_TX gpio pin? 

    Super User
    February 28, 2024

    The title says, "printf causes non-printable characters", but the body of your post says, "the terminal stays just empty"

    :thinking_face:

    So which is it?

    Getting junk (non-printable characters) usually means the baud rate is wrong;

    Getting nothing at all could be a hardware connection issue and/or configuration issue(s) at either or both ends.

    Do you have an oscilloscope to confirm whether  anything at all is actually coming out of your STM32 and, if anything is coming out, that it's a valid UART signal at the correct baud rate?

    As @mƎALLEm said, are you sure you have the correct connections from your UART to the ST-Link for the USB-to-UART? Or are you using a separate USB-to-UART?

    stmaxAuthor
    Graduate
    February 28, 2024

    Sorry for the confusion. I corrected the title now. Sadly I don't have an oscilloscope to check.

     

    To the last point, I'm currently trying to figure how I can check/connect the UART to the ST-Link correctly. I'm not using a seperate USB-to-UART.

    mƎALLEmAnswer
    Technical Moderator
    February 28, 2024

    You need to look at the NUCLEO-L4A6ZG schematics from this link.

    SofLit_0-1709116064368.png

    You need to connect USART2_Tx to STLK_RX and USART2_Rx to STLK_TX.

    By default LPUART1_TX is connected to STLK_RX and LPUART1_RX to STLK_TX.

    So your UART2 is not exposed to the virtual comport in STLink. 

    PS: make sure LPUART1_TX pin (PG7) is not configured. Keep it in analog input configuration.

    stmaxAuthor
    Graduate
    February 28, 2024

    Sorry if i need to ask again. But does the the STLK_RX/TX refer to those two pins?

    IMG_20240228_115511_521.jpg

     

    I tried to connect RX/TX to them like this. But it didn't work either:
    IMG_20240228_115624_263.jpg

     

    I'm sorry for those dumb questions. Currently feeling totally overwhelmed by all the schematics and documentation. If there is a more straight forward way to configure printf i would of course do use it too.

    Technical Moderator
    February 28, 2024

    Remove the ones crossed.

    You have to use the one in rectangle and you have to connect UART2_Tx/UART2_Rx from CNx connectors

    SofLit_1-1709118546818.png

     

     

    stmaxAuthor
    Graduate
    February 28, 2024

    Okay now It works. Thank you so much for your time and patience!

    Technical Moderator
    February 28, 2024

    @stmax  please keep my answer marked as solution as I've already answered your question.

    Now you ask a new question so you need either to open a new thread or keep this discussion but the original question was answered.

    Thank you for your understanding.

    Super User
    February 28, 2024

    @mƎALLEm This is a common issue that comes up quite a lot.

    As well as clarifying the VCP UART connection diagram (as already mentioned), I think the ST-Link section should specifically state what UART the VCP connects to, and cross-reference to the section with the details:

     

    AndrewNeil_0-1709120230984.png

     

    Perhaps also something could be added  on "how to use a different UART" ... ?

    Technical Moderator
    February 28, 2024

    I think it's something managed in transversal. Each board has it's own UART (sometimes USART1, others USART3, others LPUART1 etc ..). So I think it's not easy to manage in the documentation.

    Super User
    February 28, 2024

    Fair, although the UM2179 here does specifically refer to LPUART1:

    AndrewNeil_0-1709121471586.png

    There should at least be a cross-reference from the ST-Link section to this section (or its equivalent).