blob: c0c3bdf85b60fb5d101904e2d1357b8b6bd08b52 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include "timer_mps3.h"
18
19#include "bsp_core_log.h"
20#include "device_mps3.h"
21
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010022#include <inttypes.h>
23
alexander3c798932021-03-26 21:42:19 +000024void timer_reset(void)
25{
26 MPS3_FPGAIO->CLK1HZ = 0;
27 MPS3_FPGAIO->CLK100HZ = 0;
28 MPS3_FPGAIO->COUNTER = 0;
29
30 if (0 != Init_SysTick()) {
31 printf_err("Failed to initialise system tick config\n");
32 }
33 debug("system tick config ready\n");
34}
35
36mps3_time_counter get_time_counter(void)
37{
38 mps3_time_counter t = {
39 .counter_1Hz = MPS3_FPGAIO->CLK1HZ,
40 .counter_100Hz = MPS3_FPGAIO->CLK100HZ,
41 .counter_fpga = MPS3_FPGAIO->COUNTER,
42 .counter_systick = Get_SysTick_Cycle_Count()
43 };
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010044 debug("Timestamp:\n");
45 debug("\tCounter 1 Hz: %" PRIu32 "\n", t.counter_1Hz);
46 debug("\tCounter 100 Hz: %" PRIu32 "\n", t.counter_100Hz);
47 debug("\tCounter FPGA: %" PRIu32 "\n", t.counter_fpga);
48 debug("\tCounter CPU: %" PRIu64 "\n", t.counter_systick);
alexander3c798932021-03-26 21:42:19 +000049 return t;
50}
51
52/**
53 * Please note, that there are no checks for overflow in this function => if
54 * the time elapsed has been big (in days) this could happen and is currently
55 * not handled.
56 **/
57uint32_t get_duration_milliseconds(mps3_time_counter *start,
58 mps3_time_counter *end)
59{
60 uint32_t time_elapsed = 0;
61 if (end->counter_100Hz > start->counter_100Hz) {
62 time_elapsed = (end->counter_100Hz - start->counter_100Hz) * 10;
63 } else {
64 time_elapsed = (end->counter_1Hz - start->counter_1Hz) * 1000 +
65 ((0xFFFFFFFF - start->counter_100Hz) + end->counter_100Hz + 1) * 10;
66 }
67
68 /* If the time elapsed is less than 100ms, use microseconds count to be
69 * more precise */
70 if (time_elapsed < 100) {
71 debug("Using the microsecond function instead..\n");
72 return get_duration_microseconds(start, end)/1000;
73 }
74
75 return time_elapsed;
76}
77
78/**
79 * Like the microsecond counterpart, this function could return wrong results when
80 * the counter (MAINCLK) overflows. There are no overflow counters available.
81 **/
82uint32_t get_duration_microseconds(mps3_time_counter *start,
83 mps3_time_counter *end)
84{
85 const int divisor = GetMPS3CoreClock()/1000000;
86 uint32_t time_elapsed = 0;
87 if (end->counter_fpga > start->counter_fpga) {
88 time_elapsed = (end->counter_fpga - start->counter_fpga)/divisor;
89 } else {
90 time_elapsed = ((0xFFFFFFFF - end->counter_fpga)
91 + start->counter_fpga + 1)/divisor;
92 }
93 return time_elapsed;
94}
95
96uint64_t get_cycle_count_diff(mps3_time_counter *start,
97 mps3_time_counter *end)
98{
99 if (start->counter_systick > end->counter_systick) {
100 warn("start > end; counter might have overflown\n");
101 }
102 return end->counter_systick - start->counter_systick;
103}
104
105void start_cycle_counter(void)
106{
107 /* Nothing to do for FPGA */
108}
109
110void stop_cycle_counter(void)
111{
112 /* Nothing to do for FPGA */
113}