1 #ifndef MBED_INTERRUPTMANAGER_H
2 #define MBED_INTERRUPTMANAGER_H
10 /** Use this singleton if you need to chain interrupt handlers.
12 * Example (for LPC1768):
14 * #include "InterruptManager.h"
18 * DigitalOut led1(LED1);
19 * DigitalOut led2(LED2);
25 * void handler(void) {
31 * flipper.attach(&flip, 1.0);
32 * InterruptManager::get()->add_handler(handler, TIMER3_IRQn);
36 class InterruptManager
{
38 /** Return the only instance of this class
40 static InterruptManager
* get();
42 /** Destroy the current instance of the interrupt manager
44 static void destroy();
46 /** Add a handler for an interrupt at the end of the handler list
48 * @param function the handler to add
49 * @param irq interrupt number
52 * The function object created for 'function'
54 pFunctionPointer_t
add_handler(void (*function
)(void), IRQn_Type irq
) {
55 return add_common(function
, irq
);
58 /** Add a handler for an interrupt at the beginning of the handler list
60 * @param function the handler to add
61 * @param irq interrupt number
64 * The function object created for 'function'
66 pFunctionPointer_t
add_handler_front(void (*function
)(void), IRQn_Type irq
) {
67 return add_common(function
, irq
, true);
70 /** Add a handler for an interrupt at the end of the handler list
72 * @param tptr pointer to the object that has the handler function
73 * @param mptr pointer to the actual handler function
74 * @param irq interrupt number
77 * The function object created for 'tptr' and 'mptr'
80 pFunctionPointer_t
add_handler(T
* tptr
, void (T::*mptr
)(void), IRQn_Type irq
) {
81 return add_common(tptr
, mptr
, irq
);
84 /** Add a handler for an interrupt at the beginning of the handler list
86 * @param tptr pointer to the object that has the handler function
87 * @param mptr pointer to the actual handler function
88 * @param irq interrupt number
91 * The function object created for 'tptr' and 'mptr'
94 pFunctionPointer_t
add_handler_front(T
* tptr
, void (T::*mptr
)(void), IRQn_Type irq
) {
95 return add_common(tptr
, mptr
, irq
, true);
98 /** Remove a handler from an interrupt
100 * @param handler the function object for the handler to remove
101 * @param irq the interrupt number
104 * true if the handler was found and removed, false otherwise
106 bool remove_handler(pFunctionPointer_t handler
, IRQn_Type irq
);
112 // We declare the copy contructor and the assignment operator, but we don't
113 // implement them. This way, if someone tries to copy/assign our instance,
114 // he will get an error at compile time.
115 InterruptManager(const InterruptManager
&);
116 InterruptManager
& operator =(const InterruptManager
&);
119 pFunctionPointer_t
add_common(T
*tptr
, void (T::*mptr
)(void), IRQn_Type irq
, bool front
=false) {
120 int irq_pos
= get_irq_index(irq
);
121 bool change
= must_replace_vector(irq
);
123 pFunctionPointer_t pf
= front
? _chains
[irq_pos
]->add_front(tptr
, mptr
) : _chains
[irq_pos
]->add(tptr
, mptr
);
125 NVIC_SetVector(irq
, (uint32_t)&InterruptManager::static_irq_helper
);
129 pFunctionPointer_t
add_common(void (*function
)(void), IRQn_Type irq
, bool front
=false);
130 bool must_replace_vector(IRQn_Type irq
);
131 int get_irq_index(IRQn_Type irq
);
133 void add_helper(void (*function
)(void), IRQn_Type irq
, bool front
=false);
134 static void static_irq_helper();
136 CallChain
* _chains
[NVIC_NUM_VECTORS
];
137 static InterruptManager
* _instance
;