2020-11-12 05:29 AM
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);
2020-11-12 06:20 AM
Read the RCC_CSR register to find out the reason for the reset and fix it.
2020-11-12 06:25 AM
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..
2020-11-12 06:47 AM
@Community member
Hi,
There is no problem with HAL library for the same pins (PD5 and PD6). I am in trouble with StdPeripheral library.
2020-11-12 09:49 AM
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
2020-11-12 10:57 AM
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);
..