mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
mbed library release version 165

Who changed what in which revision?

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