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