From Ben Katz mbed-dev library. Removed unnecessary target files to reduce the overall size by a factor of 10 to make it easier to import into the online IDE.

Dependents:   motor_driver motor_driver_screaming_fix

Committer:
saloutos
Date:
Thu Nov 26 04:08:56 2020 +0000
Revision:
0:083111ae2a11
first commit of leaned mbed dev lib

Who changed what in which revision?

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