Opencv 3.1 project on GR-PEACH board

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

Committer:
thedo
Date:
Tue Jul 04 06:23:13 2017 +0000
Revision:
170:54ff26da7eb6
Parent:
167:1657b442184c
project opencv 3.1 on GR PEACH board, no use SD card.

Who changed what in which revision?

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