CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Calling disable_interrupts() within an interrupt

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

Calling disable_interrupts() within an interrupt
PostPosted: Tue May 15, 2012 8:29 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website

Re: Calling disable_interrupts() within an interrupt
PostPosted: Tue May 15, 2012 9:04 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue May 15, 2012 9:26 am     Reply with quote

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: 1331

View user's profile Send private message

PostPosted: Tue May 15, 2012 9:53 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue May 15, 2012 10:21 am     Reply with quote

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: 19401

View user's profile Send private message

PostPosted: Tue May 15, 2012 11:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 16, 2012 2:08 am     Reply with quote

Hey Ttelmah,

Thanks for the response, it's good to understand what's happening there.

Ed
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group