inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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