Facing issue while writing USART driver for 32F429IDISCOVERY Discovery kit( STM32F429ZI MCU)

Hello everyone I have been hitting my head against keyboard from last 5 hours now, I am unable to debug what’s the problem with the code,
from the user manual of the board I got to know that COM port of the board is connected to USART1 ,

and USART tx is connected to PA9, USART1 gets the clock from BUS APB2,

so I wrote the code accordingly and used putty to check the serial output but it displays nothing (I checked the COM port its COM3). Can anyone help ?

#include <stdint.h>

#include "stm32f4xx.h"


#define GPIOAEN (1U<<0)
#define USART1EN (1U<<4)

#define SYS_FREQ 16000000U
#define APB2_CLK SYS_FREQ
#define UART_BAUDRATE 115200
#define CR1_TE (1U<<3)
#define CR1_UE (1U<<13)
#define SR_TXE (1U<<7)

//how many UART? Which one to use
// PA9 and PA10 are TX and RX pin 
static void uart_set_baudrate(USART_TypeDef *USARTx,uint32_t PeriphClk,uint32_t BaudRate);
static uint16_t compute_uart_bd(uint32_t PeriphClk,uint32_t BaudRate);
void uart_write(int ch);
void uar1_tx_init(void);
int main(void)
{
	uar1_tx_init();
	while(1)
	{
		uart_write('Y');
	}
}

void uar1_tx_init(void)
{
	/* *********Configure USART gpio pin******
	 * Enable clock access to gpioa
	 * set PA9 to alt func mode
	 * set PA9 alt func type to UART_TX (AF07)*/
	RCC->AHB1ENR |=GPIOAEN;
	GPIOA->MODER &=~(1U<<18);
	GPIOA->MODER |=(1U<<19);
	GPIOA->AFR[1] |=(1U<<4);
	GPIOA->AFR[1] |=(1U<<5);
	GPIOA->AFR[1] |=(1U<<6);
	GPIOA->AFR[1] &=~(1U<<7);


	/*****Configure usart module***
	 * Enable clock access to usart
	 * configure baudrate
	 *configure the transfer direction
	 Enable the usart module */
	RCC->APB2ENR |= USART1EN;
	uart_set_baudrate(USART1,APB2_CLK,UART_BAUDRATE);
	USART1->CR1=CR1_TE;  //enable txUSART
	USART1->CR1|=CR1_UE;

}

void uart_write(int ch)
{
	/*make sute tx data register is empty
	 * write to tx reg*/
	while(!(USART1->SR & SR_TXE)){};
	USART1->DR=(ch & 0xFF);//since we want to transmit 8 bit

}

static void uart_set_baudrate(USART_TypeDef *USARTx,uint32_t PeriphClk,uint32_t BaudRate)
		{
			USARTx->BRR=compute_uart_bd(PeriphClk,BaudRate);
		}

static uint16_t compute_uart_bd(uint32_t PeriphClk,uint32_t BaudRate)
{

	return (PeriphClk+(BaudRate/2U)/BaudRate);
}




Hi Aditya,

I’ll try to not bury the lede :wink:

If you can confirm the signal is present on the Tx pin with external test tools, then the focus is on the serial interface back to your computer. If there is no signal on Tx, the focus is on your code.

So, if you have a scope, I would take a look at the signal on pin 52 of P1 (PA9) and see if anything is happening. If you don’t see a signal there, then we can focus on your code.

If that looks ok, or at least you see some activity, then the focus goes to how to see the com port over SWO, or whatever your interface happens to be.

If you don’t have a scope you can try setting your UART baud really low (DMM’s can be slow to respond to fast signals) and try to capture the max voltage on pin PA9 with a DMM set to DCV and enable the max function. A max of ~3.3V means that there is a pulse train that should correspond to the ‘Y’ you are trying to send. If you don’t see anything on your DMM, then again the focus is on your code.

In case that doesn’t work, a separate USB to Serial converter is always very helpful - I’ve had spotty results with serial over SWO - and then we try to look at issues with the code.

Can you give a little more info about the connection between the serial port on the development kit and your computer? Are you trying to output serial over the built in ST-link (SWO debug interface), or are you looking directly at PA9 on header P1?

Best,
Pete

1 Like