cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in transmitting character over UART using STM32L476RG EVB

DivyaJayan
Associate II

I am trying to transmit character over USART2 using STM32L476RG EVB. I am getting "NUL" at the debug terminal instead of the transmitted data. please help. Here is the code and debug terminal:Debug TerminalDebug Terminal

#include <stdint.h>
#include"stm32l476xx.h"

#define GPIOAEN        (1U << 0)
#define UART2EN        (1U << 17)

#define CR1_TE         (1U << 3)
#define CR1_UE         (1U << 0)
#define ISR_TXE        (1U << 7)
#define USART_TDR      (1U << 4)

#define SYS_FREQ        80000000
#define APB1_CLK        SYS_FREQ


static void uart_set_baudrate(USART_TypeDef *USART, uint32_t PeriphClk, uint32_t UART_BAUDRATE);
static uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t UART_BAUDRATE);

uint32_t UART_BAUDRATE = 115200;

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

	RCC -> AHB2ENR |= GPIOAEN;

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


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

	    
	RCC -> AHB1ENR |= UART2EN;

	uart_set_baudrate(USART2, APB1_CLK,UART_BAUDRATE);

	USART2 -> CR1 = CR1_TE;
	USART2 -> CR1 = CR1_UE;
}

void uart2_write(int ch){

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

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

}


static void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t PeriphClk, uint32_t UART_BAUDRATE ){

	USARTx -> BRR = compute_uart_bd(PeriphClk,UART_BAUDRATE);
}

static uint16_t compute_uart_bd(uint32_t PeriphClk,uint32_t UART_BAUDRATE){

	return ((PeriphClk + (UART_BAUDRATE /2U))/ UART_BAUDRATE);

}

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
KDJEM.1
ST Employee

Hello @DivyaJayan ,

 

I advise you to start your project with STM32CubeMX and HAL functions.

For that, I recommend you to refer to STM32StepByStep:Step3 Introduction to the UART - stm32mcu.

When this working, you can refer to this project to check the bare metal code.

May LL_USART and HAL_UART examples help you. 

Thank you.

Kaouthar

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

2 REPLIES 2
KDJEM.1
ST Employee

Hello @DivyaJayan ,

 

I advise you to start your project with STM32CubeMX and HAL functions.

For that, I recommend you to refer to STM32StepByStep:Step3 Introduction to the UART - stm32mcu.

When this working, you can refer to this project to check the bare metal code.

May LL_USART and HAL_UART examples help you. 

Thank you.

Kaouthar

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hi,

Thank you for your replay.

 

Using STM32CubeMX and HAL functions, UART data transmission and reception are successful. However, when attempting to implement the same functionality with bare-metal coding, data transmission and reception fails. Initially, I attempted to use a baud rate of 115200 bps and subsequently tested several other baud rates, but observed no difference in performance. Is this suggests there might be an issue with the BRR value calculation.

Please help.