cancel
Showing results for 
Search instead for 
Did you mean: 

USART error in STM32F4

Yoshimitsu_Manji_clan
Associate II

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');
	}
}

 

11 REPLIES 11

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

> 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