Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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