mbed-drivers
InterruptManager.h
1 /*
2  * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MBED_INTERRUPTMANAGER_H
18 #define MBED_INTERRUPTMANAGER_H
19 
20 #include "cmsis.h"
21 #include "CallChain.h"
22 #include <string.h>
23 
24 namespace mbed {
25 
53 public:
56  static InterruptManager* get();
57 
60  static void destroy();
61 
70  pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) {
71  return add_common(function, irq);
72  }
73 
82  pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) {
83  return add_common(function, irq, true);
84  }
85 
95  template<typename T>
96  pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
97  return add_common(tptr, mptr, irq);
98  }
99 
109  template<typename T>
110  pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
111  return add_common(tptr, mptr, irq, true);
112  }
113 
122  bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
123 
124 private:
126  ~InterruptManager();
127 
128  // We declare the copy contructor and the assignment operator, but we don't
129  // implement them. This way, if someone tries to copy/assign our instance,
130  // he will get an error at compile time.
132  InterruptManager& operator =(const InterruptManager&);
133 
134  template<typename T>
135  pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) {
136  int irq_pos = get_irq_index(irq);
137  bool change = must_replace_vector(irq);
138 
139  pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr);
140  if (change)
141  NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
142  return pf;
143  }
144 
145  pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false);
146  bool must_replace_vector(IRQn_Type irq);
147  int get_irq_index(IRQn_Type irq);
148  void irq_helper();
149  void add_helper(void (*function)(void), IRQn_Type irq, bool front=false);
150  static void static_irq_helper();
151 
152  CallChain* _chains[NVIC_NUM_VECTORS];
153  static InterruptManager* _instance;
154 };
155 
156 } // namespace mbed
157 
158 #endif
159 
pFunctionPointer_t add_front(void(*function)(void))
Definition: CallChain.cpp:35
pFunctionPointer_t add_handler(void(*function)(void), IRQn_Type irq)
Definition: InterruptManager.h:70
static void destroy()
Definition: InterruptManager.cpp:41
bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq)
Definition: InterruptManager.cpp:78
pFunctionPointer_t add_handler_front(T *tptr, void(T::*mptr)(void), IRQn_Type irq)
Definition: InterruptManager.h:110
pFunctionPointer_t add_handler_front(void(*function)(void), IRQn_Type irq)
Definition: InterruptManager.h:82
pFunctionPointer_t add(void(*function)(void))
Definition: CallChain.cpp:31
pFunctionPointer_t add_handler(T *tptr, void(T::*mptr)(void), IRQn_Type irq)
Definition: InterruptManager.h:96
Definition: InterruptManager.h:52
Definition: BusIn.cpp:19
Definition: CallChain.h:63