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