]> cloudbase.mooo.com Git - irmp-demo.git/blame - timer.c
Add .gitignore
[irmp-demo.git] / timer.c
CommitLineData
b1a276a1 1/*
b8dc4070 2 * (C) Copyright 20014 - 2017 Leo C. <erbl259-lmu@yahoo.de>
b1a276a1
L
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7#include "timer.h"
8#include <libopencm3/cm3/nvic.h>
9#include <libopencm3/cm3/systick.h>
10#include <libopencm3/stm32/rcc.h>
11#include <libopencm3/stm32/gpio.h>
12
13
b8dc4070
L
14/*
15 * timer interrupt/overflow counter, counts up every ms.
16 */
17static
18volatile uint32_t timestamp;
b1a276a1
L
19
20void systick_setup(void)
21{
22 /* SysTick interrupt every N clock pulses: set reload to N-1 */
23 STK_RVR = rcc_ahb_frequency/1000 - 1;
24
25 /* Set source to core clock, enable int and start counting. */
26 STK_CSR = STK_CSR_CLKSOURCE_AHB | STK_CSR_TICKINT | STK_CSR_ENABLE;
27}
28
29/*--------------------------------------------------------------------------*/
30
31/*
b1a276a1
L
32 * 1000Hz timer interrupt generated by System Timer
33 */
34void sys_tick_handler(void)
35{
36 ++timestamp;
37}
38
39/*--------------------------------------------------------------------------*/
40
b8dc4070
L
41/*
42 * Return elapsed time in ms since base.
43 */
b1a276a1
L
44uint32_t get_timer(uint32_t base)
45{
46 return timestamp - base;
47}