DHT11

Committer:
jhon309
Date:
Thu Aug 13 00:21:57 2015 +0000
Revision:
0:c52df770855b
DHT11

Who changed what in which revision?

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