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