I2C_EEPROM

Committer:
jhon309
Date:
Thu Aug 13 00:23:16 2015 +0000
Revision:
0:ac8863619623
I2C

Who changed what in which revision?

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