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