2024-05-08 02:43 AM
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');
}
}
2024-05-08 03:07 AM
Please use this button to properly post source code:
@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:
2024-05-08 03:30 AM
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.
2024-05-09 01:37 AM
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');
}
}
2024-05-09 01:42 AM
Here is the debug terminal:
2024-05-09 01:57 AM
@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?
That looks like it thinks it's receiving groups of 8 characters,, 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.