4545
Fork of mbed-dev by
drivers/PwmOut.h@166:33361e55dd8c, 2017-08-23 (annotated)
- Committer:
- iftaziz
- Date:
- Wed Aug 23 10:32:38 2017 +0000
- Revision:
- 166:33361e55dd8c
- Parent:
- 160:d5399cc887bb
r1
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 | |
<> | 149:156823d33999 | 21 | #if DEVICE_PWMOUT |
<> | 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 | |
<> | 149:156823d33999 | 29 | /** A pulse-width modulation digital output |
<> | 149:156823d33999 | 30 | * |
<> | 149:156823d33999 | 31 | * @Note Synchronization level: Interrupt safe |
<> | 149:156823d33999 | 32 | * |
<> | 149:156823d33999 | 33 | * Example |
<> | 149:156823d33999 | 34 | * @code |
<> | 149:156823d33999 | 35 | * // Fade a led on. |
<> | 149:156823d33999 | 36 | * #include "mbed.h" |
<> | 149:156823d33999 | 37 | * |
<> | 149:156823d33999 | 38 | * PwmOut led(LED1); |
<> | 149:156823d33999 | 39 | * |
<> | 149:156823d33999 | 40 | * int main() { |
<> | 149:156823d33999 | 41 | * while(1) { |
<> | 149:156823d33999 | 42 | * led = led + 0.01; |
<> | 149:156823d33999 | 43 | * wait(0.2); |
<> | 149:156823d33999 | 44 | * if(led == 1.0) { |
<> | 149:156823d33999 | 45 | * led = 0; |
<> | 149:156823d33999 | 46 | * } |
<> | 149:156823d33999 | 47 | * } |
<> | 149:156823d33999 | 48 | * } |
<> | 149:156823d33999 | 49 | * @endcode |
<> | 149:156823d33999 | 50 | * |
<> | 149:156823d33999 | 51 | * @note |
<> | 149:156823d33999 | 52 | * On the LPC1768 and LPC2368, the PWMs all share the same |
<> | 149:156823d33999 | 53 | * period - if you change the period for one, you change it for all. |
<> | 149:156823d33999 | 54 | * Although routines that change the period maintain the duty cycle |
<> | 149:156823d33999 | 55 | * for its PWM, all other PWMs will require their duty cycle to be |
<> | 149:156823d33999 | 56 | * refreshed. |
<> | 149:156823d33999 | 57 | */ |
<> | 149:156823d33999 | 58 | class PwmOut { |
<> | 149:156823d33999 | 59 | |
<> | 149:156823d33999 | 60 | public: |
<> | 149:156823d33999 | 61 | |
<> | 149:156823d33999 | 62 | /** Create a PwmOut connected to the specified pin |
<> | 149:156823d33999 | 63 | * |
<> | 149:156823d33999 | 64 | * @param pin PwmOut pin to connect to |
<> | 149:156823d33999 | 65 | */ |
<> | 149:156823d33999 | 66 | PwmOut(PinName pin) { |
<> | 149:156823d33999 | 67 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 68 | pwmout_init(&_pwm, pin); |
<> | 149:156823d33999 | 69 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 70 | } |
<> | 149:156823d33999 | 71 | |
<> | 149:156823d33999 | 72 | /** Set the ouput duty-cycle, specified as a percentage (float) |
<> | 149:156823d33999 | 73 | * |
<> | 149:156823d33999 | 74 | * @param value A floating-point value representing the output duty-cycle, |
<> | 149:156823d33999 | 75 | * specified as a percentage. The value should lie between |
<> | 149:156823d33999 | 76 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
<> | 149:156823d33999 | 77 | * Values outside this range will be saturated to 0.0f or 1.0f. |
<> | 149:156823d33999 | 78 | */ |
<> | 149:156823d33999 | 79 | void write(float value) { |
<> | 149:156823d33999 | 80 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 81 | pwmout_write(&_pwm, value); |
<> | 149:156823d33999 | 82 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 83 | } |
<> | 149:156823d33999 | 84 | |
<> | 149:156823d33999 | 85 | /** Return the current output duty-cycle setting, measured as a percentage (float) |
<> | 149:156823d33999 | 86 | * |
<> | 149:156823d33999 | 87 | * @returns |
<> | 149:156823d33999 | 88 | * A floating-point value representing the current duty-cycle being output on the pin, |
<> | 149:156823d33999 | 89 | * measured as a percentage. The returned value will lie between |
<> | 149:156823d33999 | 90 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
<> | 149:156823d33999 | 91 | * |
<> | 149:156823d33999 | 92 | * @note |
<> | 149:156823d33999 | 93 | * This value may not match exactly the value set by a previous <write>. |
<> | 149:156823d33999 | 94 | */ |
<> | 149:156823d33999 | 95 | float read() { |
<> | 149:156823d33999 | 96 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 97 | float val = pwmout_read(&_pwm); |
<> | 149:156823d33999 | 98 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 99 | return val; |
<> | 149:156823d33999 | 100 | } |
<> | 149:156823d33999 | 101 | |
<> | 149:156823d33999 | 102 | /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same. |
<> | 149:156823d33999 | 103 | * |
<> | 149:156823d33999 | 104 | * @note |
<> | 149:156823d33999 | 105 | * The resolution is currently in microseconds; periods smaller than this |
<> | 149:156823d33999 | 106 | * will be set to zero. |
<> | 149:156823d33999 | 107 | */ |
<> | 149:156823d33999 | 108 | void period(float seconds) { |
<> | 149:156823d33999 | 109 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 110 | pwmout_period(&_pwm, seconds); |
<> | 149:156823d33999 | 111 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 112 | } |
<> | 149:156823d33999 | 113 | |
<> | 149:156823d33999 | 114 | /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same. |
<> | 149:156823d33999 | 115 | */ |
<> | 149:156823d33999 | 116 | void period_ms(int ms) { |
<> | 149:156823d33999 | 117 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 118 | pwmout_period_ms(&_pwm, ms); |
<> | 149:156823d33999 | 119 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 120 | } |
<> | 149:156823d33999 | 121 | |
<> | 149:156823d33999 | 122 | /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same. |
<> | 149:156823d33999 | 123 | */ |
<> | 149:156823d33999 | 124 | void period_us(int us) { |
<> | 149:156823d33999 | 125 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 126 | pwmout_period_us(&_pwm, us); |
<> | 149:156823d33999 | 127 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 128 | } |
<> | 149:156823d33999 | 129 | |
<> | 149:156823d33999 | 130 | /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. |
<> | 149:156823d33999 | 131 | */ |
<> | 149:156823d33999 | 132 | void pulsewidth(float seconds) { |
<> | 149:156823d33999 | 133 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 134 | pwmout_pulsewidth(&_pwm, seconds); |
<> | 149:156823d33999 | 135 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 136 | } |
<> | 149:156823d33999 | 137 | |
<> | 149:156823d33999 | 138 | /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. |
<> | 149:156823d33999 | 139 | */ |
<> | 149:156823d33999 | 140 | void pulsewidth_ms(int ms) { |
<> | 149:156823d33999 | 141 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 142 | pwmout_pulsewidth_ms(&_pwm, ms); |
<> | 149:156823d33999 | 143 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 144 | } |
<> | 149:156823d33999 | 145 | |
<> | 149:156823d33999 | 146 | /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. |
<> | 149:156823d33999 | 147 | */ |
<> | 149:156823d33999 | 148 | void pulsewidth_us(int us) { |
<> | 149:156823d33999 | 149 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 150 | pwmout_pulsewidth_us(&_pwm, us); |
<> | 149:156823d33999 | 151 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 152 | } |
<> | 149:156823d33999 | 153 | |
<> | 149:156823d33999 | 154 | /** A operator shorthand for 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 | |
<> | 149:156823d33999 | 162 | PwmOut& operator= (PwmOut& rhs) { |
<> | 149:156823d33999 | 163 | // Underlying call is thread safe |
<> | 149:156823d33999 | 164 | write(rhs.read()); |
<> | 149:156823d33999 | 165 | return *this; |
<> | 149:156823d33999 | 166 | } |
<> | 149:156823d33999 | 167 | |
<> | 149:156823d33999 | 168 | /** An operator shorthand for read() |
<> | 149:156823d33999 | 169 | */ |
<> | 149:156823d33999 | 170 | operator float() { |
<> | 149:156823d33999 | 171 | // Underlying call is thread safe |
<> | 149:156823d33999 | 172 | return read(); |
<> | 149:156823d33999 | 173 | } |
<> | 149:156823d33999 | 174 | |
<> | 149:156823d33999 | 175 | protected: |
<> | 149:156823d33999 | 176 | pwmout_t _pwm; |
<> | 149:156823d33999 | 177 | }; |
<> | 149:156823d33999 | 178 | |
<> | 149:156823d33999 | 179 | } // namespace mbed |
<> | 149:156823d33999 | 180 | |
<> | 149:156823d33999 | 181 | #endif |
<> | 149:156823d33999 | 182 | |
<> | 149:156823d33999 | 183 | #endif |
<> | 149:156823d33999 | 184 | |
<> | 149:156823d33999 | 185 | /** @}*/ |