MCU driver/HAL for the Picocell Gateway concentrator board. The firmware implements either a USB CDC protocol or a UART protocol to bridge commands coming from host to the SX1308 SPI interface.

Committer:
dgabino
Date:
Wed Apr 11 14:42:47 2018 +0000
Revision:
0:c76361bd82e8
Initial commit

Who changed what in which revision?

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