fft

Dependencies:   BSP_DISCO_F746NG SDFileSystem_Warning_Fixed

Committer:
MikamiUitOpen
Date:
Sun Oct 09 10:11:14 2016 +0000
Revision:
0:3e46577dc273
1

Who changed what in which revision?

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