mbed library sources

Fork of mbed-src by mbed official

Committer:
rllamado
Date:
Sun Feb 21 07:27:08 2016 +0000
Revision:
636:21ce76d41a4f
Parent:
221:8276e3a4886f
something

Who changed what in which revision?

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