Search code examples
puttystm32halstm32ldiscovery

Using PuTTY to print from STM32


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?


Solution

  • 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);