test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Committer:
ommpy
Date:
Mon Jul 06 17:18:59 2020 +0530
Revision:
0:d383e2dee0f7
first commit

Who changed what in which revision?

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