summaryrefslogtreecommitdiff
path: root/include/arch
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2018-10-12 10:44:11 +0200
committerDaniel Friesel <derf@finalrewind.org>2018-10-12 10:44:11 +0200
commitfd1aabd0417bef53e7ff54df52eba80be5494d56 (patch)
treee0cb6183e82f1fbf78fb66c673eee8cb497bc658 /include/arch
parent4853a5593cc6147e1851e537b46a5128792939a5 (diff)
half-baked timer implementation for MSP430 and Arduino
Diffstat (limited to 'include/arch')
-rw-r--r--include/arch/arduino-nano/driver/timer.h15
-rw-r--r--include/arch/msp430fr5969lp/driver/timer.h40
2 files changed, 51 insertions, 4 deletions
diff --git a/include/arch/arduino-nano/driver/timer.h b/include/arch/arduino-nano/driver/timer.h
index 267cb1d..98ef20e 100644
--- a/include/arch/arduino-nano/driver/timer.h
+++ b/include/arch/arduino-nano/driver/timer.h
@@ -1,23 +1,30 @@
#include <avr/io.h>
#include <avr/interrupt.h>
-#define ON_TIMER_INTERRUPT ISR(TIMER0_COMPA_vect)
+#define ON_TIMER_INTERRUPT_head ISR(TIMER0_COMPA_vect) {
+#define ON_TIMER_INTERRUPT_tail }
class Timer {
private:
Timer(const Timer &copy);
-
+ unsigned char prescaler;
public:
Timer() {}
- inline void setup(unsigned char const frequency) {
+ inline void setup_khz(unsigned char const frequency) {
+ OCR0A = frequency ? 255 / frequency : 1;
+ TCCR0A = _BV(WGM01);
+ prescaler = _BV(CS01) | _BV(CS00);
+ }
+ inline void setup_hz(unsigned char const frequency) {
OCR0A = frequency ? 255 / frequency : 1;
TCCR0A = _BV(WGM01);
+ prescaler = _BV(CS02) | _BV(CS00);
}
inline void start(unsigned char const interrupt) {
TCNT0 = 0;
- TCCR0B = _BV(CS01) | _BV(CS00);
+ TCCR0B = prescaler;
if (interrupt) {
TIMSK0 = _BV(OCIE0A);
}
diff --git a/include/arch/msp430fr5969lp/driver/timer.h b/include/arch/msp430fr5969lp/driver/timer.h
new file mode 100644
index 0000000..34feb12
--- /dev/null
+++ b/include/arch/msp430fr5969lp/driver/timer.h
@@ -0,0 +1,40 @@
+#include <msp430.h>
+
+#define ON_TIMER_INTERRUPT_head __attribute__((interrupt(TIMER0_A1_VECTOR))) __attribute__((wakeup)) void handle_timer0_overflow() { if (TA0IV == 0x0e) {
+#define ON_TIMER_INTERRUPT_tail } }
+
+class Timer {
+ private:
+ Timer(const Timer &copy);
+
+ public:
+ Timer() {}
+
+ inline void setup_khz(uint16_t const frequency) {
+ TA0CTL = TASSEL__SMCLK | ID__8 | MC__UP;
+ TA0EX0 = 1;
+ TA0CCR0 = 1000 / frequency;
+ TA0CTL |= TACLR;
+ }
+
+ inline void setup_hz(uint16_t const frequency) {
+ TA0CTL = TASSEL__SMCLK | ID__8 | MC__UP;
+ TA0EX0 = 1;
+ TA0CCR0 = 1000000 / frequency;
+ TA0CTL |= TACLR;
+ }
+
+ inline void start(unsigned char const interrupt) {
+ if (interrupt) {
+ TA0CTL |= TACLR | TAIE;
+ } else {
+ TA0CTL |= TACLR;
+ }
+ }
+
+ inline void stop() {
+ TA0CTL = 0;
+ }
+};
+
+extern Timer timer;