max32630fthr quad spi , unexpected spi behavior

Committer:
boonshen
Date:
Tue Mar 13 21:12:00 2018 +0000
Revision:
0:a35c40f49345
MAX32630FTHR QuadSPI test

Who changed what in which revision?

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