summaryrefslogtreecommitdiffstats
path: root/app/ticker.c
blob: 791839e171b84a47aa791605da30ed555cafd11a (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
83
84
85
#include "project.h"

static volatile uint32_t delay_ms_count;
static uint32_t scale = 35;

volatile uint32_t ticks;

void
delay_us (uint32_t d)
{
  d *= scale;

  while (d--)
    __asm__ ("nop");
}

void
sys_tick_handler (void)
{
  static int slow;

  if (delay_ms_count)
    delay_ms_count--;

  ticks++;

  led_tick();
  cdcacm_tick();

  slow++;

  if (slow < 1000)
    return;

  slow = 0;

  ptp_slow_tick();

  ref_slow_tick();
  steth_slow_tick();
  led_slow_tick();
}



void
delay_ms (uint32_t d)
{
  delay_ms_count = d;

  while (delay_ms_count);
}


void
ticker_init (void)
{
  uint32_t v, w;

  /*168MHz 1ms */
  systick_set_reload (168000);
  systick_set_clocksource (STK_CSR_CLKSOURCE_AHB);
  systick_counter_enable();
  /* this done last */
  systick_interrupt_enable();


  /*Calibrate the delay loop */

  do {
    scale--;
    v = ticks;

    while (v == ticks);

    delay_us (1000);
    w = ticks;
    v++;
    w -= v;
  } while (w);

  SCS_DEMCR |= SCS_DEMCR_TRCENA;
  SCS_DWT_CTRL |= SCS_DWT_CTRL_CYCCNTENA;

}