summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/shared/armv7m/clock/armv7m-clock-config.c
blob: e03ba9d83fe6f9fb4840f084154279373ba0e25c (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (c) 2011-2012 Sebastian Huber.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Obere Lagerstr. 30
 *  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.h>
#include <rtems/timecounter.h>
#include <rtems/score/armv7m.h>

#include <bsp.h>

#ifdef ARM_MULTILIB_ARCH_V7M

/* This is defined in clockdrv_shell.h */
static void Clock_isr(void *arg);

typedef struct {
  rtems_timecounter_simple base;
  void (*tick)(void);
  bool countflag;
} ARMV7M_Timecounter;

static ARMV7M_Timecounter _ARMV7M_TC;

static uint32_t _ARMV7M_TC_systick_get(rtems_timecounter_simple *tc)
{
  volatile ARMV7M_Systick *systick = _ARMV7M_Systick;

  return systick->cvr;
}

static bool _ARMV7M_TC_systick_is_pending(rtems_timecounter_simple *base)
{
  ARMV7M_Timecounter *tc = (ARMV7M_Timecounter *) base;
  rtems_interrupt_level level;
  bool countflag;

  rtems_interrupt_disable(level);

  countflag = tc->countflag;
  if (!countflag) {
    volatile ARMV7M_Systick *systick = _ARMV7M_Systick;

    countflag = ((systick->csr & ARMV7M_SYSTICK_CSR_COUNTFLAG) != 0);
    tc->countflag = countflag;
  }

  rtems_interrupt_enable(level);

  return countflag;
}

static uint32_t _ARMV7M_TC_systick_get_timecount(struct timecounter *tc)
{
  return rtems_timecounter_simple_downcounter_get(
    tc,
    _ARMV7M_TC_systick_get,
    _ARMV7M_TC_systick_is_pending
  );
}

static void _ARMV7M_TC_systick_at_tick(rtems_timecounter_simple *base)
{
  ARMV7M_Timecounter *tc = (ARMV7M_Timecounter *) base;
  volatile ARMV7M_Systick *systick = _ARMV7M_Systick;

  tc->countflag = false;

  /* Clear COUNTFLAG */
  systick->csr;
}

static void _ARMV7M_TC_systick_tick(void)
{
  rtems_timecounter_simple_downcounter_tick(
    &_ARMV7M_TC.base,
    _ARMV7M_TC_systick_get,
    _ARMV7M_TC_systick_at_tick
  );
}

static uint32_t _ARMV7M_TC_dwt_get_timecount(struct timecounter *tc)
{
  volatile ARMV7M_DWT *dwt = _ARMV7M_DWT;

  return dwt->cyccnt;
}

static void _ARMV7M_TC_dwt_tick(void)
{
  rtems_timecounter_tick();
}

static void _ARMV7M_TC_tick(void)
{
  (*_ARMV7M_TC.tick)();
}

static void _ARMV7M_Systick_handler(void)
{
  _ARMV7M_Interrupt_service_enter();
  Clock_isr(NULL);
  _ARMV7M_Interrupt_service_leave();
}

static void _ARMV7M_Systick_handler_install(void)
{
  _ARMV7M_Set_exception_priority_and_handler(
    ARMV7M_VECTOR_SYSTICK,
    BSP_ARMV7M_SYSTICK_PRIORITY,
    _ARMV7M_Systick_handler
  );
}

static void _ARMV7M_Systick_initialize(void)
{
  volatile ARMV7M_Systick *systick = _ARMV7M_Systick;
  #ifdef BSP_ARMV7M_SYSTICK_FREQUENCY
    uint64_t freq = BSP_ARMV7M_SYSTICK_FREQUENCY;
  #else
    uint64_t freq = ARMV7M_SYSTICK_CALIB_TENMS_GET(systick->calib) * 100ULL;
  #endif
  uint64_t us_per_tick = rtems_configuration_get_microseconds_per_tick();
  uint64_t interval = (freq * us_per_tick) / 1000000ULL;
  bool cyccnt_enabled;

  systick->rvr = (uint32_t) interval;
  systick->cvr = 0;
  systick->csr = ARMV7M_SYSTICK_CSR_ENABLE
    | ARMV7M_SYSTICK_CSR_TICKINT
    | ARMV7M_SYSTICK_CSR_CLKSOURCE;

  cyccnt_enabled = _ARMV7M_DWT_Enable_CYCCNT();
  if (cyccnt_enabled) {
    _ARMV7M_TC.base.tc.tc_get_timecount = _ARMV7M_TC_dwt_get_timecount;
    _ARMV7M_TC.base.tc.tc_counter_mask = 0xffffffff;
    _ARMV7M_TC.base.tc.tc_frequency = freq;
    _ARMV7M_TC.base.tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
    _ARMV7M_TC.tick = _ARMV7M_TC_dwt_tick;
    rtems_timecounter_install(&_ARMV7M_TC.base.tc);
  } else {
    _ARMV7M_TC.tick = _ARMV7M_TC_systick_tick;
    rtems_timecounter_simple_install(
      &_ARMV7M_TC.base,
      freq,
      interval,
      _ARMV7M_TC_systick_get_timecount
    );
  }
}

static void _ARMV7M_Systick_cleanup(void)
{
  volatile ARMV7M_Systick *systick = _ARMV7M_Systick;

  systick->csr = 0;
}

#define Clock_driver_timecounter_tick() _ARMV7M_TC_tick()

#define Clock_driver_support_initialize_hardware() \
  _ARMV7M_Systick_initialize()

#define Clock_driver_support_install_isr(isr) \
  _ARMV7M_Systick_handler_install()

#define Clock_driver_support_shutdown_hardware() \
  _ARMV7M_Systick_cleanup()

/* Include shared source clock driver code */
#include "../../../../shared/clockdrv_shell.h"

#endif /* ARM_MULTILIB_ARCH_V7M */