alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 19 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 20 | #include "device_mps3.h" |
| 21 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 22 | #include <inttypes.h> |
| 23 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 24 | void 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 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 36 | base_time_counter get_time_counter(void) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 37 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 38 | base_time_counter t = { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 Sisodia | 3c8256d | 2021-05-24 16:12:40 +0100 | [diff] [blame] | 44 | 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); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 49 | 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 | **/ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 57 | uint32_t get_duration_milliseconds(base_time_counter *start, |
| 58 | base_time_counter *end) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 | **/ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 82 | uint32_t get_duration_microseconds(base_time_counter *start, |
| 83 | base_time_counter *end) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 96 | uint64_t get_cycle_count_diff(base_time_counter *start, |
| 97 | base_time_counter *end) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 | |
| 105 | void start_cycle_counter(void) |
| 106 | { |
| 107 | /* Nothing to do for FPGA */ |
| 108 | } |
| 109 | |
| 110 | void stop_cycle_counter(void) |
| 111 | { |
| 112 | /* Nothing to do for FPGA */ |
| 113 | } |