From f7e2accd8de2b7b38a6f36940413ffe75908007b Mon Sep 17 00:00:00 2001 From: midilab Date: Mon, 26 Oct 2020 10:29:17 -0400 Subject: [PATCH] added support for teensy 2.x avr series. revise interruptions variable access and volatilize those in needed of it. --- library.properties | 2 +- src/uClock.cpp | 29 ++++++++++++++--------------- src/uClock.h | 14 +++++++++----- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/library.properties b/library.properties index 2c30a06..630f2cd 100755 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=uClock -version=0.9.0 +version=0.9.1 author=Romulo Silva , Manuel Odendahl maintainer=Romulo Silva sentence=BPM clock generator for Arduino and Teensy boards diff --git a/src/uClock.cpp b/src/uClock.cpp index a91c477..2641dcd 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -34,7 +34,7 @@ // Timer setup // Work clock at: 62.5kHz/16usec // -#if defined(TEENSYDUINO) +#if defined(TEENSYDUINO) && !defined(__AVR_ATmega32U4__) IntervalTimer _teensyTimer; void teensyInterrupt(); void initTeensyTimer() @@ -73,7 +73,7 @@ void initArduinoTimer() #endif void initWorkTimer() { -#if defined(TEENSYDUINO) +#if defined(TEENSYDUINO) && !defined(__AVR_ATmega32U4__) initTeensyTimer(); #else initArduinoTimer(); @@ -104,6 +104,8 @@ uClockClass::uClockClass() drift = 11; pll_x = 220; start_timer = 0; + last_interval = 0; + sync_interval = 0; state = PAUSED; mode = INTERNAL_CLOCK; resetCounters(); @@ -240,11 +242,9 @@ void uClockClass::shuffle() void uClockClass::handleExternalClock() { - uint16_t cur_clock = _clock; - uint16_t diff = clock_diff(last_clock, cur_clock); - - last_interval = diff; - last_clock = cur_clock; + last_interval = clock_diff(last_clock, _clock); + last_clock = _clock; + indiv96th_counter++; inmod6_counter++; @@ -263,9 +263,9 @@ void uClockClass::handleExternalClock() case STARTED: if (indiv96th_counter == 2) { - interval = diff; + interval = last_interval; } else { - interval = (((uint32_t)interval * (uint32_t)pll_x) + (uint32_t)(256 - pll_x) * (uint32_t)diff) >> 8; + interval = (((uint32_t)interval * (uint32_t)pll_x) + (uint32_t)(256 - pll_x) * (uint32_t)last_interval) >> 8; } break; } @@ -303,17 +303,16 @@ void uClockClass::handleTimerInt() mod6_counter++; if (mode == EXTERNAL_CLOCK) { - uint16_t cur_clock = _clock; - uint16_t diff = clock_diff(last_clock, cur_clock); + sync_interval = clock_diff(last_clock, _clock); if ((div96th_counter < indiv96th_counter) || (div96th_counter > (indiv96th_counter + 1))) { div96th_counter = indiv96th_counter; mod6_counter = inmod6_counter; } if (div96th_counter <= indiv96th_counter) { - counter -= phase_mult(diff); + counter -= phase_mult(sync_interval); } else { - if (counter > diff) { - counter += phase_mult(counter - diff); + if (counter > sync_interval) { + counter += phase_mult(counter - sync_interval); } } } @@ -382,7 +381,7 @@ volatile uint32_t _timer = 0; // TIMER INTERRUPT HANDLER // Clocked at: 62.5kHz/16usec // -#if defined(TEENSYDUINO) +#if defined(TEENSYDUINO) && !defined(__AVR_ATmega32U4__) void teensyInterrupt() #else ISR(TIMER1_OVF_vect) diff --git a/src/uClock.h b/src/uClock.h index 8944478..20fa059 100755 --- a/src/uClock.h +++ b/src/uClock.h @@ -62,22 +62,26 @@ class uClockClass { void (*onClockStartCallback)(); void (*onClockStopCallback)(); + volatile uint8_t inmod6_counter; + volatile uint32_t indiv96th_counter; + volatile uint16_t interval; + volatile uint16_t last_clock; + uint32_t div96th_counter; uint32_t div32th_counter; uint32_t div16th_counter; uint8_t mod6_counter; - uint8_t inmod6_counter; - volatile uint16_t interval; uint16_t counter; - uint16_t last_clock; - uint16_t last_interval; - uint32_t indiv96th_counter; + uint16_t pll_x; uint8_t drift; uint16_t tempo; uint32_t start_timer; uint8_t mode; + uint16_t last_interval; + uint16_t sync_interval; + public: uint8_t state;