summaryrefslogtreecommitdiff
path: root/include/arch/stm32f446re-nucleo/driver/counter.h
blob: f7d1e09fca23059c65542b0bb61f8c430b62d382 (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
/*
 * Copyright 2020 Daniel Friesel
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */
#ifndef COUNTER_H
#define COUNTER_H

#include <libopencm3/stm32/timer.h>

#include "arch.h"

typedef uint32_t counter_value_t;
typedef uint32_t counter_overflow_t;

class Counter {
	private:
		Counter(const Counter &copy);

	public:
		counter_value_t value;
		volatile counter_overflow_t overflow;

		Counter() : overflow(0) {}

		inline void start() {
			overflow = 0;
			timer_set_counter(TIM2, 0);
			timer_enable_counter(TIM2);
		}

		inline void stop() {
			timer_disable_counter(TIM2);
			value = timer_get_counter(TIM2);
		}
};

extern Counter counter;

#endif