cancel
Showing results for 
Search instead for 
Did you mean: 

UART not transmitting in baremetal or cubeide

seb5767
Associate II

From my understanding im doing everything right im using a stm32f446re. I've tried using cube ide where all the peripherals are loaded for me and it still doesn't seem to transmit i cannot see anything in the Putty or even the stm32 console. someone please help me understand

#include "Registers.h"

#define USART2_SR  *(volatile uint32_t *) (USART2_BASE + 0x00U)
//Data Register (The mailbox slot where you load characters)
#define USART2_DR  *(volatile uint32_t *) (USART2_BASE + 0x04U)
//Baud Rate Register (Controls transmission speed)
#define USART2_BRR *(volatile uint32_t *) (USART2_BASE + 0x08U)
//Control Register 1 (The main power and pin enable dashboard
#define USART2_CR1 *(volatile uint32_t *) (USART2_BASE + 0x0CU)


void init_uart()
{
	RCC_APB1ENR |= (1U<<17);
	GPIOA_MODER &= ~(3 << 2*2);
	GPIOA_MODER |= (2U << 2*2);
	GPIOA_AFRL &= ~(0xF<<2*4);
	GPIOA_AFRL |= (7<<2*4);
	USART2_BRR = 0x08B;
	USART2_CR1 |= (1U<<13);
	USART2_CR1 |= (1U<<3);
}

void uart_send_byte(uint8_t data)
{
	// Wait until the Transmit Data Register Empty (TXE) flag is set
	    // TXE is Bit 7 in the Status Register (USART2_SR)
	    while (!(USART2_SR & (1U << 7)))
	    {
	        // Do nothing until the hardware is ready for the next byte
	    }

	    // Stuff the byte into the mailbox slot
	    USART2_DR = data;
}

 

12 REPLIES 12

I'd say it's a common issue caused by forgetting to enable the UART clock.

bit USART2EN in register RCC_APB1ENR 

If you were to debug or step through the program and check the registers, you would likely see that the contents of USART_BRR and USART_CR1 are zero and don't match what you're writing to them, which is a typical sign that you forgot to enable the clock.


@Michal Dudka wrote:

I'd say it's a common issue caused by forgetting to enable the UART clock.
bit USART2EN in register RCC_APB1ENR 


Well, the posted code does set something in RCC_APB1ENR:

void init_uart()
{
	RCC_APB1ENR |= (1U<<17);

 @seb5767 this is where your use of "magic numbers" makes life difficult.

Use a meaningful name - and preferably a standard one - so that it's obvious what you're doing here.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

Oh, I'm sorry. In that case, the clock signal for GPIOA isn't being activated.