cancel
Showing results for 
Search instead for 
Did you mean: 

printf causes terminal to stay just empty.

stmax
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

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.

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.

View solution in original post

18 REPLIES 18
SofLit
ST Employee

Hello,

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

Try with "8 Bits (including parity)":

 

huart2.Init.WordLength = UART_WORDLENGTH_8B;

 

 

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.

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;
}

 

 

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? 

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.

Sorry I didn't really understand what you mean by "Did you connect USART2 to STLK_TX/STLK_RX lines of the STLINK block (replacement of LPUART1)?", since I'm an absolute beginner. How can I check wheter it's connected. Is it something on the physical board or in the software?

I haven't change anything on the board so far:

IMG_20240228_111443_472.jpg

 

To the point "what is the config of USART2_TX gpio pin?" I just selected the PA2 Pin and assigned USART2_TX.

Screenshot from 2024-02-28 11-18-42.png

 

Andrew Neil
Evangelist III

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

🤔

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 @SofLit 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?

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.

SofLit
ST Employee

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.

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.

@stmax wrote:

 How can I check wheter it's connected. Is it something on the physical board or in the software?

 

 


As @SofLit  suggested, the schematics are the key to see what's connected to what.

These things are also documented in the User Manual, UM2179:

AndrewNeil_0-1709116720144.png

This tells you that the default (out-of-the-box) configuration of the board is to have LPUART1 connected to the ST-Link's VCP.

Therefore, if you want to connect a UART other than LPUART1 to the VCP, you will first have to disconnect LPUART1, and then make the connections to your desired UART.

 

He can keep SB130 and SB131 soldered (as he's a beginner and can damage the PCB) but he must ensure that PG7 and PG8 IOs keep default GPIO pin settings (GPIO input).

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.