cancel
Showing results for 
Search instead for 
Did you mean: 

USART2 setup problem

er3481
Senior

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);

5 REPLIES 5
TDK
Guru

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".

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
er3481
Senior

@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

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

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 Venmo
Up vote any posts that you find helpful, it shows what's working..