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

#include <msp430.h>
#include <stdint.h>

typedef uint16_t counter_value_t;
typedef uint16_t counter_overflow_t;

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

	public:
		uint16_t value;
		volatile uint16_t overflow;

		Counter() : overflow(0) {}

		inline void start() {
			overflow = 0;
			TA2CTL = TASSEL__SMCLK | ID__1 | MC__CONTINUOUS;
			TA2EX0 = 0;
			TA2CTL |= TACLR | TAIE;
			asm volatile("nop");
			__eint();
			asm volatile("nop");
		}

		inline void stop() {
			TA2CTL = 0;
			value = TA2R;
			__dint();
		}
};

extern Counter counter;

#endif