mbed library sources. Supersedes mbed-src.

Fork of mbed by teralytic

Committer:
rodriguise
Date:
Mon Oct 17 18:47:01 2016 +0000
Revision:
148:4802eb17e82b
Parent:
144:ef7eb2e8f9f7
backup

Who changed what in which revision?

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