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