Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
PwmOut.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2019 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #include "drivers/PwmOut.h" 00019 00020 00021 #if DEVICE_PWMOUT 00022 00023 #include "platform/mbed_critical.h" 00024 #include "platform/mbed_power_mgmt.h" 00025 #include "platform/mbed_assert.h" 00026 00027 namespace mbed { 00028 00029 PwmOut::PwmOut(PinName pin) : 00030 _pin(pin), 00031 _deep_sleep_locked(false), 00032 _initialized(false), 00033 _duty_cycle(0) 00034 { 00035 PwmOut::init(); 00036 } 00037 00038 PwmOut::PwmOut(const PinMap &pinmap) : _deep_sleep_locked(false) 00039 { 00040 core_util_critical_section_enter(); 00041 pwmout_init_direct(&_pwm, &pinmap); 00042 core_util_critical_section_exit(); 00043 } 00044 00045 PwmOut::~PwmOut() 00046 { 00047 PwmOut::deinit(); 00048 } 00049 00050 void PwmOut::write(float value) 00051 { 00052 core_util_critical_section_enter(); 00053 pwmout_write(&_pwm, value); 00054 core_util_critical_section_exit(); 00055 } 00056 00057 float PwmOut::read() 00058 { 00059 core_util_critical_section_enter(); 00060 float val = pwmout_read(&_pwm); 00061 core_util_critical_section_exit(); 00062 return val; 00063 } 00064 00065 void PwmOut::period(float seconds) 00066 { 00067 core_util_critical_section_enter(); 00068 pwmout_period(&_pwm, seconds); 00069 core_util_critical_section_exit(); 00070 } 00071 00072 void PwmOut::period_ms(int ms) 00073 { 00074 core_util_critical_section_enter(); 00075 pwmout_period_ms(&_pwm, ms); 00076 core_util_critical_section_exit(); 00077 } 00078 00079 void PwmOut::period_us(int us) 00080 { 00081 core_util_critical_section_enter(); 00082 pwmout_period_us(&_pwm, us); 00083 core_util_critical_section_exit(); 00084 } 00085 00086 void PwmOut::pulsewidth(float seconds) 00087 { 00088 core_util_critical_section_enter(); 00089 pwmout_pulsewidth(&_pwm, seconds); 00090 core_util_critical_section_exit(); 00091 } 00092 00093 void PwmOut::pulsewidth_ms(int ms) 00094 { 00095 core_util_critical_section_enter(); 00096 pwmout_pulsewidth_ms(&_pwm, ms); 00097 core_util_critical_section_exit(); 00098 } 00099 00100 void PwmOut::pulsewidth_us(int us) 00101 { 00102 core_util_critical_section_enter(); 00103 pwmout_pulsewidth_us(&_pwm, us); 00104 core_util_critical_section_exit(); 00105 } 00106 00107 void PwmOut::suspend() 00108 { 00109 core_util_critical_section_enter(); 00110 if (_initialized) { 00111 _duty_cycle = PwmOut::read(); 00112 PwmOut::deinit(); 00113 } 00114 core_util_critical_section_exit(); 00115 } 00116 00117 void PwmOut::resume() 00118 { 00119 core_util_critical_section_enter(); 00120 if (!_initialized) { 00121 PwmOut::init(); 00122 PwmOut::write(_duty_cycle); 00123 } 00124 core_util_critical_section_exit(); 00125 } 00126 00127 void PwmOut::lock_deep_sleep() 00128 { 00129 if (_deep_sleep_locked == false) { 00130 sleep_manager_lock_deep_sleep(); 00131 _deep_sleep_locked = true; 00132 } 00133 } 00134 00135 void PwmOut::unlock_deep_sleep() 00136 { 00137 if (_deep_sleep_locked == true) { 00138 sleep_manager_unlock_deep_sleep(); 00139 _deep_sleep_locked = false; 00140 } 00141 } 00142 00143 void PwmOut::init() 00144 { 00145 core_util_critical_section_enter(); 00146 00147 if (!_initialized) { 00148 pwmout_init(&_pwm, _pin); 00149 lock_deep_sleep(); 00150 _initialized = true; 00151 } 00152 00153 core_util_critical_section_exit(); 00154 } 00155 00156 void PwmOut::deinit() 00157 { 00158 core_util_critical_section_enter(); 00159 00160 if (_initialized) { 00161 pwmout_free(&_pwm); 00162 unlock_deep_sleep(); 00163 _initialized = false; 00164 } 00165 00166 core_util_critical_section_exit(); 00167 } 00168 00169 } // namespace mbed 00170 00171 #endif // #if DEVICE_PWMOUT
Generated on Tue Jul 12 2022 13:54:46 by
