Skip to main content
Yoshimitsu_Manji_clan
Associate II
September 10, 2024
Question

USART error in STM32F4

  • September 10, 2024
  • 5 replies
  • 2251 views

Good evening community members,

today I have been trying to activate USART module of my STM32F446 board. However even after trying everything, I could not sense Tx signal on PA2 pin of the board. This is the low level code I used. I had been trying to capture the signal using Oscilloscope but got nothing. Could someone help me how to resolve the issue? I tried my best but found nothing so far. Thanks in advance. 

 

#include "stm32f4xx.h"
#include <stdint.h>
#include <stdio.h>

void gpio_init(void)
{

	RCC->APB1ENR |= (1U<<17); //enable clock for USART2
	GPIOA->MODER |= (0b10<<4); //setting PA2 in alternate function mode
	GPIOA->AFR[0] |= (0b0111<<8); //setting PA2 in AF7 mode
	RCC->AHB1ENR |= (1U<<0); //enable GPIO Port A
}

void usart_init(void)
{
	USART2->BRR = (0x8B); //setting baud rate as 115200
	USART2->CR1 = (1U<<3); //transmitter enable
	USART2->CR1 |= (1U<<13); //enable USART
}

void usart2_data(char c)
{
	while (!(USART2->SR & (1U<<7))){}
	USART2->DR = c;
}

#include <usart_tx.h>


int main(void)
{
	gpio_init();
	usart_init();
	while (1)
	{
		usart2_data('Y');
	}
}

 

5 replies

ST Technical Moderator
September 10, 2024

Hello @Yoshimitsu_Manji_clan,

Ensure that UART settings (stop bits, word length, parity, baud rate...) are configured correctly.

Check if there is no other peripherals are interfering with PA2.

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
Visitor II
September 10, 2024

Recheck your baud rate calculation.

Yoshimitsu_Manji_clan
Associate II
September 11, 2024

Baudrate calculations are as follows:

USARTDIV = (16 * 1000000)/(115200 * 16) =  8.68

Mantissa = 8

Fraction = 0.68 * 16 = 10.88 ~~ 11

Hence BRR = 0x8B

waclawek.jan
Super User
September 11, 2024
GPIOA->MODER |= (0b10<<4); //setting PA2 in alternate function mode
	GPIOA->AFR[0] |= (0b0111<<8); //setting PA2 in AF7 mode
	RCC->AHB1ENR |= (1U<<0); //enable GPIO Port A

You have to set RCC_AHB1ENR.GPIOAEN *before* accessing the GPIOA registers.

You may also want to do that so that there is a delay between setting the clock enable in RCC and first accessing the given peripheral; see errata.

At any case, when in doubts, start debugging with reading out and checking content of the relevant registers.

JW

 

waclawek.jan
Super User
September 12, 2024

Read out and check/post content of UART and relevant GPIO registers.

Make sure the hardware is OK by setting PA2 to GPIO Out and toggling it either manually in debugger, or in program using a simple loopdelay (as in the basic blinky).

JW

waclawek.jan
Super User
September 14, 2024

I took the above code with the correction for GPIO clock enable, commenting out #include <usart_tx.h> which I don't have, and in a DiscoF4 it works as expected, transmitting infinitely on PA2, as observed by oscilloscope.

Now if you observe this using a UART receiver (e.g. UART-USB converter, fed into a PC), depending on how it is started, it may get confused by the infinite stream with no spaces if it does not get synchronized properly on the first character. Try to place delays between the characters.

JW

 

Yoshimitsu_Manji_clan
Associate II
September 16, 2024

Okay community members, here is the conclusion. Instead of USART2, I changed my program for UART4 and the same program started working. I could trace the bits on oscilloscope at PA0 pin. The question why it worked with UART and not with USART remains unsolved. Is there some sort of synchronisaton or USART receiver needed for USART Tx program ??

waclawek.jan
Super User
September 16, 2024

> Is there some sort of synchronisaton or USART receiver needed for USART Tx program ??

No.

Read out and check/post content of USART2 and relevant GPIO registers.

Make sure the hardware is OK by setting PA2 to GPIO Out and toggling it either manually in debugger, or in program using a simple loopdelay (as in the basic blinky).

If this is a Nucleo board, note, that PA2 may be connected to STLink through solder bridges. At any case, make sure that there is nothing on the board connected to PA2 that would prevent if from changing its level.

JW