View previous topic :: View next topic |
Author |
Message |
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
Calling disable_interrupts() within an interrupt |
Posted: Tue May 15, 2012 8:29 am |
|
|
Hi all,
I hope all is going smoothly. I was looking at a piece of my library code and realised it was making a call to disable_interrupts inside the interrupt, for some reasons I have got in my head that this is a bad idea although looking at the CCS manual I can't see it is forbidden.
Looking at the PIC18F87K22 manual I can see that I should not use a MOVFF to modify any interrupt register while it is enabled but I can see in the list the compiler uses BCF:
Code: |
.................... if(rda2_opread_pos == rda2_opwrite_pos) disable_interrupts(INT_TBE2); // If buffer is empty disable the interrupts
003DE: MOVF x59,W
003E0: SUBWF x58,W
003E2: BTFSC FD8.2
003E4: BCF FA3.4
.................... }
|
This seems like it should be fine to me, any comments?
Cheers
Ed |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
Re: Calling disable_interrupts() within an interrupt |
Posted: Tue May 15, 2012 9:04 am |
|
|
EdWaugh wrote: | Hi all,
I hope all is going smoothly. I was looking at a piece of my library code and realised it was making a call to disable_interrupts inside the interrupt, for some reasons I have got in my head that this is a bad idea although looking at the CCS manual I can't see it is forbidden.
Looking at the PIC18F87K22 manual I can see that I should not use a MOVFF to modify any interrupt register while it is enabled but I can see in the list the compiler uses BCF:
Code: |
.................... if(rda2_opread_pos == rda2_opwrite_pos) disable_interrupts(INT_TBE2); // If buffer is empty disable the interrupts
003DE: MOVF x59,W
003E0: SUBWF x58,W
003E2: BTFSC FD8.2
003E4: BCF FA3.4
.................... }
|
This seems like it should be fine to me, any comments?
Cheers
Ed |
Disabling interrupts inside an interrupt handler is a standard programming technique. For example, a serial transmit handler responsible for sending the contents of a buffer to the serial port would disable the TX interrupt once the last character from the buffer has been transmitted. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
|
Posted: Tue May 15, 2012 9:26 am |
|
|
Thanks Andrew, that's just what this code does. I'm not sure why I decided it might be a problem but I'm sure I read it somewhere.
Cheers
Ed |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1346
|
|
Posted: Tue May 15, 2012 9:53 am |
|
|
I think the gotcha is when you disable GLOBAL interrupts in an ISR. I don't think you are supposed to do that. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue May 15, 2012 10:21 am |
|
|
Quote: |
I'm not sure why I decided it might be a problem but I'm sure I read it somewhere.
|
Like asmallri says, it's standard practice. However CCS and the PIC handle this MOST of the time, there are exceptions.
This is taken from PIC18F458 manual, I'm certain others are similar.
Quote: |
When an interrupt is responded to, the Global Interrupt
Enable bit is cleared to disable further interrupts. If the
IPEN bit is cleared, this is the GIE bit. If interrupt priority
levels are used, this will be either the GIEH or GIEL bit.
High priority interrupt sources can interrupt a low
priority interrupt.
|
Most of the time the CCS compilers also clear interrupt flags, so you don't have to. If you disable global interrupts, it can become your responsibility to enable them again if you over-ride either the PIC or the compiler.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue May 15, 2012 11:08 am |
|
|
The key one, is you must never _enable_ the global interrupt inside a handler. This one is an absolute 'no-no' on the PIC. The chip has a special instruction 'RETFIE', which performs a return, and enables the global interrupt on the first cycle of the _next_ instruction, specifically for use by interrupt handlers, since if you enable the global interrupt inside the handler. you immediately risk recursion, since the code is still inside the handler for the next instruction, even if this is the actual return....
Best Wishes |
|
|
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
|
Posted: Wed May 16, 2012 2:08 am |
|
|
Hey Ttelmah,
Thanks for the response, it's good to understand what's happening there.
Ed |
|
|
|