1
\$\begingroup\$

I'm using an STM32F103C8T6 (aka blue pill).

GPIO Init and interrupt handler:

static void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_15, GPIO_PIN_RESET);

  /*Configure GPIO pins : PC13 PC15 */
  GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pin : PA0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  HAL_NVIC_EnableIRQ(EXTI0_IRQn);

  while(1);

}

void EXTI0_IRQHandler(void)
{
    __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_0);
    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_14);
}

The problem is that the interrupt handler (EXTI0_IRQHandler) is never called. I tried also with other pins (e.g. using EXTI15_10_IRQHandler). I checked the switch and LED separately (in the main function) and those work. I also tried with PULLUP and PULLDOWN (which makes no difference).

Is there more needed to trigger the interrupt function?

\$\endgroup\$
6
  • 1
    \$\begingroup\$ not sure, but is it maybe necessary to globally enable interrupts, or assign a priority to your specific interrupt? Is EXTI0_IRQn a constant or should it be something like EXTI0_IRQ0? \$\endgroup\$ Commented Aug 21, 2017 at 21:04
  • \$\begingroup\$ @MarcusMüller It is a constant and I will check for setting a priority, thanks for the remark. \$\endgroup\$ Commented Aug 21, 2017 at 21:28
  • \$\begingroup\$ @PeterJ Thanks ... I will check, __HAL_RCC_GPIOA_CLK_ENABLE(); I already did (also for GPIOC and GPIOD. \$\endgroup\$ Commented Aug 22, 2017 at 8:43
  • 1
    \$\begingroup\$ @Marcus Müller - did you ever have any contact with those micros? I think you did not \$\endgroup\$ Commented Aug 22, 2017 at 8:50
  • \$\begingroup\$ @PeterJ I don't know why you're being so rude, I explicitly uttered a vague suspicion. Yes, I've been working with stm32f1 before, but without the clunky HAL lib. Excuse me if I don't have the docs to all the microcontrollers I've worked with in my head. \$\endgroup\$ Commented Aug 22, 2017 at 9:44

1 Answer 1

1
\$\begingroup\$

Do the steps from my comment, Here is the sample code. I had a different hardware connections so I used a different pins.

int main(void) {
    RCC->APB2ENR = RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_AFIOEN;


    //confure PB2  PC13 as inputs, PB0 output

    AFIO->EXTICR[0] = AFIO_EXTICR1_EXTI2_PB;
    AFIO->EXTICR[3] = AFIO_EXTICR4_EXTI13_PC;
    EXTI->IMR = EXTI_IMR_MR2 | EXTI_IMR_MR13;
    EXTI->RTSR = EXTI_RTSR_TR2;
    EXTI->FTSR = EXTI_FTSR_TR13;
    NVIC_EnableIRQ(EXTI2_IRQn);
    NVIC_EnableIRQ(EXTI15_10_IRQn);
    while (1);
} 
void EXTI2_IRQHandler(void) {
    if (EXTI->PR & EXTI_PR_PR2) {
        EXTI->PR = EXTI_PR_PR2;
        GPIOB -> ODR |= GPIO_ODR_0;
    }
}

void EXTI15_10_IRQHandler(void) {
    if (EXTI->PR & EXTI_PR_PR13) {
        EXTI->PR = EXTI_PR_PR13;
        GPIOB -> ODR &= ~GPIO_ODR_0;            
    }
}

one pin turns led on another turns off

\$\endgroup\$
10
  • \$\begingroup\$ Thanks for this comment, I use HAL so I first need to find out what all those numbers mean (good exercise for me). Btw, is there a reason you use PC13 as input since for my board PC13 is connected to an output (builtin LED)? \$\endgroup\$ Commented Aug 22, 2017 at 8:57
  • 1
    \$\begingroup\$ I havw only one board with 103 as it is an obsolete family of micros. And on this board I have different connections than the "bluepil". Just change to the one you have. I just wanted to give you the working example - so I have tested it on my actual hardware \$\endgroup\$ Commented Aug 22, 2017 at 9:55
  • \$\begingroup\$ When I came home it suddenly worked (only change: I took the breadboard including stm32 by car) ... so maybe it was some electronic problem (loose wire). But I will study your example to see the difference between Hal and 'direct calls'. \$\endgroup\$ Commented Aug 22, 2017 at 19:55
  • \$\begingroup\$ I honestly advise the bare registers way. \$\endgroup\$ Commented Aug 22, 2017 at 19:57
  • 1
    \$\begingroup\$ No SPL is dead and unsupported for years. Just include CMSIS headers, download the Reference Manual and enjoy :) \$\endgroup\$ Commented Aug 22, 2017 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.