|
|
@ -3,7 +3,7 @@ |
|
|
|
* Project BPM clock generator for Arduino |
|
|
|
* Project BPM clock generator for Arduino |
|
|
|
* @brief A Library to implement BPM clock tick calls using hardware timer1 interruption. Tested on ATmega168/328, ATmega16u4/32u4 and ATmega2560. |
|
|
|
* @brief A Library to implement BPM clock tick calls using hardware timer1 interruption. Tested on ATmega168/328, ATmega16u4/32u4 and ATmega2560. |
|
|
|
* Derived work from mididuino MidiClock class. (c) 2008 - 2011 - Manuel Odendahl - wesen@ruinwesen.com |
|
|
|
* Derived work from mididuino MidiClock class. (c) 2008 - 2011 - Manuel Odendahl - wesen@ruinwesen.com |
|
|
|
* @version 0.10.0 |
|
|
|
* @version 0.10.2 |
|
|
|
* @author Romulo Silva |
|
|
|
* @author Romulo Silva |
|
|
|
* @date 08/21/2020 |
|
|
|
* @date 08/21/2020 |
|
|
|
* @license MIT - (c) 2020 - Romulo Silva - contact@midilab.co |
|
|
|
* @license MIT - (c) 2020 - Romulo Silva - contact@midilab.co |
|
|
@ -36,93 +36,50 @@ |
|
|
|
#if defined(TEENSYDUINO) && !defined(__AVR_ATmega32U4__) |
|
|
|
#if defined(TEENSYDUINO) && !defined(__AVR_ATmega32U4__) |
|
|
|
IntervalTimer _teensyTimer; |
|
|
|
IntervalTimer _teensyTimer; |
|
|
|
void teensyInterrupt(); |
|
|
|
void teensyInterrupt(); |
|
|
|
void workClock(uint32_t freq_resolution) |
|
|
|
void workClock() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// fallback default frequency (CLOCK_250000HZ) if no requested freq available
|
|
|
|
_teensyTimer.begin(teensyInterrupt, 16); |
|
|
|
uint8_t microseconds = 4; |
|
|
|
// Set the interrupt priority level, controlling which other interrupts
|
|
|
|
const bool running = false; |
|
|
|
// this timer is allowed to interrupt. Lower numbers are higher priority,
|
|
|
|
|
|
|
|
// with 0 the highest and 255 the lowest. Most other interrupts default to 128.
|
|
|
|
switch(freq_resolution) { |
|
|
|
// As a general guideline, interrupt routines that run longer should be given
|
|
|
|
case CLOCK_62500HZ: |
|
|
|
// lower priority (higher numerical values).
|
|
|
|
microseconds = 16; |
|
|
|
_teensyTimer.priority(0); |
|
|
|
break; |
|
|
|
|
|
|
|
case CLOCK_125000HZ: |
|
|
|
|
|
|
|
microseconds = 8; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case CLOCK_250000HZ: |
|
|
|
|
|
|
|
microseconds = 4; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (running) { |
|
|
|
|
|
|
|
_teensyTimer.update(microseconds); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
_teensyTimer.begin(teensyInterrupt, microseconds); |
|
|
|
|
|
|
|
// Set the interrupt priority level, controlling which other interrupts
|
|
|
|
|
|
|
|
// this timer is allowed to interrupt. Lower numbers are higher priority,
|
|
|
|
|
|
|
|
// with 0 the highest and 255 the lowest. Most other interrupts default to 128.
|
|
|
|
|
|
|
|
// As a general guideline, interrupt routines that run longer should be given
|
|
|
|
|
|
|
|
// lower priority (higher numerical values).
|
|
|
|
|
|
|
|
_teensyTimer.priority(0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
#else |
|
|
|
#else |
|
|
|
void workClock(uint32_t freq_resolution) |
|
|
|
void workClock() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// fallback default frequency (CLOCK_62500HZ) if no requested freq available
|
|
|
|
ATOMIC( |
|
|
|
uint8_t comparator = 255; |
|
|
|
|
|
|
|
const bool running = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch(freq_resolution) { |
|
|
|
|
|
|
|
case CLOCK_62500HZ: |
|
|
|
|
|
|
|
comparator = 255; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
//case CLOCK_125000HZ:
|
|
|
|
|
|
|
|
// comparator = 127;
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
//case CLOCK_250000HZ:
|
|
|
|
|
|
|
|
// comparator = 63;
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (running) { |
|
|
|
|
|
|
|
// update comparator speed of our internal clock system
|
|
|
|
|
|
|
|
OCR1A = comparator; |
|
|
|
|
|
|
|
//OCR2A = comparator;
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// Timer1
|
|
|
|
// Timer1
|
|
|
|
TCCR1A = 0; |
|
|
|
TCCR1A = 0; |
|
|
|
TCCR1B = 0; |
|
|
|
TCCR1B = 0; |
|
|
|
TCNT1 = 0; |
|
|
|
TCNT1 = 0; |
|
|
|
// set the speed of our internal clock system
|
|
|
|
// set the speed of our internal clock system
|
|
|
|
OCR1A = comparator; |
|
|
|
OCR1A = 255; |
|
|
|
// turn on CTC mode
|
|
|
|
// turn on CTC mode
|
|
|
|
TCCR1B |= (1 << WGM12); |
|
|
|
TCCR1B |= (1 << WGM12); |
|
|
|
// Set CS12, CS11 and CS10 bits for 1 prescaler
|
|
|
|
// Set CS12, CS11 and CS10 bits for 1 prescaler
|
|
|
|
TCCR1B |= (0 << CS12) | (0 << CS11) | (1 << CS10); |
|
|
|
TCCR1B |= (0 << CS12) | (0 << CS11) | (1 << CS10); |
|
|
|
// enable timer compare interrupt
|
|
|
|
// enable timer compare interrupt
|
|
|
|
TIMSK1 |= (1 << OCIE1A); |
|
|
|
TIMSK1 |= (1 << OCIE1A); |
|
|
|
|
|
|
|
) |
|
|
|
/*
|
|
|
|
/*
|
|
|
|
|
|
|
|
ATOMIC( |
|
|
|
// Timer2
|
|
|
|
// Timer2
|
|
|
|
TCCR2A = 0; |
|
|
|
TCCR2A = 0; |
|
|
|
TCCR2B = 0; |
|
|
|
TCCR2B = 0; |
|
|
|
TCNT2 = 0; |
|
|
|
TCNT2 = 0; |
|
|
|
// set the speed of our internal clock system
|
|
|
|
// set the speed of our internal clock system
|
|
|
|
OCR2A = comparator; |
|
|
|
OCR2A = 255; |
|
|
|
// turn on CTC mode
|
|
|
|
// turn on CTC mode
|
|
|
|
TCCR2B |= (1 << WGM21); |
|
|
|
TCCR2B |= (1 << WGM21); |
|
|
|
// Set CS22, CS21 and CS20 bits for 1 prescaler
|
|
|
|
// Set CS22, CS21 and CS20 bits for 1 prescaler
|
|
|
|
TCCR2B |= (0 << CS22) | (0 << CS21) | (1 << CS20); |
|
|
|
TCCR2B |= (0 << CS22) | (0 << CS21) | (1 << CS20); |
|
|
|
// enable timer compare interrupt
|
|
|
|
// enable timer compare interrupt
|
|
|
|
TIMSK2 |= (1 << OCIE2A); |
|
|
|
TIMSK2 |= (1 << OCIE2A); |
|
|
|
*/ |
|
|
|
) |
|
|
|
} |
|
|
|
*/ |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
@ -144,16 +101,10 @@ static inline uint16_t clock_diff(uint16_t old_clock, uint16_t new_clock) |
|
|
|
|
|
|
|
|
|
|
|
uClockClass::uClockClass() |
|
|
|
uClockClass::uClockClass() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// some tested values
|
|
|
|
// drift is used to sligth calibrate with your slave clock
|
|
|
|
// 1 is good for native 31250bps midi interface
|
|
|
|
drift = 1; |
|
|
|
// 4 is good for usb-to-midi hid on leonardo
|
|
|
|
slave_drift = 0; |
|
|
|
// (6, 1) is good on teensy lc usb midi
|
|
|
|
|
|
|
|
// internal drift is used to calibrate master clock
|
|
|
|
|
|
|
|
internal_drift = 1; |
|
|
|
|
|
|
|
// internal drift is used to calibrate slave clock
|
|
|
|
|
|
|
|
external_drift = 1; |
|
|
|
|
|
|
|
tempo = 120; |
|
|
|
tempo = 120; |
|
|
|
pll_x = 220; |
|
|
|
|
|
|
|
start_timer = 0; |
|
|
|
start_timer = 0; |
|
|
|
last_interval = 0; |
|
|
|
last_interval = 0; |
|
|
|
sync_interval = 0; |
|
|
|
sync_interval = 0; |
|
|
@ -162,58 +113,14 @@ uClockClass::uClockClass() |
|
|
|
ext_interval_acc = 0; |
|
|
|
ext_interval_acc = 0; |
|
|
|
resetCounters(); |
|
|
|
resetCounters(); |
|
|
|
|
|
|
|
|
|
|
|
onClock96PPQNCallback = NULL; |
|
|
|
|
|
|
|
onClock32PPQNCallback = NULL; |
|
|
|
|
|
|
|
onClock16PPQNCallback = NULL; |
|
|
|
|
|
|
|
onClockStartCallback = NULL; |
|
|
|
|
|
|
|
onClockStopCallback = NULL; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// set initial default clock operate frequency
|
|
|
|
|
|
|
|
// to higher one. If you experience problems
|
|
|
|
|
|
|
|
// with your sequencer app process try to go lower
|
|
|
|
|
|
|
|
// avr at 16mhz suffers from bellow 16us clock
|
|
|
|
|
|
|
|
// but lets get teensy running at higher clock!
|
|
|
|
|
|
|
|
#if defined(TEENSYDUINO) && !defined(__AVR_ATmega32U4__) |
|
|
|
|
|
|
|
//freq_resolution = CLOCK_62500HZ; // 62500Hz/16us
|
|
|
|
|
|
|
|
//freq_resolution = CLOCK_125000HZ; // 125000Hz/8us
|
|
|
|
|
|
|
|
freq_resolution = CLOCK_250000HZ; // 250000Hz/4us
|
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
freq_resolution = CLOCK_62500HZ; // 62500Hz/16us
|
|
|
|
|
|
|
|
//freq_resolution = CLOCK_125000HZ; // 125000Hz/8us
|
|
|
|
|
|
|
|
//freq_resolution = CLOCK_250000HZ; // 250000Hz/4us
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
// first interval calculus
|
|
|
|
// first interval calculus
|
|
|
|
setTempo(tempo); |
|
|
|
setTempo(tempo); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void uClockClass::init()
|
|
|
|
void uClockClass::init()
|
|
|
|
{ |
|
|
|
{ |
|
|
|
// init work clock timer interrupt
|
|
|
|
// init work clock timer interrupt in Hz
|
|
|
|
workClock(freq_resolution); |
|
|
|
workClock(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void uClockClass::setResolution(uint32_t hertz) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// only registred frequencies!
|
|
|
|
|
|
|
|
switch(hertz) { |
|
|
|
|
|
|
|
case CLOCK_62500HZ: |
|
|
|
|
|
|
|
case CLOCK_125000HZ: |
|
|
|
|
|
|
|
case CLOCK_250000HZ: |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ATOMIC( |
|
|
|
|
|
|
|
freq_resolution = hertz; |
|
|
|
|
|
|
|
setTempo(tempo); |
|
|
|
|
|
|
|
workClock(freq_resolution); |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t uClockClass::getResolution() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return freq_resolution; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void uClockClass::start()
|
|
|
|
void uClockClass::start()
|
|
|
@ -266,9 +173,9 @@ void uClockClass::setTempo(float bpm) |
|
|
|
tempo = bpm; |
|
|
|
tempo = bpm; |
|
|
|
|
|
|
|
|
|
|
|
ATOMIC( |
|
|
|
ATOMIC( |
|
|
|
interval = (freq_resolution / (tempo * 24 / 60)) - internal_drift; |
|
|
|
//interval = (freq_resolution / (tempo * 24 / 60)) - drift;
|
|
|
|
//interval = (uint16_t)((156250.0 / tempo) - internal_drift);
|
|
|
|
//interval = 62500 / (tempo * 24 / 60) - drift;
|
|
|
|
//interval = 62500 / (tempo * 24 / 60) - internal_drift;
|
|
|
|
interval = (uint16_t)((156250.0 / tempo) - drift); |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -285,43 +192,50 @@ float uClockClass::getTempo() |
|
|
|
} |
|
|
|
} |
|
|
|
if (acc != 0) { |
|
|
|
if (acc != 0) { |
|
|
|
// get average interval, because MIDI sync world is a wild place...
|
|
|
|
// get average interval, because MIDI sync world is a wild place...
|
|
|
|
tempo = (((float)freq_resolution/24) * 60) / (acc / acc_counter); |
|
|
|
//tempo = (((float)freq_resolution/24) * 60) / (float)(acc / acc_counter);
|
|
|
|
// derivated one time calc value = ( freq_resolution / 24 ) * 60
|
|
|
|
// derivated one time calc value = ( freq_resolution / 24 ) * 60
|
|
|
|
//tempo = (float)(156250.0 / ((acc / acc_counter)));
|
|
|
|
tempo = (float)(156250.0 / (acc / acc_counter)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return tempo; |
|
|
|
return tempo; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void uClockClass::setDrift(uint8_t internal, uint8_t external) |
|
|
|
void uClockClass::setDrift(uint8_t value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
ATOMIC( |
|
|
|
ATOMIC(drift = value) |
|
|
|
internal_drift = internal; |
|
|
|
|
|
|
|
external_drift = external == 255 ? internal : external; |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
// force set tempo to update runtime interval
|
|
|
|
// force set tempo to update runtime interval
|
|
|
|
setTempo(tempo); |
|
|
|
setTempo(tempo); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint8_t uClockClass::getInternalDrift() |
|
|
|
void uClockClass::setSlaveDrift(uint8_t value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return internal_drift; |
|
|
|
ATOMIC(slave_drift = value) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint8_t uClockClass::getExternalDrift() |
|
|
|
|
|
|
|
|
|
|
|
uint8_t uClockClass::getDrift() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return external_drift; |
|
|
|
return drift; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// each interval is 16us
|
|
|
|
|
|
|
|
// this method is usefull for debug
|
|
|
|
uint16_t uClockClass::getInterval() |
|
|
|
uint16_t uClockClass::getInterval() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// since this is a debug method
|
|
|
|
|
|
|
|
// we are not going to stop interrupt here
|
|
|
|
|
|
|
|
// avoiding jitter
|
|
|
|
|
|
|
|
// so interval returned here are not always trust data!
|
|
|
|
|
|
|
|
return interval; |
|
|
|
return interval; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Main poolling tick call
|
|
|
|
|
|
|
|
uint8_t uClockClass::getTick(uint32_t *_tick) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ATOMIC(uint32_t last_tick = tick) |
|
|
|
|
|
|
|
if (*_tick != last_tick) { |
|
|
|
|
|
|
|
*_tick = last_tick; |
|
|
|
|
|
|
|
return 1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void uClockClass::setMode(uint8_t tempo_mode)
|
|
|
|
void uClockClass::setMode(uint8_t tempo_mode)
|
|
|
|
{ |
|
|
|
{ |
|
|
|
mode = tempo_mode; |
|
|
|
mode = tempo_mode; |
|
|
@ -345,12 +259,8 @@ void uClockClass::resetCounters() |
|
|
|
{ |
|
|
|
{ |
|
|
|
counter = 0; |
|
|
|
counter = 0; |
|
|
|
last_clock = 0; |
|
|
|
last_clock = 0; |
|
|
|
div96th_counter = 0; |
|
|
|
tick = 0; |
|
|
|
div32th_counter = 0; |
|
|
|
intick = 0; |
|
|
|
div16th_counter = 0; |
|
|
|
|
|
|
|
mod6_counter = 0; |
|
|
|
|
|
|
|
indiv96th_counter = 0; |
|
|
|
|
|
|
|
inmod6_counter = 0; |
|
|
|
|
|
|
|
ext_interval_idx = 0; |
|
|
|
ext_interval_idx = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -370,16 +280,10 @@ void uClockClass::handleExternalClock() |
|
|
|
{ |
|
|
|
{ |
|
|
|
last_interval = clock_diff(last_clock, _clock); |
|
|
|
last_interval = clock_diff(last_clock, _clock); |
|
|
|
last_clock = _clock; |
|
|
|
last_clock = _clock; |
|
|
|
|
|
|
|
// slave tick me!
|
|
|
|
indiv96th_counter++; |
|
|
|
intick++; |
|
|
|
inmod6_counter++; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (inmod6_counter == 6) { |
|
|
|
|
|
|
|
inmod6_counter = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (state) { |
|
|
|
switch (state) { |
|
|
|
|
|
|
|
|
|
|
|
case PAUSED: |
|
|
|
case PAUSED: |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
@ -388,14 +292,9 @@ void uClockClass::handleExternalClock() |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case STARTED: |
|
|
|
case STARTED: |
|
|
|
if (indiv96th_counter == 2) { |
|
|
|
interval = last_interval + slave_drift; |
|
|
|
interval = last_interval + external_drift; |
|
|
|
// accumulate interval incomming ticks data for getTempo() smooth reads on slave mode
|
|
|
|
} else { |
|
|
|
ext_interval_buffer[ext_interval_idx++ % EXT_INTERVAL_BUFFER_SIZE] = interval; |
|
|
|
interval = ((((uint32_t)interval * (uint32_t)pll_x) + (uint32_t)(256 - pll_x) * (uint32_t)last_interval) >> 8) + external_drift; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// accumulate interval incomming ticks data(for a better getTempo stability over bad clocks)
|
|
|
|
|
|
|
|
ext_interval_buffer[ext_interval_idx] = interval; |
|
|
|
|
|
|
|
ext_interval_idx = ++ext_interval_idx % EXT_INTERVAL_BUFFER_SIZE; |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -403,41 +302,21 @@ void uClockClass::handleExternalClock() |
|
|
|
void uClockClass::handleTimerInt()
|
|
|
|
void uClockClass::handleTimerInt()
|
|
|
|
{ |
|
|
|
{ |
|
|
|
if (counter == 0) { |
|
|
|
if (counter == 0) { |
|
|
|
|
|
|
|
// need a callback?
|
|
|
|
counter = interval; |
|
|
|
// please, use the polling method with getTick() instead...
|
|
|
|
|
|
|
|
|
|
|
|
if (onClock96PPQNCallback) { |
|
|
|
if (onClock96PPQNCallback) { |
|
|
|
onClock96PPQNCallback(&div96th_counter); |
|
|
|
onClock96PPQNCallback(&tick); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mod6_counter == 0) { |
|
|
|
|
|
|
|
if (onClock32PPQNCallback) { |
|
|
|
|
|
|
|
onClock32PPQNCallback(&div32th_counter); |
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onClock16PPQNCallback) { |
|
|
|
|
|
|
|
onClock16PPQNCallback(&div16th_counter); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
div16th_counter++; |
|
|
|
|
|
|
|
div32th_counter++; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (mod6_counter == 3) { |
|
|
|
// tick me!
|
|
|
|
if (onClock32PPQNCallback) { |
|
|
|
tick++; |
|
|
|
onClock32PPQNCallback(&div32th_counter); |
|
|
|
counter = interval; |
|
|
|
} |
|
|
|
|
|
|
|
div32th_counter++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
div96th_counter++; |
|
|
|
|
|
|
|
mod6_counter++; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mode == EXTERNAL_CLOCK) { |
|
|
|
if (mode == EXTERNAL_CLOCK) { |
|
|
|
sync_interval = clock_diff(last_clock, _clock); |
|
|
|
sync_interval = clock_diff(last_clock, _clock); |
|
|
|
if ((div96th_counter < indiv96th_counter) || (div96th_counter > (indiv96th_counter + 1))) { |
|
|
|
if ((tick < intick) || (tick > (intick + 1))) { |
|
|
|
div96th_counter = indiv96th_counter; |
|
|
|
tick = intick; |
|
|
|
mod6_counter = inmod6_counter; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (div96th_counter <= indiv96th_counter) { |
|
|
|
if (tick <= intick) { |
|
|
|
counter -= phase_mult(sync_interval); |
|
|
|
counter -= phase_mult(sync_interval); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (counter > sync_interval) { |
|
|
|
if (counter > sync_interval) { |
|
|
@ -445,15 +324,9 @@ void uClockClass::handleTimerInt() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (mod6_counter == 6) { |
|
|
|
|
|
|
|
mod6_counter = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
counter--; |
|
|
|
counter--; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// elapsed time support
|
|
|
|
// elapsed time support
|
|
|
@ -514,6 +387,7 @@ volatile uint32_t _timer = 0; |
|
|
|
void teensyInterrupt()
|
|
|
|
void teensyInterrupt()
|
|
|
|
#else |
|
|
|
#else |
|
|
|
ISR(TIMER1_COMPA_vect)
|
|
|
|
ISR(TIMER1_COMPA_vect)
|
|
|
|
|
|
|
|
//ISR(TIMER2_COMPA_vect)
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|
{ |
|
|
|
{ |
|
|
|
// global timer counter
|
|
|
|
// global timer counter
|
|
|
|