Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

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