The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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