Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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