version_2.0
Dependents: cc3000_ping_demo_try_2
Fork of mbed by
InterruptManager.h
00001 #ifndef MBED_INTERRUPTMANAGER_H 00002 #define MBED_INTERRUPTMANAGER_H 00003 00004 #include "cmsis.h" 00005 #include "CallChain.h" 00006 #include <string.h> 00007 00008 namespace mbed { 00009 00010 /** Use this singleton if you need to chain interrupt handlers. 00011 * 00012 * Example (for LPC1768): 00013 * @code 00014 * #include "InterruptManager.h" 00015 * #include "mbed.h" 00016 * 00017 * Ticker flipper; 00018 * DigitalOut led1(LED1); 00019 * DigitalOut led2(LED2); 00020 * 00021 * void flip(void) { 00022 * led1 = !led1; 00023 * } 00024 * 00025 * void handler(void) { 00026 * led2 = !led1; 00027 * } 00028 * 00029 * int main() { 00030 * led1 = led2 = 0; 00031 * flipper.attach(&flip, 1.0); 00032 * InterruptManager::get()->add_handler(handler, TIMER3_IRQn); 00033 * } 00034 * @endcode 00035 */ 00036 class InterruptManager { 00037 public: 00038 /** Return the only instance of this class 00039 */ 00040 static InterruptManager* get(); 00041 00042 /** Destroy the current instance of the interrupt manager 00043 */ 00044 static void destroy(); 00045 00046 /** Add a handler for an interrupt at the end of the handler list 00047 * 00048 * @param function the handler to add 00049 * @param irq interrupt number 00050 * 00051 * @returns 00052 * The function object created for 'function' 00053 */ 00054 pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) { 00055 return add_common(function, irq); 00056 } 00057 00058 /** Add a handler for an interrupt at the beginning of the handler list 00059 * 00060 * @param function the handler to add 00061 * @param irq interrupt number 00062 * 00063 * @returns 00064 * The function object created for 'function' 00065 */ 00066 pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) { 00067 return add_common(function, irq, true); 00068 } 00069 00070 /** Add a handler for an interrupt at the end of the handler list 00071 * 00072 * @param tptr pointer to the object that has the handler function 00073 * @param mptr pointer to the actual handler function 00074 * @param irq interrupt number 00075 * 00076 * @returns 00077 * The function object created for 'tptr' and 'mptr' 00078 */ 00079 template<typename T> 00080 pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { 00081 return add_common(tptr, mptr, irq); 00082 } 00083 00084 /** Add a handler for an interrupt at the beginning of the handler list 00085 * 00086 * @param tptr pointer to the object that has the handler function 00087 * @param mptr pointer to the actual handler function 00088 * @param irq interrupt number 00089 * 00090 * @returns 00091 * The function object created for 'tptr' and 'mptr' 00092 */ 00093 template<typename T> 00094 pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { 00095 return add_common(tptr, mptr, irq, true); 00096 } 00097 00098 /** Remove a handler from an interrupt 00099 * 00100 * @param handler the function object for the handler to remove 00101 * @param irq the interrupt number 00102 * 00103 * @returns 00104 * true if the handler was found and removed, false otherwise 00105 */ 00106 bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq); 00107 00108 private: 00109 InterruptManager(); 00110 ~InterruptManager(); 00111 00112 // We declare the copy contructor and the assignment operator, but we don't 00113 // implement them. This way, if someone tries to copy/assign our instance, 00114 // he will get an error at compile time. 00115 InterruptManager(const InterruptManager&); 00116 InterruptManager& operator =(const InterruptManager&); 00117 00118 template<typename T> 00119 pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) { 00120 int irq_pos = get_irq_index(irq); 00121 bool change = must_replace_vector(irq); 00122 00123 pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr); 00124 if (change) 00125 NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper); 00126 return pf; 00127 } 00128 00129 pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false); 00130 bool must_replace_vector(IRQn_Type irq); 00131 int get_irq_index(IRQn_Type irq); 00132 void irq_helper(); 00133 void add_helper(void (*function)(void), IRQn_Type irq, bool front=false); 00134 static void static_irq_helper(); 00135 00136 CallChain* _chains[NVIC_NUM_VECTORS]; 00137 static InterruptManager* _instance; 00138 }; 00139 00140 } // namespace mbed 00141 00142 #endif 00143
Generated on Thu Jul 14 2022 10:44:06 by 1.7.2