Skip to main content
er3481
Associate III
November 12, 2020
Question

USART2 setup problem

  • November 12, 2020
  • 5 replies
  • 938 views

Hi,

I want to use USART2 interrupt for STM32F103VET mcu. I have to use StdPeripheral Library for my project. I am using the init code below, but system is reset after each init. Do you have any advise for me?

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
 
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//TX pin
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
 GPIO_Init(GPIOD, &GPIO_InitStructure);
 
 
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//RX pin
 GPIO_Init(GPIOD, &GPIO_InitStructure);
 
 
 USART2_StructInit(&huart2);
 USART_Init(USART2, &huart2);
 USART_ClockStructInit(&usart_clock2);
 USART_ClockInit(USART2, &usart_clock2);
 USART_ITConfig( USART2, USART_IT_RXNE, ENABLE);
 
 USART_Cmd(USART2, ENABLE);
 
 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);
 NVIC_EnableIRQ(USART2_IRQn);

This topic has been closed for replies.

5 replies

TDK
Super User
November 12, 2020

Read the RCC_CSR register to find out the reason for the reset and fix it.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Tesla DeLorean
Guru
November 12, 2020

Check that you're actually remapping the USART2 pins properly, could swear default mapping in PA2/PA3, but not used the F1 actively in a decade..

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
er3481
er3481Author
Associate III
November 12, 2020

@Community member​ 

Hi,

There is no problem with HAL library for the same pins (PD5 and PD6). I am in trouble with StdPeripheral library.

Bassett.David
Associate III
November 12, 2020

Hello Er3481,

You're not enabling the peripheral clocks correctly: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);

Note some of these are on AHB1 and some are on AHB2 - you need to group them correctly and make two separate calls to initialize.

Regards,

Dave

Tesla DeLorean
Guru
November 12, 2020

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);

 /* Enable the USART2 Pins Software Remapping */

 GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);

..

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..