Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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