LPC11U35 ADC Tick & USBSerial

Dependencies:   mbed

Dependents:   SmallDoseMeter_SingleCH_AE_lpc11u35_V1_00

Committer:
H_Tsunemoto
Date:
Mon Feb 19 09:09:52 2018 +0000
Revision:
1:b1a3be5f48ab
Parent:
0:871ab6846b18
test

Who changed what in which revision?

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