Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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