Nothing Special / mbed-STM32F103C8

Fork of mbed-STM32F103C8_org by Nothing Special

Committer:
mega64
Date:
Thu Apr 27 23:56:38 2017 +0000
Revision:
148:8b0b02bf146f
Parent:
146:03e976389d16
Remove unnecessary folders

Who changed what in which revision?

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