fix nrf51822 i2c & spi conflict

Dependencies:   BLE_API eMPL_MPU6050 nRF51822

Fork of Seeed_Tiny_BLE_Flash by Darren Huang

Committer:
yihui
Date:
Tue Nov 17 07:48:56 2015 +0000
Revision:
5:b8c02645e6af
fix i2c & spi conflict

Who changed what in which revision?

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