cancel
Showing results for 
Search instead for 
Did you mean: 

Data transmission and reception using UART

DivyaJayan
Associate III

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

}

}

 

 

5 REPLIES 5
Andrew Neil
Evangelist III

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

 

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 Venmo
Up vote any posts that you find helpful, it shows what's working..

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

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

 


@DivyaJayan wrote:

It is on the ST-board.


Which ST board, exactly ?

 


@DivyaJayan wrote:

I am attempting  to write bare metal code for the same. 


As @Tesla DeLorean suggested, before doing that, try doing it using CubeMX and HAL functions.

Get that working, so that you have a reference against which to check your bare metal code.

 


@DivyaJayan wrote:

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


I don't think that's what your terminal screenshot is showing?

AndrewNeil_0-1715244678699.png

 

That looks like it thinks it's receiving groups of 8 characters,AndrewNeil_1-1715244850489.png, each followed by one NUL.

What do the Hex and Binary options show?

Again, probably the commonest cause of "junk characters" on a terminal like that is wrong baud rate.

To test baud rate, transmit a single uppercase 'U' in a tight loop, and look at the timing on an oscilloscope.