]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/common/InterruptManager.cpp
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / common / InterruptManager.cpp
1 #include "cmsis.h"
2 #if defined(NVIC_NUM_VECTORS)
3
4 #include "InterruptManager.h"
5 #include <string.h>
6
7 #define CHAIN_INITIAL_SIZE 4
8
9 namespace mbed {
10
11 typedef void (*pvoidf)(void);
12
13 InterruptManager* InterruptManager::_instance = (InterruptManager*)NULL;
14
15 InterruptManager* InterruptManager::get() {
16 if (NULL == _instance)
17 _instance = new InterruptManager();
18 return _instance;
19 }
20
21 InterruptManager::InterruptManager() {
22 memset(_chains, 0, NVIC_NUM_VECTORS * sizeof(CallChain*));
23 }
24
25 void InterruptManager::destroy() {
26 // Not a good idea to call this unless NO interrupt at all
27 // is under the control of the handler; otherwise, a system crash
28 // is very likely to occur
29 if (NULL != _instance) {
30 delete _instance;
31 _instance = (InterruptManager*)NULL;
32 }
33 }
34
35 InterruptManager::~InterruptManager() {
36 for(int i = 0; i < NVIC_NUM_VECTORS; i++)
37 if (NULL != _chains[i])
38 delete _chains[i];
39 }
40
41 bool InterruptManager::must_replace_vector(IRQn_Type irq) {
42 int irq_pos = get_irq_index(irq);
43
44 if (NULL == _chains[irq_pos]) {
45 _chains[irq_pos] = new CallChain(CHAIN_INITIAL_SIZE);
46 _chains[irq_pos]->add((pvoidf)NVIC_GetVector(irq));
47 return true;
48 }
49 return false;
50 }
51
52 pFunctionPointer_t InterruptManager::add_common(void (*function)(void), IRQn_Type irq, bool front) {
53 int irq_pos = get_irq_index(irq);
54 bool change = must_replace_vector(irq);
55
56 pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(function) : _chains[irq_pos]->add(function);
57 if (change)
58 NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
59 return pf;
60 }
61
62 bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq) {
63 int irq_pos = get_irq_index(irq);
64
65 if (NULL == _chains[irq_pos])
66 return false;
67 if (!_chains[irq_pos]->remove(handler))
68 return false;
69 // If there's a single function left in the chain, swith the interrupt vector
70 // to call that function directly. This way we save both time and space.
71 if (_chains[irq_pos]->size() == 1 && NULL != _chains[irq_pos]->get(0)->get_function()) {
72 NVIC_SetVector(irq, (uint32_t)_chains[irq_pos]->get(0)->get_function());
73 delete _chains[irq_pos];
74 _chains[irq_pos] = (CallChain*) NULL;
75 }
76 return true;
77 }
78
79 void InterruptManager::irq_helper() {
80 _chains[__get_IPSR()]->call();
81 }
82
83 int InterruptManager::get_irq_index(IRQn_Type irq) {
84 return (int)irq + NVIC_USER_IRQ_OFFSET;
85 }
86
87 void InterruptManager::static_irq_helper() {
88 InterruptManager::get()->irq_helper();
89 }
90
91 } // namespace mbed
92
93 #endif
Imprint / Impressum