Skip to main content
Associate III
May 8, 2024
Question

Data transmission and reception using UART

  • May 8, 2024
  • 2 replies
  • 2163 views

Hi,

I am attempting bare-metal programming for UART data transmission and reception on the STM32L476RG microcontroller. After transmitting data, observed that the debug terminal is only receiving NULL characters. Please help.

 

Here is the code:

 

void SystemInit(void);

void USART2_Init(void);

void USART2_TransmitChar(char data);

 

uint32_t SystemCoreClock = 80000000;

void USART2_Init(void) {

 

RCC->APB1ENR1 |= RCC_APB1ENR1_USART2EN;

RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;

 

 

GPIOA->MODER &= ~(GPIO_MODER_MODE2 | GPIO_MODER_MODE3);

GPIOA->MODER |= GPIO_MODER_MODE2_1 | GPIO_MODER_MODE3_1;

GPIOA->AFR[0] |= (7U << (4 * 2)) | (7U << (4 * 3));

 

 

USART2->BRR = (uint16_t)(SystemCoreClock / 115200);

USART2->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;

 

NVIC_EnableIRQ(USART2_IRQn);

}

 

void USART2_Transmitdata(char data) {

 

while (!(USART2->ISR & USART_ISR_TXE_Msk));

USART2->TDR = data;

}

 

 

void USART2_IRQHandler(void){

 

if(USART2 -> CR1 & USART_ISR_TXE){

char temp = USART2 -> TDR;

USART2 -> TDR = temp;

while(!(USART2 -> ISR & USART_ISR_TC));

}

 

}

 

int main(void) {

USART2_Init();

 

char user_data[5] = "HELLO";

for (int i = 0; user_data[i] != '\0'; ++i) {

USART2_Transmitdata(user_data[i]);

}

 

while (1) {

 

USART2_Transmitdata('H');

USART2_Transmitdata('E');

USART2_Transmitdata('L');

USART2_Transmitdata('L');

USART2_Transmitdata('O');

}

}

 

 

This topic has been closed for replies.

2 replies

Andrew Neil
Super User
May 8, 2024

Please use this button to properly post source code:

AndrewNeil_0-1715162523806.png

 


@DivyaJayan wrote:

on the STM32L476RG microcontroller.


Is that on an ST board, or your custom board?

Please describe how the STM32's UART is connected through to your terminal.

 


@DivyaJayan wrote:

After transmitting data, observed that the debug terminal is only receiving NULL characters.


What do you mean by that?

Please post a screenshot

https://www.techrepublic.com/article/how-to-take-screenshots-in-windows-10/

 

Probably the commonest cause of "junk characters" on a terminal is wrong baud rate:

https://learn.sparkfun.com/tutorials/serial-communication/all#:~:text=If%20all%20the%20receiving%20device%20sees%20on%20its%20receive%20line%20is%20garbage%2C%20check%20to%20make%20sure%20the%20baud%20rates%20match%20up

 

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.
Associate III
May 9, 2024

Hi, 

Thank you for your replay.

It is on the ST-board.

I want to transmit data using UART and I am attempting  to write bare metal code for the same. I am trying to do UART polling method.

Here is my code:

void USART2_Init(void) {

 RCC->APB1ENR1 |= RCC_APB1ENR1_USART2EN;
 RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;


 GPIOA->MODER &= ~(GPIO_MODER_MODE2 | GPIO_MODER_MODE3);
 GPIOA->MODER |= GPIO_MODER_MODE2_1 | GPIO_MODER_MODE3_1;
 GPIOA->AFR[0] |= (7U << (4 * 2)) | (7U << (4 * 3));


 USART2->BRR = 139;
 USART2->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;
}

void USART2_Transmitdata(char data) {

 while (!(USART2->ISR & USART_ISR_TXE_Msk));
 USART2->TDR = data;
}

int main(void) {
 USART2_Init();

 char user_data[5] = "TEST";
 for (int i = 0; user_data[i] != '\0'; ++i) {
 USART2_Transmitdata(user_data[i]);
 }

 while (1) {

 USART2_Transmitdata('T');
 USART2_Transmitdata('E');
 USART2_Transmitdata('S');
 USART2_Transmitdata('T');
 }
}
Associate III
May 9, 2024

Here is the debug terminal: Screenshot (19).png

 

Tesla DeLorean
Guru
May 8, 2024

Likely not the issue, but

char user_data[5] = "HELLO";

This string with a NUL will take SIX bytes.

char user_data[] = "HELLO"; // use this form so the compiler can do it's job

Does it work if you use the HAL to initialize the pins and USART?

If you have something what works you can review what you're doing differently.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..