Update actions to ubuntu-latest (#14114)
[betaflight.git] / src / main / common / stopwatch.c
blob8df1980c9e64957d7a22fd4a8567f2b97c06cb7f
1 /*
2 * This file is part of Betaflight.
4 * Betaflight is free software. You can redistribute this software
5 * and/or modify this software under the terms of the GNU General
6 * Public License as published by the Free Software Foundation,
7 * either version 3 of the License, or (at your option) any later
8 * version.
10 * Betaflight is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this software.
19 * If not, see <http://www.gnu.org/licenses/>.
22 #include "platform.h"
24 #include "common/time.h"
26 #include "drivers/system.h"
28 #include "stopwatch.h"
30 void stopwatchInit(stopwatch_t *watch)
32 watch->start = 0;
33 watch->stop = 0;
34 stopwatchReset(watch);
37 void stopwatchReset(stopwatch_t *watch)
39 watch->isRunning = false;
40 watch->elapsed = 0;
43 uint32_t stopwatchStart(stopwatch_t *watch)
45 if (watch->isRunning == false) {
46 watch->isRunning = true;
47 watch->start = getCycleCounter();
49 return watch->start;
52 uint32_t stopwatchStop(stopwatch_t *watch)
54 if (watch->isRunning == true) {
55 watch->stop = getCycleCounter();
56 watch->elapsed += cmpTimeCycles(watch->stop, watch->start);
57 watch->isRunning = false;
59 return watch->stop;
62 uint32_t stopwatchGetCycles(stopwatch_t *watch)
64 if (watch->isRunning == true) {
65 return watch->elapsed + cmpTimeCycles(getCycleCounter(), watch->start);
66 } else {
67 return watch->elapsed;
71 uint32_t stopwatchGetMicros(stopwatch_t *watch)
73 return clockCyclesToMicros(stopwatchGetCycles(watch));
76 float stopwatchGetMicrosf(stopwatch_t *watch)
78 return clockCyclesToMicrosf(stopwatchGetCycles(watch));