Using PuTTY to print from STM32

15.4k views Asked by At

I want to print messages from my STM32 Nucleo-L073RZ microcontroller. How should I go about it? Should I use UART? Where can I get the corresponding code?

#include "stm32l0xx.h"
#include "stm32l0xx_nucleo.h"
#include "stm32l0xx_hal.h"
#include "stdio.h"

static void GPIO_Init (void);
static void UART_Init (void);

int main(void)
{
HAL_Init();
GPIO_Init();
printf("Hello");
while(1)
{

}

}

static void GPIO_Init(void)
{
BSP_LED_Init(LED2);
BSP_LED_On(LED2);

GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();


/*Configure GPIO pin : PA13*/
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
}
/*Uart Init Function*/
static void UART_Init(void)
{

}

void EXTI4_15_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);
 }

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_PIN)
{
BSP_LED_Toggle(LED2);
counter();
}

int counter()
{
int i;
i = 0;
i++;
printf("/n %d", i);
}

How do I display the counter on my PC? I want the number of times the interrupt is given to be seen on PuTTY. Should I interface an UART or is it possible to print?

2

There are 2 answers

1
George Troulis On BEST ANSWER

You can use UART on the Nucleo

All Nucleo boards have a built-in UART-to-USB module that automatically transmits data to a Serial Port on your computer. If on windows, open your Control Panel, go to Device Manager, and under COM Ports you should see your Nucleo.

Initialize the UART Peripheral

Reference your Nucleo user manual to see which UART pins connect to the USB port (STM32CubeMX might have these already mapped).

When initializing the peripheral, select a baud rate like 9600, and remember it

Configure PuTTy

Enter the COM port of the Nucleo and the Baud Rate that you selected earlier, and select Serial as the transmission method. You might have to disable some of the hardware flow control options if they are enabled

Code to transmit

HAL has functions for transmitting over UART. Something like HAL_UART_Transmit(...). You'll have to look up how to use the function specifically, plenty of great tutorials out there.

I personally use sprintf to print nicely formatted strings over UART like this:

char buf[64];
sprintf(buf, "Value of counter: %d\r\n", i);

// change huartX to your initialized HAL UART peripheral
HAL_UART_Transmit(&huartX, buf, strlen(buf), HAL_MAX_DELAY);
0
Youcef Ali On

First Add use UART Handler and its init in this function i used UART2 change it to your periph if you use Stm32 Cube or IDE just select the periph it is automatically generated.

Use this function in order to use the print function it's act the same like Printf.

#include <stdint.h>
#include <stdarg.h>    

void printmsg(char *format,...) {
    char str[80];

    /*Extract the the argument list using VA apis */
    va_list args;
    va_start(args, format);
    vsprintf(str, format,args);
    HAL_UART_Transmit(&huart2,(uint8_t *)str, strlen(str),HAL_MAX_DELAY);
    va_end(args);
}

In your Counter function just Change printf to printmsg

int counter()
{
    int i;
    i = 0;
    i++;
    printmsg("/n %d", i);
 }

Remember to change Printmsg uart Handler .