TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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