librfn
An ad-hoc utility library
time_libopencm3.c
Go to the documentation of this file.
1 /*
2  * time_libopencm3.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2014 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #include "librfn/time.h"
15 
16 #include <libopencm3/cm3/nvic.h>
17 #include <libopencm3/cm3/systick.h>
18 
19 #ifndef F_CPU
20 #error F_CPU is not defined
21 #endif
22 
23 
24 static uint64_t sys_tick_counter;
25 
26 void time_init()
27 {
28  /* Set the systick to interrupt once each millisecond */
29  systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8);
30  systick_set_reload((F_CPU / (8 * 1000))-1);
31 
32  /* Start counting. */
33  systick_interrupt_enable();
34  systick_counter_enable();
35 }
36 
37 void sys_tick_handler(void)
38 {
39  sys_tick_counter += 1000;
40 }
41 
42 uint32_t time_now()
43 {
44  return sys_tick_counter;
45 }
46 
47 
48 uint64_t time64_now()
49 {
50  return sys_tick_counter;
51 }
uint64_t time64_now()
uint32_t time_now()
void sys_tick_handler(void)
void time_init()