Skip to main content
Visitor II
January 17, 2025
Question

UART basic TX with nucleo L053R8

  • January 17, 2025
  • 2 replies
  • 857 views

Hello there,

 

I've been stuck for hours on setting up a simple UART transmission of the character 'A' on my

Nucleo L053R8 Board.

Thing is, my oscilloscope does not show anything on my PA2 pin. It should be HIGH in idle state though.

Am I missing something, because this is a low power board and it needs special configuration?

My other fear is that I'm missing some linking options, since I'm not using HAL libaries and have configured

it myself. I've checked the registers like 100 times. Both pins PA2 and P3 are configured for AF4, just

as the datasheet says.

So here is my reduced code:

 

#include <stdint.h>
#include <ctype.h>
#include <stm32l053xx.h>

void UART2_Transmit(char data) {
 while ((USART2->ISR & (1<<7)) == 0) {
 }
 USART2->TDR = data; // Write the character to the TDR register for transmission
}

int main(void){
	/*Initialisierung GPIO + Takt*/
	RCC->IOPENR |= RCC_IOPENR_GPIOAEN; // Anders auf diesem STM32 sonst AHBENR
	RCC->IOPENR |= RCC_IOPENR_GPIOBEN; // Takt B Enable
	RCC->IOPENR |= RCC_IOPENR_GPIOCEN; // Takt C Enable

	GPIOA->MODER = 0xE8000000; //alles auf 0 bis auf PA13 und PA14, damit debugger tut
	GPIOA->MODER |= (1 << 5); // PA3 als AF setzbar machen RX
	GPIOA->MODER |= (1 << 7); // Setzt PA2 Alternate Function mode, 
	GPIOA->AFR[0] |= (1 << 14); // und PA3 AF setzen
	GPIOA->AFR[0] |= (1 << 10); // PA2 als AF setzen
	RCC->APB1ENR |= RCC_APB1ENR_USART2EN; // Takt USART enable


	#define UART1_BAUD_RATE 9600
	#define SYSTEM_TAKT 8000000
	USART2->BRR = 833; // Baudrate setzen
	USART2->CR1 = USART_CR1_UE | USART_CR1_RE | USART_CR1_TE; 
	for(;;){

		UART2_Transmit('A');
		UART2_Transmit('B');
		UART2_Transmit('C');

	}
}

 

 My debugger does show that TDR is getting the correct values though, so

USART2 is definitely up and running.

 

Any help is highly appreciated.

    This topic has been closed for replies.

    2 replies

    Super User
    January 17, 2025

    Please get the user guide for your Nucleo board and check that the PA2, PA3 pins are free to use. They may be occupied by something else on the board.

     

     

    Visitor II
    January 17, 2025

    Well, it does say the following:


    USART communication
    The USART2 interface available on PA2 and PA3 of the STM32 microcontroller can be
    connected to ST-LINK MCU, ST morpho connector, or to ARDUINO® connector

    It most certainly is configurable, I'm just missing something.

    Super User
    January 21, 2025

    @Michael8278 wrote:


    It most certainly is configurable, I'm just missing something.


    and it does go on to say that by default the connection is enabled:

    AndrewNeil_0-1737457693141.png

     

    Super User
    January 17, 2025

    Try to configure PA2, PA2 as plain push-pull GPIOs and toggle from your code. Will you see this toggling on a scope?

    Visitor II
    January 18, 2025

    I was heavily using the rest of PORTA so that I did not even think about this.

    You are completely right, neither PA2 nor PA3 are working as basic push-pull GPIOs.

    PA10, which is directly above, is working again.

    I'm not sure why though... The debugger does show, that OD2 and OD3 are set,

    it also shows up on ID2 and ID3, LCKR is 0, the MODE is set correctly...

    I get the feeling that it might be turned off due to some power-saving mode reset state.

    Visitor II
    January 18, 2025

    Ok, it seems the pins are used for serial connection with my PC over STLink... I will try different pins.