00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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