Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

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