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