SDCard version

Fork of gr-peach-opencv-project-sd-card by the do

Committer:
thedo
Date:
Fri Jul 21 01:26:54 2017 +0000
Revision:
167:2ee3e82cb6f5
gr-peach-opencv-project-sd-card

Who changed what in which revision?

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