blob: a72103cdbb1b90af849cf8324b093de6dc3f0fab [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 Sisodiaf9c19ea2021-05-07 16:08:14 +010044 debug("Timestamp:"
45 "\n\tCounter 1 Hz: %" PRIu32
46 "\n\tCounter 100 Hz: %" PRIu32
47 "\n\tCounter FPGA: %" PRIu32
48 "\n\tCounter CPU: %" PRIu64 "\n",
alexander3c798932021-03-26 21:42:19 +000049 t.counter_1Hz, t.counter_100Hz, t.counter_fpga, t.counter_systick);
50 return t;
51}
52
53/**
54 * Please note, that there are no checks for overflow in this function => if
55 * the time elapsed has been big (in days) this could happen and is currently
56 * not handled.
57 **/
58uint32_t get_duration_milliseconds(mps3_time_counter *start,
59 mps3_time_counter *end)
60{
61 uint32_t time_elapsed = 0;
62 if (end->counter_100Hz > start->counter_100Hz) {
63 time_elapsed = (end->counter_100Hz - start->counter_100Hz) * 10;
64 } else {
65 time_elapsed = (end->counter_1Hz - start->counter_1Hz) * 1000 +
66 ((0xFFFFFFFF - start->counter_100Hz) + end->counter_100Hz + 1) * 10;
67 }
68
69 /* If the time elapsed is less than 100ms, use microseconds count to be
70 * more precise */
71 if (time_elapsed < 100) {
72 debug("Using the microsecond function instead..\n");
73 return get_duration_microseconds(start, end)/1000;
74 }
75
76 return time_elapsed;
77}
78
79/**
80 * Like the microsecond counterpart, this function could return wrong results when
81 * the counter (MAINCLK) overflows. There are no overflow counters available.
82 **/
83uint32_t get_duration_microseconds(mps3_time_counter *start,
84 mps3_time_counter *end)
85{
86 const int divisor = GetMPS3CoreClock()/1000000;
87 uint32_t time_elapsed = 0;
88 if (end->counter_fpga > start->counter_fpga) {
89 time_elapsed = (end->counter_fpga - start->counter_fpga)/divisor;
90 } else {
91 time_elapsed = ((0xFFFFFFFF - end->counter_fpga)
92 + start->counter_fpga + 1)/divisor;
93 }
94 return time_elapsed;
95}
96
97uint64_t get_cycle_count_diff(mps3_time_counter *start,
98 mps3_time_counter *end)
99{
100 if (start->counter_systick > end->counter_systick) {
101 warn("start > end; counter might have overflown\n");
102 }
103 return end->counter_systick - start->counter_systick;
104}
105
106void start_cycle_counter(void)
107{
108 /* Nothing to do for FPGA */
109}
110
111void stop_cycle_counter(void)
112{
113 /* Nothing to do for FPGA */
114}