Nicolas Borla / Mbed OS ROME2_Robot_Firmware
Committer:
boro
Date:
Mon Mar 16 13:12:31 2020 +0000
Revision:
0:4beb2ea291ec
a

Who changed what in which revision?

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