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