InterruptMask library allows masking/unmasking GPIO interrupts (attached to either the rise or falling edges). It also allows clearing pending interrupts which may come handy before unmasking them.
InterruptMask.h@4:a34eddd0a2be, 2014-02-19 (annotated)
- Committer:
- nleoni
- Date:
- Wed Feb 19 04:35:24 2014 +0000
- Revision:
- 4:a34eddd0a2be
- Parent:
- 3:7b8a744ac27a
Updated comments in main CPP file
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nleoni | 4:a34eddd0a2be | 1 | //***************************************************************************// |
nleoni | 2:0e3b2bc65131 | 2 | /* InterruptMask Library */ |
nleoni | 4:a34eddd0a2be | 3 | /* Napoleon Leoni, February 18,2014*/ |
nleoni | 0:9db76e6c71ce | 4 | /* This class enables masking a GPIO interrupt from a specific MBED pin*/ |
nleoni | 0:9db76e6c71ce | 5 | // Note to get the actual pin number in the LPC of an mbed pin in P0 |
nleoni | 0:9db76e6c71ce | 6 | // simply do pN-P0_0, for example p12-P0_0=17, that is pin 12 in the mbed |
nleoni | 0:9db76e6c71ce | 7 | // corresponds to Port[0].17 or bit 17 of port 0. |
nleoni | 2:0e3b2bc65131 | 8 | //***************************************************************************// |
nleoni | 4:a34eddd0a2be | 9 | //* Example usage */ |
nleoni | 4:a34eddd0a2be | 10 | //* >#include "InterruptMask.h" |
nleoni | 4:a34eddd0a2be | 11 | //* >InterruptIn upPressed(p15); |
nleoni | 4:a34eddd0a2be | 12 | //* >InterruptMask maskUpButtonInt(p15); //Interrupt mask object allows easy access to |
nleoni | 4:a34eddd0a2be | 13 | //* > //mask/unmask interrupts from a GPIO source |
nleoni | 4:a34eddd0a2be | 14 | //* >void ISRup(void){ |
nleoni | 4:a34eddd0a2be | 15 | //*> myled1=!myled1; |
nleoni | 4:a34eddd0a2be | 16 | //*>} |
nleoni | 4:a34eddd0a2be | 17 | //*>int main() { |
nleoni | 4:a34eddd0a2be | 18 | //*> upPressed.rise(&ISRup); |
nleoni | 4:a34eddd0a2be | 19 | //*> //downPressed.rise(&ISRdown); |
nleoni | 4:a34eddd0a2be | 20 | //*> bool disableInterrupt=false; |
nleoni | 4:a34eddd0a2be | 21 | //*> int counter=0,tdelay=10; |
nleoni | 4:a34eddd0a2be | 22 | //*> |
nleoni | 4:a34eddd0a2be | 23 | //*> while (1) { |
nleoni | 4:a34eddd0a2be | 24 | //*> //This section of code periodically (every 10 seconds) masks the specified GPIO interrupt |
nleoni | 4:a34eddd0a2be | 25 | //*> //showing how to both mask it and unmask it as well as how to clear pending interrupts |
nleoni | 4:a34eddd0a2be | 26 | //*> counter++; |
nleoni | 4:a34eddd0a2be | 27 | //*> if(counter>=tdelay){ |
nleoni | 4:a34eddd0a2be | 28 | //*> disableInterrupt=!disableInterrupt; |
nleoni | 4:a34eddd0a2be | 29 | //*> if(disableInterrupt) maskUpButtonInt.maskIntR() ; //mask interrupt |
nleoni | 4:a34eddd0a2be | 30 | //*> if(!disableInterrupt){ |
nleoni | 4:a34eddd0a2be | 31 | //*> maskUpButtonInt.ClrInt(); //clear any pending interrupt before unmasking |
nleoni | 4:a34eddd0a2be | 32 | //*> maskUpButtonInt.unMaskIntR(); //unmask interrupt |
nleoni | 4:a34eddd0a2be | 33 | //*> } |
nleoni | 4:a34eddd0a2be | 34 | //*> counter=0; |
nleoni | 4:a34eddd0a2be | 35 | //*> } |
nleoni | 4:a34eddd0a2be | 36 | //*> } |
nleoni | 4:a34eddd0a2be | 37 | //*>} |
nleoni | 4:a34eddd0a2be | 38 | |
nleoni | 2:0e3b2bc65131 | 39 | // |
nleoni | 2:0e3b2bc65131 | 40 | // TESTS RUN // |
nleoni | 2:0e3b2bc65131 | 41 | // I print to the LCD three important LPC1768 registers: |
nleoni | 2:0e3b2bc65131 | 42 | // ISER0..Interrupt set Enable register(address 0xE000E100), |
nleoni | 2:0e3b2bc65131 | 43 | // contents of ISER0 are accesible via: *(NVIC->ISER) |
nleoni | 2:0e3b2bc65131 | 44 | // |
nleoni | 2:0e3b2bc65131 | 45 | // IntEnR..GPIO interrupt enable on rising, shows which pins are attached to EINT3 |
nleoni | 2:0e3b2bc65131 | 46 | // on their rising edge (these are only for mbed pins mapped to LPC pins on Port 0) |
nleoni | 2:0e3b2bc65131 | 47 | // its contents are available using th macro LPC_GPIOINT->IO0IntEnR |
nleoni | 2:0e3b2bc65131 | 48 | // IntEnF..GPIO interrupt enable on falling, shows which pins are attached to EINT3 |
nleoni | 2:0e3b2bc65131 | 49 | // on their falling edge (these are only for mbed pins mapped to LPC pins on Port 0) |
nleoni | 2:0e3b2bc65131 | 50 | // its contents are available using th macro LPC_GPIOINT->IO0IntEnF |
nleoni | 2:0e3b2bc65131 | 51 | // |
nleoni | 2:0e3b2bc65131 | 52 | // The test program periodically masks/unmasks (every 10 seconds) a specified interrupt |
nleoni | 2:0e3b2bc65131 | 53 | // from a GPIO pin, |
nleoni | 2:0e3b2bc65131 | 54 | // |
nleoni | 2:0e3b2bc65131 | 55 | //*********************************************************************************** |
nleoni | 2:0e3b2bc65131 | 56 | // TEST DESCRIPTION Resulting Register Values ANALYSYS |
nleoni | 2:0e3b2bc65131 | 57 | // 1 ISR attached to rise ISER0 0x0020 0010 Works as expected when p15 interrupt on rise edge |
nleoni | 2:0e3b2bc65131 | 58 | // edge of p15(toggle LED1) IO0IntEnR 0x0082 0000 (when p15 unmasked) is masked the proper bit appears masked in IO0IntEnR |
nleoni | 2:0e3b2bc65131 | 59 | // and p12(toggle LED2) IO0IntEnR 0x0002 0000 (when p15 masked) and the up action of the joystick does not toggle the LED |
nleoni | 2:0e3b2bc65131 | 60 | // while the down motion continues to work. Once the interrupt |
nleoni | 2:0e3b2bc65131 | 61 | // from p15 is unmasked the up joysstick action works again. |
nleoni | 2:0e3b2bc65131 | 62 | // TEST DESCRIPTION STATUS |
nleoni | 2:0e3b2bc65131 | 63 | // 2 check mask functionality for all PASS |
nleoni | 2:0e3b2bc65131 | 64 | // GPIO pins attached to joystick , both |
nleoni | 2:0e3b2bc65131 | 65 | // for rise and fall attached interrupts |
nleoni | 2:0e3b2bc65131 | 66 | // |
nleoni | 2:0e3b2bc65131 | 67 | // 3 When interrupt is cleared (ClrInt() ) PASS |
nleoni | 2:0e3b2bc65131 | 68 | // before unmasking there should be no pending |
nleoni | 2:0e3b2bc65131 | 69 | // interrupts |
nleoni | 2:0e3b2bc65131 | 70 | // |
nleoni | 2:0e3b2bc65131 | 71 | //*********************************************************************************** |
nleoni | 2:0e3b2bc65131 | 72 | |
nleoni | 0:9db76e6c71ce | 73 | #include "mbed.h" |
nleoni | 0:9db76e6c71ce | 74 | |
nleoni | 1:f2ea37f17679 | 75 | #ifndef INTERRUPTMASK |
nleoni | 1:f2ea37f17679 | 76 | #define INTERRUPTMASK |
nleoni | 1:f2ea37f17679 | 77 | |
nleoni | 0:9db76e6c71ce | 78 | class InterruptMask{ |
nleoni | 0:9db76e6c71ce | 79 | |
nleoni | 0:9db76e6c71ce | 80 | public: |
nleoni | 0:9db76e6c71ce | 81 | |
nleoni | 0:9db76e6c71ce | 82 | private: |
nleoni | 0:9db76e6c71ce | 83 | int mPin; |
nleoni | 0:9db76e6c71ce | 84 | int mask; |
nleoni | 0:9db76e6c71ce | 85 | int unmask; |
nleoni | 0:9db76e6c71ce | 86 | volatile unsigned int* IOIntEnF; // pointer to register with fall enable interrupt for |
nleoni | 0:9db76e6c71ce | 87 | // appropriate LPC port depending on the GPIO pin |
nleoni | 0:9db76e6c71ce | 88 | volatile unsigned int* IOIntEnR;// pointer to register with rise enable interrupt for |
nleoni | 0:9db76e6c71ce | 89 | // appropriate LPC port depending on the GPIO pin |
nleoni | 0:9db76e6c71ce | 90 | volatile unsigned int* IOIntClr; //pointer to register to clear interrupt for |
nleoni | 0:9db76e6c71ce | 91 | // appropriate LPC port depending on the GPIO pin |
nleoni | 0:9db76e6c71ce | 92 | public: |
nleoni | 2:0e3b2bc65131 | 93 | //default constructor required, should not be use will yield error |
nleoni | 0:9db76e6c71ce | 94 | InterruptMask(); |
nleoni | 2:0e3b2bc65131 | 95 | //Constructor to be used, initialize with any valid mbed pin (p5 thru p30 except pins 19-20) |
nleoni | 2:0e3b2bc65131 | 96 | //you will need one InterruptMask for every pin you want ot have control over. |
nleoni | 3:7b8a744ac27a | 97 | InterruptMask(PinName GPIOpin); |
nleoni | 0:9db76e6c71ce | 98 | |
nleoni | 2:0e3b2bc65131 | 99 | //This method will mask (disable) any rising edge interrupts of the specified pin |
nleoni | 0:9db76e6c71ce | 100 | void maskIntR(void); |
nleoni | 0:9db76e6c71ce | 101 | |
nleoni | 2:0e3b2bc65131 | 102 | //This method will mask (disable) any faling edge interrupts of the specified pin |
nleoni | 0:9db76e6c71ce | 103 | void maskIntF(void); |
nleoni | 0:9db76e6c71ce | 104 | |
nleoni | 2:0e3b2bc65131 | 105 | //This method will unmask (enable) any rising edge interrupts of the specified pin |
nleoni | 0:9db76e6c71ce | 106 | void unMaskIntR(void); |
nleoni | 0:9db76e6c71ce | 107 | |
nleoni | 2:0e3b2bc65131 | 108 | //This method will unmask (enable) any falling edge interrupts of the specified pin |
nleoni | 0:9db76e6c71ce | 109 | void unMaskIntF(void); |
nleoni | 0:9db76e6c71ce | 110 | |
nleoni | 2:0e3b2bc65131 | 111 | //This method will clear any pending interrupts of the specified pin |
nleoni | 0:9db76e6c71ce | 112 | void ClrInt(void); |
nleoni | 0:9db76e6c71ce | 113 | |
nleoni | 1:f2ea37f17679 | 114 | }; |
nleoni | 1:f2ea37f17679 | 115 | |
nleoni | 1:f2ea37f17679 | 116 | #endif |