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  * wait(0.2);
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  /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
114  * @param seconds Change the pulse width of a PWM signal specified in seconds (float)
115  */
116  void pulsewidth(float seconds);
117 
118  /** Set the PWM pulsewidth, specified in milliseconds (int), keeping the period the same.
119  * @param ms Change the pulse width of a PWM signal specified in milliseconds
120  */
121  void pulsewidth_ms(int ms);
122 
123  /** Set the PWM pulsewidth, specified in microseconds (int), keeping the period the same.
124  * @param us Change the pulse width of a PWM signal specified in microseconds
125  */
126  void pulsewidth_us(int us);
127 
128  /** Suspend PWM operation
129  *
130  * Control the PWM state. This is primarily intended
131  * for temporary power-saving; This call can
132  * allow pwm to be temporarily disabled to permit power saving without
133  * losing device state. The subsequent function call must be PwmOut::resume
134  * for PWM to resume; any other calls prior to resuming are undefined behavior.
135  */
136  void suspend();
137 
138  /** Resume PWM operation
139  *
140  * Control the PWM state. This is primarily intended
141  * to resume PWM operations after a previous PwmOut::suspend call;
142  * This call restores the device state prior to suspension.
143  */
144  void resume();
145 
146  /** A operator shorthand for write()
147  * \sa PwmOut::write()
148  */
149  PwmOut &operator= (float value)
150  {
151  // Underlying call is thread safe
152  write(value);
153  return *this;
154  }
155 
156  /** A operator shorthand for write()
157  * \sa PwmOut::write()
158  */
160  {
161  // Underlying call is thread safe
162  write(rhs.read());
163  return *this;
164  }
165 
166  /** An operator shorthand for read()
167  * \sa PwmOut::read()
168  */
169  operator float()
170  {
171  // Underlying call is thread safe
172  return read();
173  }
174 
175 #if !(DOXYGEN_ONLY)
176 protected:
177  /** Lock deep sleep only if it is not yet locked */
178  void lock_deep_sleep();
179 
180  /** Unlock deep sleep in case it is locked */
181  void unlock_deep_sleep();
182 
183  /** Initialize this instance */
184  void init();
185 
186  /** Power down this instance */
187  void deinit();
188 
189  pwmout_t _pwm;
190  PinName _pin;
191  bool _deep_sleep_locked;
192  bool _initialized;
193  float _duty_cycle;
194 #endif
195 };
196 
197 /** @}*/
198 
199 } // namespace mbed
200 
201 #endif
202 
203 #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
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.
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:149
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.