cancel
Showing results for 
Search instead for 
Did you mean: 

Uart usage with STM32F411RE Nucleo

Sule_Akcay
Associate III

Hello everyone I'm working with Uart on STM32F411RE Nucleo board. It gives a code error, but no data is provided through USART2. Can anyone help me with this problem? The code is as follows: 

void Delay(int time);
static void uart_set_baudrate(uint32_t periph_clk, uint32_t baudrate);
 static uint16_t compute_uart_bd(uint32_t periph_clk, uint32_t baudrate);
 static void Uart_Write(int ch); int __io_putchar(int ch)
 { Uart_Write(ch);
 return ch; } 
int main(void)
 { 
RCC->AHB1ENR |= (1U<<0);
 GPIOA->MODER &= ~(1U<<4);
 GPIOA->MODER |= (1U<<5);
 GPIOA->AFR[0] |= (1U<<8);
 GPIOA->AFR[0] |= (1U<<9);
 GPIOA->AFR[0] |= (1U<<10);
 GPIOA->AFR[0] &=~ (1U<<11);
 RCC->APB1ENR |= (1U<<17);
 uart_set_baudrate(16000000, 115200);
 USART2->CR1 = (1U<<3);
 USART2->CR1 |= (1U<<13);
 while(1) 
{ 
printf("hello\n\r");
 for(int i=0; i<100000; i++)
{} //Delay(10); 
}
} 
void Delay(int time) 
{
 for(int i; time>0; time--) 
{
 
 for(i=0; i<9600; i++) 
{
}
}
} 
static void Uart_Write(int ch)
 { 
while(!(USART2->SR & (1U<<7)))
{};
 USART2->DR = (ch & 0xFF);
 } 
static uint16_t compute_uart_bd(uint32_t periph_clk, uint32_t baudrate) 
{ 
return ((periph_clk + (baudrate/2U))/baudrate);
 } static void uart_set_baudrate(uint32_t periph_clk, uint32_t baudrate) 
{ 
USART2->BRR = compute_uart_bd(periph_clk, baudrate);
 }

Edited (tradition to English) 

 

 

 

 

 

 

 

6 REPLIES 6
STea
ST Employee

Hello @Sule_Akcay ,

I don't know if it is necessary to use direct register access to configure your Uart instance if it is not, I recommend using Hal API which will make your life easier.

otherwise here is the procedure you should follow described in RM0383 section 19.3:

Procedure:
1. Enable the USART by writing the UE bit in USART_CR1 register to 1.
2. Program the M bit in USART_CR1 to define the word length.
3. Program the number of stop bits in USART_CR2.
4. Select DMA enable (DMAT) in USART_CR3 if Multi buffer Communication is to take
place. Configure the DMA register as explained in multibuffer communication.
5. Select the desired baud rate using the USART_BRR register.
6. Set the TE bit in USART_CR1 to send an idle frame as first transmission.
7. Write the data to send in the USART_DR register (this clears the TXE bit). Repeat this for each data to be transmitted in case of single buffer.
8. After writing the last data into the USART_DR register, wait until TC=1. This indicates that the transmission of the last frame is complete. This is required for instance when the USART is disabled or enters the Halt mode to avoid corrupting the last transmission.

you can get a working example of printf surcharging with Uart in the CubeF4 package which can be downloaded from this link or from this GitHub repo.

BR

 

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hello @STea 

I am aware of the existence of the Hal library, but I wanted to set the registers myself to learn coding.
I was able to send from Usart1 and Usart6 by following these steps, but I cannot send from Usart2.

Is there a different setting for Usart2?

You can inspire from HAL and compare the content of your registers to find the issue.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Andrew Neil
Evangelist III

@Sule_Akcay wrote:

 It gives a code error


what do you mean by that?


@Sule_Akcay wrote:

I am aware of the existence of the Hal library, but I wanted to set the registers myself to learn coding.


That's probably not a great approach:  all the arcane details of the registers are likely to get in the way of learning coding - rather than help it.

Probably better to learn the coding first - then move on to understanding the details of the inner workings..

Hello @Andrew Neil 

Actually, this is a matter of curiosity and I am trying it out because I am curious.
Still, thank you for your suggestions.