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_PWMOUT_H
boonshen 0:a35c40f49345 17 #define MBED_PWMOUT_H
boonshen 0:a35c40f49345 18
boonshen 0:a35c40f49345 19 #include "platform/platform.h"
boonshen 0:a35c40f49345 20
boonshen 0:a35c40f49345 21 #if defined (DEVICE_PWMOUT) || defined(DOXYGEN_ONLY)
boonshen 0:a35c40f49345 22 #include "hal/pwmout_api.h"
boonshen 0:a35c40f49345 23 #include "platform/mbed_critical.h"
boonshen 0:a35c40f49345 24 #include "platform/mbed_sleep.h"
boonshen 0:a35c40f49345 25
boonshen 0:a35c40f49345 26 namespace mbed {
boonshen 0:a35c40f49345 27 /** \addtogroup drivers */
boonshen 0:a35c40f49345 28
boonshen 0:a35c40f49345 29 /** A pulse-width modulation digital output
boonshen 0:a35c40f49345 30 *
boonshen 0:a35c40f49345 31 * @note Synchronization level: Interrupt safe
boonshen 0:a35c40f49345 32 *
boonshen 0:a35c40f49345 33 * Example
boonshen 0:a35c40f49345 34 * @code
boonshen 0:a35c40f49345 35 * // Fade a led on.
boonshen 0:a35c40f49345 36 * #include "mbed.h"
boonshen 0:a35c40f49345 37 *
boonshen 0:a35c40f49345 38 * PwmOut led(LED1);
boonshen 0:a35c40f49345 39 *
boonshen 0:a35c40f49345 40 * int main() {
boonshen 0:a35c40f49345 41 * while(1) {
boonshen 0:a35c40f49345 42 * led = led + 0.01;
boonshen 0:a35c40f49345 43 * wait(0.2);
boonshen 0:a35c40f49345 44 * if(led == 1.0) {
boonshen 0:a35c40f49345 45 * led = 0;
boonshen 0:a35c40f49345 46 * }
boonshen 0:a35c40f49345 47 * }
boonshen 0:a35c40f49345 48 * }
boonshen 0:a35c40f49345 49 * @endcode
boonshen 0:a35c40f49345 50 * @ingroup drivers
boonshen 0:a35c40f49345 51 */
boonshen 0:a35c40f49345 52 class PwmOut {
boonshen 0:a35c40f49345 53
boonshen 0:a35c40f49345 54 public:
boonshen 0:a35c40f49345 55
boonshen 0:a35c40f49345 56 /** Create a PwmOut connected to the specified pin
boonshen 0:a35c40f49345 57 *
boonshen 0:a35c40f49345 58 * @param pin PwmOut pin to connect to
boonshen 0:a35c40f49345 59 */
boonshen 0:a35c40f49345 60 PwmOut(PinName pin) : _deep_sleep_locked(false) {
boonshen 0:a35c40f49345 61 core_util_critical_section_enter();
boonshen 0:a35c40f49345 62 pwmout_init(&_pwm, pin);
boonshen 0:a35c40f49345 63 core_util_critical_section_exit();
boonshen 0:a35c40f49345 64 }
boonshen 0:a35c40f49345 65
boonshen 0:a35c40f49345 66 ~PwmOut() {
boonshen 0:a35c40f49345 67 core_util_critical_section_enter();
boonshen 0:a35c40f49345 68 unlock_deep_sleep();
boonshen 0:a35c40f49345 69 core_util_critical_section_exit();
boonshen 0:a35c40f49345 70 }
boonshen 0:a35c40f49345 71
boonshen 0:a35c40f49345 72 /** Set the ouput duty-cycle, specified as a percentage (float)
boonshen 0:a35c40f49345 73 *
boonshen 0:a35c40f49345 74 * @param value A floating-point value representing the output duty-cycle,
boonshen 0:a35c40f49345 75 * specified as a percentage. The value should lie between
boonshen 0:a35c40f49345 76 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
boonshen 0:a35c40f49345 77 * Values outside this range will be saturated to 0.0f or 1.0f.
boonshen 0:a35c40f49345 78 */
boonshen 0:a35c40f49345 79 void write(float value) {
boonshen 0:a35c40f49345 80 core_util_critical_section_enter();
boonshen 0:a35c40f49345 81 lock_deep_sleep();
boonshen 0:a35c40f49345 82 pwmout_write(&_pwm, value);
boonshen 0:a35c40f49345 83 core_util_critical_section_exit();
boonshen 0:a35c40f49345 84 }
boonshen 0:a35c40f49345 85
boonshen 0:a35c40f49345 86 /** Return the current output duty-cycle setting, measured as a percentage (float)
boonshen 0:a35c40f49345 87 *
boonshen 0:a35c40f49345 88 * @returns
boonshen 0:a35c40f49345 89 * A floating-point value representing the current duty-cycle being output on the pin,
boonshen 0:a35c40f49345 90 * measured as a percentage. The returned value will lie between
boonshen 0:a35c40f49345 91 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
boonshen 0:a35c40f49345 92 *
boonshen 0:a35c40f49345 93 * @note
boonshen 0:a35c40f49345 94 * This value may not match exactly the value set by a previous write().
boonshen 0:a35c40f49345 95 */
boonshen 0:a35c40f49345 96 float read() {
boonshen 0:a35c40f49345 97 core_util_critical_section_enter();
boonshen 0:a35c40f49345 98 float val = pwmout_read(&_pwm);
boonshen 0:a35c40f49345 99 core_util_critical_section_exit();
boonshen 0:a35c40f49345 100 return val;
boonshen 0:a35c40f49345 101 }
boonshen 0:a35c40f49345 102
boonshen 0:a35c40f49345 103 /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
boonshen 0:a35c40f49345 104 *
boonshen 0:a35c40f49345 105 * @param seconds Change the period of a PWM signal in seconds (float) without modifying the duty cycle
boonshen 0:a35c40f49345 106 * @note
boonshen 0:a35c40f49345 107 * The resolution is currently in microseconds; periods smaller than this
boonshen 0:a35c40f49345 108 * will be set to zero.
boonshen 0:a35c40f49345 109 */
boonshen 0:a35c40f49345 110 void period(float seconds) {
boonshen 0:a35c40f49345 111 core_util_critical_section_enter();
boonshen 0:a35c40f49345 112 pwmout_period(&_pwm, seconds);
boonshen 0:a35c40f49345 113 core_util_critical_section_exit();
boonshen 0:a35c40f49345 114 }
boonshen 0:a35c40f49345 115
boonshen 0:a35c40f49345 116 /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
boonshen 0:a35c40f49345 117 * @param ms Change the period of a PWM signal in milli-seconds without modifying the duty cycle
boonshen 0:a35c40f49345 118 */
boonshen 0:a35c40f49345 119 void period_ms(int ms) {
boonshen 0:a35c40f49345 120 core_util_critical_section_enter();
boonshen 0:a35c40f49345 121 pwmout_period_ms(&_pwm, ms);
boonshen 0:a35c40f49345 122 core_util_critical_section_exit();
boonshen 0:a35c40f49345 123 }
boonshen 0:a35c40f49345 124
boonshen 0:a35c40f49345 125 /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
boonshen 0:a35c40f49345 126 * @param us Change the period of a PWM signal in micro-seconds without modifying the duty cycle
boonshen 0:a35c40f49345 127 */
boonshen 0:a35c40f49345 128 void period_us(int us) {
boonshen 0:a35c40f49345 129 core_util_critical_section_enter();
boonshen 0:a35c40f49345 130 pwmout_period_us(&_pwm, us);
boonshen 0:a35c40f49345 131 core_util_critical_section_exit();
boonshen 0:a35c40f49345 132 }
boonshen 0:a35c40f49345 133
boonshen 0:a35c40f49345 134 /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
boonshen 0:a35c40f49345 135 * @param seconds Change the pulse width of a PWM signal specified in seconds (float)
boonshen 0:a35c40f49345 136 */
boonshen 0:a35c40f49345 137 void pulsewidth(float seconds) {
boonshen 0:a35c40f49345 138 core_util_critical_section_enter();
boonshen 0:a35c40f49345 139 pwmout_pulsewidth(&_pwm, seconds);
boonshen 0:a35c40f49345 140 core_util_critical_section_exit();
boonshen 0:a35c40f49345 141 }
boonshen 0:a35c40f49345 142
boonshen 0:a35c40f49345 143 /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
boonshen 0:a35c40f49345 144 * @param ms Change the pulse width of a PWM signal specified in milli-seconds
boonshen 0:a35c40f49345 145 */
boonshen 0:a35c40f49345 146 void pulsewidth_ms(int ms) {
boonshen 0:a35c40f49345 147 core_util_critical_section_enter();
boonshen 0:a35c40f49345 148 pwmout_pulsewidth_ms(&_pwm, ms);
boonshen 0:a35c40f49345 149 core_util_critical_section_exit();
boonshen 0:a35c40f49345 150 }
boonshen 0:a35c40f49345 151
boonshen 0:a35c40f49345 152 /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
boonshen 0:a35c40f49345 153 * @param us Change the pulse width of a PWM signal specified in micro-seconds
boonshen 0:a35c40f49345 154 */
boonshen 0:a35c40f49345 155 void pulsewidth_us(int us) {
boonshen 0:a35c40f49345 156 core_util_critical_section_enter();
boonshen 0:a35c40f49345 157 pwmout_pulsewidth_us(&_pwm, us);
boonshen 0:a35c40f49345 158 core_util_critical_section_exit();
boonshen 0:a35c40f49345 159 }
boonshen 0:a35c40f49345 160
boonshen 0:a35c40f49345 161 /** A operator shorthand for write()
boonshen 0:a35c40f49345 162 * \sa PwmOut::write()
boonshen 0:a35c40f49345 163 */
boonshen 0:a35c40f49345 164 PwmOut& operator= (float value) {
boonshen 0:a35c40f49345 165 // Underlying call is thread safe
boonshen 0:a35c40f49345 166 write(value);
boonshen 0:a35c40f49345 167 return *this;
boonshen 0:a35c40f49345 168 }
boonshen 0:a35c40f49345 169
boonshen 0:a35c40f49345 170 /** A operator shorthand for write()
boonshen 0:a35c40f49345 171 * \sa PwmOut::write()
boonshen 0:a35c40f49345 172 */
boonshen 0:a35c40f49345 173 PwmOut& operator= (PwmOut& rhs) {
boonshen 0:a35c40f49345 174 // Underlying call is thread safe
boonshen 0:a35c40f49345 175 write(rhs.read());
boonshen 0:a35c40f49345 176 return *this;
boonshen 0:a35c40f49345 177 }
boonshen 0:a35c40f49345 178
boonshen 0:a35c40f49345 179 /** An operator shorthand for read()
boonshen 0:a35c40f49345 180 * \sa PwmOut::read()
boonshen 0:a35c40f49345 181 */
boonshen 0:a35c40f49345 182 operator float() {
boonshen 0:a35c40f49345 183 // Underlying call is thread safe
boonshen 0:a35c40f49345 184 return read();
boonshen 0:a35c40f49345 185 }
boonshen 0:a35c40f49345 186
boonshen 0:a35c40f49345 187 protected:
boonshen 0:a35c40f49345 188 /** Lock deep sleep only if it is not yet locked */
boonshen 0:a35c40f49345 189 void lock_deep_sleep() {
boonshen 0:a35c40f49345 190 if (_deep_sleep_locked == false) {
boonshen 0:a35c40f49345 191 sleep_manager_lock_deep_sleep();
boonshen 0:a35c40f49345 192 _deep_sleep_locked = true;
boonshen 0:a35c40f49345 193 }
boonshen 0:a35c40f49345 194 }
boonshen 0:a35c40f49345 195
boonshen 0:a35c40f49345 196 /** Unlock deep sleep in case it is locked */
boonshen 0:a35c40f49345 197 void unlock_deep_sleep() {
boonshen 0:a35c40f49345 198 if (_deep_sleep_locked == true) {
boonshen 0:a35c40f49345 199 sleep_manager_unlock_deep_sleep();
boonshen 0:a35c40f49345 200 _deep_sleep_locked = false;
boonshen 0:a35c40f49345 201 }
boonshen 0:a35c40f49345 202 }
boonshen 0:a35c40f49345 203
boonshen 0:a35c40f49345 204 pwmout_t _pwm;
boonshen 0:a35c40f49345 205 bool _deep_sleep_locked;
boonshen 0:a35c40f49345 206 };
boonshen 0:a35c40f49345 207
boonshen 0:a35c40f49345 208 } // namespace mbed
boonshen 0:a35c40f49345 209
boonshen 0:a35c40f49345 210 #endif
boonshen 0:a35c40f49345 211
boonshen 0:a35c40f49345 212 #endif