summaryrefslogtreecommitdiffstats
path: root/bsps/arm/include/bsp/clock-armv7m.h
blob: 317502734ee29ff1db809a658a832d8726ca5d39 (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
/*
 * Copyright (c) 2011, 2018 Sebastian Huber.  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.
 */

#ifndef BSP_CLOCK_ARMV7M_H
#define BSP_CLOCK_ARMV7M_H

#include <rtems/score/armv7m.h>
#include <rtems/timecounter.h>

#include <bsp.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#ifdef ARM_MULTILIB_ARCH_V7M

typedef struct {
  struct timecounter base;
  uint32_t ticks;
} ARMV7M_Timecounter;

extern ARMV7M_Timecounter _ARMV7M_TC;

static inline uint32_t _ARMV7M_Clock_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
}

static uint32_t _ARMV7M_Clock_counter(ARMV7M_Timecounter *tc)
{
  volatile ARMV7M_Systick *systick;
  rtems_interrupt_level level;
  uint32_t interval;
  uint32_t counter;
  uint32_t ticks;
  uint32_t csr;

  rtems_interrupt_disable(level);
  systick = _ARMV7M_Systick;
  counter = systick->cvr;
  csr = systick->csr;
  interval = systick->rvr;
  ticks = tc->ticks;

  if (RTEMS_PREDICT_FALSE((csr & ARMV7M_SYSTICK_CSR_COUNTFLAG) != 0)) {
    counter = systick->cvr;
    ticks += interval;
    tc->ticks = ticks;
  }

  counter = interval - counter + ticks;
  rtems_interrupt_enable(level);

  return counter;
}

#endif /* ARM_MULTILIB_ARCH_V7M */

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* BSP_CLOCK_ARMV7M_H */