change some io settings for TWR-K22F-120M

Dependents:   twr_helloworld

Committer:
Jasper_lee
Date:
Tue Dec 23 03:35:08 2014 +0000
Revision:
0:b16d94660a33
change some io setting used in TWR-K22F120M

Who changed what in which revision?

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