Forked.

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Fri Oct 28 11:17:30 2016 +0100
Revision:
149:156823d33999
Child:
160:d5399cc887bb
This updates the lib to the mbed lib v128

NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.

Who changed what in which revision?

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