,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Thu Feb 15 14:29:23 2018 +0000
Revision:
15:2a20c3d2616e
Parent:
10:41552d038a69
j

Who changed what in which revision?

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