.

Dependents:   RTC

Committer:
jhon309
Date:
Thu Aug 13 00:20:09 2015 +0000
Revision:
0:88e313c910d0
RTC Example

Who changed what in which revision?

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