summaryrefslogtreecommitdiffstats
path: root/stm32/app/ticker.c
blob: f89d855f3757923a66197b8916f91645903051d8 (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
#include "project.h"


volatile uint32_t ticks;
static uint32_t scale = 7;




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

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

void
sys_tick_handler (void)
{
  ticks++;
  cdcacm_tick();
  hands_tick();
  motor_tick();

  led_tick();
}

void
ticker_init (void)
{
  uint32_t v, w;
  /*Start periodic timer */
  systick_set_clocksource (STK_CSR_CLKSOURCE_AHB_DIV8);
  /* 72MHz / 8 = > 9Mhz */
  systick_set_reload (9000);
  /* 9MHz / 9000 => 1kHz */
  systick_interrupt_enable();
  systick_counter_enable();

  /*Calibrate the delay loop */
  do {
    scale--;
    v = ticks;

    while (v == ticks)
      ;

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

}


void delay_ms (uint32_t d)
{
  uint32_t v = ticks;

  while ((ticks - v) < d);
}