-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtick.c
79 lines (58 loc) · 1.35 KB
/
tick.c
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
#include <libopencm3/cm3/systick.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/cm3/cortex.h>
#include "tick.h"
#include "led.h"
void systick_disable(void)
{
systick_interrupt_disable();
systick_counter_disable();
}
void systick_setup(void)
{
systick_disable();
/* (ahb_frequency / 8) counts per second */
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8);
/* clear counter so it starts right away */
STK_CVR = 0;
systick_set_reload((rcc_ahb_frequency / 8) / TICK_HZ);
systick_interrupt_enable();
/* Start counting. */
systick_counter_enable();
}
volatile tick_t tick = { 0, 0, 0, 0, 0, 0 };
volatile uint32_t usecs = 0;
void sys_tick_handler(void)
{
static uint16_t div_1000ms = 0;
static uint16_t div_250ms = 0;
static uint16_t div_100ms = 0;
tick.flag_tick = 1;
tick.msec++;
usecs += 1000;
if (++div_1000ms >= SEC_TO_TICK(1)) {
div_1000ms = 0;
tick.flag_1000ms = 1;
tick.sec++;
tick.msec = 0;
}
if (++div_250ms >= MSEC_TO_TICK(250)) {
div_250ms = 0;
tick.flag_250ms = 1;
}
if (++div_100ms >= MSEC_TO_TICK(50)) {
div_100ms = 0;
tick.flag_100ms = 1;
}
led_tick();
}
uint32_t systick_get_usecs(void)
{
cm_disable_interrupts();
uint32_t v = systick_get_value();
uint32_t value = usecs;
cm_enable_interrupts();
value += (1000 * v) / systick_get_reload();
return value;
}