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