Mistake on this page?
Report an issue in GitHub or email us
PwmOut.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2019 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 
25 namespace mbed {
26 /**
27  * \defgroup drivers_PwmOut PwmOut class
28  * \ingroup drivers-public-api-gpio
29  * @{
30  */
31 
32 /** A pulse-width modulation digital output
33  *
34  * @note Synchronization level: Interrupt safe
35  *
36  * Example
37  * @code
38  * // Gradually change the intensity of the LED.
39  * #include "mbed.h"
40  *
41  * PwmOut led(LED1);
42  *
43  * int main() {
44  * while(1) {
45  * led = led + 0.01;
46  * ThisThread::sleep_for(200);
47  * if(led == 1.0) {
48  * led = 0;
49  * }
50  * }
51  * }
52  * @endcode
53  */
54 class PwmOut {
55 
56 public:
57 
58  /** Create a PwmOut connected to the specified pin
59  *
60  * @param pin PwmOut pin to connect to
61  */
62  PwmOut(PinName pin);
63 
64  /** Create a PwmOut connected to the specified pin
65  *
66  * @param pinmap reference to structure which holds static pinmap.
67  */
68  PwmOut(const PinMap &pinmap);
69  PwmOut(const PinMap &&) = delete; // prevent passing of temporary objects
70 
71  ~PwmOut();
72 
73  /** Set the output duty-cycle, specified as a percentage (float)
74  *
75  * @param value A floating-point value representing the output duty-cycle,
76  * specified as a percentage. The value should lie between
77  * 0.0f (representing on 0%) and 1.0f (representing on 100%).
78  * Values outside this range will be saturated to 0.0f or 1.0f.
79  */
80  void write(float value);
81 
82  /** Return the current output duty-cycle setting, measured as a percentage (float)
83  *
84  * @returns
85  * A floating-point value representing the current duty-cycle being output on the pin,
86  * measured as a percentage. The returned value will lie between
87  * 0.0f (representing on 0%) and 1.0f (representing on 100%).
88  *
89  * @note
90  * This value may not match exactly the value set by a previous write().
91  */
92  float read();
93 
94  /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
95  *
96  * @param seconds Change the period of a PWM signal in seconds (float) without modifying the duty cycle
97  * @note
98  * The resolution is currently in microseconds; periods smaller than this
99  * will be set to zero.
100  */
101  void period(float seconds);
102 
103  /** Set the PWM period, specified in milliseconds (int), keeping the duty cycle the same.
104  * @param ms Change the period of a PWM signal in milliseconds without modifying the duty cycle
105  */
106  void period_ms(int ms);
107 
108  /** Set the PWM period, specified in microseconds (int), keeping the duty cycle the same.
109  * @param us Change the period of a PWM signal in microseconds without modifying the duty cycle
110  */
111  void period_us(int us);
112 
113  /** Read the PWM period
114  * @returns
115  * The PWM period, specified in microseconds (int)
116  */
117  int read_period_us();
118 
119  /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
120  * @param seconds Change the pulse width of a PWM signal specified in seconds (float)
121  */
122  void pulsewidth(float seconds);
123 
124  /** Set the PWM pulsewidth, specified in milliseconds (int), keeping the period the same.
125  * @param ms Change the pulse width of a PWM signal specified in milliseconds
126  */
127  void pulsewidth_ms(int ms);
128 
129  /** Set the PWM pulsewidth, specified in microseconds (int), keeping the period the same.
130  * @param us Change the pulse width of a PWM signal specified in microseconds
131  */
132  void pulsewidth_us(int us);
133 
134  /** Read the PWM pulsewidth
135  * @returns
136  * The PWM pulsewith, specified in microseconds (int)
137  */
138  int read_pulsewitdth_us();
139 
140  /** Suspend PWM operation
141  *
142  * Control the PWM state. This is primarily intended
143  * for temporary power-saving; This call can
144  * allow pwm to be temporarily disabled to permit power saving without
145  * losing device state. The subsequent function call must be PwmOut::resume
146  * for PWM to resume; any other calls prior to resuming are undefined behavior.
147  */
148  void suspend();
149 
150  /** Resume PWM operation
151  *
152  * Control the PWM state. This is primarily intended
153  * to resume PWM operations after a previous PwmOut::suspend call;
154  * This call restores the device state prior to suspension.
155  */
156  void resume();
157 
158  /** A operator shorthand for write()
159  * \sa PwmOut::write()
160  */
161  PwmOut &operator= (float value)
162  {
163  // Underlying call is thread safe
164  write(value);
165  return *this;
166  }
167 
168  /** A operator shorthand for write()
169  * \sa PwmOut::write()
170  */
172  {
173  // Underlying call is thread safe
174  write(rhs.read());
175  return *this;
176  }
177 
178  /** An operator shorthand for read()
179  * \sa PwmOut::read()
180  */
181  operator float()
182  {
183  // Underlying call is thread safe
184  return read();
185  }
186 
187 #if !(DOXYGEN_ONLY)
188 protected:
189  /** Lock deep sleep only if it is not yet locked */
190  void lock_deep_sleep();
191 
192  /** Unlock deep sleep in case it is locked */
193  void unlock_deep_sleep();
194 
195  /** Initialize this instance */
196  void init();
197 
198  /** Power down this instance */
199  void deinit();
200 
201  pwmout_t _pwm;
202  PinName _pin;
203  bool _deep_sleep_locked;
204  bool _initialized;
205  float _duty_cycle;
206  int _period_us;
207 #endif
208 };
209 
210 /** @}*/
211 
212 } // namespace mbed
213 
214 #endif
215 
216 #endif
void write(float value)
Set the output duty-cycle, specified as a percentage (float)
PwmOut(PinName pin)
Create a PwmOut connected to the specified pin.
void resume()
Resume PWM operation.
void pulsewidth_us(int us)
Set the PWM pulsewidth, specified in microseconds (int), keeping the period the same.
A pulse-width modulation digital output.
Definition: PwmOut.h:54
int read_pulsewitdth_us()
Read the PWM pulsewidth.
float read()
Return the current output duty-cycle setting, measured as a percentage (float)
void period_us(int us)
Set the PWM period, specified in microseconds (int), keeping the duty cycle the same.
void suspend()
Suspend PWM operation.
void period_ms(int ms)
Set the PWM period, specified in milliseconds (int), keeping the duty cycle the same.
void period(float seconds)
Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
int read_period_us()
Read the PWM period.
void pulsewidth(float seconds)
Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
Definition: pinmap.h:31
struct pwmout_s pwmout_t
Pwmout hal structure.
Definition: pwmout_api.h:34
PwmOut & operator=(float value)
A operator shorthand for write()
Definition: PwmOut.h:161
Definition: ATHandler.h:46
void pulsewidth_ms(int ms)
Set the PWM pulsewidth, specified in milliseconds (int), keeping the period the same.
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.