customized mbed library sources for nrf51822

Dependents:   Grove_Node Potentiometer BLE_Beacon I2C_Scanner

Committer:
yihui
Date:
Tue Nov 04 07:38:53 2014 +0000
Revision:
0:700cadd8b708
customized mbed-src library for nrf51822

Who changed what in which revision?

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