Interrupt question.
my code (incomplete, hitting datasheet a chapter at a time, now 'stuck' in usb)
https://github.com/cv007/PIC32MM_Curiosity_CPPhttps://github.com/cv007/PIC32MM_Curiosity_CPP/blob/master/Usb.hpp - line 320
(no defines or mchp headers, except I broke down to add defines/macros in Isr.hpp for easier ISR creation)
The PIC32MM has interrupt levels and many irq sources, so is it best to just run through an interrupt once, or check it until no more (specific) irq flag, letting any other higher level irq interrupt as needed, or does it make no difference.
original way, with just returning when done-
void USB_ISR(){
Usb u; Irq ir;
static Usb::state_t last_state; //keep track of usb state for resumes
uint8_t flags = u.flags(); //get all irq flags
ir.flag_clr(ir.USB); //clear usb irq flag before early returns
//ATTACHED->POWERED if vbus_pin high
if(u.state == u.ATTACHED){
if(vbus_pin.ison()) u.state = u.POWERED;
else { //no power (not sure how we would get irq with no vbus)
u.eflags_clr(u.ALLEFLAGS);
u.flags_clr(u.ERROR);
return;
}
}
//more code
}
if USB flag was set again while processing, we go through isr register restore, then backup registers again since we will be right back here
OR, something like this, where it loops until usb flag no longer set (replacing early returns with continue)-
(I also changed flag getting/clearing so only needs to be done in one spot, one time- which I should do anyway)
void USB_ISR(){
Usb u; Irq ir;
static Usb::state_t last_state; //keep track of usb state for resumes
for(; ir.flag(ir.USB); ir.flag_clr(ir.USB)){ //added loop
uint8_t flags = u.flags(); //get all irq flags
uint8_t eflags = u.eflags(); //get all irq error flags
u.flags_clr(flags); //clear only what we got (1=clear)
u.eflags_clr(flags); //clear only what we got (1=clear)
//ATTACHED->POWERED if vbus_pin high
if(u.state == u.ATTACHED){
if(vbus_pin.ison()) u.state = u.POWERED;
else continue; //no power- how we get here with no vbus?
}
...
} //for loop
}
Maybe the loop and flag check make it not worth it? or maybe its a good idea as maybe usb gets multiple consecutive irq's since the stat fifo is 4 long? The usb isr does seem to require a fair amount of code to run so most likely takes longer than normal isr's.
For a other 8bit pic's I use (only 1 isr, which is processed differently of course), when checking the rx flag I usually use a loop that to get the rx buffer flushed right away.