mbed library sources with internal temperature sensor for nucleo f401

Committer:
elessair
Date:
Sat Jan 17 18:03:58 2015 +0000
Revision:
0:7e2bd16f80af
nucleo f401re internal temperature added

Who changed what in which revision?

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