Why does my interrupt get called, but won't enter the handler?

[亡魂溺海] 提交于 2021-01-29 05:40:38

问题


I'm trying to recieve communication from an USART in interrupt mode. The debugger shows me that the interrupt is getting called on a keypress, but the execution gets stuck in the vector table definition.

I initialize my usart with the following.

static void MX_USART2_UART_Init(void)
{
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 19200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }

  /* USER CODE END USART2_Init 2 */
  LED_Initialize();
  USART2->CR1=USART_CR1_RE|USART_CR1_TE|USART_CR1_UE|USART_CR1_RXNEIE; // Enable interrupt
  NVIC_SetPriority(USART2_IRQn, 2); // set priority level
  NVIC_EnableIRQ(USART2_IRQn); // Enable in NVIC
}


void USART2_IRQHandler(void)
{
    blink_led();
}

If I run the application in debug mode, hit a key, stop and step through the code, I find it's looping on the branch instruction here, inside of the included startup_stm32f446xx.s.

USART2_IRQHandler
        B USART2_IRQHandler
        PUBWEAK USART3_IRQHandler
        SECTION .text:CODE:REORDER:NOROOT(1)

So I know the interrupt is getting generated, but it can't seem to find my handler. Even not in debug mode, my LED doesn't blink, which is a function I've tested seperateky. I'm not sure if this issue is from from the HAL library. I've read through their documentation and variations with their NVIC enable instructions to the same result. The usart works fine in polling, but I need interrupts for my functionality.


回答1:


Your question has "c++" tag, so I assume you compile your project with a C++ compiler. C++ compilers name-mangle function names, which prevents the branch instruction to find its target, because its real name isn't USART2_IRQHandler anymore.

You need prefix your ISR with extern "C" to tell C++ not to name-mangle it.

extern "C" void USART2_IRQHandler(void) {
    blink_led(); 
}


来源:https://stackoverflow.com/questions/57147348/why-does-my-interrupt-get-called-but-wont-enter-the-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!