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