mbed

Dependents:   DHTSensor_Test K64F_eCompass_OneNET_JW

Committer:
mbotkinl
Date:
Wed Feb 25 20:22:22 2015 +0000
Revision:
0:2cc6bb4d7fea
Working code to read Temperature and Humidity readings

Who changed what in which revision?

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