summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/tms570/misc/cpucounterread.c
blob: 3ce2f63f61ebc7f833044cd955db878b2c80b22d (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
 * @file
 *
 * @ingroup tms570_clocks
 *
 * @brief Cortex-R performace counters
 *
 * The counters setup functions are these which has been suggested
 * on StackOverflow
 *
 * Code is probably for use on Cortex-A without modifications as well.
 *
 * http://stackoverflow.com/questions/3247373/how-to-measure-program-execution-time-in-arm-cortex-a8-processor
 */

/*
 * Copyright (c) 2014 Pavel Pisa <pisa@cmp.felk.cvut.cz>
 *
 * Czech Technical University in Prague
 * Zikova 1903/4
 * 166 36 Praha 6
 * Czech Republic
 *
 * 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 <stdlib.h>

#include <rtems.h>
#include <bsp.h>

static int cpu_counter_initialized;


/**
 * @brief set mode of Cortex-R performance counters
 *
 * Based on example found on http://stackoverflow.com
 *
 * @param[in] do_reset if set, values of the counters are reset
 * @param[in] enable_divider if set, CCNT counts clocks divided by 64
 * @retval Void
 */
static inline void _CPU_Counter_init_perfcounters(
    int32_t do_reset,
    int32_t enable_divider
)
{
  /* in general enable all counters (including cycle counter) */
  int32_t value = 1;

  /* peform reset */
  if (do_reset)
  {
    value |= 2;     /* reset all counters to zero */
    value |= 4;     /* reset cycle counter to zero */
  }

  if (enable_divider)
    value |= 8;     /* enable "by 64" divider for CCNT */

  value |= 16;

  /* program the performance-counter control-register */
  asm volatile ("mcr p15, 0, %0, c9, c12, 0\t\n" :: "r"(value));

  /* enable all counters */
  asm volatile ("mcr p15, 0, %0, c9, c12, 1\t\n" :: "r"(0x8000000f));

  /* clear overflows */
  asm volatile ("mcr p15, 0, %0, c9, c12, 3\t\n" :: "r"(0x8000000f));
}

/**
 * @brief initialize Cortex-R performance counters subsystem
 *
 * Based on example found on http://stackoverflow.com
 *
 * @retval Void
 *
 */
static void _CPU_Counter_initialize(void)
{
  rtems_interrupt_level level;

  rtems_interrupt_disable(level);

  if ( cpu_counter_initialized ) {
    rtems_interrupt_enable(level);
    return;
  }

  /* enable user-mode access to the performance counter */
  asm volatile ("mcr p15, 0, %0, c9, c14, 0\n\t" :: "r"(1));

  /* disable counter overflow interrupts (just in case) */
  asm volatile ("mcr p15, 0, %0, c9, c14, 2\n\t" :: "r"(0x8000000f));

  _CPU_Counter_init_perfcounters(false, false);

  cpu_counter_initialized = 1;

  rtems_interrupt_enable(level);
}

/**
 * @brief returns the actual value of Cortex-R cycle counter register
 *
 * The register is incremented at each core clock period
 *
 * @retval x actual core clock counter value
 *
 */
CPU_Counter_ticks _CPU_Counter_read(void)
{
  uint32_t ticks;
  if ( !cpu_counter_initialized ) {
    _CPU_Counter_initialize();
  }
  asm volatile ("mrc p15, 0, %0, c9, c13, 0\n": "=r" (ticks));
  return ticks;
}