Skip to main content
DKalm.1
Associate
January 30, 2020
Question

Activating USART

  • January 30, 2020
  • 2 replies
  • 622 views

Hi,

I have a Problem with the NUCLEO-L476RG.

I tried to activate USART2.

Sending a single Char to a Terminal Programm like Putty or HTERM.

I took the guide from the reference Manual on Page 1341.

But it wont work.

#include "stm32l4xx.h"
#include "stm32l4xx_nucleo.h"
 
int main(void)
{
 //MSI CLK setup
 RCC->CR |=RCC_CR_MSIRGSEL;// enable the MSI CLK
 RCC->CR &= RCC_CR_MSIRANGE_0; 
 // deleting the reset value
 RCC->CR |= RCC_CR_MSIRANGE_11;
 // setting MSI CLK to 48MHz
 
 // USART Setting
 RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
 GPIOA->MODER &= ~GPIO_MODER_MODER2_1;
 //alternetive mode for PA2
 GPIOA->AFR[0] |= 0b0111 << GPIO_AFRL_AFSEL2_Pos;
 //alternetive function 7 for PA2
 RCC->APB1ENR1 |= RCC_APB1ENR1_USART2EN;
 //enable clock for Usart2
 
 //Guide on Page 1341
 USART2->CR1 |= USART_CR1_M; 
 // setting the word length
 USART2->BRR = 417; 
 // setting 115200 Baud 
 //=> 48e6/115200=416,666666
 
 // Stop Bit Programming not necessary, 
 //reset value -> 1 Stop Bit
 
 	 // DMA not necessary , or isn't it ?
 	USART2->CR1 |= USART_CR1_UE|USART_CR1_TE; 
 	// USART2 Enable , Transmitter Enable
 
 	char c = 'C';
 
 for(;;){
 volatile unsigned int i =4800000;
 while(i--);
 while (
 (USART2->ISR & USART_ISR_TXE) == 0);
 	
 USART2->TDR=c;
 }
 
}

Have i forgotten something or done anything wrong?

Thanks,

Dominik

This topic has been closed for replies.

2 replies

Tesla DeLorean
Guru
January 30, 2020

Honestly, if you choose to program at the register level, you really need to learn how to debug at that level also.

If the debugger has a "Peripheral View" use that to descend into the register nodes and check them against expectations set by the Data Sheet and Reference Manual.

Pretty sure you can't arbitrarily change the clock you're currently running from.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
January 30, 2020

> Pretty sure you can't arbitrarily change the clock you're currently running from.

Namely you can't switch to high clock without changing FLASH latency (and in some families, maybe not in 'L4, internal LDO regulator setting).

JW