Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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