summaryrefslogtreecommitdiffstats
path: root/bsps/arm/shared/cpucounter/cpucounter-armv7m.c
blob: b7593602eda882731e8910760352fdd237b0f736 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
 * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#include <rtems/score/armv7m.h>
#include <rtems/counter.h>
#include <rtems/sysinit.h>

#include <bsp.h>
#include <bsp/fatal.h>

uint32_t _CPU_Counter_frequency(void)
{
#ifdef BSP_ARMV7M_SYSTICK_FREQUENCY
  return = BSP_ARMV7M_SYSTICK_FREQUENCY;
#else
  volatile ARMV7M_Systick *systick = _ARMV7M_Systick;
  return ARMV7M_SYSTICK_CALIB_TENMS_GET(systick->calib) * 100;
#endif
}

CPU_Counter_ticks _CPU_Counter_read(void)
{
  volatile ARMV7M_DWT *dwt = _ARMV7M_DWT;

  return dwt->cyccnt;
}

static void armv7m_cpu_counter_initialize(void)
{
  bool cyccnt_enabled;

  cyccnt_enabled = _ARMV7M_DWT_Enable_CYCCNT();

  if (!cyccnt_enabled) {
    bsp_fatal(BSP_ARM_ARMV7M_CPU_COUNTER_INIT);
  }
}

RTEMS_SYSINIT_ITEM(
  armv7m_cpu_counter_initialize,
  RTEMS_SYSINIT_CPU_COUNTER,
  RTEMS_SYSINIT_ORDER_FIRST
);