Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dev by
drivers/InterruptManager.cpp@180:96ed750bd169, 2018-01-17 (annotated)
- Committer:
- Anna Bridge
- Date:
- Wed Jan 17 15:23:54 2018 +0000
- Revision:
- 180:96ed750bd169
- Parent:
- 160:d5399cc887bb
mbed-dev libray. Release version 158
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 |
<> | 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 | |
<> | 149:156823d33999 | 35 | InterruptManager* InterruptManager::_instance = (InterruptManager*)NULL; |
<> | 149:156823d33999 | 36 | |
<> | 149:156823d33999 | 37 | InterruptManager* InterruptManager::get() { |
<> | 149:156823d33999 | 38 | |
<> | 149:156823d33999 | 39 | if (NULL == _instance) { |
<> | 149:156823d33999 | 40 | InterruptManager* temp = new InterruptManager(); |
<> | 149:156823d33999 | 41 | |
<> | 149:156823d33999 | 42 | // Atomically set _instance |
<> | 149:156823d33999 | 43 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 44 | if (NULL == _instance) { |
<> | 149:156823d33999 | 45 | _instance = temp; |
<> | 149:156823d33999 | 46 | } |
<> | 149:156823d33999 | 47 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 48 | |
<> | 149:156823d33999 | 49 | // Another thread got there first so delete ours |
<> | 149:156823d33999 | 50 | if (temp != _instance) { |
<> | 149:156823d33999 | 51 | delete temp; |
<> | 149:156823d33999 | 52 | } |
<> | 149:156823d33999 | 53 | |
<> | 149:156823d33999 | 54 | } |
<> | 149:156823d33999 | 55 | return _instance; |
<> | 149:156823d33999 | 56 | } |
<> | 149:156823d33999 | 57 | |
<> | 149:156823d33999 | 58 | InterruptManager::InterruptManager() { |
<> | 149:156823d33999 | 59 | // No mutex needed in constructor |
<> | 149:156823d33999 | 60 | memset(_chains, 0, NVIC_NUM_VECTORS * sizeof(CallChain*)); |
<> | 149:156823d33999 | 61 | } |
<> | 149:156823d33999 | 62 | |
<> | 149:156823d33999 | 63 | void InterruptManager::destroy() { |
<> | 149:156823d33999 | 64 | // Not a good idea to call this unless NO interrupt at all |
<> | 149:156823d33999 | 65 | // is under the control of the handler; otherwise, a system crash |
<> | 149:156823d33999 | 66 | // is very likely to occur |
<> | 149:156823d33999 | 67 | if (NULL != _instance) { |
<> | 149:156823d33999 | 68 | delete _instance; |
<> | 149:156823d33999 | 69 | _instance = (InterruptManager*)NULL; |
<> | 149:156823d33999 | 70 | } |
<> | 149:156823d33999 | 71 | } |
<> | 149:156823d33999 | 72 | |
<> | 149:156823d33999 | 73 | InterruptManager::~InterruptManager() { |
<> | 149:156823d33999 | 74 | for(int i = 0; i < NVIC_NUM_VECTORS; i++) |
<> | 149:156823d33999 | 75 | if (NULL != _chains[i]) |
<> | 149:156823d33999 | 76 | delete _chains[i]; |
<> | 149:156823d33999 | 77 | } |
<> | 149:156823d33999 | 78 | |
<> | 149:156823d33999 | 79 | bool InterruptManager::must_replace_vector(IRQn_Type irq) { |
<> | 149:156823d33999 | 80 | lock(); |
<> | 149:156823d33999 | 81 | |
<> | 149:156823d33999 | 82 | int ret = false; |
<> | 149:156823d33999 | 83 | int irq_pos = get_irq_index(irq); |
<> | 149:156823d33999 | 84 | if (NULL == _chains[irq_pos]) { |
<> | 149:156823d33999 | 85 | _chains[irq_pos] = new CallChain(CHAIN_INITIAL_SIZE); |
<> | 149:156823d33999 | 86 | _chains[irq_pos]->add((pvoidf)NVIC_GetVector(irq)); |
<> | 149:156823d33999 | 87 | ret = true; |
<> | 149:156823d33999 | 88 | } |
<> | 149:156823d33999 | 89 | unlock(); |
<> | 149:156823d33999 | 90 | return ret; |
<> | 149:156823d33999 | 91 | } |
<> | 149:156823d33999 | 92 | |
<> | 149:156823d33999 | 93 | pFunctionPointer_t InterruptManager::add_common(void (*function)(void), IRQn_Type irq, bool front) { |
<> | 149:156823d33999 | 94 | lock(); |
<> | 149:156823d33999 | 95 | int irq_pos = get_irq_index(irq); |
<> | 149:156823d33999 | 96 | bool change = must_replace_vector(irq); |
<> | 149:156823d33999 | 97 | |
<> | 149:156823d33999 | 98 | pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(function) : _chains[irq_pos]->add(function); |
<> | 149:156823d33999 | 99 | if (change) |
<> | 149:156823d33999 | 100 | NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper); |
<> | 149:156823d33999 | 101 | unlock(); |
<> | 149:156823d33999 | 102 | return pf; |
<> | 149:156823d33999 | 103 | } |
<> | 149:156823d33999 | 104 | |
<> | 149:156823d33999 | 105 | bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq) { |
<> | 149:156823d33999 | 106 | int irq_pos = get_irq_index(irq); |
<> | 149:156823d33999 | 107 | bool ret = false; |
<> | 149:156823d33999 | 108 | |
<> | 149:156823d33999 | 109 | lock(); |
<> | 149:156823d33999 | 110 | if (_chains[irq_pos] != NULL) { |
<> | 149:156823d33999 | 111 | if (_chains[irq_pos]->remove(handler)) { |
<> | 149:156823d33999 | 112 | ret = true; |
<> | 149:156823d33999 | 113 | } |
<> | 149:156823d33999 | 114 | } |
<> | 149:156823d33999 | 115 | unlock(); |
<> | 149:156823d33999 | 116 | |
<> | 149:156823d33999 | 117 | return ret; |
<> | 149:156823d33999 | 118 | } |
<> | 149:156823d33999 | 119 | |
<> | 149:156823d33999 | 120 | void InterruptManager::irq_helper() { |
<> | 149:156823d33999 | 121 | _chains[__get_IPSR()]->call(); |
<> | 149:156823d33999 | 122 | } |
<> | 149:156823d33999 | 123 | |
<> | 149:156823d33999 | 124 | int InterruptManager::get_irq_index(IRQn_Type irq) { |
<> | 149:156823d33999 | 125 | // Pure function - no lock needed |
<> | 149:156823d33999 | 126 | return (int)irq + NVIC_USER_IRQ_OFFSET; |
<> | 149:156823d33999 | 127 | } |
<> | 149:156823d33999 | 128 | |
<> | 149:156823d33999 | 129 | void InterruptManager::static_irq_helper() { |
<> | 149:156823d33999 | 130 | InterruptManager::get()->irq_helper(); |
<> | 149:156823d33999 | 131 | } |
<> | 149:156823d33999 | 132 | |
<> | 149:156823d33999 | 133 | void InterruptManager::lock() { |
<> | 149:156823d33999 | 134 | _mutex.lock(); |
<> | 149:156823d33999 | 135 | } |
<> | 149:156823d33999 | 136 | |
<> | 149:156823d33999 | 137 | void InterruptManager::unlock() { |
<> | 149:156823d33999 | 138 | _mutex.unlock(); |
<> | 149:156823d33999 | 139 | } |
<> | 149:156823d33999 | 140 | |
<> | 149:156823d33999 | 141 | } // namespace mbed |
<> | 149:156823d33999 | 142 | |
<> | 149:156823d33999 | 143 | #endif |