added support for teensy 2.x avr series. revise interruptions variable access and volatilize those in needed of it.

pull/7/head 0.9.1
midilab 4 years ago
parent 1c74658a9a
commit f7e2accd8d
  1. 2
      library.properties
  2. 29
      src/uClock.cpp
  3. 14
      src/uClock.h

@ -1,5 +1,5 @@
name=uClock name=uClock
version=0.9.0 version=0.9.1
author=Romulo Silva <contact@midilab.co>, Manuel Odendahl <wesen@ruinwesen.com> author=Romulo Silva <contact@midilab.co>, Manuel Odendahl <wesen@ruinwesen.com>
maintainer=Romulo Silva <contact@midilab.co> maintainer=Romulo Silva <contact@midilab.co>
sentence=BPM clock generator for Arduino and Teensy boards sentence=BPM clock generator for Arduino and Teensy boards

@ -34,7 +34,7 @@
// Timer setup // Timer setup
// Work clock at: 62.5kHz/16usec // Work clock at: 62.5kHz/16usec
// //
#if defined(TEENSYDUINO) #if defined(TEENSYDUINO) && !defined(__AVR_ATmega32U4__)
IntervalTimer _teensyTimer; IntervalTimer _teensyTimer;
void teensyInterrupt(); void teensyInterrupt();
void initTeensyTimer() void initTeensyTimer()
@ -73,7 +73,7 @@ void initArduinoTimer()
#endif #endif
void initWorkTimer() { void initWorkTimer() {
#if defined(TEENSYDUINO) #if defined(TEENSYDUINO) && !defined(__AVR_ATmega32U4__)
initTeensyTimer(); initTeensyTimer();
#else #else
initArduinoTimer(); initArduinoTimer();
@ -104,6 +104,8 @@ uClockClass::uClockClass()
drift = 11; drift = 11;
pll_x = 220; pll_x = 220;
start_timer = 0; start_timer = 0;
last_interval = 0;
sync_interval = 0;
state = PAUSED; state = PAUSED;
mode = INTERNAL_CLOCK; mode = INTERNAL_CLOCK;
resetCounters(); resetCounters();
@ -240,11 +242,9 @@ void uClockClass::shuffle()
void uClockClass::handleExternalClock() void uClockClass::handleExternalClock()
{ {
uint16_t cur_clock = _clock; last_interval = clock_diff(last_clock, _clock);
uint16_t diff = clock_diff(last_clock, cur_clock); last_clock = _clock;
last_interval = diff;
last_clock = cur_clock;
indiv96th_counter++; indiv96th_counter++;
inmod6_counter++; inmod6_counter++;
@ -263,9 +263,9 @@ void uClockClass::handleExternalClock()
case STARTED: case STARTED:
if (indiv96th_counter == 2) { if (indiv96th_counter == 2) {
interval = diff; interval = last_interval;
} else { } 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; break;
} }
@ -303,17 +303,16 @@ void uClockClass::handleTimerInt()
mod6_counter++; mod6_counter++;
if (mode == EXTERNAL_CLOCK) { if (mode == EXTERNAL_CLOCK) {
uint16_t cur_clock = _clock; sync_interval = clock_diff(last_clock, _clock);
uint16_t diff = clock_diff(last_clock, cur_clock);
if ((div96th_counter < indiv96th_counter) || (div96th_counter > (indiv96th_counter + 1))) { if ((div96th_counter < indiv96th_counter) || (div96th_counter > (indiv96th_counter + 1))) {
div96th_counter = indiv96th_counter; div96th_counter = indiv96th_counter;
mod6_counter = inmod6_counter; mod6_counter = inmod6_counter;
} }
if (div96th_counter <= indiv96th_counter) { if (div96th_counter <= indiv96th_counter) {
counter -= phase_mult(diff); counter -= phase_mult(sync_interval);
} else { } else {
if (counter > diff) { if (counter > sync_interval) {
counter += phase_mult(counter - diff); counter += phase_mult(counter - sync_interval);
} }
} }
} }
@ -382,7 +381,7 @@ volatile uint32_t _timer = 0;
// TIMER INTERRUPT HANDLER // TIMER INTERRUPT HANDLER
// Clocked at: 62.5kHz/16usec // Clocked at: 62.5kHz/16usec
// //
#if defined(TEENSYDUINO) #if defined(TEENSYDUINO) && !defined(__AVR_ATmega32U4__)
void teensyInterrupt() void teensyInterrupt()
#else #else
ISR(TIMER1_OVF_vect) ISR(TIMER1_OVF_vect)

@ -62,22 +62,26 @@ class uClockClass {
void (*onClockStartCallback)(); void (*onClockStartCallback)();
void (*onClockStopCallback)(); 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 div96th_counter;
uint32_t div32th_counter; uint32_t div32th_counter;
uint32_t div16th_counter; uint32_t div16th_counter;
uint8_t mod6_counter; uint8_t mod6_counter;
uint8_t inmod6_counter;
volatile uint16_t interval;
uint16_t counter; uint16_t counter;
uint16_t last_clock;
uint16_t last_interval;
uint32_t indiv96th_counter;
uint16_t pll_x; uint16_t pll_x;
uint8_t drift; uint8_t drift;
uint16_t tempo; uint16_t tempo;
uint32_t start_timer; uint32_t start_timer;
uint8_t mode; uint8_t mode;
uint16_t last_interval;
uint16_t sync_interval;
public: public:
uint8_t state; uint8_t state;

Loading…
Cancel
Save