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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.