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