Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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