SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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