-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtimer.h
78 lines (63 loc) · 2.15 KB
/
timer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
/**
@file timer.h
Timer struct and timing functions. Mainly useful for testing. We leave it as part of the library in
case we want to time internal functions.
*/
#pragma once
#include "defines.h"
#ifdef SE_ENABLE_TIMERS
#include <stdint.h>
#ifdef SE_ON_SPHERE_M4
#include "os_hal_gpt.h"
// -- We set these to the clock we want to use
// There are 5 GPT clocks: {0, 1, 3} --> interrupt based, {2, 4} --> free-run
// GPT2 ~= 32KHz or 1Hz, GPT4 = ~= bus clock speed or (1/2)*bus clockspeed
static const uint8_t global_gpt_timer_id = OS_HAL_GPT4;
static const bool global_gpt_high_speed = 1;
#elif defined(SE_ON_NRF5)
#include "nrf.h"
#include "nrf_drv_timer.h" // TODO: Check if this is needed
#include "sdk_config.h"
#elif !defined(SE_ON_SPHERE_A7)
#include <time.h>
#endif
/**
Struct for storing time points.
@param start Starting time
@param stop End time
@param elapsed_time Elapsed time (start-stop) in nanoseconds
@param timer_instance Timer instance (Used only with SE_ON_NRF5). Equal to NRFX_TIMER_INSTANCE(0)
@param timer_config Timer config (Used only with SE_ON_NRF5). Equal to NRFX_TIMER_DEFAULT_CONFIG
*/
typedef struct Timer
{
#if defined(SE_ON_SPHERE_M4) || defined(SE_ON_NRF5)
uint32_t start; // Starting time
uint32_t stop; // End time
#elif defined(SE_ON_SPHERE_A7)
uint64_t start; // Starting time
uint64_t stop; // End time
#else
struct timespec start; // Starting time
struct timespec stop; // End time
#endif
float elapsed_time; // Elapsed time (start-stop) in nanoseconds
#ifdef SE_ON_NRF5
nrfx_timer_t *timer_instance; // Timer instance (= NRFX_TIMER_INSTANCE(0))
nrfx_timer_config_t *timer_config; // Timer config (= NRFX_TIMER_DEFAULT_CONFIG)
#endif
} Timer;
typedef enum TimeUnit {
SEC = 1UL,
MILLI_SEC = 1000UL,
MICRO_SEC = 1000000UL,
NANO_SEC = 1000000000UL
} TimeUnit;
void start_timer(Timer *timer);
void stop_timer(Timer *timer);
void reset_timer(Timer *timer);
void reset_start_timer(Timer *timer);
float read_timer(Timer timer, TimeUnit unit);
#endif