mbed-dev-f303

Committer:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4
Date:
Tue Jun 14 09:21:18 2022 +0000
Revision:
0:bdf663c61a82
lib

Who changed what in which revision?

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