PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
Dependents: YATTT sd_map_test cPong SnowDemo ... more
PokittoLib
Library for programming Pokitto hardware
How to Use
- Import this library to online compiler (see button "import" on the right hand side
- DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
- Change My_settings.h according to your project
- Start coding!
mbed-pokitto/api/InterruptManager.h@71:531419862202, 2019-12-25 (annotated)
- Committer:
- Pokitto
- Date:
- Wed Dec 25 23:59:52 2019 +0000
- Revision:
- 71:531419862202
- Parent:
- 5:ea7377f3d1af
Changed Mode2 C++ refresh code (graphical errors)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 5:ea7377f3d1af | 1 | #ifndef MBED_INTERRUPTMANAGER_H |
Pokitto | 5:ea7377f3d1af | 2 | #define MBED_INTERRUPTMANAGER_H |
Pokitto | 5:ea7377f3d1af | 3 | |
Pokitto | 5:ea7377f3d1af | 4 | #include "cmsis.h" |
Pokitto | 5:ea7377f3d1af | 5 | #include "CallChain.h" |
Pokitto | 5:ea7377f3d1af | 6 | #include <string.h> |
Pokitto | 5:ea7377f3d1af | 7 | |
Pokitto | 5:ea7377f3d1af | 8 | namespace mbed { |
Pokitto | 5:ea7377f3d1af | 9 | |
Pokitto | 5:ea7377f3d1af | 10 | /** Use this singleton if you need to chain interrupt handlers. |
Pokitto | 5:ea7377f3d1af | 11 | * |
Pokitto | 5:ea7377f3d1af | 12 | * Example (for LPC1768): |
Pokitto | 5:ea7377f3d1af | 13 | * @code |
Pokitto | 5:ea7377f3d1af | 14 | * #include "InterruptManager.h" |
Pokitto | 5:ea7377f3d1af | 15 | * #include "mbed.h" |
Pokitto | 5:ea7377f3d1af | 16 | * |
Pokitto | 5:ea7377f3d1af | 17 | * Ticker flipper; |
Pokitto | 5:ea7377f3d1af | 18 | * DigitalOut led1(LED1); |
Pokitto | 5:ea7377f3d1af | 19 | * DigitalOut led2(LED2); |
Pokitto | 5:ea7377f3d1af | 20 | * |
Pokitto | 5:ea7377f3d1af | 21 | * void flip(void) { |
Pokitto | 5:ea7377f3d1af | 22 | * led1 = !led1; |
Pokitto | 5:ea7377f3d1af | 23 | * } |
Pokitto | 5:ea7377f3d1af | 24 | * |
Pokitto | 5:ea7377f3d1af | 25 | * void handler(void) { |
Pokitto | 5:ea7377f3d1af | 26 | * led2 = !led1; |
Pokitto | 5:ea7377f3d1af | 27 | * } |
Pokitto | 5:ea7377f3d1af | 28 | * |
Pokitto | 5:ea7377f3d1af | 29 | * int main() { |
Pokitto | 5:ea7377f3d1af | 30 | * led1 = led2 = 0; |
Pokitto | 5:ea7377f3d1af | 31 | * flipper.attach(&flip, 1.0); |
Pokitto | 5:ea7377f3d1af | 32 | * InterruptManager::get()->add_handler(handler, TIMER3_IRQn); |
Pokitto | 5:ea7377f3d1af | 33 | * } |
Pokitto | 5:ea7377f3d1af | 34 | * @endcode |
Pokitto | 5:ea7377f3d1af | 35 | */ |
Pokitto | 5:ea7377f3d1af | 36 | class InterruptManager { |
Pokitto | 5:ea7377f3d1af | 37 | public: |
Pokitto | 5:ea7377f3d1af | 38 | /** Return the only instance of this class |
Pokitto | 5:ea7377f3d1af | 39 | */ |
Pokitto | 5:ea7377f3d1af | 40 | static InterruptManager* get(); |
Pokitto | 5:ea7377f3d1af | 41 | |
Pokitto | 5:ea7377f3d1af | 42 | /** Destroy the current instance of the interrupt manager |
Pokitto | 5:ea7377f3d1af | 43 | */ |
Pokitto | 5:ea7377f3d1af | 44 | static void destroy(); |
Pokitto | 5:ea7377f3d1af | 45 | |
Pokitto | 5:ea7377f3d1af | 46 | /** Add a handler for an interrupt at the end of the handler list |
Pokitto | 5:ea7377f3d1af | 47 | * |
Pokitto | 5:ea7377f3d1af | 48 | * @param function the handler to add |
Pokitto | 5:ea7377f3d1af | 49 | * @param irq interrupt number |
Pokitto | 5:ea7377f3d1af | 50 | * |
Pokitto | 5:ea7377f3d1af | 51 | * @returns |
Pokitto | 5:ea7377f3d1af | 52 | * The function object created for 'function' |
Pokitto | 5:ea7377f3d1af | 53 | */ |
Pokitto | 5:ea7377f3d1af | 54 | pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) { |
Pokitto | 5:ea7377f3d1af | 55 | return add_common(function, irq); |
Pokitto | 5:ea7377f3d1af | 56 | } |
Pokitto | 5:ea7377f3d1af | 57 | |
Pokitto | 5:ea7377f3d1af | 58 | /** Add a handler for an interrupt at the beginning of the handler list |
Pokitto | 5:ea7377f3d1af | 59 | * |
Pokitto | 5:ea7377f3d1af | 60 | * @param function the handler to add |
Pokitto | 5:ea7377f3d1af | 61 | * @param irq interrupt number |
Pokitto | 5:ea7377f3d1af | 62 | * |
Pokitto | 5:ea7377f3d1af | 63 | * @returns |
Pokitto | 5:ea7377f3d1af | 64 | * The function object created for 'function' |
Pokitto | 5:ea7377f3d1af | 65 | */ |
Pokitto | 5:ea7377f3d1af | 66 | pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) { |
Pokitto | 5:ea7377f3d1af | 67 | return add_common(function, irq, true); |
Pokitto | 5:ea7377f3d1af | 68 | } |
Pokitto | 5:ea7377f3d1af | 69 | |
Pokitto | 5:ea7377f3d1af | 70 | /** Add a handler for an interrupt at the end of the handler list |
Pokitto | 5:ea7377f3d1af | 71 | * |
Pokitto | 5:ea7377f3d1af | 72 | * @param tptr pointer to the object that has the handler function |
Pokitto | 5:ea7377f3d1af | 73 | * @param mptr pointer to the actual handler function |
Pokitto | 5:ea7377f3d1af | 74 | * @param irq interrupt number |
Pokitto | 5:ea7377f3d1af | 75 | * |
Pokitto | 5:ea7377f3d1af | 76 | * @returns |
Pokitto | 5:ea7377f3d1af | 77 | * The function object created for 'tptr' and 'mptr' |
Pokitto | 5:ea7377f3d1af | 78 | */ |
Pokitto | 5:ea7377f3d1af | 79 | template<typename T> |
Pokitto | 5:ea7377f3d1af | 80 | pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { |
Pokitto | 5:ea7377f3d1af | 81 | return add_common(tptr, mptr, irq); |
Pokitto | 5:ea7377f3d1af | 82 | } |
Pokitto | 5:ea7377f3d1af | 83 | |
Pokitto | 5:ea7377f3d1af | 84 | /** Add a handler for an interrupt at the beginning of the handler list |
Pokitto | 5:ea7377f3d1af | 85 | * |
Pokitto | 5:ea7377f3d1af | 86 | * @param tptr pointer to the object that has the handler function |
Pokitto | 5:ea7377f3d1af | 87 | * @param mptr pointer to the actual handler function |
Pokitto | 5:ea7377f3d1af | 88 | * @param irq interrupt number |
Pokitto | 5:ea7377f3d1af | 89 | * |
Pokitto | 5:ea7377f3d1af | 90 | * @returns |
Pokitto | 5:ea7377f3d1af | 91 | * The function object created for 'tptr' and 'mptr' |
Pokitto | 5:ea7377f3d1af | 92 | */ |
Pokitto | 5:ea7377f3d1af | 93 | template<typename T> |
Pokitto | 5:ea7377f3d1af | 94 | pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) { |
Pokitto | 5:ea7377f3d1af | 95 | return add_common(tptr, mptr, irq, true); |
Pokitto | 5:ea7377f3d1af | 96 | } |
Pokitto | 5:ea7377f3d1af | 97 | |
Pokitto | 5:ea7377f3d1af | 98 | /** Remove a handler from an interrupt |
Pokitto | 5:ea7377f3d1af | 99 | * |
Pokitto | 5:ea7377f3d1af | 100 | * @param handler the function object for the handler to remove |
Pokitto | 5:ea7377f3d1af | 101 | * @param irq the interrupt number |
Pokitto | 5:ea7377f3d1af | 102 | * |
Pokitto | 5:ea7377f3d1af | 103 | * @returns |
Pokitto | 5:ea7377f3d1af | 104 | * true if the handler was found and removed, false otherwise |
Pokitto | 5:ea7377f3d1af | 105 | */ |
Pokitto | 5:ea7377f3d1af | 106 | bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq); |
Pokitto | 5:ea7377f3d1af | 107 | |
Pokitto | 5:ea7377f3d1af | 108 | private: |
Pokitto | 5:ea7377f3d1af | 109 | InterruptManager(); |
Pokitto | 5:ea7377f3d1af | 110 | ~InterruptManager(); |
Pokitto | 5:ea7377f3d1af | 111 | |
Pokitto | 5:ea7377f3d1af | 112 | // We declare the copy contructor and the assignment operator, but we don't |
Pokitto | 5:ea7377f3d1af | 113 | // implement them. This way, if someone tries to copy/assign our instance, |
Pokitto | 5:ea7377f3d1af | 114 | // he will get an error at compile time. |
Pokitto | 5:ea7377f3d1af | 115 | InterruptManager(const InterruptManager&); |
Pokitto | 5:ea7377f3d1af | 116 | InterruptManager& operator =(const InterruptManager&); |
Pokitto | 5:ea7377f3d1af | 117 | |
Pokitto | 5:ea7377f3d1af | 118 | template<typename T> |
Pokitto | 5:ea7377f3d1af | 119 | pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) { |
Pokitto | 5:ea7377f3d1af | 120 | int irq_pos = get_irq_index(irq); |
Pokitto | 5:ea7377f3d1af | 121 | bool change = must_replace_vector(irq); |
Pokitto | 5:ea7377f3d1af | 122 | |
Pokitto | 5:ea7377f3d1af | 123 | pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr); |
Pokitto | 5:ea7377f3d1af | 124 | if (change) |
Pokitto | 5:ea7377f3d1af | 125 | NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper); |
Pokitto | 5:ea7377f3d1af | 126 | return pf; |
Pokitto | 5:ea7377f3d1af | 127 | } |
Pokitto | 5:ea7377f3d1af | 128 | |
Pokitto | 5:ea7377f3d1af | 129 | pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false); |
Pokitto | 5:ea7377f3d1af | 130 | bool must_replace_vector(IRQn_Type irq); |
Pokitto | 5:ea7377f3d1af | 131 | int get_irq_index(IRQn_Type irq); |
Pokitto | 5:ea7377f3d1af | 132 | void irq_helper(); |
Pokitto | 5:ea7377f3d1af | 133 | void add_helper(void (*function)(void), IRQn_Type irq, bool front=false); |
Pokitto | 5:ea7377f3d1af | 134 | static void static_irq_helper(); |
Pokitto | 5:ea7377f3d1af | 135 | |
Pokitto | 5:ea7377f3d1af | 136 | CallChain* _chains[NVIC_NUM_VECTORS]; |
Pokitto | 5:ea7377f3d1af | 137 | static InterruptManager* _instance; |
Pokitto | 5:ea7377f3d1af | 138 | }; |
Pokitto | 5:ea7377f3d1af | 139 | |
Pokitto | 5:ea7377f3d1af | 140 | } // namespace mbed |
Pokitto | 5:ea7377f3d1af | 141 | |
Pokitto | 5:ea7377f3d1af | 142 | #endif |
Pokitto | 5:ea7377f3d1af | 143 |