SilentSensors / mbed-dev

Fork of mbed-dev by mbed official

Committer:
WaleedElmughrabi
Date:
Thu Sep 20 16:11:23 2018 +0000
Revision:
188:60408c49b6d4
Parent:
187:0387e8f68319
Fork modified for BG96 error

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /* mbed Microcontroller Library
<> 149:156823d33999 2 * Copyright (c) 2006-2013 ARM Limited
<> 149:156823d33999 3 *
<> 149:156823d33999 4 * Licensed under the Apache License, Version 2.0 (the "License");
<> 149:156823d33999 5 * you may not use this file except in compliance with the License.
<> 149:156823d33999 6 * You may obtain a copy of the License at
<> 149:156823d33999 7 *
<> 149:156823d33999 8 * http://www.apache.org/licenses/LICENSE-2.0
<> 149:156823d33999 9 *
<> 149:156823d33999 10 * Unless required by applicable law or agreed to in writing, software
<> 149:156823d33999 11 * distributed under the License is distributed on an "AS IS" BASIS,
<> 149:156823d33999 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 149:156823d33999 13 * See the License for the specific language governing permissions and
<> 149:156823d33999 14 * limitations under the License.
<> 149:156823d33999 15 */
<> 149:156823d33999 16 #include "cmsis.h"
<> 149:156823d33999 17 #if defined(NVIC_NUM_VECTORS)
<> 149:156823d33999 18
Anna Bridge 180:96ed750bd169 19 // Suppress deprecation warnings since this whole
Anna Bridge 180:96ed750bd169 20 // class is deprecated already
Anna Bridge 180:96ed750bd169 21 #include "mbed_toolchain.h"
Anna Bridge 180:96ed750bd169 22 #undef MBED_DEPRECATED_SINCE
Anna Bridge 180:96ed750bd169 23 #define MBED_DEPRECATED_SINCE(...)
Anna Bridge 180:96ed750bd169 24
<> 149:156823d33999 25 #include "drivers/InterruptManager.h"
<> 160:d5399cc887bb 26 #include "platform/mbed_critical.h"
<> 149:156823d33999 27 #include <string.h>
<> 149:156823d33999 28
<> 149:156823d33999 29 #define CHAIN_INITIAL_SIZE 4
<> 149:156823d33999 30
<> 149:156823d33999 31 namespace mbed {
<> 149:156823d33999 32
<> 149:156823d33999 33 typedef void (*pvoidf)(void);
<> 149:156823d33999 34
AnnaBridge 187:0387e8f68319 35 InterruptManager *InterruptManager::_instance = (InterruptManager *)NULL;
<> 149:156823d33999 36
AnnaBridge 187:0387e8f68319 37 InterruptManager *InterruptManager::get()
AnnaBridge 187:0387e8f68319 38 {
<> 149:156823d33999 39
<> 149:156823d33999 40 if (NULL == _instance) {
AnnaBridge 187:0387e8f68319 41 InterruptManager *temp = new InterruptManager();
<> 149:156823d33999 42
<> 149:156823d33999 43 // Atomically set _instance
<> 149:156823d33999 44 core_util_critical_section_enter();
<> 149:156823d33999 45 if (NULL == _instance) {
<> 149:156823d33999 46 _instance = temp;
<> 149:156823d33999 47 }
<> 149:156823d33999 48 core_util_critical_section_exit();
<> 149:156823d33999 49
<> 149:156823d33999 50 // Another thread got there first so delete ours
<> 149:156823d33999 51 if (temp != _instance) {
<> 149:156823d33999 52 delete temp;
<> 149:156823d33999 53 }
<> 149:156823d33999 54
<> 149:156823d33999 55 }
<> 149:156823d33999 56 return _instance;
<> 149:156823d33999 57 }
<> 149:156823d33999 58
AnnaBridge 187:0387e8f68319 59 InterruptManager::InterruptManager()
AnnaBridge 187:0387e8f68319 60 {
<> 149:156823d33999 61 // No mutex needed in constructor
AnnaBridge 187:0387e8f68319 62 memset(_chains, 0, NVIC_NUM_VECTORS * sizeof(CallChain *));
<> 149:156823d33999 63 }
<> 149:156823d33999 64
AnnaBridge 187:0387e8f68319 65 void InterruptManager::destroy()
AnnaBridge 187:0387e8f68319 66 {
<> 149:156823d33999 67 // Not a good idea to call this unless NO interrupt at all
<> 149:156823d33999 68 // is under the control of the handler; otherwise, a system crash
<> 149:156823d33999 69 // is very likely to occur
<> 149:156823d33999 70 if (NULL != _instance) {
<> 149:156823d33999 71 delete _instance;
AnnaBridge 187:0387e8f68319 72 _instance = (InterruptManager *)NULL;
<> 149:156823d33999 73 }
<> 149:156823d33999 74 }
<> 149:156823d33999 75
AnnaBridge 187:0387e8f68319 76 InterruptManager::~InterruptManager()
AnnaBridge 187:0387e8f68319 77 {
AnnaBridge 187:0387e8f68319 78 for (int i = 0; i < NVIC_NUM_VECTORS; i++)
AnnaBridge 187:0387e8f68319 79 if (NULL != _chains[i]) {
<> 149:156823d33999 80 delete _chains[i];
AnnaBridge 187:0387e8f68319 81 }
<> 149:156823d33999 82 }
<> 149:156823d33999 83
AnnaBridge 187:0387e8f68319 84 bool InterruptManager::must_replace_vector(IRQn_Type irq)
AnnaBridge 187:0387e8f68319 85 {
<> 149:156823d33999 86 lock();
<> 149:156823d33999 87
<> 149:156823d33999 88 int ret = false;
<> 149:156823d33999 89 int irq_pos = get_irq_index(irq);
<> 149:156823d33999 90 if (NULL == _chains[irq_pos]) {
<> 149:156823d33999 91 _chains[irq_pos] = new CallChain(CHAIN_INITIAL_SIZE);
<> 149:156823d33999 92 _chains[irq_pos]->add((pvoidf)NVIC_GetVector(irq));
<> 149:156823d33999 93 ret = true;
<> 149:156823d33999 94 }
<> 149:156823d33999 95 unlock();
<> 149:156823d33999 96 return ret;
<> 149:156823d33999 97 }
<> 149:156823d33999 98
AnnaBridge 187:0387e8f68319 99 pFunctionPointer_t InterruptManager::add_common(void (*function)(void), IRQn_Type irq, bool front)
AnnaBridge 187:0387e8f68319 100 {
<> 149:156823d33999 101 lock();
<> 149:156823d33999 102 int irq_pos = get_irq_index(irq);
<> 149:156823d33999 103 bool change = must_replace_vector(irq);
<> 149:156823d33999 104
<> 149:156823d33999 105 pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(function) : _chains[irq_pos]->add(function);
AnnaBridge 187:0387e8f68319 106 if (change) {
<> 149:156823d33999 107 NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
AnnaBridge 187:0387e8f68319 108 }
<> 149:156823d33999 109 unlock();
<> 149:156823d33999 110 return pf;
<> 149:156823d33999 111 }
<> 149:156823d33999 112
AnnaBridge 187:0387e8f68319 113 bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq)
AnnaBridge 187:0387e8f68319 114 {
<> 149:156823d33999 115 int irq_pos = get_irq_index(irq);
<> 149:156823d33999 116 bool ret = false;
<> 149:156823d33999 117
<> 149:156823d33999 118 lock();
<> 149:156823d33999 119 if (_chains[irq_pos] != NULL) {
<> 149:156823d33999 120 if (_chains[irq_pos]->remove(handler)) {
<> 149:156823d33999 121 ret = true;
<> 149:156823d33999 122 }
<> 149:156823d33999 123 }
<> 149:156823d33999 124 unlock();
<> 149:156823d33999 125
<> 149:156823d33999 126 return ret;
<> 149:156823d33999 127 }
<> 149:156823d33999 128
AnnaBridge 187:0387e8f68319 129 void InterruptManager::irq_helper()
AnnaBridge 187:0387e8f68319 130 {
<> 149:156823d33999 131 _chains[__get_IPSR()]->call();
<> 149:156823d33999 132 }
<> 149:156823d33999 133
AnnaBridge 187:0387e8f68319 134 int InterruptManager::get_irq_index(IRQn_Type irq)
AnnaBridge 187:0387e8f68319 135 {
<> 149:156823d33999 136 // Pure function - no lock needed
<> 149:156823d33999 137 return (int)irq + NVIC_USER_IRQ_OFFSET;
<> 149:156823d33999 138 }
<> 149:156823d33999 139
AnnaBridge 187:0387e8f68319 140 void InterruptManager::static_irq_helper()
AnnaBridge 187:0387e8f68319 141 {
<> 149:156823d33999 142 InterruptManager::get()->irq_helper();
<> 149:156823d33999 143 }
<> 149:156823d33999 144
AnnaBridge 187:0387e8f68319 145 void InterruptManager::lock()
AnnaBridge 187:0387e8f68319 146 {
<> 149:156823d33999 147 _mutex.lock();
<> 149:156823d33999 148 }
<> 149:156823d33999 149
AnnaBridge 187:0387e8f68319 150 void InterruptManager::unlock()
AnnaBridge 187:0387e8f68319 151 {
<> 149:156823d33999 152 _mutex.unlock();
<> 149:156823d33999 153 }
<> 149:156823d33999 154
<> 149:156823d33999 155 } // namespace mbed
<> 149:156823d33999 156
<> 149:156823d33999 157 #endif