Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

External interputions EXTI don't work in nucleo STM32F103RB

lucas_mur

Newbie
Joined
May 15, 2023
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
34
Hello Everyone,

one of EXTI doesn't work. In fact, I have two EXTI interruptions from PB3 and PB4. whenever I push the button from PB3, the LED power_ON, and whenever I push the button from PB4, the LED power_OFF. The issue that I have, is EXTI3 work and EXTI4 doesn't.


First I activate AFIO clock :


Code:
RCC->APB2ENR |= (1 << 0); //activate AFIO


second I enable RCC for GPIOB :

C:
    RCC->APB2ENR &=~(1 << 3)); // clear RCC for GPIOB
    RCC->APB2ENR |= (1 << 3)); // Enable RCC for GPIOB
    GPIOB->CRL &= ~((1 << 10) | (1 << 9) | (1 << 11) | (1 << 8)); // Clear GPIO PB2 MODE  LED
    GPIOB->CRL &= ~((1 << 12) | (1 << 13) | (1 << 14) | (1 << 15)); // Clear GPIO PB3 MODE  Button1
    GPIOB->CRL &= ~((1 << 16) | (1 << 17) | (1 << 18) | (1 << 19)); // Clear GPIO PB4 MODE  Button2

    GPIOB->CRL |= 0x00000200; // Set GPIO PB2 (01 : output)
    GPIOC->CRL |= ((1 << 15) | (1<<19)); // Set GPIO PB3 and PB4 MODE (10 : input)

After that, I configure both EXTI3 and EXTI4 :

C:
    AFIO->EXTICR[0] &= ~AFIO_EXTICR2_EXTI3;
    AFIO->EXTICR[1] &= ~AFIO_EXTICR2_EXTI4;
    AFIO->EXTICR[0] |= AFIO_EXTICR2_EXTI3_PB;
    AFIO->EXTICR[1] |= AFIO_EXTICR2_EXTI4_PB;
    EXTI->RTSR |= EXTI_RTSR_TR3;
    EXTI->RTSR |= EXTI_RTSR_TR4;
    EXTI->FTSR &=~(EXTI_FTSR_FT3);
    EXTI->FTSR &=~(EXTI_FTSR_FT4);
    EXTI->IMR |= EXTI_IMR_MR3;
    EXTI->IMR |= EXTI_IMR_MR4;
    NVIC_SetPriority(EXTI3_IRQn, 0);
    NVIC_SetPriority(EXTI4_IRQn, 0);
    NVIC_EnableIRQ(EXTI3_IRQn);
    NVIC_EnableIRQ(EXTI4_IRQn);
    GPIOB->ODR &= ~(1<<2);  //Initialize LED with power_OFF state

and Finally I call the function headers for both EXTIs:

C:
void EXTI3_IRQHandler(void)
    {
     if (EXTI->PR & EXTI_PR_PR3)
       {
         GPIOB->ODR |= 1<<2;
         for(volatile int i=0; i<20000000; ++i){
            __asm("nop");
           }

        EXTI->PR |= EXTI_PR_PR3;
      }
     }

    void EXTI4_IRQHandler(void)
     {
      if (EXTI->PR & EXTI_PR_PR4)
        {
         GPIOB->ODR &= ~(1<<2);
         for(volatile int i=0; i<20000000; ++i){
            __asm("nop");
            }

        EXTI->PR |= EXTI_PR_PR4;
      }
     }

I changed the pins but I got the same issue, where do you think the issue came from ?
 
Hi,

for a forum question: Please tell us what IDE, what language, what compiler you use.

Interrupts:
Generally the ISRs should be fast (in execution time).You need to avoid busy waits like your "for loop".
ISRs are meant to save processing power, but your ISR wastes processing. power. No other job can be done. Bot interrupts have identical priority (not wrong), thus the one interrupt will not interrupt the other ISR.

I did not go deep into your code. Just my recommendation:

Debugging:
It´s rather easy to detect whether the ISRs are executed:
Set two GPIOs when entering the ISR, clear one of them when leaving the ISR.
Check the one GPIO and you will see if the ISR is being called at all.
The other GPIO tells you how often it is called and how long it takes to process.

Do you have a scope to check the GPIOs?

***
Code recommendations:
* Use "defines" so your code becomes more readable. (1 << 10) has no meaning for us. (1 << GPIOB3) tells much more.
* use meaningful text indents. If you do the opening bracket in the same column as the closing bracket you get a clear view of which lines belog together and you don´t miss a closing bracket.

example:
C:
void EXTI3_IRQHandler(void)
{
   if (EXTI->PR & EXTI_PR_PR3)
   {
      GPIOB->ODR |= 1<<2;
      for(volatile int i=0; i<20000000; ++i)
      {
         __asm("nop");
      }
      EXTI->PR |= EXTI_PR_PR3;
   }
}

void EXTI4_IRQHandler(void)
{
   if (EXTI->PR & EXTI_PR_PR4)
   {
      GPIOB->ODR &= ~(1<<2);
      for(volatile int i=0; i<20000000; ++i)
      {
         __asm("nop");
      }
      EXTI->PR |= EXTI_PR_PR4;
   }
}

Klaus
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top