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