Skip to main content
Graduate
November 25, 2025
Solved

I make a boot on stm32f103, after I enable __enable_irq(); then hang

  • November 25, 2025
  • 2 replies
  • 144 views

I want to make a project on stm32f103, I write some code. When I want to add sd card support, I need to use spi interrrupt enable.

But after I enable the global irq then the code is hang. Besides I debug the code with gdb, then I do ctrl + c , then I found the code hang in some nvic irq handler like WWDG_IRQHandler, UsageFault_Handler, USB_LP_CAN1_RX0_IRQHandler randomly.

I try to disable all nvic irq and clear the pendings , But I found it's useless, the code as below

 int ret;
 LL_SPI_InitTypeDef spi_init;
 LL_GPIO_InitTypeDef gpio_instance = {0};
 LL_GPIO_AF_Remap_SWJ_NOJTAG();
 __HAL_RCC_SPI3_CLK_ENABLE();
 __HAL_RCC_GPIOB_CLK_ENABLE();
 __HAL_RCC_GPIOA_CLK_ENABLE();
 /**SPI GPIO Configuration
 PA15 ------> SPI3_NSS
 PB3 ------> SPI3_CLK
 PB4 ------> SPI3_MISO
 PB5 ------> SPI3_MOSI
 */
 gpio_instance.Pin = LL_GPIO_PIN_15;
 gpio_instance.Mode = LL_GPIO_MODE_ALTERNATE;
 gpio_instance.Speed = LL_GPIO_SPEED_FREQ_HIGH;
 LL_GPIO_Init(GPIOA, &gpio_instance);
 gpio_instance.Pin = LL_GPIO_PIN_3;
 LL_GPIO_Init(GPIOB, &gpio_instance);
 gpio_instance.Pin = LL_GPIO_PIN_4;
 LL_GPIO_Init(GPIOB, &gpio_instance);
 gpio_instance.Pin = LL_GPIO_PIN_5;
 LL_GPIO_Init(GPIOB, &gpio_instance);
 __HAL_RCC_SPI3_CLK_ENABLE();
 LL_SPI_StructInit(&spi_init);
 spi_init.Mode = LL_SPI_MODE_MASTER;
 spi_init.NSS = LL_SPI_NSS_HARD_OUTPUT;
 ret = LL_SPI_Init(SPI3, &spi_init);
 LL_SPI_EnableIT_ERR(SPI3);
 LL_SPI_EnableIT_RXNE(SPI3);
 /* LL_SPI_EnableIT_TXE(SPI3); */
 NVIC_DisableIRQ(WWDG_IRQn);
 HAL_NVIC_SetPriority(SPI3_IRQn, 6U, 0U);
 /* HAL_NVIC_EnableIRQ(SPI3_IRQn); */
 LL_SPI_Enable(SPI3);
 WWDG->CR &= ~WWDG_CR_WDGA;
 WWDG->CFR &= ~WWDG_CFR_EWI;
 WWDG->SR = 0x00;
 NVIC_ClearPendingIRQ(WWDG_IRQn);
 __HAL_RCC_WWDG_CLK_DISABLE();
 __HAL_RCC_WWDG_CLK_ENABLE(); // 确保外设有时钟
 WWDG->CR &= ~WWDG_CR_WDGA; // 停止 WWDG
 WWDG->CFR &= ~WWDG_CFR_EWI; // 关闭早期唤醒
 WWDG->SR = 0x00; // 清 EWIF
 NVIC_ClearPendingIRQ(WWDG_IRQn); // 清 NVIC pending
 __HAL_RCC_WWDG_CLK_DISABLE(); // 如需彻底关掉再关时钟
 rboot_kprintf("%s %d\r\n", __func__, __LINE__);
 int i = 0;
 for (i = 0; i < sizeof(NVIC->ICER); i++)
 {
 NVIC->ICER[i] = 0XFFFFFFFF;
 rboot_kprintf("%s %d %x\r\n", __func__, __LINE__, NVIC->ICER[i]);
 NVIC->ICPR[i] = 0XFFFFFFFF;
 rboot_kprintf("%s %d %x\r\n", __func__, __LINE__, NVIC->ICPR[i]);
 }

 for (i = 0; i < sizeof(NVIC->IABR); i++)
 {
 rboot_kprintf("%s %d %x\r\n", __func__, __LINE__, NVIC->IABR[i]);
 }
 __enable_irq();
 rboot_kprintf("%s %d\r\n", __func__, __LINE__);
 return ret;

 

    This topic has been closed for replies.
    Best answer by TDK

    Why are you messing with WWDG at all in there?

    Sounds like it's in the default interrupt handler. Read the VECTACTIVE bits to see which interrupt the chip is in. Probably it's in an interrupt you haven't defined. Possibly due to a typo.

    2 replies

    Graduate
    November 25, 2025

    the debug console as below:

    clock freq=36000000
    sdcard_port_init 201
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 206 0
    sdcard_port_init 208 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0
    sdcard_port_init 213 0

    we can see that the code 

    rboot_kprintf("%s %d\r\n", __func__, __LINE__);

    after 

     __enable_irq();
    not print.
    TDKAnswer
    Super User
    November 25, 2025

    Why are you messing with WWDG at all in there?

    Sounds like it's in the default interrupt handler. Read the VECTACTIVE bits to see which interrupt the chip is in. Probably it's in an interrupt you haven't defined. Possibly due to a typo.

    Graduate
    November 26, 2025

    thanks for your patient replay. after I check  VECTACTIVE bits , I found the value is 0XF, It's systick handler. I do not defined the systick hander before, after I define a SysTick_Handler function , it's ok now.