mbed library sources. Supersedes mbed-src.

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PwmOut.h Source File

PwmOut.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef MBED_PWMOUT_H
00018 #define MBED_PWMOUT_H
00019 
00020 #include "platform/platform.h"
00021 
00022 #if DEVICE_PWMOUT || defined(DOXYGEN_ONLY)
00023 #include "hal/pwmout_api.h"
00024 #include "platform/mbed_critical.h"
00025 #include "platform/mbed_power_mgmt.h"
00026 
00027 namespace mbed {
00028 /** \addtogroup drivers */
00029 
00030 /** A pulse-width modulation digital output
00031  *
00032  * @note Synchronization level: Interrupt safe
00033  *
00034  * Example
00035  * @code
00036  * // Gradually change the intensity of the LED.
00037  * #include "mbed.h"
00038  *
00039  * PwmOut led(LED1);
00040  *
00041  * int main() {
00042  *     while(1) {
00043  *         led = led + 0.01;
00044  *         wait(0.2);
00045  *         if(led == 1.0) {
00046  *             led = 0;
00047  *         }
00048  *     }
00049  * }
00050  * @endcode
00051  * @ingroup drivers
00052  */
00053 class PwmOut {
00054 
00055 public:
00056 
00057     /** Create a PwmOut connected to the specified pin
00058      *
00059      *  @param pin PwmOut pin to connect to
00060      */
00061     PwmOut(PinName pin) : _deep_sleep_locked(false)
00062     {
00063         core_util_critical_section_enter();
00064         pwmout_init(&_pwm, pin);
00065         core_util_critical_section_exit();
00066     }
00067 
00068     ~PwmOut()
00069     {
00070         core_util_critical_section_enter();
00071         unlock_deep_sleep();
00072         core_util_critical_section_exit();
00073     }
00074 
00075     /** Set the output duty-cycle, specified as a percentage (float)
00076      *
00077      *  @param value A floating-point value representing the output duty-cycle,
00078      *    specified as a percentage. The value should lie between
00079      *    0.0f (representing on 0%) and 1.0f (representing on 100%).
00080      *    Values outside this range will be saturated to 0.0f or 1.0f.
00081      */
00082     void write(float value)
00083     {
00084         core_util_critical_section_enter();
00085         lock_deep_sleep();
00086         pwmout_write(&_pwm, value);
00087         core_util_critical_section_exit();
00088     }
00089 
00090     /** Return the current output duty-cycle setting, measured as a percentage (float)
00091      *
00092      *  @returns
00093      *    A floating-point value representing the current duty-cycle being output on the pin,
00094      *    measured as a percentage. The returned value will lie between
00095      *    0.0f (representing on 0%) and 1.0f (representing on 100%).
00096      *
00097      *  @note
00098      *  This value may not match exactly the value set by a previous write().
00099      */
00100     float read()
00101     {
00102         core_util_critical_section_enter();
00103         float val = pwmout_read(&_pwm);
00104         core_util_critical_section_exit();
00105         return val;
00106     }
00107 
00108     /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
00109      *
00110      *  @param seconds Change the period of a PWM signal in seconds (float) without modifying the duty cycle
00111      *  @note
00112      *   The resolution is currently in microseconds; periods smaller than this
00113      *   will be set to zero.
00114      */
00115     void period(float seconds)
00116     {
00117         core_util_critical_section_enter();
00118         pwmout_period(&_pwm, seconds);
00119         core_util_critical_section_exit();
00120     }
00121 
00122     /** Set the PWM period, specified in milliseconds (int), keeping the duty cycle the same.
00123      *  @param ms Change the period of a PWM signal in milliseconds without modifying the duty cycle
00124      */
00125     void period_ms(int ms)
00126     {
00127         core_util_critical_section_enter();
00128         pwmout_period_ms(&_pwm, ms);
00129         core_util_critical_section_exit();
00130     }
00131 
00132     /** Set the PWM period, specified in microseconds (int), keeping the duty cycle the same.
00133      *  @param us Change the period of a PWM signal in microseconds without modifying the duty cycle
00134      */
00135     void period_us(int us)
00136     {
00137         core_util_critical_section_enter();
00138         pwmout_period_us(&_pwm, us);
00139         core_util_critical_section_exit();
00140     }
00141 
00142     /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
00143      *  @param seconds Change the pulse width of a PWM signal specified in seconds (float)
00144      */
00145     void pulsewidth(float seconds)
00146     {
00147         core_util_critical_section_enter();
00148         pwmout_pulsewidth(&_pwm, seconds);
00149         core_util_critical_section_exit();
00150     }
00151 
00152     /** Set the PWM pulsewidth, specified in milliseconds (int), keeping the period the same.
00153      *  @param ms Change the pulse width of a PWM signal specified in milliseconds
00154      */
00155     void pulsewidth_ms(int ms)
00156     {
00157         core_util_critical_section_enter();
00158         pwmout_pulsewidth_ms(&_pwm, ms);
00159         core_util_critical_section_exit();
00160     }
00161 
00162     /** Set the PWM pulsewidth, specified in microseconds (int), keeping the period the same.
00163      *  @param us Change the pulse width of a PWM signal specified in microseconds
00164      */
00165     void pulsewidth_us(int us)
00166     {
00167         core_util_critical_section_enter();
00168         pwmout_pulsewidth_us(&_pwm, us);
00169         core_util_critical_section_exit();
00170     }
00171 
00172     /** A operator shorthand for write()
00173      *  \sa PwmOut::write()
00174      */
00175     PwmOut &operator= (float value)
00176     {
00177         // Underlying call is thread safe
00178         write(value);
00179         return *this;
00180     }
00181 
00182     /** A operator shorthand for write()
00183      * \sa PwmOut::write()
00184      */
00185     PwmOut &operator= (PwmOut &rhs)
00186     {
00187         // Underlying call is thread safe
00188         write(rhs.read());
00189         return *this;
00190     }
00191 
00192     /** An operator shorthand for read()
00193      * \sa PwmOut::read()
00194      */
00195     operator float()
00196     {
00197         // Underlying call is thread safe
00198         return read();
00199     }
00200 
00201 #if !(DOXYGEN_ONLY)
00202 protected:
00203     /** Lock deep sleep only if it is not yet locked */
00204     void lock_deep_sleep()
00205     {
00206         if (_deep_sleep_locked == false) {
00207             sleep_manager_lock_deep_sleep();
00208             _deep_sleep_locked = true;
00209         }
00210     }
00211 
00212     /** Unlock deep sleep in case it is locked */
00213     void unlock_deep_sleep()
00214     {
00215         if (_deep_sleep_locked == true) {
00216             sleep_manager_unlock_deep_sleep();
00217             _deep_sleep_locked = false;
00218         }
00219     }
00220 
00221     pwmout_t _pwm;
00222     bool _deep_sleep_locked;
00223 #endif
00224 };
00225 
00226 } // namespace mbed
00227 
00228 #endif
00229 
00230 #endif