2020-08-18 08:19:30 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Intel Corporation.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2023-07-04 16:01:39 +08:00
|
|
|
#include <zephyr/arch/arm/arch.h>
|
2022-05-06 17:11:04 +08:00
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/sys_clock.h>
|
|
|
|
#include <zephyr/timing/timing.h>
|
2020-08-18 08:19:30 +08:00
|
|
|
#include <nrfx.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_NRF_RTC_TIMER)
|
|
|
|
|
|
|
|
#define CYCLES_PER_SEC (16000000 / (1 << NRF_TIMER2->PRESCALER))
|
|
|
|
|
2020-10-15 02:15:40 +08:00
|
|
|
void soc_timing_init(void)
|
2020-08-18 08:19:30 +08:00
|
|
|
{
|
|
|
|
NRF_TIMER2->TASKS_CLEAR = 1; /* Clear Timer */
|
|
|
|
NRF_TIMER2->MODE = 0; /* Timer Mode */
|
|
|
|
NRF_TIMER2->PRESCALER = 0; /* 16M Hz */
|
2020-09-03 18:05:49 +08:00
|
|
|
#if defined(CONFIG_SOC_SERIES_NRF51X)
|
|
|
|
NRF_TIMER2->BITMODE = 0; /* 16 - bit */
|
|
|
|
#else
|
2020-08-18 08:19:30 +08:00
|
|
|
NRF_TIMER2->BITMODE = 3; /* 32 - bit */
|
2020-09-03 18:05:49 +08:00
|
|
|
#endif
|
2020-08-18 08:19:30 +08:00
|
|
|
}
|
|
|
|
|
2020-10-15 02:15:40 +08:00
|
|
|
void soc_timing_start(void)
|
2020-08-18 08:19:30 +08:00
|
|
|
{
|
|
|
|
NRF_TIMER2->TASKS_START = 1;
|
|
|
|
}
|
|
|
|
|
2020-10-15 02:15:40 +08:00
|
|
|
void soc_timing_stop(void)
|
2020-08-18 08:19:30 +08:00
|
|
|
{
|
|
|
|
NRF_TIMER2->TASKS_STOP = 1; /* Stop Timer */
|
|
|
|
}
|
|
|
|
|
2020-10-15 02:15:40 +08:00
|
|
|
timing_t soc_timing_counter_get(void)
|
2020-08-18 08:19:30 +08:00
|
|
|
{
|
|
|
|
NRF_TIMER2->TASKS_CAPTURE[0] = 1;
|
|
|
|
return NRF_TIMER2->CC[0] * ((SystemCoreClock) / CYCLES_PER_SEC);
|
|
|
|
}
|
|
|
|
|
2020-10-15 02:15:40 +08:00
|
|
|
uint64_t soc_timing_cycles_get(volatile timing_t *const start,
|
|
|
|
volatile timing_t *const end)
|
2020-08-18 08:19:30 +08:00
|
|
|
{
|
2020-09-03 18:05:49 +08:00
|
|
|
#if defined(CONFIG_SOC_SERIES_NRF51X)
|
|
|
|
#define COUNTER_SPAN BIT(16)
|
2021-09-24 14:09:36 +08:00
|
|
|
#else
|
|
|
|
#define COUNTER_SPAN BIT64(32)
|
|
|
|
#endif
|
2020-09-03 18:05:49 +08:00
|
|
|
if (*end >= *start) {
|
|
|
|
return (*end - *start);
|
|
|
|
} else {
|
|
|
|
return COUNTER_SPAN + *end - *start;
|
|
|
|
}
|
2020-08-18 08:19:30 +08:00
|
|
|
}
|
|
|
|
|
2020-10-15 02:15:40 +08:00
|
|
|
uint64_t soc_timing_freq_get(void)
|
2020-08-18 08:19:30 +08:00
|
|
|
{
|
|
|
|
return SystemCoreClock;
|
|
|
|
}
|
|
|
|
|
2020-10-15 02:15:40 +08:00
|
|
|
uint64_t soc_timing_cycles_to_ns(uint64_t cycles)
|
2020-08-18 08:19:30 +08:00
|
|
|
{
|
|
|
|
return (cycles) * (NSEC_PER_SEC) / (SystemCoreClock);
|
|
|
|
}
|
|
|
|
|
2020-10-15 02:15:40 +08:00
|
|
|
uint64_t soc_timing_cycles_to_ns_avg(uint64_t cycles, uint32_t count)
|
2020-08-18 08:19:30 +08:00
|
|
|
{
|
2020-10-15 02:15:40 +08:00
|
|
|
return soc_timing_cycles_to_ns(cycles) / count;
|
2020-08-18 08:19:30 +08:00
|
|
|
}
|
|
|
|
|
2020-10-15 02:15:40 +08:00
|
|
|
uint32_t soc_timing_freq_get_mhz(void)
|
2020-08-18 08:19:30 +08:00
|
|
|
{
|
2020-10-15 02:15:40 +08:00
|
|
|
return (uint32_t)(soc_timing_freq_get() / 1000000);
|
2020-08-18 08:19:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|