The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Oct 11 12:36:33 2017 +0100
Revision:
153:b484a57bc302
Parent:
145:64910690c574
Release 153 of the mbed library.

Who changed what in which revision?

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