cancel
Showing results for 
Search instead for 
Did you mean: 

Function for printing values

skon.1
Senior

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 ?

13 REPLIES 13

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
skon.1
Senior

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.

@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."

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal

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.

It is tuned...

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

@clive1 (NFA Crew) (Community Member)​ ,

When defining the function you suggested - I get the following warning :

"implicit declaration of function strlen"

It points to this line:

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

Should I declare the header file that defines "strlen" ?

Can I use "sizeof" instead ?

S.Ma
Principal

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?

S.Ma
Principal

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...