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