Skip to main content
Panagiotis
Associate III
December 6, 2019
Question

Print to Serial port of an STM32F030F4P6

  • December 6, 2019
  • 5 replies
  • 5660 views

Hi.

I have a diy board with STM32F030F4P6 Atollic TrueSTUDIO 9.3.0 and STM32CubeMX 5.2.1.

I want to use it 's serial port in order to print (for debugging), like in Arduino we use the Serial.print().

I 've followed a few things I found on the internet but without success.

I use an FTDI board whose:

  • RX is connected to the TX(pin PA2) of the MCU.
  • TX is connected to the RX(pin PA3) of the MCU.

So, I made a project with STM32CubeMX, where I configured pins PA2 as USART1_TX and PA3 as USART1_RX. Here is the usart.c file:

/**
 ******************************************************************************
 * File Name : USART.c
 * Description : This file provides code for the configuration
 * of the USART instances.
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 * opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
 */
 
/* Includes ------------------------------------------------------------------*/
#include "usart.h"
 
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
UART_HandleTypeDef huart1;
 
/* USART1 init function */
 
void MX_USART1_UART_Init(void)
{
 
 huart1.Instance = USART1;
 huart1.Init.BaudRate = 9600;
 huart1.Init.WordLength = UART_WORDLENGTH_8B;
 huart1.Init.StopBits = UART_STOPBITS_1;
 huart1.Init.Parity = UART_PARITY_NONE;
 huart1.Init.Mode = UART_MODE_TX_RX;
 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
 huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
 huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
 if (HAL_UART_Init(&huart1) != HAL_OK)
 {
 Error_Handler();
 }
 
}
 
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
 
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 if(uartHandle->Instance==USART1)
 {
 /* USER CODE BEGIN USART1_MspInit 0 */
 
 /* USER CODE END USART1_MspInit 0 */
 /* USART1 clock enable */
 __HAL_RCC_USART1_CLK_ENABLE();
 
 __HAL_RCC_GPIOA_CLK_ENABLE();
 /**USART1 GPIO Configuration 
 PA2 ------> USART1_TX
 PA3 ------> USART1_RX 
 */
 GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 /* USART1 interrupt Init */
 HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(USART1_IRQn);
 /* USER CODE BEGIN USART1_MspInit 1 */
 
 /* USER CODE END USART1_MspInit 1 */
 }
}
 
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
 
 if(uartHandle->Instance==USART1)
 {
 /* USER CODE BEGIN USART1_MspDeInit 0 */
 
 /* USER CODE END USART1_MspDeInit 0 */
 /* Peripheral clock disable */
 __HAL_RCC_USART1_CLK_DISABLE();
 
 /**USART1 GPIO Configuration 
 PA2 ------> USART1_TX
 PA3 ------> USART1_RX 
 */
 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);
 
 /* USART1 interrupt Deinit */
 HAL_NVIC_DisableIRQ(USART1_IRQn);
 /* USER CODE BEGIN USART1_MspDeInit 1 */
 
 /* USER CODE END USART1_MspDeInit 1 */
 }
} 
 
/* USER CODE BEGIN 1 */
 
/* USER CODE END 1 */
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

And this is the code in main.c while loop:

/* USER CODE BEGIN WHILE */
 while (1)
 {
	 //print out static text
	 int len=sprintf(buffer,"Hello\r\n"); //sprintf will return the length of 'buffer'
	 HAL_UART_Transmit(&huart1, buffer, len, 1000);
 
	 //print out variable
	 len=sprintf(buffer,"This is i:%i\r\n",counter); //sprintf will return the length of 'buffer'
	 HAL_UART_Transmit(&huart1, buffer, len, 1000);
 
	 counter++;
 
 /* USER CODE END WHILE */
 
 /* USER CODE BEGIN 3 */
 }

I used the Arduino IDE 's Serial Monitor, (I found the correct port from Devices Manager) but I saw no result being printed.

When I enter a character and press 'Send' an LED blinks on the FTDI board, so I guess...something happens.

I also tried Putty terminal and also nothing was being printed.

Any help is welcome.

This topic has been closed for replies.

5 replies

Panagiotis
Associate III
December 6, 2019

I grounded Boot0, removed from power and reconnected and it worked!

dbgarasiya
Senior
December 7, 2019

you can also print on terminal using printf on teminal

dbgarasiya
Senior
December 28, 2019

have you tried ?

Panagiotis
Associate III
January 13, 2020

Dbgarasiya thank you for your interest and forgive me for the late answer but I just now saw your comments!

This is what I did:

In my main.c I put this code:

int _write(int file, char *ptr, int len) {
	HAL_UART_Transmit(&huart1,(uint8_t)ptr,len,50);
	return len;
}

So when I use the printf the output is printed in the Serial port!

dbgarasiya
Senior
January 18, 2020

great ,you have solved out problem