Skip to content

Commit

Permalink
timing: add support for nordic SoCs with RTC timer
Browse files Browse the repository at this point in the history
Add abstraction for nordic SoCs using Nordic RTC as the source for
timestamps and cycles.

Signed-off-by: Anas Nashif <[email protected]>
  • Loading branch information
nashif authored and MaureenHelm committed Sep 5, 2020
1 parent d896dec commit a180b33
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions soc/arm/nordic_nrf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ zephyr_sources(
validate_base_addresses.c
validate_enabled_instances.c
)

if(CONFIG_SOC_HAS_TIMING_FUNCTIONS AND NOT CONFIG_BOARD_HAS_TIMING_FUNCTIONS)
zephyr_library_sources_ifdef(CONFIG_TIMING_FUNCTIONS timing.c)
endif()
3 changes: 3 additions & 0 deletions soc/arm/nordic_nrf/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ config SYS_POWER_MANAGEMENT
config BUILD_OUTPUT_HEX
default y

config SOC_HAS_TIMING_FUNCTIONS
default y

config GPIO
default y
depends on SPI
Expand Down
68 changes: 68 additions & 0 deletions soc/arm/nordic_nrf/timing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2020 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <arch/arm/aarch32/arch.h>
#include <kernel.h>
#include <sys_clock.h>
#include <timing/timing.h>
#include <nrfx.h>

#if defined(CONFIG_NRF_RTC_TIMER)


#define CYCLES_PER_SEC (16000000 / (1 << NRF_TIMER2->PRESCALER))

void timing_init(void)
{
NRF_TIMER2->TASKS_CLEAR = 1; /* Clear Timer */
NRF_TIMER2->MODE = 0; /* Timer Mode */
NRF_TIMER2->PRESCALER = 0; /* 16M Hz */
NRF_TIMER2->BITMODE = 3; /* 32 - bit */
}

void timing_start(void)
{
NRF_TIMER2->TASKS_START = 1;
}

void timing_stop(void)
{
NRF_TIMER2->TASKS_STOP = 1; /* Stop Timer */
}

timing_t timing_counter_get(void)
{
NRF_TIMER2->TASKS_CAPTURE[0] = 1;
return NRF_TIMER2->CC[0] * ((SystemCoreClock) / CYCLES_PER_SEC);
}

uint64_t timing_cycles_get(volatile timing_t *const start,
volatile timing_t *const end)
{
return (*end - *start);
}

uint64_t timing_freq_get(void)
{
return SystemCoreClock;
}

uint64_t timing_cycles_to_ns(uint64_t cycles)
{
return (cycles) * (NSEC_PER_SEC) / (SystemCoreClock);
}

uint64_t timing_cycles_to_ns_avg(uint64_t cycles, uint32_t count)
{
return timing_cycles_to_ns(cycles) / count;
}

uint32_t timing_freq_get_mhz(void)
{
return (uint32_t)(timing_freq_get() / 1000000);
}

#endif

0 comments on commit a180b33

Please sign in to comment.