summaryrefslogtreecommitdiff
path: root/include/arch/esp8266/driver/counter.h
blob: beb36e59ee92321567cd78571dd4c7856b98371c (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
#ifndef COUNTER_H
#define COUNTER_H

extern "C" {
#include "osapi.h"
#include "user_interface.h"
}
#include "c_types.h"

typedef uint32_t counter_value_t;
typedef uint32_t counter_overflow_t;

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

	public:
		uint32_t value;
		uint32_t overflow;

		Counter() : start_cycles(0), value(0), overflow(0) {}

		inline void start() {
			asm volatile ("esync; rsr %0,ccount":"=a" (start_cycles));
		}

		inline void stop() {
			uint32_t stop_cycles;
			asm volatile ("esync; rsr %0,ccount":"=a" (stop_cycles));
			if (stop_cycles > start_cycles) {
				value = stop_cycles - start_cycles;
			} else {
				overflow = 1;
			}
		}
};

extern Counter counter;

#endif