Test code for Grove Node BLE

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
yihui
Date:
Thu Nov 27 09:30:36 2014 +0000
Revision:
10:22480ac31879
Parent:
9:05f0b5a3a70a
change to new revision hardware

Who changed what in which revision?

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