1232

Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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