mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Thu Sep 06 13:40:20 2018 +0100
Revision:
187:0387e8f68319
Parent:
184:08ed48f1de7f
Child:
188:bcfe06ba3d64
mbed-dev library. Release version 163

Who changed what in which revision?

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