blob: 3d282618fa977b9094530795f6fdbe7c375e1b7a [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +00002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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 Sisodia659fcd92021-05-19 10:30:06 +010017#include "timer_simple_platform.h"
alexander3c798932021-03-26 21:42:19 +000018
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000019#include "log_macros.h" /* Logging macros */
20#include "cmsis.h" /* For CPU related defintiions */
alexander3c798932021-03-26 21:42:19 +000021
Kshitij Sisodia659fcd92021-05-19 10:30:06 +010022#include <inttypes.h>
23
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000024static uint64_t cpu_cycle_count = 0; /* 64-bit cpu cycle counter */
25extern 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 **/
31static uint64_t Get_SysTick_Cycle_Count(void);
32
33/**
34 * SysTick initialisation
35 */
36static int Init_SysTick(void);
37
38
alexander31ae9f02022-02-10 16:15:54 +000039base_time_counter get_time_counter(void)
alexander3c798932021-03-26 21:42:19 +000040{
alexander31ae9f02022-02-10 16:15:54 +000041 base_time_counter t = {
alexander3c798932021-03-26 21:42:19 +000042 .counter_systick = Get_SysTick_Cycle_Count()
43 };
Kshitij Sisodia659fcd92021-05-19 10:30:06 +010044 debug("counter_systick: %" PRIu64 "\n", t.counter_systick);
alexander3c798932021-03-26 21:42:19 +000045 return t;
46}
47
48void 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
alexander31ae9f02022-02-10 16:15:54 +000056uint64_t get_cycle_count_diff(base_time_counter *start,
57 base_time_counter *end)
alexander3c798932021-03-26 21:42:19 +000058{
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
65void start_cycle_counter(void)
66{
67 /* Add any custom requirement for this platform here */
68}
69
70void stop_cycle_counter(void)
71{
72 /* Add any custom requirement for this platform here */
73}
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000074
75
76void 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 */
85static 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 */
99static 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}