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