source: rtems/c/src/lib/libbsp/arm/shared/arm-a9mpcore-clock-config.c @ 24bf11e

4.115
Last change on this file since 24bf11e was 24bf11e, checked in by Sebastian Huber <sebastian.huber@…>, on 02/12/14 at 09:31:38

score: Add CPU counter support

Add a CPU counter interface to allow access to a free-running counter.
It is useful to measure short time intervals. This can be used for
example to enable profiling of critical low-level functions.

Add two busy wait functions rtems_counter_delay_ticks() and
rtems_counter_delay_nanoseconds() implemented via the CPU counter.

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 * Copyright (c) 2013-2014 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <info@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.com/license/LICENSE.
13 */
14
15#include <rtems/counter.h>
16
17#include <bsp.h>
18#include <bsp/irq.h>
19#include <bsp/arm-a9mpcore-regs.h>
20#include <bsp/arm-a9mpcore-clock.h>
21
22#define A9MPCORE_GT ((volatile a9mpcore_gt *) BSP_ARM_A9MPCORE_GT_BASE)
23
24static uint64_t a9mpcore_clock_last_tick_k;
25
26/* This is defined in clockdrv_shell.h */
27void Clock_isr(rtems_irq_hdl_param arg);
28
29__attribute__ ((weak)) uint32_t a9mpcore_clock_periphclk(void)
30{
31  /* default to the BSP option. */
32  return BSP_ARM_A9MPCORE_PERIPHCLK;
33}
34
35static void a9mpcore_clock_at_tick(void)
36{
37  volatile a9mpcore_gt *gt = A9MPCORE_GT;
38
39  gt->irqst = A9MPCORE_GT_IRQST_EFLG;
40}
41
42static void a9mpcore_clock_handler_install(void)
43{
44  rtems_status_code sc;
45
46  sc = rtems_interrupt_handler_install(
47    A9MPCORE_IRQ_GT,
48    "Clock",
49    RTEMS_INTERRUPT_UNIQUE,
50    (rtems_interrupt_handler) Clock_isr,
51    NULL
52  );
53  if (sc != RTEMS_SUCCESSFUL) {
54    rtems_fatal(
55      RTEMS_FATAL_SOURCE_BSP_SPECIFIC,
56      BSP_ARM_A9MPCORE_FATAL_CLOCK_IRQ_INSTALL
57    );
58  }
59}
60
61static uint64_t a9mpcore_clock_get_counter(volatile a9mpcore_gt *gt)
62{
63  uint32_t cl;
64  uint32_t cu1;
65  uint32_t cu2;
66
67  do {
68    cu1 = gt->cntrupper;
69    cl = gt->cntrlower;
70    cu2 = gt->cntrupper;
71  } while (cu1 != cu2);
72
73  return ((uint64_t) cu2 << 32) | cl;
74}
75
76static void a9mpcore_clock_initialize(void)
77{
78  volatile a9mpcore_gt *gt = A9MPCORE_GT;
79  uint64_t periphclk = a9mpcore_clock_periphclk();
80  uint64_t us_per_tick = rtems_configuration_get_microseconds_per_tick();
81  uint32_t interval = (uint32_t) ((periphclk * us_per_tick) / 1000000);
82  uint64_t cmpval;
83
84  a9mpcore_clock_last_tick_k = (UINT64_C(1000000000) << 32) / periphclk;
85
86  gt->ctrl &= A9MPCORE_GT_CTRL_TMR_EN;
87  gt->irqst = A9MPCORE_GT_IRQST_EFLG;
88
89  cmpval = a9mpcore_clock_get_counter(gt);
90  cmpval += interval;
91
92  gt->cmpvallower = (uint32_t) cmpval;
93  gt->cmpvalupper = (uint32_t) (cmpval >> 32);
94  gt->autoinc = interval;
95  gt->ctrl = A9MPCORE_GT_CTRL_AUTOINC_EN
96    | A9MPCORE_GT_CTRL_IRQ_EN
97    | A9MPCORE_GT_CTRL_COMP_EN
98    | A9MPCORE_GT_CTRL_TMR_EN;
99
100  rtems_counter_initialize_converter((uint32_t) periphclk);
101}
102
103CPU_Counter_ticks _CPU_Counter_read(void)
104{
105  volatile a9mpcore_gt *gt = A9MPCORE_GT;
106
107  return gt->cntrlower;
108}
109
110static void a9mpcore_clock_cleanup(void)
111{
112  volatile a9mpcore_gt *gt = A9MPCORE_GT;
113  rtems_status_code sc;
114
115  gt->ctrl &= A9MPCORE_GT_CTRL_TMR_EN;
116  gt->irqst = A9MPCORE_GT_IRQST_EFLG;
117
118  sc = rtems_interrupt_handler_remove(
119    A9MPCORE_IRQ_GT,
120    (rtems_interrupt_handler) Clock_isr,
121    NULL
122  );
123  if (sc != RTEMS_SUCCESSFUL) {
124    rtems_fatal(
125      RTEMS_FATAL_SOURCE_BSP_SPECIFIC,
126      BSP_ARM_A9MPCORE_FATAL_CLOCK_IRQ_REMOVE
127    );
128  }
129}
130
131static uint32_t a9mpcore_clock_nanoseconds_since_last_tick(void)
132{
133  volatile a9mpcore_gt *gt = A9MPCORE_GT;
134  uint64_t k = a9mpcore_clock_last_tick_k;
135  uint32_t c = gt->cntrlower;
136  uint32_t n = gt->cmpvallower;
137  uint32_t i = gt->autoinc;
138
139  if ((gt->irqst & A9MPCORE_GT_IRQST_EFLG) != 0) {
140    n = gt->cmpvallower - i;
141  }
142
143  return (uint32_t) (((c - n + i) * k) >> 32);
144}
145
146#define Clock_driver_support_at_tick() \
147  a9mpcore_clock_at_tick()
148
149#define Clock_driver_support_initialize_hardware() \
150  a9mpcore_clock_initialize()
151
152#define Clock_driver_support_install_isr(isr, old_isr) \
153  do { \
154    a9mpcore_clock_handler_install();   \
155    old_isr = NULL; \
156  } while (0)
157
158#define Clock_driver_support_shutdown_hardware() \
159  a9mpcore_clock_cleanup()
160
161#define Clock_driver_nanoseconds_since_last_tick \
162  a9mpcore_clock_nanoseconds_since_last_tick
163
164/* Include shared source clock driver code */
165#include "../../shared/clockdrv_shell.h"
Note: See TracBrowser for help on using the repository browser.