summaryrefslogtreecommitdiffstats
path: root/app/sysclk.c
blob: 98b5ed8952f6aaf57921aec81df6ebce3fb7b4ac (plain)
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
#include "project.h"



static Event_ring sysclk_ring;

void sysclk_event (void)
{
  uint32_t refclk_now = HW_CLOCK_REG;
  uint32_t sysclk_now = SCS_DWT_CYCCNT;


  sysclk_ring.events[sysclk_ring.tx_ptr].when = refclk_now;
  sysclk_ring.events[sysclk_ring.tx_ptr].ptp = ptp_get();
  sysclk_ring.events[sysclk_ring.tx_ptr].value = sysclk_now;
  sysclk_ring.tx_ptr = (sysclk_ring.tx_ptr + 1) & ERING_MASK;

}

static uint32_t high_tick;

#define QUARTER (1UL << 29)
#define HALF (1UL << 30)
#define THREE_QUARTERS (HALF+QUARTER)
#define ONE (~(uint32_t)0)

uint64_t sysclk_extend (uint32_t now)
{
  static int m;
  uint64_t ret;


  if (!m) {
    ret = high_tick;
    ret <<= 32;
    ret |= now;


    if ((now > THREE_QUARTERS) && (now <= ONE)) {
      high_tick++;
      m = 1;
    }

  } else {
    if (now < HALF) {
      ret = high_tick;
      ret <<= 32;
      ret |= now;
    } else {
      ret = high_tick - 1;
      ret <<= 32;
      ret |= now;
    }

    if ((now > QUARTER) && (now < HALF))
      m = 0;
  }

  return ret;
}
void sysclk_dispatch (void)
{
  //char buf[80];
  uint64_t refclk_abs;
  uint32_t refclk_now, sysclk_now;
  EPOCH e;
  //UTC u;

  if (sysclk_ring.rx_ptr == sysclk_ring.tx_ptr) return;

  sysclk_now = sysclk_ring.events[sysclk_ring.rx_ptr].value;
  refclk_now = sysclk_ring.events[sysclk_ring.rx_ptr].when;
  sysclk_ring.rx_ptr = (sysclk_ring.rx_ptr + 1) & ERING_MASK;

  refclk_abs = ref_extend (refclk_now);
  e.s = sysclk_extend (sysclk_now);
  e.ns = 0;

  report_time ("SYS", e, refclk_abs, "");
}