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