inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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