initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:638edba3adf6 1 #ifndef MBED_INTERRUPTMANAGER_H
yihui 0:638edba3adf6 2 #define MBED_INTERRUPTMANAGER_H
yihui 0:638edba3adf6 3
yihui 0:638edba3adf6 4 #include "cmsis.h"
yihui 0:638edba3adf6 5 #include "CallChain.h"
yihui 0:638edba3adf6 6 #include <string.h>
yihui 0:638edba3adf6 7
yihui 0:638edba3adf6 8 namespace mbed {
yihui 0:638edba3adf6 9
yihui 0:638edba3adf6 10 /** Use this singleton if you need to chain interrupt handlers.
yihui 0:638edba3adf6 11 *
yihui 0:638edba3adf6 12 * Example (for LPC1768):
yihui 0:638edba3adf6 13 * @code
yihui 0:638edba3adf6 14 * #include "InterruptManager.h"
yihui 0:638edba3adf6 15 * #include "mbed.h"
yihui 0:638edba3adf6 16 *
yihui 0:638edba3adf6 17 * Ticker flipper;
yihui 0:638edba3adf6 18 * DigitalOut led1(LED1);
yihui 0:638edba3adf6 19 * DigitalOut led2(LED2);
yihui 0:638edba3adf6 20 *
yihui 0:638edba3adf6 21 * void flip(void) {
yihui 0:638edba3adf6 22 * led1 = !led1;
yihui 0:638edba3adf6 23 * }
yihui 0:638edba3adf6 24 *
yihui 0:638edba3adf6 25 * void handler(void) {
yihui 0:638edba3adf6 26 * led2 = !led1;
yihui 0:638edba3adf6 27 * }
yihui 0:638edba3adf6 28 *
yihui 0:638edba3adf6 29 * int main() {
yihui 0:638edba3adf6 30 * led1 = led2 = 0;
yihui 0:638edba3adf6 31 * flipper.attach(&flip, 1.0);
yihui 0:638edba3adf6 32 * InterruptManager::get()->add_handler(handler, TIMER3_IRQn);
yihui 0:638edba3adf6 33 * }
yihui 0:638edba3adf6 34 * @endcode
yihui 0:638edba3adf6 35 */
yihui 0:638edba3adf6 36 class InterruptManager {
yihui 0:638edba3adf6 37 public:
yihui 0:638edba3adf6 38 /** Return the only instance of this class
yihui 0:638edba3adf6 39 */
yihui 0:638edba3adf6 40 static InterruptManager* get();
yihui 0:638edba3adf6 41
yihui 0:638edba3adf6 42 /** Destroy the current instance of the interrupt manager
yihui 0:638edba3adf6 43 */
yihui 0:638edba3adf6 44 static void destroy();
yihui 0:638edba3adf6 45
yihui 0:638edba3adf6 46 /** Add a handler for an interrupt at the end of the handler list
yihui 0:638edba3adf6 47 *
yihui 0:638edba3adf6 48 * @param function the handler to add
yihui 0:638edba3adf6 49 * @param irq interrupt number
yihui 0:638edba3adf6 50 *
yihui 0:638edba3adf6 51 * @returns
yihui 0:638edba3adf6 52 * The function object created for 'function'
yihui 0:638edba3adf6 53 */
yihui 0:638edba3adf6 54 pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) {
yihui 0:638edba3adf6 55 return add_common(function, irq);
yihui 0:638edba3adf6 56 }
yihui 0:638edba3adf6 57
yihui 0:638edba3adf6 58 /** Add a handler for an interrupt at the beginning of the handler list
yihui 0:638edba3adf6 59 *
yihui 0:638edba3adf6 60 * @param function the handler to add
yihui 0:638edba3adf6 61 * @param irq interrupt number
yihui 0:638edba3adf6 62 *
yihui 0:638edba3adf6 63 * @returns
yihui 0:638edba3adf6 64 * The function object created for 'function'
yihui 0:638edba3adf6 65 */
yihui 0:638edba3adf6 66 pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) {
yihui 0:638edba3adf6 67 return add_common(function, irq, true);
yihui 0:638edba3adf6 68 }
yihui 0:638edba3adf6 69
yihui 0:638edba3adf6 70 /** Add a handler for an interrupt at the end of the handler list
yihui 0:638edba3adf6 71 *
yihui 0:638edba3adf6 72 * @param tptr pointer to the object that has the handler function
yihui 0:638edba3adf6 73 * @param mptr pointer to the actual handler function
yihui 0:638edba3adf6 74 * @param irq interrupt number
yihui 0:638edba3adf6 75 *
yihui 0:638edba3adf6 76 * @returns
yihui 0:638edba3adf6 77 * The function object created for 'tptr' and 'mptr'
yihui 0:638edba3adf6 78 */
yihui 0:638edba3adf6 79 template<typename T>
yihui 0:638edba3adf6 80 pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
yihui 0:638edba3adf6 81 return add_common(tptr, mptr, irq);
yihui 0:638edba3adf6 82 }
yihui 0:638edba3adf6 83
yihui 0:638edba3adf6 84 /** Add a handler for an interrupt at the beginning of the handler list
yihui 0:638edba3adf6 85 *
yihui 0:638edba3adf6 86 * @param tptr pointer to the object that has the handler function
yihui 0:638edba3adf6 87 * @param mptr pointer to the actual handler function
yihui 0:638edba3adf6 88 * @param irq interrupt number
yihui 0:638edba3adf6 89 *
yihui 0:638edba3adf6 90 * @returns
yihui 0:638edba3adf6 91 * The function object created for 'tptr' and 'mptr'
yihui 0:638edba3adf6 92 */
yihui 0:638edba3adf6 93 template<typename T>
yihui 0:638edba3adf6 94 pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
yihui 0:638edba3adf6 95 return add_common(tptr, mptr, irq, true);
yihui 0:638edba3adf6 96 }
yihui 0:638edba3adf6 97
yihui 0:638edba3adf6 98 /** Remove a handler from an interrupt
yihui 0:638edba3adf6 99 *
yihui 0:638edba3adf6 100 * @param handler the function object for the handler to remove
yihui 0:638edba3adf6 101 * @param irq the interrupt number
yihui 0:638edba3adf6 102 *
yihui 0:638edba3adf6 103 * @returns
yihui 0:638edba3adf6 104 * true if the handler was found and removed, false otherwise
yihui 0:638edba3adf6 105 */
yihui 0:638edba3adf6 106 bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
yihui 0:638edba3adf6 107
yihui 0:638edba3adf6 108 private:
yihui 0:638edba3adf6 109 InterruptManager();
yihui 0:638edba3adf6 110 ~InterruptManager();
yihui 0:638edba3adf6 111
yihui 0:638edba3adf6 112 // We declare the copy contructor and the assignment operator, but we don't
yihui 0:638edba3adf6 113 // implement them. This way, if someone tries to copy/assign our instance,
yihui 0:638edba3adf6 114 // he will get an error at compile time.
yihui 0:638edba3adf6 115 InterruptManager(const InterruptManager&);
yihui 0:638edba3adf6 116 InterruptManager& operator =(const InterruptManager&);
yihui 0:638edba3adf6 117
yihui 0:638edba3adf6 118 template<typename T>
yihui 0:638edba3adf6 119 pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) {
yihui 0:638edba3adf6 120 int irq_pos = get_irq_index(irq);
yihui 0:638edba3adf6 121 bool change = must_replace_vector(irq);
yihui 0:638edba3adf6 122
yihui 0:638edba3adf6 123 pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr);
yihui 0:638edba3adf6 124 if (change)
yihui 0:638edba3adf6 125 NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
yihui 0:638edba3adf6 126 return pf;
yihui 0:638edba3adf6 127 }
yihui 0:638edba3adf6 128
yihui 0:638edba3adf6 129 pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false);
yihui 0:638edba3adf6 130 bool must_replace_vector(IRQn_Type irq);
yihui 0:638edba3adf6 131 int get_irq_index(IRQn_Type irq);
yihui 0:638edba3adf6 132 void irq_helper();
yihui 0:638edba3adf6 133 void add_helper(void (*function)(void), IRQn_Type irq, bool front=false);
yihui 0:638edba3adf6 134 static void static_irq_helper();
yihui 0:638edba3adf6 135
yihui 0:638edba3adf6 136 CallChain* _chains[NVIC_NUM_VECTORS];
yihui 0:638edba3adf6 137 static InterruptManager* _instance;
yihui 0:638edba3adf6 138 };
yihui 0:638edba3adf6 139
yihui 0:638edba3adf6 140 } // namespace mbed
yihui 0:638edba3adf6 141
yihui 0:638edba3adf6 142 #endif
yihui 0:638edba3adf6 143