summaryrefslogtreecommitdiff
path: root/include/arch/rm46l8lp/driver/counter.h
blob: ee4b595d5668e613da0c25b01c97df3c729cbe4c (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 "rti.h"
#include <stdint.h>

typedef uint32_t counter_value_t;
typedef uint32_t counter_overflow_t;

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

	public:
		uint32_t value;
		uint32_t overflow;

		Counter() : overflow(0) {}

		inline void start() {
			rtiREG1->CNT[0].UCx = 0;
			rtiREG1->CNT[0].FRCx = 0;
			rtiREG1->GCTRL |= ((uint32)1 << (rtiCOUNTER_BLOCK0 & 3));
		}

		inline void stop() {
			rtiREG1->GCTRL &= ~(uint32)((uint32)1 << (rtiCOUNTER_BLOCK0 & 3));
			overflow = rtiREG1->CNT[0].FRCx;
			value = rtiREG1->CNT[0].UCx;
		}
};

extern Counter counter;

#endif