Describes predefine macros for mbed online compiler (armcc)

Committer:
MACRUM
Date:
Thu Mar 16 21:58:09 2017 +0900
Revision:
6:40e873bbc5f7
Add licence header info

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 6:40e873bbc5f7 1 /* mbed Microcontroller Library
MACRUM 6:40e873bbc5f7 2 * Copyright (c) 2006-2013 ARM Limited
MACRUM 6:40e873bbc5f7 3 *
MACRUM 6:40e873bbc5f7 4 * Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 6:40e873bbc5f7 5 * you may not use this file except in compliance with the License.
MACRUM 6:40e873bbc5f7 6 * You may obtain a copy of the License at
MACRUM 6:40e873bbc5f7 7 *
MACRUM 6:40e873bbc5f7 8 * http://www.apache.org/licenses/LICENSE-2.0
MACRUM 6:40e873bbc5f7 9 *
MACRUM 6:40e873bbc5f7 10 * Unless required by applicable law or agreed to in writing, software
MACRUM 6:40e873bbc5f7 11 * distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 6:40e873bbc5f7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 6:40e873bbc5f7 13 * See the License for the specific language governing permissions and
MACRUM 6:40e873bbc5f7 14 * limitations under the License.
MACRUM 6:40e873bbc5f7 15 */
MACRUM 6:40e873bbc5f7 16 #ifndef MBED_INTERRUPTMANAGER_H
MACRUM 6:40e873bbc5f7 17 #define MBED_INTERRUPTMANAGER_H
MACRUM 6:40e873bbc5f7 18
MACRUM 6:40e873bbc5f7 19 #include "cmsis.h"
MACRUM 6:40e873bbc5f7 20 #include "platform/CallChain.h"
MACRUM 6:40e873bbc5f7 21 #include "platform/PlatformMutex.h"
MACRUM 6:40e873bbc5f7 22 #include <string.h>
MACRUM 6:40e873bbc5f7 23
MACRUM 6:40e873bbc5f7 24 namespace mbed {
MACRUM 6:40e873bbc5f7 25 /** \addtogroup drivers */
MACRUM 6:40e873bbc5f7 26 /** @{*/
MACRUM 6:40e873bbc5f7 27
MACRUM 6:40e873bbc5f7 28 /** Use this singleton if you need to chain interrupt handlers.
MACRUM 6:40e873bbc5f7 29 *
MACRUM 6:40e873bbc5f7 30 * @Note Synchronization level: Thread safe
MACRUM 6:40e873bbc5f7 31 *
MACRUM 6:40e873bbc5f7 32 * Example (for LPC1768):
MACRUM 6:40e873bbc5f7 33 * @code
MACRUM 6:40e873bbc5f7 34 * #include "InterruptManager.h"
MACRUM 6:40e873bbc5f7 35 * #include "mbed.h"
MACRUM 6:40e873bbc5f7 36 *
MACRUM 6:40e873bbc5f7 37 * Ticker flipper;
MACRUM 6:40e873bbc5f7 38 * DigitalOut led1(LED1);
MACRUM 6:40e873bbc5f7 39 * DigitalOut led2(LED2);
MACRUM 6:40e873bbc5f7 40 *
MACRUM 6:40e873bbc5f7 41 * void flip(void) {
MACRUM 6:40e873bbc5f7 42 * led1 = !led1;
MACRUM 6:40e873bbc5f7 43 * }
MACRUM 6:40e873bbc5f7 44 *
MACRUM 6:40e873bbc5f7 45 * void handler(void) {
MACRUM 6:40e873bbc5f7 46 * led2 = !led1;
MACRUM 6:40e873bbc5f7 47 * }
MACRUM 6:40e873bbc5f7 48 *
MACRUM 6:40e873bbc5f7 49 * int main() {
MACRUM 6:40e873bbc5f7 50 * led1 = led2 = 0;
MACRUM 6:40e873bbc5f7 51 * flipper.attach(&flip, 1.0);
MACRUM 6:40e873bbc5f7 52 * InterruptManager::get()->add_handler(handler, TIMER3_IRQn);
MACRUM 6:40e873bbc5f7 53 * }
MACRUM 6:40e873bbc5f7 54 * @endcode
MACRUM 6:40e873bbc5f7 55 */
MACRUM 6:40e873bbc5f7 56 class InterruptManager {
MACRUM 6:40e873bbc5f7 57 public:
MACRUM 6:40e873bbc5f7 58 /** Return the only instance of this class
MACRUM 6:40e873bbc5f7 59 */
MACRUM 6:40e873bbc5f7 60 static InterruptManager* get();
MACRUM 6:40e873bbc5f7 61
MACRUM 6:40e873bbc5f7 62 /** Destroy the current instance of the interrupt manager
MACRUM 6:40e873bbc5f7 63 */
MACRUM 6:40e873bbc5f7 64 static void destroy();
MACRUM 6:40e873bbc5f7 65
MACRUM 6:40e873bbc5f7 66 /** Add a handler for an interrupt at the end of the handler list
MACRUM 6:40e873bbc5f7 67 *
MACRUM 6:40e873bbc5f7 68 * @param function the handler to add
MACRUM 6:40e873bbc5f7 69 * @param irq interrupt number
MACRUM 6:40e873bbc5f7 70 *
MACRUM 6:40e873bbc5f7 71 * @returns
MACRUM 6:40e873bbc5f7 72 * The function object created for 'function'
MACRUM 6:40e873bbc5f7 73 */
MACRUM 6:40e873bbc5f7 74 pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) {
MACRUM 6:40e873bbc5f7 75 // Underlying call is thread safe
MACRUM 6:40e873bbc5f7 76 return add_common(function, irq);
MACRUM 6:40e873bbc5f7 77 }
MACRUM 6:40e873bbc5f7 78
MACRUM 6:40e873bbc5f7 79 /** Add a handler for an interrupt at the beginning of the handler list
MACRUM 6:40e873bbc5f7 80 *
MACRUM 6:40e873bbc5f7 81 * @param function the handler to add
MACRUM 6:40e873bbc5f7 82 * @param irq interrupt number
MACRUM 6:40e873bbc5f7 83 *
MACRUM 6:40e873bbc5f7 84 * @returns
MACRUM 6:40e873bbc5f7 85 * The function object created for 'function'
MACRUM 6:40e873bbc5f7 86 */
MACRUM 6:40e873bbc5f7 87 pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) {
MACRUM 6:40e873bbc5f7 88 // Underlying call is thread safe
MACRUM 6:40e873bbc5f7 89 return add_common(function, irq, true);
MACRUM 6:40e873bbc5f7 90 }
MACRUM 6:40e873bbc5f7 91
MACRUM 6:40e873bbc5f7 92 /** Add a handler for an interrupt at the end of the handler list
MACRUM 6:40e873bbc5f7 93 *
MACRUM 6:40e873bbc5f7 94 * @param tptr pointer to the object that has the handler function
MACRUM 6:40e873bbc5f7 95 * @param mptr pointer to the actual handler function
MACRUM 6:40e873bbc5f7 96 * @param irq interrupt number
MACRUM 6:40e873bbc5f7 97 *
MACRUM 6:40e873bbc5f7 98 * @returns
MACRUM 6:40e873bbc5f7 99 * The function object created for 'tptr' and 'mptr'
MACRUM 6:40e873bbc5f7 100 */
MACRUM 6:40e873bbc5f7 101 template<typename T>
MACRUM 6:40e873bbc5f7 102 pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
MACRUM 6:40e873bbc5f7 103 // Underlying call is thread safe
MACRUM 6:40e873bbc5f7 104 return add_common(tptr, mptr, irq);
MACRUM 6:40e873bbc5f7 105 }
MACRUM 6:40e873bbc5f7 106
MACRUM 6:40e873bbc5f7 107 /** Add a handler for an interrupt at the beginning of the handler list
MACRUM 6:40e873bbc5f7 108 *
MACRUM 6:40e873bbc5f7 109 * @param tptr pointer to the object that has the handler function
MACRUM 6:40e873bbc5f7 110 * @param mptr pointer to the actual handler function
MACRUM 6:40e873bbc5f7 111 * @param irq interrupt number
MACRUM 6:40e873bbc5f7 112 *
MACRUM 6:40e873bbc5f7 113 * @returns
MACRUM 6:40e873bbc5f7 114 * The function object created for 'tptr' and 'mptr'
MACRUM 6:40e873bbc5f7 115 */
MACRUM 6:40e873bbc5f7 116 template<typename T>
MACRUM 6:40e873bbc5f7 117 pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
MACRUM 6:40e873bbc5f7 118 // Underlying call is thread safe
MACRUM 6:40e873bbc5f7 119 return add_common(tptr, mptr, irq, true);
MACRUM 6:40e873bbc5f7 120 }
MACRUM 6:40e873bbc5f7 121
MACRUM 6:40e873bbc5f7 122 /** Remove a handler from an interrupt
MACRUM 6:40e873bbc5f7 123 *
MACRUM 6:40e873bbc5f7 124 * @param handler the function object for the handler to remove
MACRUM 6:40e873bbc5f7 125 * @param irq the interrupt number
MACRUM 6:40e873bbc5f7 126 *
MACRUM 6:40e873bbc5f7 127 * @returns
MACRUM 6:40e873bbc5f7 128 * true if the handler was found and removed, false otherwise
MACRUM 6:40e873bbc5f7 129 */
MACRUM 6:40e873bbc5f7 130 bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
MACRUM 6:40e873bbc5f7 131
MACRUM 6:40e873bbc5f7 132 private:
MACRUM 6:40e873bbc5f7 133 InterruptManager();
MACRUM 6:40e873bbc5f7 134 ~InterruptManager();
MACRUM 6:40e873bbc5f7 135
MACRUM 6:40e873bbc5f7 136 void lock();
MACRUM 6:40e873bbc5f7 137 void unlock();
MACRUM 6:40e873bbc5f7 138
MACRUM 6:40e873bbc5f7 139 // We declare the copy contructor and the assignment operator, but we don't
MACRUM 6:40e873bbc5f7 140 // implement them. This way, if someone tries to copy/assign our instance,
MACRUM 6:40e873bbc5f7 141 // he will get an error at compile time.
MACRUM 6:40e873bbc5f7 142 InterruptManager(const InterruptManager&);
MACRUM 6:40e873bbc5f7 143 InterruptManager& operator =(const InterruptManager&);
MACRUM 6:40e873bbc5f7 144
MACRUM 6:40e873bbc5f7 145 template<typename T>
MACRUM 6:40e873bbc5f7 146 pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) {
MACRUM 6:40e873bbc5f7 147 _mutex.lock();
MACRUM 6:40e873bbc5f7 148 int irq_pos = get_irq_index(irq);
MACRUM 6:40e873bbc5f7 149 bool change = must_replace_vector(irq);
MACRUM 6:40e873bbc5f7 150
MACRUM 6:40e873bbc5f7 151 pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr);
MACRUM 6:40e873bbc5f7 152 if (change)
MACRUM 6:40e873bbc5f7 153 NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
MACRUM 6:40e873bbc5f7 154 _mutex.unlock();
MACRUM 6:40e873bbc5f7 155 return pf;
MACRUM 6:40e873bbc5f7 156 }
MACRUM 6:40e873bbc5f7 157
MACRUM 6:40e873bbc5f7 158 pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false);
MACRUM 6:40e873bbc5f7 159 bool must_replace_vector(IRQn_Type irq);
MACRUM 6:40e873bbc5f7 160 int get_irq_index(IRQn_Type irq);
MACRUM 6:40e873bbc5f7 161 void irq_helper();
MACRUM 6:40e873bbc5f7 162 void add_helper(void (*function)(void), IRQn_Type irq, bool front=false);
MACRUM 6:40e873bbc5f7 163 static void static_irq_helper();
MACRUM 6:40e873bbc5f7 164
MACRUM 6:40e873bbc5f7 165 CallChain* _chains[NVIC_NUM_VECTORS];
MACRUM 6:40e873bbc5f7 166 static InterruptManager* _instance;
MACRUM 6:40e873bbc5f7 167 PlatformMutex _mutex;
MACRUM 6:40e873bbc5f7 168 };
MACRUM 6:40e873bbc5f7 169
MACRUM 6:40e873bbc5f7 170 } // namespace mbed
MACRUM 6:40e873bbc5f7 171
MACRUM 6:40e873bbc5f7 172 #endif
MACRUM 6:40e873bbc5f7 173
MACRUM 6:40e873bbc5f7 174
MACRUM 6:40e873bbc5f7 175 /** @}*/