6 years, 8 months ago.

STM32 NUCLEO F767ZI Interrupt

Hello, I am working on interrupts. Whenever my interrupt is called by pressing the push button, it works fine. But after the ISR function is executed completely, the command never returns to the main function. How could I resolve this? Following is the code that I am using.

/media/uploads/rishabh0820/capture.png

Rather than publishing a picture (png, bmp ...) you'd better paste your code here and enclose it with <<code>> and <</code>> marks (each on it's separate line). See the "Editing tips" below for more details. People then can copy and paste it into the online/offline compiler and help you with less effort.

posted by Zoltan Hudak 01 Mar 2018

2 Answers

6 years, 8 months ago.

Hi Rishabh,

After testing your code I concluded that, once the ISR function is executed completely, the command always returns to the main function. Then, while executing the pc.scanf("%c", &led) command at line #21, it is waiting for arrival of serial data . You can verify it by sending a 'Y' or 'y' character from the PC's serial monitor.

NOTES:

  • It isn't a good strategy to call a wait function in ISR. However, you are allowed to do so and it will work.
  • It's sufficient to attach the ISR function to the Interrupt fall event only once. So you can move it before the while loop.

...
    interrupt_button.fall(&led_interrupt);    
    while(1)
    {
        ...
    }
....

Accepted Answer

Hi Zoltan, Thank you for your reply. I tried testing the code after removing the wait(5) and attaching the ISR above the while(). But it still didn't make any difference. 1. Whenever the interrupt button is pressed, the serial print doesn't display pc.printf("\n\r LED ON/OFF: "); statement. But when y character is pressed, the LED1 continues to glow forever and the command never returns to the main function. 2. When the interrupt button is pressed, while the basic led function is executing, the serial print displays the pc.printf statement and it works normally. But, this is not how I want my device to work. Can you please suggest any solution?

posted by Rishabh Gupta 03 Mar 2018

Hi Rishabh,

  • On a start the program executes the main function in which it attaches an ISR for handling the 'button pressed ' events.
  • Then it enters the while loop. There it prints a "LED ON/OFF" message to the serial port. You should be able to see it on the serial monitor of your PC.
  • Next it executes the pc.scanf("%c", &led) function. This has a built-in loop. So the program is looping here and waiting for a character to arrive over the serial line. If you now press the button then this loop is interrupted and the attached ISR is executed. That sends a serial message "Blinking LED in interrupt mode" over the serial line to the connected PC and blinks led2 (if you keep the wait function in place). Once this is finished, the program returns back to the point at which the interrupt occured. So it continues in execution of the built-in loop of the pc.scanf("%c", &led) function and waits for a character to arrive over the serial line. (There is no pc.printf("LED ON/OFF: " ); statement in this built-in loop.). You can press the button again with the same result as described above.
  • However, if you send a character over the serial line then it is received by the pc.scanf("%c", &led) funtion. That will result in finishing the execution of it's built-in loop and the program proceeds to execute the next statement in the main function. If the received character was a 'Y' or 'y' then the program sends a "Blinking LED in normal mode" message over the serial line and blinks the led1. Otherwise, no messge is sent and led1 dosn't blink.
  • In next step the program starts to loop in frame of the while block. So it prints a "LED ON/OFF" message to the serial port and executes the pc.scanf("%c", &led) function. Here it starts to loop and wait for a character to arrive over the serial line. If you press the button then this loop will be interrupted and the attached ISR is going to be executed ...
posted by Zoltan Hudak 04 Mar 2018
6 years, 8 months ago.

Hello Rishab,

After going through the code I could see that there is wait(5); in the interrupt routine. Never use wait in the interrupts. That was the reason why it does not return from ISR routine. Kindly go through with the below link. remove the wait in ISR and it would work.

https://os.mbed.com/handbook/InterruptIn