alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Kshitij Sisodia | d5679cc | 2022-03-03 16:30:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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" |
Kshitij Sisodia | d5679cc | 2022-03-03 16:30:07 +0000 | [diff] [blame] | 20 | #include "smm_mps3.h" /* Memory map for MPS3. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 21 | |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 22 | static uint64_t cpu_cycle_count = 0; /* 64-bit cpu cycle counter */ |
| 23 | |
| 24 | /** |
| 25 | * @brief Gets the system tick triggered cycle counter for the CPU. |
| 26 | * @return 64-bit counter value. |
| 27 | **/ |
| 28 | static uint64_t Get_SysTick_Cycle_Count(void); |
| 29 | |
| 30 | /** |
| 31 | * SysTick initialisation |
| 32 | */ |
| 33 | static int Init_SysTick(void); |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 34 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 35 | void timer_reset(void) |
| 36 | { |
| 37 | MPS3_FPGAIO->CLK1HZ = 0; |
| 38 | MPS3_FPGAIO->CLK100HZ = 0; |
| 39 | MPS3_FPGAIO->COUNTER = 0; |
| 40 | |
| 41 | if (0 != Init_SysTick()) { |
| 42 | printf_err("Failed to initialise system tick config\n"); |
| 43 | } |
| 44 | debug("system tick config ready\n"); |
| 45 | } |
| 46 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 47 | base_time_counter get_time_counter(void) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 48 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 49 | base_time_counter t = { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 50 | .counter_1Hz = MPS3_FPGAIO->CLK1HZ, |
| 51 | .counter_100Hz = MPS3_FPGAIO->CLK100HZ, |
| 52 | .counter_fpga = MPS3_FPGAIO->COUNTER, |
| 53 | .counter_systick = Get_SysTick_Cycle_Count() |
| 54 | }; |
Kshitij Sisodia | 3c8256d | 2021-05-24 16:12:40 +0100 | [diff] [blame] | 55 | debug("Timestamp:\n"); |
| 56 | debug("\tCounter 1 Hz: %" PRIu32 "\n", t.counter_1Hz); |
| 57 | debug("\tCounter 100 Hz: %" PRIu32 "\n", t.counter_100Hz); |
| 58 | debug("\tCounter FPGA: %" PRIu32 "\n", t.counter_fpga); |
| 59 | debug("\tCounter CPU: %" PRIu64 "\n", t.counter_systick); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 60 | return t; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Please note, that there are no checks for overflow in this function => if |
| 65 | * the time elapsed has been big (in days) this could happen and is currently |
| 66 | * not handled. |
| 67 | **/ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 68 | uint32_t get_duration_milliseconds(base_time_counter *start, |
| 69 | base_time_counter *end) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 70 | { |
| 71 | uint32_t time_elapsed = 0; |
| 72 | if (end->counter_100Hz > start->counter_100Hz) { |
| 73 | time_elapsed = (end->counter_100Hz - start->counter_100Hz) * 10; |
| 74 | } else { |
| 75 | time_elapsed = (end->counter_1Hz - start->counter_1Hz) * 1000 + |
| 76 | ((0xFFFFFFFF - start->counter_100Hz) + end->counter_100Hz + 1) * 10; |
| 77 | } |
| 78 | |
| 79 | /* If the time elapsed is less than 100ms, use microseconds count to be |
| 80 | * more precise */ |
| 81 | if (time_elapsed < 100) { |
| 82 | debug("Using the microsecond function instead..\n"); |
| 83 | return get_duration_microseconds(start, end)/1000; |
| 84 | } |
| 85 | |
| 86 | return time_elapsed; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Like the microsecond counterpart, this function could return wrong results when |
| 91 | * the counter (MAINCLK) overflows. There are no overflow counters available. |
| 92 | **/ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 93 | uint32_t get_duration_microseconds(base_time_counter *start, |
| 94 | base_time_counter *end) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 95 | { |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame^] | 96 | const int divisor = get_mps3_core_clock()/1000000; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 97 | uint32_t time_elapsed = 0; |
| 98 | if (end->counter_fpga > start->counter_fpga) { |
| 99 | time_elapsed = (end->counter_fpga - start->counter_fpga)/divisor; |
| 100 | } else { |
| 101 | time_elapsed = ((0xFFFFFFFF - end->counter_fpga) |
| 102 | + start->counter_fpga + 1)/divisor; |
| 103 | } |
| 104 | return time_elapsed; |
| 105 | } |
| 106 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 107 | uint64_t get_cycle_count_diff(base_time_counter *start, |
| 108 | base_time_counter *end) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 109 | { |
| 110 | if (start->counter_systick > end->counter_systick) { |
| 111 | warn("start > end; counter might have overflown\n"); |
| 112 | } |
| 113 | return end->counter_systick - start->counter_systick; |
| 114 | } |
| 115 | |
| 116 | void start_cycle_counter(void) |
| 117 | { |
| 118 | /* Nothing to do for FPGA */ |
| 119 | } |
| 120 | |
| 121 | void stop_cycle_counter(void) |
| 122 | { |
| 123 | /* Nothing to do for FPGA */ |
| 124 | } |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 125 | |
| 126 | void SysTick_Handler(void) |
| 127 | { |
| 128 | /* Increment the cycle counter based on load value. */ |
| 129 | cpu_cycle_count += SysTick->LOAD + 1; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Gets the current SysTick derived counter value |
| 134 | */ |
| 135 | static uint64_t Get_SysTick_Cycle_Count(void) |
| 136 | { |
| 137 | uint32_t systick_val; |
| 138 | |
| 139 | NVIC_DisableIRQ(SysTick_IRQn); |
| 140 | systick_val = SysTick->VAL & SysTick_VAL_CURRENT_Msk; |
| 141 | NVIC_EnableIRQ(SysTick_IRQn); |
| 142 | |
| 143 | return cpu_cycle_count + (SysTick->LOAD - systick_val); |
| 144 | } |
| 145 | |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame^] | 146 | |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 147 | /** |
| 148 | * SysTick initialisation |
| 149 | */ |
| 150 | static int Init_SysTick(void) |
| 151 | { |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame^] | 152 | const uint32_t ticks_10ms = get_mps3_core_clock()/100 + 1; |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 153 | int err = 0; |
| 154 | |
| 155 | /* Reset CPU cycle count value. */ |
| 156 | cpu_cycle_count = 0; |
| 157 | |
| 158 | /* Changing configuration for sys tick => guard from being |
| 159 | * interrupted. */ |
| 160 | NVIC_DisableIRQ(SysTick_IRQn); |
| 161 | |
| 162 | /* SysTick init - this will enable interrupt too. */ |
| 163 | err = SysTick_Config(ticks_10ms); |
| 164 | |
| 165 | /* Enable interrupt again. */ |
| 166 | NVIC_EnableIRQ(SysTick_IRQn); |
| 167 | |
| 168 | /* Wait for SysTick to kick off */ |
| 169 | while (!err && !SysTick->VAL) { |
| 170 | __NOP(); |
| 171 | } |
| 172 | |
| 173 | return err; |
| 174 | } |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame^] | 175 | |
| 176 | uint32_t get_mps3_core_clock(void) |
| 177 | { |
| 178 | const uint32_t default_clock = 32000000 /* 32 MHz clock */; |
| 179 | static int warned_once = 0; |
| 180 | if (0 != MPS3_SCC->CFG_ACLK) { |
| 181 | if (default_clock != MPS3_SCC->CFG_ACLK) { |
| 182 | warn("System clock is different to the MPS3 config set clock.\n"); |
| 183 | } |
| 184 | return MPS3_SCC->CFG_ACLK; |
| 185 | } |
| 186 | |
| 187 | if (!warned_once) { |
| 188 | warn("MPS3_SCC->CFG_ACLK reads 0. Assuming default clock of %" PRIu32 "\n", |
| 189 | default_clock); |
| 190 | warned_once = 1; |
| 191 | } |
| 192 | return default_clock; |
| 193 | } |