EL4121 Embedded System / mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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