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 pulsewidth, specified in microseconds (int)
137  */
138  int read_pulsewidth_us();
139 
140  /** Read the PWM pulsewidth
141  * @returns
142  * The PWM pulsewidth, specified in microseconds (int)
143  */
144  MBED_DEPRECATED("use read_pulsewidth_us() instead")
145  int read_pulsewitdth_us();
146 
147  /** Suspend PWM operation
148  *
149  * Control the PWM state. This is primarily intended
150  * for temporary power-saving; This call can
151  * allow pwm to be temporarily disabled to permit power saving without
152  * losing device state. The subsequent function call must be PwmOut::resume
153  * for PWM to resume; any other calls prior to resuming are undefined behavior.
154  */
155  void suspend();
156 
157  /** Resume PWM operation
158  *
159  * Control the PWM state. This is primarily intended
160  * to resume PWM operations after a previous PwmOut::suspend call;
161  * This call restores the device state prior to suspension.
162  */
163  void resume();
164 
165  /** A operator shorthand for write()
166  * \sa PwmOut::write()
167  */
168  PwmOut &operator= (float value)
169  {
170  // Underlying call is thread safe
171  write(value);
172  return *this;
173  }
174 
175  /** A operator shorthand for write()
176  * \sa PwmOut::write()
177  */
179  {
180  // Underlying call is thread safe
181  write(rhs.read());
182  return *this;
183  }
184 
185  /** An operator shorthand for read()
186  * \sa PwmOut::read()
187  */
188  operator float()
189  {
190  // Underlying call is thread safe
191  return read();
192  }
193 
194 #if !(DOXYGEN_ONLY)
195 protected:
196  /** Lock deep sleep only if it is not yet locked */
197  void lock_deep_sleep();
198 
199  /** Unlock deep sleep in case it is locked */
200  void unlock_deep_sleep();
201 
202  /** Initialize this instance */
203  void init();
204 
205  /** Power down this instance */
206  void deinit();
207 
208  pwmout_t _pwm;
209  PinName _pin;
210  bool _deep_sleep_locked;
211  bool _initialized;
212  float _duty_cycle;
213  int _period_us;
214 #endif
215 };
216 
217 /** @}*/
218 
219 } // namespace mbed
220 
221 #endif
222 
223 #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.
int read_pulsewidth_us()
Read the PWM pulsewidth.
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:168
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.