Final version.

Dependencies:   F746_GUI F746_SAI_IO UIT_FFT_Real

Fork of F746_Spectrogram by 不韋 呂

Committer:
mladjo1993
Date:
Wed Aug 30 07:47:58 2017 +0000
Revision:
9:444e58089d09
Parent:
4:c1beacfc42c7
Final version.

Who changed what in which revision?

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