Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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