diff options
author | Daniel Friesel <derf@finalrewind.org> | 2018-11-15 15:37:32 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2018-11-15 15:37:32 +0100 |
commit | ec15c42ab8f5919d62d563798c62e12bed9c843e (patch) | |
tree | 2804cf0b51d0d805cd77a1504ad7a42126e3cf09 | |
parent | b2554d7bb3bbd1faf42737731f749b8a434e0db3 (diff) |
arduino-nano: Add counter driver. Conflicts with timer_s=1 / loop=1
-rw-r--r-- | include/arch/arduino-nano/driver/counter.h | 27 | ||||
-rw-r--r-- | src/arch/arduino-nano-168/Makefile.inc | 4 | ||||
-rw-r--r-- | src/arch/arduino-nano/Makefile.inc | 4 | ||||
-rw-r--r-- | src/arch/arduino-nano/driver/counter.cc | 10 |
4 files changed, 45 insertions, 0 deletions
diff --git a/include/arch/arduino-nano/driver/counter.h b/include/arch/arduino-nano/driver/counter.h new file mode 100644 index 0000000..d387a35 --- /dev/null +++ b/include/arch/arduino-nano/driver/counter.h @@ -0,0 +1,27 @@ +#include <avr/io.h> +#include <avr/interrupt.h> + +class Counter { + private: + Counter(const Counter ©); + + public: + uint8_t overflowed; + + Counter() : overflowed(0) {} + + inline void start() { + overflowed = 0; + TCNT1 = 0; + TCCR1A = 0; + TCCR1B = _BV(CS10); + TIMSK1 = _BV(TOIE1); + } + + inline uint16_t stop() { + TCCR1B = 0; + return TCNT1; + } +}; + +extern Counter counter; diff --git a/src/arch/arduino-nano-168/Makefile.inc b/src/arch/arduino-nano-168/Makefile.inc index 2f501f6..61492db 100644 --- a/src/arch/arduino-nano-168/Makefile.inc +++ b/src/arch/arduino-nano-168/Makefile.inc @@ -40,6 +40,10 @@ ifneq ($(findstring timer,${arch_drivers}), ) CXX_TARGETS += src/arch/arduino-nano/driver/timer.cc endif +ifneq ($(findstring counter,${arch_drivers}), ) + CXX_TARGETS += src/arch/arduino-nano/driver/counter.cc +endif + ifeq (${cpu_freq}, 16000000) uart_baud = 57600 else ifeq (${cpu_freq}, 8000000) diff --git a/src/arch/arduino-nano/Makefile.inc b/src/arch/arduino-nano/Makefile.inc index 30d9638..8896681 100644 --- a/src/arch/arduino-nano/Makefile.inc +++ b/src/arch/arduino-nano/Makefile.inc @@ -40,6 +40,10 @@ ifneq ($(findstring timer,${arch_drivers}), ) CXX_TARGETS += src/arch/arduino-nano/driver/timer.cc endif +ifneq ($(findstring counter,${arch_drivers}), ) + CXX_TARGETS += src/arch/arduino-nano/driver/counter.cc +endif + ifeq (${cpu_freq}, 16000000) uart_baud = 57600 else ifeq (${cpu_freq}, 8000000) diff --git a/src/arch/arduino-nano/driver/counter.cc b/src/arch/arduino-nano/driver/counter.cc new file mode 100644 index 0000000..4e62983 --- /dev/null +++ b/src/arch/arduino-nano/driver/counter.cc @@ -0,0 +1,10 @@ +#include "driver/counter.h" + +Counter counter; + +ISR(TIMER1_OVF_vect) +{ + if (counter.overflowed < 255) { + counter.overflowed++; + } +} |