inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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