FRDM-K64F, Avnet M14A2A, Grove Shield, to create smart home system. In use with AT&Ts M2x & Flow.

Dependencies:   mbed FXOS8700CQ MODSERIAL

Committer:
jwhammel
Date:
Mon Apr 29 04:24:38 2019 +0000
Revision:
85:0cf65ceb4492
Parent:
84:fc8c9b39723a
We have added an alarm trigger that gets sent to AT&T Flow.

Who changed what in which revision?

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