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