Skip to main content
SKhan.7
Associate
December 26, 2022
Question

Need help in making USART2 work on STM32F303RE

Hi, I am doing a MOOC for learning STM32 programming, in the UART topic, we are supposed to implement UART. The course follows STM32F4 but I have an STM32F303RE and I tried to adapt it to my board but it is still not showing anything on the serial monitor (I tried CuteCom and Arduino Serial Monitors as I use a Mac). Can you please tell me what is wrong with the code below? I tried to input various values directly into the BRR register as I had doubts about the baud rate formula in the course.

#include <stdio.h>

#include <stdint.h>

#include "stm32f303xe.h"

#define IOPAEN (1U<<17)

#define UART2EN (1U<<17)

#define CR1_TE (1U<<3)

#define CR1_UE (1U<<13)

#define SR_TXE (1U<<7)

#define SYS_FREQ 16000000

#define APB1_CLK SYS_FREQ

#define UART_BAUDRATE 115200

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 uart2_tx_init(void);

void uart2_write (int ch);

int main (void){

uart2_tx_init();

while(1){

uart2_write('Y');

}

}

void uart2_tx_init(void){

//Configure UART GPIO pin

// Enable clock access to GPIOA

RCC->AHBENR |= IOPAEN;

// Set PA2 to Alternate Function Mode

GPIOA->MODER &= ~(1U<<4);

GPIOA->MODER |= (1U<<5);

// Set PA2 Alternate Function type to UART_TX (AF07)

GPIOA->AFR[0] |= (1U<<8);

GPIOA->AFR[0] |= (1U<<9);

GPIOA->AFR[0] |= (1U<<10);

GPIOA->AFR[0] &= ~(1U<<11);

//Configure UART Module

// Enable clock access to UART2

RCC->APB1ENR |= UART2EN;

// Configure baud rate

uart_set_baudrate(USART2, APB1_CLK, UART_BAUDRATE);

// Configure the transfer direction

USART2->CR1 = CR1_TE;

// Enable UART module

USART2->CR1 |= CR1_UE;

}

void uart2_write (int ch){

//Make sure transmit data register is empty

while (!(USART2->ISR & SR_TXE)){}

//Write to transmit data register

USART2->TDR = (ch & 0xFF);

}

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

}

This topic has been closed for replies.

17 replies

SKhan.7
SKhan.7Author
Associate
December 26, 2022

This is one of the things I like about embedded programming, it makes you go crazy but when it works, it feels sooooooo goooddd...!

SKhan.7
SKhan.7Author
Associate
December 26, 2022

If someone can, please tell me why 8Mhz frequency worked rather than 16Mhz? I see in the reference manual that APB1 runs on 36Mhz.