Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /* mbed Microcontroller Library
lypinator 0:bb348c97df44 2 * Copyright (c) 2006-2013 ARM Limited
lypinator 0:bb348c97df44 3 *
lypinator 0:bb348c97df44 4 * Licensed under the Apache License, Version 2.0 (the "License");
lypinator 0:bb348c97df44 5 * you may not use this file except in compliance with the License.
lypinator 0:bb348c97df44 6 * You may obtain a copy of the License at
lypinator 0:bb348c97df44 7 *
lypinator 0:bb348c97df44 8 * http://www.apache.org/licenses/LICENSE-2.0
lypinator 0:bb348c97df44 9 *
lypinator 0:bb348c97df44 10 * Unless required by applicable law or agreed to in writing, software
lypinator 0:bb348c97df44 11 * distributed under the License is distributed on an "AS IS" BASIS,
lypinator 0:bb348c97df44 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
lypinator 0:bb348c97df44 13 * See the License for the specific language governing permissions and
lypinator 0:bb348c97df44 14 * limitations under the License.
lypinator 0:bb348c97df44 15 */
lypinator 0:bb348c97df44 16 #include "drivers/Timer.h"
lypinator 0:bb348c97df44 17 #include "hal/ticker_api.h"
lypinator 0:bb348c97df44 18 #include "hal/us_ticker_api.h"
lypinator 0:bb348c97df44 19 #include "platform/mbed_critical.h"
lypinator 0:bb348c97df44 20 #include "hal/lp_ticker_api.h"
lypinator 0:bb348c97df44 21
lypinator 0:bb348c97df44 22 namespace mbed {
lypinator 0:bb348c97df44 23
lypinator 0:bb348c97df44 24 Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()), _lock_deepsleep(true)
lypinator 0:bb348c97df44 25 {
lypinator 0:bb348c97df44 26 reset();
lypinator 0:bb348c97df44 27 }
lypinator 0:bb348c97df44 28
lypinator 0:bb348c97df44 29 Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data), _lock_deepsleep(true)
lypinator 0:bb348c97df44 30 {
lypinator 0:bb348c97df44 31 reset();
lypinator 0:bb348c97df44 32 #if DEVICE_LPTICKER
lypinator 0:bb348c97df44 33 _lock_deepsleep = (data != get_lp_ticker_data());
lypinator 0:bb348c97df44 34 #endif
lypinator 0:bb348c97df44 35 }
lypinator 0:bb348c97df44 36
lypinator 0:bb348c97df44 37 Timer::~Timer()
lypinator 0:bb348c97df44 38 {
lypinator 0:bb348c97df44 39 core_util_critical_section_enter();
lypinator 0:bb348c97df44 40 if (_running) {
lypinator 0:bb348c97df44 41 if (_lock_deepsleep) {
lypinator 0:bb348c97df44 42 sleep_manager_unlock_deep_sleep();
lypinator 0:bb348c97df44 43 }
lypinator 0:bb348c97df44 44 }
lypinator 0:bb348c97df44 45 _running = 0;
lypinator 0:bb348c97df44 46 core_util_critical_section_exit();
lypinator 0:bb348c97df44 47 }
lypinator 0:bb348c97df44 48
lypinator 0:bb348c97df44 49 void Timer::start()
lypinator 0:bb348c97df44 50 {
lypinator 0:bb348c97df44 51 core_util_critical_section_enter();
lypinator 0:bb348c97df44 52 if (!_running) {
lypinator 0:bb348c97df44 53 if (_lock_deepsleep) {
lypinator 0:bb348c97df44 54 sleep_manager_lock_deep_sleep();
lypinator 0:bb348c97df44 55 }
lypinator 0:bb348c97df44 56 _start = ticker_read_us(_ticker_data);
lypinator 0:bb348c97df44 57 _running = 1;
lypinator 0:bb348c97df44 58 }
lypinator 0:bb348c97df44 59 core_util_critical_section_exit();
lypinator 0:bb348c97df44 60 }
lypinator 0:bb348c97df44 61
lypinator 0:bb348c97df44 62 void Timer::stop()
lypinator 0:bb348c97df44 63 {
lypinator 0:bb348c97df44 64 core_util_critical_section_enter();
lypinator 0:bb348c97df44 65 _time += slicetime();
lypinator 0:bb348c97df44 66 if (_running) {
lypinator 0:bb348c97df44 67 if (_lock_deepsleep) {
lypinator 0:bb348c97df44 68 sleep_manager_unlock_deep_sleep();
lypinator 0:bb348c97df44 69 }
lypinator 0:bb348c97df44 70 }
lypinator 0:bb348c97df44 71 _running = 0;
lypinator 0:bb348c97df44 72 core_util_critical_section_exit();
lypinator 0:bb348c97df44 73 }
lypinator 0:bb348c97df44 74
lypinator 0:bb348c97df44 75 int Timer::read_us()
lypinator 0:bb348c97df44 76 {
lypinator 0:bb348c97df44 77 return read_high_resolution_us();
lypinator 0:bb348c97df44 78 }
lypinator 0:bb348c97df44 79
lypinator 0:bb348c97df44 80 float Timer::read()
lypinator 0:bb348c97df44 81 {
lypinator 0:bb348c97df44 82 return (float)read_us() / 1000000.0f;
lypinator 0:bb348c97df44 83 }
lypinator 0:bb348c97df44 84
lypinator 0:bb348c97df44 85 int Timer::read_ms()
lypinator 0:bb348c97df44 86 {
lypinator 0:bb348c97df44 87 return read_high_resolution_us() / 1000;
lypinator 0:bb348c97df44 88 }
lypinator 0:bb348c97df44 89
lypinator 0:bb348c97df44 90 us_timestamp_t Timer::read_high_resolution_us()
lypinator 0:bb348c97df44 91 {
lypinator 0:bb348c97df44 92 core_util_critical_section_enter();
lypinator 0:bb348c97df44 93 us_timestamp_t time = _time + slicetime();
lypinator 0:bb348c97df44 94 core_util_critical_section_exit();
lypinator 0:bb348c97df44 95 return time;
lypinator 0:bb348c97df44 96 }
lypinator 0:bb348c97df44 97
lypinator 0:bb348c97df44 98 us_timestamp_t Timer::slicetime()
lypinator 0:bb348c97df44 99 {
lypinator 0:bb348c97df44 100 us_timestamp_t ret = 0;
lypinator 0:bb348c97df44 101 core_util_critical_section_enter();
lypinator 0:bb348c97df44 102 if (_running) {
lypinator 0:bb348c97df44 103 ret = ticker_read_us(_ticker_data) - _start;
lypinator 0:bb348c97df44 104 }
lypinator 0:bb348c97df44 105 core_util_critical_section_exit();
lypinator 0:bb348c97df44 106 return ret;
lypinator 0:bb348c97df44 107 }
lypinator 0:bb348c97df44 108
lypinator 0:bb348c97df44 109 void Timer::reset()
lypinator 0:bb348c97df44 110 {
lypinator 0:bb348c97df44 111 core_util_critical_section_enter();
lypinator 0:bb348c97df44 112 _start = ticker_read_us(_ticker_data);
lypinator 0:bb348c97df44 113 _time = 0;
lypinator 0:bb348c97df44 114 core_util_critical_section_exit();
lypinator 0:bb348c97df44 115 }
lypinator 0:bb348c97df44 116
lypinator 0:bb348c97df44 117 Timer::operator float()
lypinator 0:bb348c97df44 118 {
lypinator 0:bb348c97df44 119 return read();
lypinator 0:bb348c97df44 120 }
lypinator 0:bb348c97df44 121
lypinator 0:bb348c97df44 122 } // namespace mbed