IOTIO

Dependencies:   Nucleo_BLE_API_IOTIO Nucleo_BLE_BlueNRG Nucleo_BLE_DemoApp Nucleo_Sensor_Shield mbed

Dependents:   Nucleo_BLE_Demo_IOTIO

Fork of Nucleo_BLE_Demo by Cortex Challenge Team

Committer:
16038618
Date:
Wed Mar 08 10:24:56 2017 +0000
Revision:
2:c69117fc79da
Parent:
1:4bdfa7d7e8bf
IOTIO

Who changed what in which revision?

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