I have two interrupt service routines (ISR) which basically do the exact same thing but each handles an interrupt from a different device (although the same type of device). Therefore, the logic is the same but they access different CPU registers and memory locations.
As a simple example consider the following code:
extern volatile unsigned int dev1_rx_buffer; extern volatile unsigned int dev2_rx_buffer; volatile unsigned char data; void __attribute__((__interrupt__)) _dev1_interrupt(void) { /* Clear interrupt flag */ dev1.IF = 0; if (dev1.IS_FULL) { /* Read data from device */ data = dev1_rx_buffer; } else { /* do something else using registers of device 1 */ } /* More stuff using registers of device 1 */ } void __attribute__((__interrupt__)) _dev2_interrupt(void) { /* Clear interrupt flag */ dev2.IF = 0; if (dev2.IS_FULL) { /* Read data from device */ data = dev2_rx_buffer; } else { /* do something else using registers of device 2 */ } /* More stuff using registers of device 2 */ }
How can I avoid the code duplication with the restrictions that apply to ISRs (i.e. I cannot pass parameters to the ISRs and function calls should be avoided because of their overhead).
I have thought of writing a template from which to generate the two ISRs using a higher level scripting language, but I’d prefer a solution using only C or C preprocessor macros.
Why don’t you use an inline helper function which gets pointers to the device and the buffer?
I would check the generated assembly to make sure the compiler does what I expect, though.
You could also use a macro, but IMHO it is not good to do this for functions this long.