Jump to content
Jacob Christ

Low And High Priority Interrupts

Recommended Posts

Is there some special switch that is needed to use high and low priority interrupts in boost C?

 

I ask because of this quote from the 18F4520 data sheet (section 5.1.3):

 

"If both low and high-priority interrupts are enabled, the stack registers cannot be used reliably to return from low-priority interrupts. If a high-priority interrupt occurs while servicing a low-priority interrupt, the stack register values stored by the low-priority interrupt will be overwritten. In these cases, users must save the key registers in software during a low-priority interrupt."

 

My project works fine if all interrupts are high priority, but when all are switched to low priority except for Timer0 then on occasion the program hangs.

 

My code is set up like this:

 

---snip---

// Adjust interrupt priorities
intcon2.TMR0IP = 1;
intcon2.RBIP = 0;
intcon3.INT2IP = 0;
intcon3.INT1IP = 0;
// All other IP bits are set here...
ipr1 = 0;
ipr2 = 0;
rcon.IPEN = 1;

// Enable Peripheral Interrupt Bit
intcon.PEIE	= 1;
// Enable Global Interrupt Bit
intcon.GIE	= 1;

---snip---

void interrupt( void ) {
// Timer0
if ( intcon.TMR0IF ) {
	timer_interrupt();
}
}

void interrupt_low( void ) {

---snip---

 

Jacob

Share this post


Link to post
Share on other sites

Looking at the assembler code, only the high priority interrupt uses the fast stack to restore STATUS, W, BSR. The low priority saves these registers in ram.

 

Check the assembler code to see that registers that are used in your interrupt functions are saved and are not being corrupted (used) by the high priority interrupt function.

 

The interrupt handlers seem to save PRODH and PRODL for some reason.

 

I assume TMR0IF is being cleared.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites
The interrupt handlers seem to save PRODH and PRODL for some reason.

 

Reynard

 

The programmer should know which SFRs (s)he has used and should know which ones require preserving during an interrupt.

However the compilors use of PRODH and PRODL is hidden from the programmer, so these registers are preserved so that multiply operations can be performed in the main and both isr threads.

Share this post


Link to post
Share on other sites

Should the compiler save the FSR1 registers as well or can we assume the compiler libraries (called from an ISR) will never use this register(s).

 

Should the call tree know which "general" registers are used and save context as appropriate ?

Share this post


Link to post
Share on other sites
Should the compiler save the FSR1 registers as well or can we assume the compiler libraries (called from an ISR) will never use this register(s).

 

Should the call tree know which "general" registers are used and save context as appropriate ?

 

The compilor doesnt use FSR1, (none of my code has so far) not sure why. It would be neater to use a different FSR set for each thread, though this would add to the complications of calling functions from two threads.

 

You can prevent context saving with the linker switch -isrnocontext, Pic 18 only

Share this post


Link to post
Share on other sites
I assume TMR0IF is being cleared.

 

Cheers

 

Reynard

 

You assume correct, the program runs and hangs between several seconds to several minutes after reset and only when the high priority interrupt is running.

 

This morning, I left interrupt priorities on and made all interrupts low priority and every thing seems to be working okay as well.

 

The other thought I had is that I'm getting a spurious interrupt that's not being handled, but I don't know why it wouldn't hang in with all interrupts at the same priority level.

 

Jacob

Share this post


Link to post
Share on other sites

I have been caught out recently by not reading the errata sheets, which Microchip have plenty off.

 

Look at the Rev A1 sheet for the 18F4520 issue #23. It explains problems with fast interrupts which may give random operation.

 

Cheers

 

Reynard

 

I should have said before, try using the linker option -isrnoshadow, which uses RAM to save the important registers rather than restore from the shadow registers (fast) for high priority interrupts.

Dave and Pavel have been here before.

Edited by Reynard

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...