2

I am working with a stm32f103 and am trying to get the external interrupt done. My code is:

void delay(unsigned int counts);

//---------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

  GPIO_Led.GPIO_Pin = LED_PIN;
  GPIO_Led.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Led.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(LED_PORT, &GPIO_Led);
  LED_OFF;

  delay(500);

  LED_ON;

  // Enable Clock for AFIOEN
  RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
  // Enable Clock for IOPBEN
  RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;

  // Set PB1 as input with pullup
  GPIOB->CRL &= ~((1 << 4) | (1 << 5));
  GPIOB->CRL |= (1 << 7);
  GPIOB->CRL &= ~(1 << 6);
  GPIOB->ODR |= (1 << 1);

  // Source input for EXTI1 is PB1
  AFIO->EXTICR[0] |= AFIO_EXTICR1_EXTI1_PB;

  // Interrupt request from Line 1 is not masked => enabled
  EXTI->IMR |= (1 << 1);
  // Rising edge
  EXTI->RTSR |= (1 << 1);

  NVIC_InitTypeDef NVIC_InitStruct;
  // Add IRQ vector to NVIC
  // PB1 is connected to EXTI_Line1, which has EXTI1_IRQn vector
  NVIC_InitStruct.NVIC_IRQChannel = EXTI1_IRQn;
  // Set priority
  NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x0f;
  // Set sub priority
  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x0f;
  // Enable interrupt
  NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStruct);

  delay(500);

  LED_OFF;

  // Infinite loop
  while (1)
  {
    LED_TOGGLE;
    delay(500);
  }
}


//---------------------------------------------------------------------------------------
void delay(unsigned int counts)
{
  volatile unsigned int i = 0, j = 0;

  while (i < counts)
  {
    j = 0;
    while (j < 0x1AFF)
    {
      j++;
    }
    i++;
  }
}

//---------------------------------------------------------------------------------------
void EXTI1_IRQHandler()
{
  /* Make sure that interrupt flag is set */
  if (EXTI_GetITStatus(EXTI_Line1) != RESET)
  {
    LED_TOGGLE;
    /* Clear interrupt flag */
    EXTI_ClearITPendingBit(EXTI_Line1);
  }
}

That is the main part of my code. LED_ON, LED_OFF and LED_TOGGLE are only some defines. They work fine.

The problem is: If there is a rising edge at pin PB1, the stm32 does not jump to the IRQ subroutine. So the stm32 got stuck.

Does anyone know what is the problem? I don't have any ideas.

Lukas_M94
  • 41
  • 1
  • 4
  • "not jump to the IRQ subroutine. So the stm32 got stuck." I'm new to programming, but maybe there is a problem in vector table. – Long Pham Aug 16 '18 at 17:21
  • I'd review this tutorial and make sure you're not missing any EXTI configurations: https://stm32f4-discovery.net/2014/08/stm32f4-external-interrupts-tutorial/ – Catsunami Aug 16 '18 at 18:39
  • @LongPham what do you mean with problem in the vector table? – Lukas_M94 Aug 16 '18 at 21:55
  • @Catsunami In the first place I did it quite similar to this tutorial, but I had the same error. – Lukas_M94 Aug 16 '18 at 21:57
  • @Lukas_M94 I mean instead of firing EXTI, it might fire one of other interrupts which might be undefined, so the chip jumps to Default_Handler instead (which is, by default, an infinite loop). – Long Pham Aug 17 '18 at 01:39
  • Things would be easier if you have a debugger. You can stop the program whenever you want to see what piece of code the CPU is executing. – Long Pham Aug 17 '18 at 01:40
  • 1
    @LongPham First i thought that the problem is something like an undefault jump, but I do not really know And yes, this is my next step I will do, to buy a debugger :-D – Lukas_M94 Aug 17 '18 at 06:26
  • Just add some LED toggles to the default handler. – Catsunami Aug 17 '18 at 14:29
  • @Catsunami I don't think it's a good practice to do so instead of finding out what caused the problem. – Long Pham Aug 17 '18 at 14:38
  • 1
    @LongPham, I never suggested that this is what he should do **instead** of finding the cause of the problem. I meant that he can **confirm** that it's in the default handler since he obviously doesn't have a debugger to do it the easy way. Toggling an LED within the default handler would confirm that the handler should be looked at. – Catsunami Aug 17 '18 at 16:30

0 Answers0