alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2021-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 | */ |
Kshitij Sisodia | 659fcd9 | 2021-05-19 10:30:06 +0100 | [diff] [blame] | 17 | #include "timer_simple_platform.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 18 | |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame^] | 19 | #include "log_macros.h" /* Logging macros */ |
| 20 | #include "cmsis.h" /* For CPU related defintiions */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 21 | |
Kshitij Sisodia | 659fcd9 | 2021-05-19 10:30:06 +0100 | [diff] [blame] | 22 | #include <inttypes.h> |
| 23 | |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame^] | 24 | static uint64_t cpu_cycle_count = 0; /* 64-bit cpu cycle counter */ |
| 25 | extern uint32_t SystemCoreClock; /* Expected to come from the cmsis-device lib */ |
| 26 | |
| 27 | /** |
| 28 | * @brief Gets the system tick triggered cycle counter for the CPU. |
| 29 | * @return 64-bit counter value. |
| 30 | **/ |
| 31 | static uint64_t Get_SysTick_Cycle_Count(void); |
| 32 | |
| 33 | /** |
| 34 | * SysTick initialisation |
| 35 | */ |
| 36 | static int Init_SysTick(void); |
| 37 | |
| 38 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 39 | base_time_counter get_time_counter(void) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 40 | { |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 41 | base_time_counter t = { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 42 | .counter_systick = Get_SysTick_Cycle_Count() |
| 43 | }; |
Kshitij Sisodia | 659fcd9 | 2021-05-19 10:30:06 +0100 | [diff] [blame] | 44 | debug("counter_systick: %" PRIu64 "\n", t.counter_systick); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | return t; |
| 46 | } |
| 47 | |
| 48 | void timer_reset(void) |
| 49 | { |
| 50 | if (0 != Init_SysTick()) { |
| 51 | printf_err("Failed to initialise system tick config\n"); |
| 52 | } |
| 53 | debug("system tick config ready\n"); |
| 54 | } |
| 55 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 56 | uint64_t get_cycle_count_diff(base_time_counter *start, |
| 57 | base_time_counter *end) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 58 | { |
| 59 | if (start->counter_systick > end->counter_systick) { |
| 60 | warn("start > end; counter might have overflown\n"); |
| 61 | } |
| 62 | return end->counter_systick - start->counter_systick; |
| 63 | } |
| 64 | |
| 65 | void start_cycle_counter(void) |
| 66 | { |
| 67 | /* Add any custom requirement for this platform here */ |
| 68 | } |
| 69 | |
| 70 | void stop_cycle_counter(void) |
| 71 | { |
| 72 | /* Add any custom requirement for this platform here */ |
| 73 | } |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame^] | 74 | |
| 75 | |
| 76 | void SysTick_Handler(void) |
| 77 | { |
| 78 | /* Increment the cycle counter based on load value. */ |
| 79 | cpu_cycle_count += SysTick->LOAD + 1; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Gets the current SysTick derived counter value |
| 84 | */ |
| 85 | static uint64_t Get_SysTick_Cycle_Count(void) |
| 86 | { |
| 87 | uint32_t systick_val; |
| 88 | |
| 89 | NVIC_DisableIRQ(SysTick_IRQn); |
| 90 | systick_val = SysTick->VAL & SysTick_VAL_CURRENT_Msk; |
| 91 | NVIC_EnableIRQ(SysTick_IRQn); |
| 92 | |
| 93 | return cpu_cycle_count + (SysTick->LOAD - systick_val); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * SysTick initialisation |
| 98 | */ |
| 99 | static int Init_SysTick(void) |
| 100 | { |
| 101 | const uint32_t ticks_10ms = SystemCoreClock/100 + 1; |
| 102 | int err = 0; |
| 103 | |
| 104 | /* Reset CPU cycle count value. */ |
| 105 | cpu_cycle_count = 0; |
| 106 | |
| 107 | /* Changing configuration for sys tick => guard from being |
| 108 | * interrupted. */ |
| 109 | NVIC_DisableIRQ(SysTick_IRQn); |
| 110 | |
| 111 | /* SysTick init - this will enable interrupt too. */ |
| 112 | err = SysTick_Config(ticks_10ms); |
| 113 | |
| 114 | /* Enable interrupt again. */ |
| 115 | NVIC_EnableIRQ(SysTick_IRQn); |
| 116 | |
| 117 | /* Wait for SysTick to kick off */ |
| 118 | while (!err && !SysTick->VAL) { |
| 119 | __NOP(); |
| 120 | } |
| 121 | |
| 122 | return err; |
| 123 | } |