Mangue Baja Box

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Mon Jul 29 20:38:00 2019 +0000
Revision:
0:0dee8840a1c0
Mangue Baja Box

Who changed what in which revision?

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