mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
drivers/source/PwmOut.cpp@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 1:9db0e321a9f4 | 1 | /* mbed Microcontroller Library |
kenjiArai | 1:9db0e321a9f4 | 2 | * Copyright (c) 2006-2019 ARM Limited |
kenjiArai | 1:9db0e321a9f4 | 3 | * SPDX-License-Identifier: Apache-2.0 |
kenjiArai | 1:9db0e321a9f4 | 4 | * |
kenjiArai | 1:9db0e321a9f4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kenjiArai | 1:9db0e321a9f4 | 6 | * you may not use this file except in compliance with the License. |
kenjiArai | 1:9db0e321a9f4 | 7 | * You may obtain a copy of the License at |
kenjiArai | 1:9db0e321a9f4 | 8 | * |
kenjiArai | 1:9db0e321a9f4 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 1:9db0e321a9f4 | 10 | * |
kenjiArai | 1:9db0e321a9f4 | 11 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 1:9db0e321a9f4 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
kenjiArai | 1:9db0e321a9f4 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 1:9db0e321a9f4 | 14 | * See the License for the specific language governing permissions and |
kenjiArai | 1:9db0e321a9f4 | 15 | * limitations under the License. |
kenjiArai | 1:9db0e321a9f4 | 16 | */ |
kenjiArai | 1:9db0e321a9f4 | 17 | |
kenjiArai | 1:9db0e321a9f4 | 18 | #include "drivers/PwmOut.h" |
kenjiArai | 1:9db0e321a9f4 | 19 | |
kenjiArai | 1:9db0e321a9f4 | 20 | |
kenjiArai | 1:9db0e321a9f4 | 21 | #if DEVICE_PWMOUT |
kenjiArai | 1:9db0e321a9f4 | 22 | |
kenjiArai | 1:9db0e321a9f4 | 23 | #include "platform/mbed_critical.h" |
kenjiArai | 1:9db0e321a9f4 | 24 | #include "platform/mbed_power_mgmt.h" |
kenjiArai | 1:9db0e321a9f4 | 25 | #include "platform/mbed_assert.h" |
kenjiArai | 1:9db0e321a9f4 | 26 | |
kenjiArai | 1:9db0e321a9f4 | 27 | namespace mbed { |
kenjiArai | 1:9db0e321a9f4 | 28 | |
kenjiArai | 1:9db0e321a9f4 | 29 | PwmOut::PwmOut(PinName pin) : |
kenjiArai | 1:9db0e321a9f4 | 30 | _pin(pin), |
kenjiArai | 1:9db0e321a9f4 | 31 | _deep_sleep_locked(false), |
kenjiArai | 1:9db0e321a9f4 | 32 | _initialized(false), |
kenjiArai | 1:9db0e321a9f4 | 33 | _duty_cycle(0) |
kenjiArai | 1:9db0e321a9f4 | 34 | { |
kenjiArai | 1:9db0e321a9f4 | 35 | PwmOut::init(); |
kenjiArai | 1:9db0e321a9f4 | 36 | } |
kenjiArai | 1:9db0e321a9f4 | 37 | |
kenjiArai | 1:9db0e321a9f4 | 38 | PwmOut::PwmOut(const PinMap &pinmap) : _deep_sleep_locked(false) |
kenjiArai | 1:9db0e321a9f4 | 39 | { |
kenjiArai | 1:9db0e321a9f4 | 40 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 41 | pwmout_init_direct(&_pwm, &pinmap); |
kenjiArai | 1:9db0e321a9f4 | 42 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 43 | } |
kenjiArai | 1:9db0e321a9f4 | 44 | |
kenjiArai | 1:9db0e321a9f4 | 45 | PwmOut::~PwmOut() |
kenjiArai | 1:9db0e321a9f4 | 46 | { |
kenjiArai | 1:9db0e321a9f4 | 47 | PwmOut::deinit(); |
kenjiArai | 1:9db0e321a9f4 | 48 | } |
kenjiArai | 1:9db0e321a9f4 | 49 | |
kenjiArai | 1:9db0e321a9f4 | 50 | void PwmOut::write(float value) |
kenjiArai | 1:9db0e321a9f4 | 51 | { |
kenjiArai | 1:9db0e321a9f4 | 52 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 53 | pwmout_write(&_pwm, value); |
kenjiArai | 1:9db0e321a9f4 | 54 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 55 | } |
kenjiArai | 1:9db0e321a9f4 | 56 | |
kenjiArai | 1:9db0e321a9f4 | 57 | float PwmOut::read() |
kenjiArai | 1:9db0e321a9f4 | 58 | { |
kenjiArai | 1:9db0e321a9f4 | 59 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 60 | float val = pwmout_read(&_pwm); |
kenjiArai | 1:9db0e321a9f4 | 61 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 62 | return val; |
kenjiArai | 1:9db0e321a9f4 | 63 | } |
kenjiArai | 1:9db0e321a9f4 | 64 | |
kenjiArai | 1:9db0e321a9f4 | 65 | void PwmOut::period(float seconds) |
kenjiArai | 1:9db0e321a9f4 | 66 | { |
kenjiArai | 1:9db0e321a9f4 | 67 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 68 | pwmout_period(&_pwm, seconds); |
kenjiArai | 1:9db0e321a9f4 | 69 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 70 | } |
kenjiArai | 1:9db0e321a9f4 | 71 | |
kenjiArai | 1:9db0e321a9f4 | 72 | void PwmOut::period_ms(int ms) |
kenjiArai | 1:9db0e321a9f4 | 73 | { |
kenjiArai | 1:9db0e321a9f4 | 74 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 75 | pwmout_period_ms(&_pwm, ms); |
kenjiArai | 1:9db0e321a9f4 | 76 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 77 | } |
kenjiArai | 1:9db0e321a9f4 | 78 | |
kenjiArai | 1:9db0e321a9f4 | 79 | void PwmOut::period_us(int us) |
kenjiArai | 1:9db0e321a9f4 | 80 | { |
kenjiArai | 1:9db0e321a9f4 | 81 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 82 | pwmout_period_us(&_pwm, us); |
kenjiArai | 1:9db0e321a9f4 | 83 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 84 | } |
kenjiArai | 1:9db0e321a9f4 | 85 | |
kenjiArai | 1:9db0e321a9f4 | 86 | void PwmOut::pulsewidth(float seconds) |
kenjiArai | 1:9db0e321a9f4 | 87 | { |
kenjiArai | 1:9db0e321a9f4 | 88 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 89 | pwmout_pulsewidth(&_pwm, seconds); |
kenjiArai | 1:9db0e321a9f4 | 90 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 91 | } |
kenjiArai | 1:9db0e321a9f4 | 92 | |
kenjiArai | 1:9db0e321a9f4 | 93 | void PwmOut::pulsewidth_ms(int ms) |
kenjiArai | 1:9db0e321a9f4 | 94 | { |
kenjiArai | 1:9db0e321a9f4 | 95 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 96 | pwmout_pulsewidth_ms(&_pwm, ms); |
kenjiArai | 1:9db0e321a9f4 | 97 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 98 | } |
kenjiArai | 1:9db0e321a9f4 | 99 | |
kenjiArai | 1:9db0e321a9f4 | 100 | void PwmOut::pulsewidth_us(int us) |
kenjiArai | 1:9db0e321a9f4 | 101 | { |
kenjiArai | 1:9db0e321a9f4 | 102 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 103 | pwmout_pulsewidth_us(&_pwm, us); |
kenjiArai | 1:9db0e321a9f4 | 104 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 105 | } |
kenjiArai | 1:9db0e321a9f4 | 106 | |
kenjiArai | 1:9db0e321a9f4 | 107 | void PwmOut::suspend() |
kenjiArai | 1:9db0e321a9f4 | 108 | { |
kenjiArai | 1:9db0e321a9f4 | 109 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 110 | if (_initialized) { |
kenjiArai | 1:9db0e321a9f4 | 111 | _duty_cycle = PwmOut::read(); |
kenjiArai | 1:9db0e321a9f4 | 112 | PwmOut::deinit(); |
kenjiArai | 1:9db0e321a9f4 | 113 | } |
kenjiArai | 1:9db0e321a9f4 | 114 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 115 | } |
kenjiArai | 1:9db0e321a9f4 | 116 | |
kenjiArai | 1:9db0e321a9f4 | 117 | void PwmOut::resume() |
kenjiArai | 1:9db0e321a9f4 | 118 | { |
kenjiArai | 1:9db0e321a9f4 | 119 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 120 | if (!_initialized) { |
kenjiArai | 1:9db0e321a9f4 | 121 | PwmOut::init(); |
kenjiArai | 1:9db0e321a9f4 | 122 | PwmOut::write(_duty_cycle); |
kenjiArai | 1:9db0e321a9f4 | 123 | } |
kenjiArai | 1:9db0e321a9f4 | 124 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 125 | } |
kenjiArai | 1:9db0e321a9f4 | 126 | |
kenjiArai | 1:9db0e321a9f4 | 127 | void PwmOut::lock_deep_sleep() |
kenjiArai | 1:9db0e321a9f4 | 128 | { |
kenjiArai | 1:9db0e321a9f4 | 129 | if (_deep_sleep_locked == false) { |
kenjiArai | 1:9db0e321a9f4 | 130 | sleep_manager_lock_deep_sleep(); |
kenjiArai | 1:9db0e321a9f4 | 131 | _deep_sleep_locked = true; |
kenjiArai | 1:9db0e321a9f4 | 132 | } |
kenjiArai | 1:9db0e321a9f4 | 133 | } |
kenjiArai | 1:9db0e321a9f4 | 134 | |
kenjiArai | 1:9db0e321a9f4 | 135 | void PwmOut::unlock_deep_sleep() |
kenjiArai | 1:9db0e321a9f4 | 136 | { |
kenjiArai | 1:9db0e321a9f4 | 137 | if (_deep_sleep_locked == true) { |
kenjiArai | 1:9db0e321a9f4 | 138 | sleep_manager_unlock_deep_sleep(); |
kenjiArai | 1:9db0e321a9f4 | 139 | _deep_sleep_locked = false; |
kenjiArai | 1:9db0e321a9f4 | 140 | } |
kenjiArai | 1:9db0e321a9f4 | 141 | } |
kenjiArai | 1:9db0e321a9f4 | 142 | |
kenjiArai | 1:9db0e321a9f4 | 143 | void PwmOut::init() |
kenjiArai | 1:9db0e321a9f4 | 144 | { |
kenjiArai | 1:9db0e321a9f4 | 145 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 146 | |
kenjiArai | 1:9db0e321a9f4 | 147 | if (!_initialized) { |
kenjiArai | 1:9db0e321a9f4 | 148 | pwmout_init(&_pwm, _pin); |
kenjiArai | 1:9db0e321a9f4 | 149 | lock_deep_sleep(); |
kenjiArai | 1:9db0e321a9f4 | 150 | _initialized = true; |
kenjiArai | 1:9db0e321a9f4 | 151 | } |
kenjiArai | 1:9db0e321a9f4 | 152 | |
kenjiArai | 1:9db0e321a9f4 | 153 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 154 | } |
kenjiArai | 1:9db0e321a9f4 | 155 | |
kenjiArai | 1:9db0e321a9f4 | 156 | void PwmOut::deinit() |
kenjiArai | 1:9db0e321a9f4 | 157 | { |
kenjiArai | 1:9db0e321a9f4 | 158 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 159 | |
kenjiArai | 1:9db0e321a9f4 | 160 | if (_initialized) { |
kenjiArai | 1:9db0e321a9f4 | 161 | pwmout_free(&_pwm); |
kenjiArai | 1:9db0e321a9f4 | 162 | unlock_deep_sleep(); |
kenjiArai | 1:9db0e321a9f4 | 163 | _initialized = false; |
kenjiArai | 1:9db0e321a9f4 | 164 | } |
kenjiArai | 1:9db0e321a9f4 | 165 | |
kenjiArai | 1:9db0e321a9f4 | 166 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 167 | } |
kenjiArai | 1:9db0e321a9f4 | 168 | |
kenjiArai | 1:9db0e321a9f4 | 169 | } // namespace mbed |
kenjiArai | 1:9db0e321a9f4 | 170 | |
kenjiArai | 1:9db0e321a9f4 | 171 | #endif // #if DEVICE_PWMOUT |