summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/arch/esp8266/driver/counter.h36
-rw-r--r--src/arch/esp8266/driver/counter.cc3
2 files changed, 39 insertions, 0 deletions
diff --git a/include/arch/esp8266/driver/counter.h b/include/arch/esp8266/driver/counter.h
new file mode 100644
index 0000000..754f4d6
--- /dev/null
+++ b/include/arch/esp8266/driver/counter.h
@@ -0,0 +1,36 @@
+#ifndef COUNTER_H
+#define COUNTER_H
+
+extern "C" {
+#include "osapi.h"
+#include "user_interface.h"
+}
+#include "c_types.h"
+
+class Counter {
+ private:
+ Counter(const Counter &copy);
+ uint32_t start_cycles;
+
+ public:
+ Counter() : start_cycles(0) {}
+ uint32_t overflowed;
+
+ inline void start() {
+ asm volatile ("esync; rsr %0,ccount":"=a" (start_cycles));
+ }
+
+ inline uint32_t stop() {
+ uint32_t stop_cycles;
+ asm volatile ("esync; rsr %0,ccount":"=a" (stop_cycles));
+ if (stop_cycles > start_cycles) {
+ return stop_cycles - start_cycles;
+ } else {
+ return 0;
+ }
+ }
+};
+
+extern Counter counter;
+
+#endif
diff --git a/src/arch/esp8266/driver/counter.cc b/src/arch/esp8266/driver/counter.cc
new file mode 100644
index 0000000..17a0f95
--- /dev/null
+++ b/src/arch/esp8266/driver/counter.cc
@@ -0,0 +1,3 @@
+#include "driver/counter.h"
+
+Counter counter;