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