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 #ifndef MBED_INTERRUPTMANAGER_H
switches 0:5c4d7b2438d3 17 #define MBED_INTERRUPTMANAGER_H
switches 0:5c4d7b2438d3 18
switches 0:5c4d7b2438d3 19 #include "cmsis.h"
switches 0:5c4d7b2438d3 20 #include "platform/CallChain.h"
switches 0:5c4d7b2438d3 21 #include "platform/PlatformMutex.h"
switches 0:5c4d7b2438d3 22 #include <string.h>
switches 0:5c4d7b2438d3 23
switches 0:5c4d7b2438d3 24 namespace mbed {
switches 0:5c4d7b2438d3 25 /** \addtogroup drivers */
switches 0:5c4d7b2438d3 26 /** @{*/
switches 0:5c4d7b2438d3 27
switches 0:5c4d7b2438d3 28 /** Use this singleton if you need to chain interrupt handlers.
switches 0:5c4d7b2438d3 29 *
switches 0:5c4d7b2438d3 30 * @Note Synchronization level: Thread safe
switches 0:5c4d7b2438d3 31 *
switches 0:5c4d7b2438d3 32 * Example (for LPC1768):
switches 0:5c4d7b2438d3 33 * @code
switches 0:5c4d7b2438d3 34 * #include "InterruptManager.h"
switches 0:5c4d7b2438d3 35 * #include "mbed.h"
switches 0:5c4d7b2438d3 36 *
switches 0:5c4d7b2438d3 37 * Ticker flipper;
switches 0:5c4d7b2438d3 38 * DigitalOut led1(LED1);
switches 0:5c4d7b2438d3 39 * DigitalOut led2(LED2);
switches 0:5c4d7b2438d3 40 *
switches 0:5c4d7b2438d3 41 * void flip(void) {
switches 0:5c4d7b2438d3 42 * led1 = !led1;
switches 0:5c4d7b2438d3 43 * }
switches 0:5c4d7b2438d3 44 *
switches 0:5c4d7b2438d3 45 * void handler(void) {
switches 0:5c4d7b2438d3 46 * led2 = !led1;
switches 0:5c4d7b2438d3 47 * }
switches 0:5c4d7b2438d3 48 *
switches 0:5c4d7b2438d3 49 * int main() {
switches 0:5c4d7b2438d3 50 * led1 = led2 = 0;
switches 0:5c4d7b2438d3 51 * flipper.attach(&flip, 1.0);
switches 0:5c4d7b2438d3 52 * InterruptManager::get()->add_handler(handler, TIMER3_IRQn);
switches 0:5c4d7b2438d3 53 * }
switches 0:5c4d7b2438d3 54 * @endcode
switches 0:5c4d7b2438d3 55 */
switches 0:5c4d7b2438d3 56 class InterruptManager {
switches 0:5c4d7b2438d3 57 public:
switches 0:5c4d7b2438d3 58 /** Return the only instance of this class
switches 0:5c4d7b2438d3 59 */
switches 0:5c4d7b2438d3 60 static InterruptManager* get();
switches 0:5c4d7b2438d3 61
switches 0:5c4d7b2438d3 62 /** Destroy the current instance of the interrupt manager
switches 0:5c4d7b2438d3 63 */
switches 0:5c4d7b2438d3 64 static void destroy();
switches 0:5c4d7b2438d3 65
switches 0:5c4d7b2438d3 66 /** Add a handler for an interrupt at the end of the handler list
switches 0:5c4d7b2438d3 67 *
switches 0:5c4d7b2438d3 68 * @param function the handler to add
switches 0:5c4d7b2438d3 69 * @param irq interrupt number
switches 0:5c4d7b2438d3 70 *
switches 0:5c4d7b2438d3 71 * @returns
switches 0:5c4d7b2438d3 72 * The function object created for 'function'
switches 0:5c4d7b2438d3 73 */
switches 0:5c4d7b2438d3 74 pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) {
switches 0:5c4d7b2438d3 75 // Underlying call is thread safe
switches 0:5c4d7b2438d3 76 return add_common(function, irq);
switches 0:5c4d7b2438d3 77 }
switches 0:5c4d7b2438d3 78
switches 0:5c4d7b2438d3 79 /** Add a handler for an interrupt at the beginning of the handler list
switches 0:5c4d7b2438d3 80 *
switches 0:5c4d7b2438d3 81 * @param function the handler to add
switches 0:5c4d7b2438d3 82 * @param irq interrupt number
switches 0:5c4d7b2438d3 83 *
switches 0:5c4d7b2438d3 84 * @returns
switches 0:5c4d7b2438d3 85 * The function object created for 'function'
switches 0:5c4d7b2438d3 86 */
switches 0:5c4d7b2438d3 87 pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) {
switches 0:5c4d7b2438d3 88 // Underlying call is thread safe
switches 0:5c4d7b2438d3 89 return add_common(function, irq, true);
switches 0:5c4d7b2438d3 90 }
switches 0:5c4d7b2438d3 91
switches 0:5c4d7b2438d3 92 /** Add a handler for an interrupt at the end of the handler list
switches 0:5c4d7b2438d3 93 *
switches 0:5c4d7b2438d3 94 * @param tptr pointer to the object that has the handler function
switches 0:5c4d7b2438d3 95 * @param mptr pointer to the actual handler function
switches 0:5c4d7b2438d3 96 * @param irq interrupt number
switches 0:5c4d7b2438d3 97 *
switches 0:5c4d7b2438d3 98 * @returns
switches 0:5c4d7b2438d3 99 * The function object created for 'tptr' and 'mptr'
switches 0:5c4d7b2438d3 100 */
switches 0:5c4d7b2438d3 101 template<typename T>
switches 0:5c4d7b2438d3 102 pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
switches 0:5c4d7b2438d3 103 // Underlying call is thread safe
switches 0:5c4d7b2438d3 104 return add_common(tptr, mptr, irq);
switches 0:5c4d7b2438d3 105 }
switches 0:5c4d7b2438d3 106
switches 0:5c4d7b2438d3 107 /** Add a handler for an interrupt at the beginning of the handler list
switches 0:5c4d7b2438d3 108 *
switches 0:5c4d7b2438d3 109 * @param tptr pointer to the object that has the handler function
switches 0:5c4d7b2438d3 110 * @param mptr pointer to the actual handler function
switches 0:5c4d7b2438d3 111 * @param irq interrupt number
switches 0:5c4d7b2438d3 112 *
switches 0:5c4d7b2438d3 113 * @returns
switches 0:5c4d7b2438d3 114 * The function object created for 'tptr' and 'mptr'
switches 0:5c4d7b2438d3 115 */
switches 0:5c4d7b2438d3 116 template<typename T>
switches 0:5c4d7b2438d3 117 pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
switches 0:5c4d7b2438d3 118 // Underlying call is thread safe
switches 0:5c4d7b2438d3 119 return add_common(tptr, mptr, irq, true);
switches 0:5c4d7b2438d3 120 }
switches 0:5c4d7b2438d3 121
switches 0:5c4d7b2438d3 122 /** Remove a handler from an interrupt
switches 0:5c4d7b2438d3 123 *
switches 0:5c4d7b2438d3 124 * @param handler the function object for the handler to remove
switches 0:5c4d7b2438d3 125 * @param irq the interrupt number
switches 0:5c4d7b2438d3 126 *
switches 0:5c4d7b2438d3 127 * @returns
switches 0:5c4d7b2438d3 128 * true if the handler was found and removed, false otherwise
switches 0:5c4d7b2438d3 129 */
switches 0:5c4d7b2438d3 130 bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
switches 0:5c4d7b2438d3 131
switches 0:5c4d7b2438d3 132 private:
switches 0:5c4d7b2438d3 133 InterruptManager();
switches 0:5c4d7b2438d3 134 ~InterruptManager();
switches 0:5c4d7b2438d3 135
switches 0:5c4d7b2438d3 136 void lock();
switches 0:5c4d7b2438d3 137 void unlock();
switches 0:5c4d7b2438d3 138
switches 0:5c4d7b2438d3 139 // We declare the copy contructor and the assignment operator, but we don't
switches 0:5c4d7b2438d3 140 // implement them. This way, if someone tries to copy/assign our instance,
switches 0:5c4d7b2438d3 141 // he will get an error at compile time.
switches 0:5c4d7b2438d3 142 InterruptManager(const InterruptManager&);
switches 0:5c4d7b2438d3 143 InterruptManager& operator =(const InterruptManager&);
switches 0:5c4d7b2438d3 144
switches 0:5c4d7b2438d3 145 template<typename T>
switches 0:5c4d7b2438d3 146 pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) {
switches 0:5c4d7b2438d3 147 _mutex.lock();
switches 0:5c4d7b2438d3 148 int irq_pos = get_irq_index(irq);
switches 0:5c4d7b2438d3 149 bool change = must_replace_vector(irq);
switches 0:5c4d7b2438d3 150
switches 0:5c4d7b2438d3 151 pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr);
switches 0:5c4d7b2438d3 152 if (change)
switches 0:5c4d7b2438d3 153 NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
switches 0:5c4d7b2438d3 154 _mutex.unlock();
switches 0:5c4d7b2438d3 155 return pf;
switches 0:5c4d7b2438d3 156 }
switches 0:5c4d7b2438d3 157
switches 0:5c4d7b2438d3 158 pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false);
switches 0:5c4d7b2438d3 159 bool must_replace_vector(IRQn_Type irq);
switches 0:5c4d7b2438d3 160 int get_irq_index(IRQn_Type irq);
switches 0:5c4d7b2438d3 161 void irq_helper();
switches 0:5c4d7b2438d3 162 void add_helper(void (*function)(void), IRQn_Type irq, bool front=false);
switches 0:5c4d7b2438d3 163 static void static_irq_helper();
switches 0:5c4d7b2438d3 164
switches 0:5c4d7b2438d3 165 CallChain* _chains[NVIC_NUM_VECTORS];
switches 0:5c4d7b2438d3 166 static InterruptManager* _instance;
switches 0:5c4d7b2438d3 167 PlatformMutex _mutex;
switches 0:5c4d7b2438d3 168 };
switches 0:5c4d7b2438d3 169
switches 0:5c4d7b2438d3 170 } // namespace mbed
switches 0:5c4d7b2438d3 171
switches 0:5c4d7b2438d3 172 #endif
switches 0:5c4d7b2438d3 173
switches 0:5c4d7b2438d3 174
switches 0:5c4d7b2438d3 175 /** @}*/