summaryrefslogtreecommitdiff
path: root/source/hal/profiles/native/timer/platform_timer.c
blob: c311125543b2e5d95bd7cfefa6eebf13a99d301b (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (c) 2021 Arm Limited. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifdef __cplusplus
extern "C" {
#endif

#include "timer.h"

#include <assert.h>
#include <time.h>
#include <string.h>

#define MILLISECONDS_IN_SECOND      1000
#define MICROSECONDS_IN_SECOND      1000000
#define NANOSECONDS_IN_MILLISECOND  1000000
#define NANOSECONDS_IN_MICROSECOND  1000

/**
 * @brief   Gets the current time counter value.
 * @return  Counter value expressed in terms of time_counter struct.
 **/
static time_counter get_time_counter(void)
{
    struct timespec current_time;
    clock_gettime(1, &current_time);
    time_counter t = {
        .current_secs = current_time.tv_sec,
        .current_nsecs = current_time.tv_nsec
    };

    return t;
}

/**
 * @brief       Gets the time duration elapsed between start and end.
 * @param[in]   start   Pointer to time_counter value at start time.
 * @param[in]   end     Pointer to time_counter value at end.
 * @return      Difference in milliseconds between the arguments expressed
 *              as unsigned 32 bit integer.
 **/
static time_t get_duration_milliseconds(time_counter *start, time_counter *end)
{
    /* Convert both parts of time struct to ms then add for complete time. */
    time_t seconds_part =
        (end->current_secs - start->current_secs) * MILLISECONDS_IN_SECOND;
    time_t nanoseconds_part =
        (end->current_nsecs - start->current_nsecs) / NANOSECONDS_IN_MILLISECOND;

    return seconds_part + nanoseconds_part;
}

/**
 * @brief       Gets the time duration elapsed between start and end.
 * @param[in]   start   Pointer to time_counter value at start time.
 * @param[in]   end     Pointer to time_counter value at end.
 * @return      Difference in microseconds between the arguments expressed
 *              as unsigned 32 bit integer.
 **/
static time_t get_duration_microseconds(time_counter *start, time_counter *end)
{
    /* Convert both parts of time struct to us then add for complete time. */
    time_t seconds_part =
        (end->current_secs - start->current_secs) * MICROSECONDS_IN_SECOND;
    time_t nanoseconds_part =
        (end->current_nsecs - start->current_nsecs) / NANOSECONDS_IN_MICROSECOND;

    return seconds_part + nanoseconds_part;
}

/**
 * @brief Stub for timer reset.
 **/
void reset_timer() {}

/**
 * @brief Initialise the timer for this platform.
 **/
void init_timer(platform_timer *timer)
{
    assert(timer);
    memset(timer, 0, sizeof(*timer));

    timer->get_time_counter = get_time_counter;
    timer->start_profiling = get_time_counter;
    timer->stop_profiling = get_time_counter;
    timer->get_duration_ms = get_duration_milliseconds;
    timer->cap.duration_ms = 1;
    timer->get_duration_us = get_duration_microseconds;
    timer->cap.duration_us = 1;
    timer->reset = reset_timer;
    timer->inited = 1;
}

#ifdef __cplusplus
}
#endif