MPLAB MCC Interrupt Setup - I don't get it. - Page 1
Forum section: electronics / general

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: May 14, 2023, 11:40:01 am
content: I'm trying to understand what is happening with the set of functions below that are setting up an interrupt for timer 5 in a PIC24. Why 5 functions? I get that it is setting up a pointer to a function that is, I assume, loaded into the interrupt vector table, but functions 1 and in particular 2. don't appear to do anything apart from establishing the existence of the two functions. I also assume that function 3. is where I insert my own code for interrupt processing, but function 4. also seems to indicate that my interrupt code goes there. What is "no_auto_PSV"? How do the "__attribute__"s work? Are five functions really required or is this just Microchip's way of creating transportable code. 6. is a few lines from the timer initialize function.Code:1. void (*TMR5_InterruptHandler)(void) = NULL;2. void TMR5_CallBack(void);3. void __attribute__ ( ( interrupt, no_auto_psv ) ) _T5Interrupt ( ){/* Check if the Timer Interrupt/Status is set *///***User Area Begin// ticker function call;// ticker is 1 -> Callback function gets called everytime this ISR executesif(TMR5_InterruptHandler){TMR5_InterruptHandler();}tmr5_obj.count++;tmr5_obj.timerElapsed = true;IFS1bits.T5IF = false;}4. void __attribute__ ((weak)) TMR5_CallBack(void){// Add your custom callback code here}5. void TMR5_SetInterruptHandler(void (* InterruptHandler)(void)){IEC1bits.T5IE = false;TMR5_InterruptHandler = InterruptHandler;IEC1bits.T5IE = true;}6. snippet from initialize routineif(TMR5_InterruptHandler == NULL){TMR5_SetInterruptHandler(&TMR5_CallBack);}

author: JPortici
author_rank: Super Contributor
timestamp: May 14, 2023, 12:27:56 pm
content: Or don't use MCC? 5 functions are what are needed by them to create as much abstraction as possbile. If you use it you should put your code in TMR5_CallBack (it is a weak function, you should reimplement it somewhere)Or you could ditch the code generator altogether, in which case you could just write the ISRCode:void __attribute__((interrupt, no_auto_psv)) _T5Interrupt(void)is the signature for the Timer 5 Interrupt routine, write your code there and remember to clear _T5IF before exitingthe two attributes, refer to the XC16 compiler manual. interrupt (obviously) denotes that it is an interrupt routine, the name is compared against the list of default ISR names and if it matches you don't need to specify the vector, the linker will place the ISR and all the required code automatically.no_auto_psv more or less denotes that the ISR doesn't have to mess with the PSV registers, unless you have a metric ton of constants (more than one PSV page worth of them) and you need to access them in the interrupt, you can safely use no_auto_psv, makes for shorter prologue/epilogue

author: bson
author_rank: Unknown
timestamp: May 19, 2023, 11:43:06 pm
content: As alluded to previously, it's quite likely there is a vector table that already is filled with symbols like "_T5Interrupt". A dummy implementation is then added so the vectors actually point somewhere, with the dummies having the "weak" attribute. This means if you define one it replaces the dummy as long as you use the same name - and its address will be in the vector table. Anyway, that's what this looks like.

author: JPortici
author_rank: Super Contributor
timestamp: May 20, 2023, 06:59:25 am
content: Quote from: bson on May 19, 2023, 11:43:06 pmActually, If the appropriate Linker option is selected, the IVT is filled with the address of the DefaultInterrupt handler, there is no actual need for this weak stubThe stub is there so that it can call the callback, and hide the interupt syntax to the user.The idea is that the same user code can be ported to another architecture supported by MCC that has a completely different interrupt syntaxThe stub is weak so that you can replace it completely if you prefer

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: May 21, 2023, 10:39:39 pm
content: Thanks for the replies. As I suspected, there is a much simpler way to go about this.