mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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