test

Dependents:   robotic_fish_7

Committer:
juansal12
Date:
Tue Jan 14 19:14:29 2020 +0000
Revision:
0:44931ff4a3ed
Sofi 7 code;

Who changed what in which revision?

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