Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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