Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

Who changed what in which revision?

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