Mbed OS Reference
Loading...
Searching...
No Matches
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
25namespace 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 */
54class PwmOut {
55
56public:
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 * This reference is stored in the PwmOut, so the pinmap needs to live as long as this object does.
68 */
69 PwmOut(const PinMap &pinmap);
70 PwmOut(const PinMap &&) = delete; // prevent passing of temporary objects
71
72 ~PwmOut();
73
74 /** Set the output duty-cycle, specified as a percentage (float)
75 *
76 * @param value A floating-point value representing the output duty-cycle,
77 * specified as a percentage. The value should lie between
78 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
79 * Values outside this range will be saturated to 0.0f or 1.0f.
80 */
81 void write(float value);
82
83 /** Return the current output duty-cycle setting, measured as a percentage (float)
84 *
85 * @returns
86 * A floating-point value representing the current duty-cycle being output on the pin,
87 * measured as a percentage. The returned value will lie between
88 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
89 *
90 * @note
91 * This value may not match exactly the value set by a previous write().
92 */
93 float read();
94
95 /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
96 *
97 * @param seconds Change the period of a PWM signal in seconds (float) without modifying the duty cycle
98 * @note
99 * The resolution is currently in microseconds; periods smaller than this
100 * will be set to zero.
101 */
102 void period(float seconds);
103
104 /** Set the PWM period, specified in milliseconds (int), keeping the duty cycle the same.
105 * @param ms Change the period of a PWM signal in milliseconds without modifying the duty cycle
106 */
107 void period_ms(int ms);
108
109 /** Set the PWM period, specified in microseconds (int), keeping the duty cycle the same.
110 * @param us Change the period of a PWM signal in microseconds without modifying the duty cycle
111 */
112 void period_us(int us);
113
114 /** Read the PWM period
115 * @returns
116 * The PWM period, specified in microseconds (int)
117 */
119
120 /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
121 * @param seconds Change the pulse width of a PWM signal specified in seconds (float)
122 */
123 void pulsewidth(float seconds);
124
125 /** Set the PWM pulsewidth, specified in milliseconds (int), keeping the period the same.
126 * @param ms Change the pulse width of a PWM signal specified in milliseconds
127 */
128 void pulsewidth_ms(int ms);
129
130 /** Set the PWM pulsewidth, specified in microseconds (int), keeping the period the same.
131 * @param us Change the pulse width of a PWM signal specified in microseconds
132 */
133 void pulsewidth_us(int us);
134
135 /** Read the PWM pulsewidth
136 * @returns
137 * The PWM pulsewidth, specified in microseconds (int)
138 */
140
141 /** Read the PWM pulsewidth
142 * @returns
143 * The PWM pulsewidth, specified in microseconds (int)
144 */
145 MBED_DEPRECATED("use read_pulsewidth_us() instead")
147
148 /** Suspend PWM operation
149 *
150 * Control the PWM state. This is primarily intended
151 * for temporary power-saving; This call can
152 * allow pwm to be temporarily disabled to permit power saving without
153 * losing device state. The subsequent function call must be PwmOut::resume
154 * for PWM to resume; any other calls prior to resuming are undefined behavior.
155 */
156 void suspend();
157
158 /** Resume PWM operation
159 *
160 * Control the PWM state. This is primarily intended
161 * to resume PWM operations after a previous PwmOut::suspend call;
162 * This call restores the device state prior to suspension.
163 */
164 void resume();
165
166 /** A operator shorthand for write()
167 * \sa PwmOut::write()
168 */
169 PwmOut &operator= (float value)
170 {
171 // Underlying call is thread safe
172 write(value);
173 return *this;
174 }
175
176 /** A operator shorthand for write()
177 * \sa PwmOut::write()
178 */
180 {
181 // Underlying call is thread safe
182 write(rhs.read());
183 return *this;
184 }
185
186 /** An operator shorthand for read()
187 * \sa PwmOut::read()
188 */
189 operator float()
190 {
191 // Underlying call is thread safe
192 return read();
193 }
194
195#if !(DOXYGEN_ONLY)
196protected:
197 /** Lock deep sleep only if it is not yet locked */
198 void lock_deep_sleep();
199
200 /** Unlock deep sleep in case it is locked */
201 void unlock_deep_sleep();
202
203 // Functions which call the underlying HAL init function. The direct one calls the static
204 // pinmap version (pwmout_init_direct()) and the normal one calls the PinName-accepting one (pwmout_init()).
205 // A pointer to one of these two functions is stored in the _init_func member.
206 // It's done this way so that references to pwmout_init(), and therefore to the pinmap tables,
207 // can be removed by the linker if only the static pinmap version is used.
208 static void _call_pwmout_init_direct(PwmOut *thisPtr);
209 static void _call_pwmout_init(PwmOut *thisPtr);
210
211 /** Initialize this instance */
212 void init();
213
214 /** Power down this instance */
215 void deinit();
216
217 pwmout_t _pwm;
218
219 const PinName _pin; // Pin, NC if using static pinmap
220 PinMap const *const _pinmap; // Static pinmap, nullptr if not used
221
222 /* Pointer to HAL init function */
223 void (*_init_func)(PwmOut *);
224
225 bool _deep_sleep_locked;
226 bool _initialized;
227 float _duty_cycle;
228 int _period_us;
229#endif
230};
231
232/** @}*/
233
234} // namespace mbed
235
236#endif
237
238#endif
A pulse-width modulation digital output.
Definition: PwmOut.h:54
void write(float value)
Set the output duty-cycle, specified as a percentage (float)
void period_ms(int ms)
Set the PWM period, specified in milliseconds (int), keeping the duty cycle the same.
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 resume()
Resume PWM operation.
int read_pulsewidth_us()
Read the PWM pulsewidth.
void period(float seconds)
Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
void pulsewidth_ms(int ms)
Set the PWM pulsewidth, specified in milliseconds (int), keeping the period the same.
float read()
Return the current output duty-cycle setting, measured as a percentage (float)
int read_pulsewitdth_us()
Read the PWM pulsewidth.
PwmOut & operator=(float value)
A operator shorthand for write()
Definition: PwmOut.h:169
void pulsewidth_us(int us)
Set the PWM pulsewidth, specified in microseconds (int), keeping the period 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.
PwmOut(PinName pin)
Create a PwmOut connected to the specified pin.
PwmOut(const PinMap &pinmap)
Create a PwmOut connected to the specified pin.
struct pwmout_s pwmout_t
Pwmout hal structure.
Definition: pwmout_api.h:34
Definition: pinmap.h:31