|
|
|
@ -3,7 +3,7 @@ |
|
|
|
|
* 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. |
|
|
|
|
* 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 |
|
|
|
|
* @date 08/21/2020 |
|
|
|
|
* @license MIT - (c) 2020 - Romulo Silva - contact@midilab.co |
|
|
|
@ -36,93 +36,50 @@ |
|
|
|
|
#if defined(TEENSYDUINO) && !defined(__AVR_ATmega32U4__) |
|
|
|
|
IntervalTimer _teensyTimer; |
|
|
|
|
void teensyInterrupt(); |
|
|
|
|
void workClock(uint32_t freq_resolution) |
|
|
|
|
void workClock() |
|
|
|
|
{ |
|
|
|
|
// fallback default frequency (CLOCK_250000HZ) if no requested freq available
|
|
|
|
|
uint8_t microseconds = 4; |
|
|
|
|
const bool running = false; |
|
|
|
|
|
|
|
|
|
switch(freq_resolution) { |
|
|
|
|
case CLOCK_62500HZ: |
|
|
|
|
microseconds = 16; |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
_teensyTimer.begin(teensyInterrupt, 16); |
|
|
|
|
// 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 |
|
|
|
|
void workClock(uint32_t freq_resolution) |
|
|
|
|
void workClock() |
|
|
|
|
{ |
|
|
|
|
// fallback default frequency (CLOCK_62500HZ) if no requested freq available
|
|
|
|
|
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 { |
|
|
|
|
ATOMIC( |
|
|
|
|
// Timer1
|
|
|
|
|
TCCR1A = 0; |
|
|
|
|
TCCR1B = 0; |
|
|
|
|
TCNT1 = 0; |
|
|
|
|
// set the speed of our internal clock system
|
|
|
|
|
OCR1A = comparator; |
|
|
|
|
OCR1A = 255; |
|
|
|
|
// turn on CTC mode
|
|
|
|
|
TCCR1B |= (1 << WGM12); |
|
|
|
|
// Set CS12, CS11 and CS10 bits for 1 prescaler
|
|
|
|
|
TCCR1B |= (0 << CS12) | (0 << CS11) | (1 << CS10); |
|
|
|
|
// enable timer compare interrupt
|
|
|
|
|
TIMSK1 |= (1 << OCIE1A); |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
) |
|
|
|
|
/*
|
|
|
|
|
ATOMIC( |
|
|
|
|
// Timer2
|
|
|
|
|
TCCR2A = 0; |
|
|
|
|
TCCR2B = 0; |
|
|
|
|
TCNT2 = 0; |
|
|
|
|
// set the speed of our internal clock system
|
|
|
|
|
OCR2A = comparator; |
|
|
|
|
OCR2A = 255; |
|
|
|
|
// turn on CTC mode
|
|
|
|
|
TCCR2B |= (1 << WGM21); |
|
|
|
|
// Set CS22, CS21 and CS20 bits for 1 prescaler
|
|
|
|
|
TCCR2B |= (0 << CS22) | (0 << CS21) | (1 << CS20); |
|
|
|
|
// enable timer compare interrupt
|
|
|
|
|
TIMSK2 |= (1 << OCIE2A); |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
@ -144,16 +101,10 @@ static inline uint16_t clock_diff(uint16_t old_clock, uint16_t new_clock) |
|
|
|
|
|
|
|
|
|
uClockClass::uClockClass() |
|
|
|
|
{ |
|
|
|
|
// some tested values
|
|
|
|
|
// 1 is good for native 31250bps midi interface
|
|
|
|
|
// 4 is good for usb-to-midi hid on leonardo
|
|
|
|
|
// (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; |
|
|
|
|
// drift is used to sligth calibrate with your slave clock
|
|
|
|
|
drift = 1; |
|
|
|
|
slave_drift = 0; |
|
|
|
|
tempo = 120; |
|
|
|
|
pll_x = 220; |
|
|
|
|
start_timer = 0; |
|
|
|
|
last_interval = 0; |
|
|
|
|
sync_interval = 0; |
|
|
|
@ -162,58 +113,14 @@ uClockClass::uClockClass() |
|
|
|
|
ext_interval_acc = 0; |
|
|
|
|
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
|
|
|
|
|
setTempo(tempo); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void uClockClass::init()
|
|
|
|
|
{ |
|
|
|
|
// init work clock timer interrupt
|
|
|
|
|
workClock(freq_resolution); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
// init work clock timer interrupt in Hz
|
|
|
|
|
workClock(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void uClockClass::start()
|
|
|
|
@ -266,9 +173,9 @@ void uClockClass::setTempo(float bpm) |
|
|
|
|
tempo = bpm; |
|
|
|
|
|
|
|
|
|
ATOMIC( |
|
|
|
|
interval = (freq_resolution / (tempo * 24 / 60)) - internal_drift; |
|
|
|
|
//interval = (uint16_t)((156250.0 / tempo) - internal_drift);
|
|
|
|
|
//interval = 62500 / (tempo * 24 / 60) - internal_drift;
|
|
|
|
|
//interval = (freq_resolution / (tempo * 24 / 60)) - drift;
|
|
|
|
|
//interval = 62500 / (tempo * 24 / 60) - drift;
|
|
|
|
|
interval = (uint16_t)((156250.0 / tempo) - drift); |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -285,43 +192,50 @@ float uClockClass::getTempo() |
|
|
|
|
} |
|
|
|
|
if (acc != 0) { |
|
|
|
|
// 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
|
|
|
|
|
//tempo = (float)(156250.0 / ((acc / acc_counter)));
|
|
|
|
|
tempo = (float)(156250.0 / (acc / acc_counter)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return tempo; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void uClockClass::setDrift(uint8_t internal, uint8_t external) |
|
|
|
|
void uClockClass::setDrift(uint8_t value) |
|
|
|
|
{ |
|
|
|
|
ATOMIC( |
|
|
|
|
internal_drift = internal; |
|
|
|
|
external_drift = external == 255 ? internal : external; |
|
|
|
|
) |
|
|
|
|
ATOMIC(drift = value) |
|
|
|
|
// force set tempo to update runtime interval
|
|
|
|
|
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() |
|
|
|
|
{ |
|
|
|
|
// 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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
{ |
|
|
|
|
mode = tempo_mode; |
|
|
|
@ -345,12 +259,8 @@ void uClockClass::resetCounters() |
|
|
|
|
{ |
|
|
|
|
counter = 0; |
|
|
|
|
last_clock = 0; |
|
|
|
|
div96th_counter = 0; |
|
|
|
|
div32th_counter = 0; |
|
|
|
|
div16th_counter = 0; |
|
|
|
|
mod6_counter = 0; |
|
|
|
|
indiv96th_counter = 0; |
|
|
|
|
inmod6_counter = 0; |
|
|
|
|
tick = 0; |
|
|
|
|
intick = 0; |
|
|
|
|
ext_interval_idx = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -370,16 +280,10 @@ void uClockClass::handleExternalClock() |
|
|
|
|
{ |
|
|
|
|
last_interval = clock_diff(last_clock, _clock); |
|
|
|
|
last_clock = _clock; |
|
|
|
|
|
|
|
|
|
indiv96th_counter++; |
|
|
|
|
inmod6_counter++; |
|
|
|
|
|
|
|
|
|
if (inmod6_counter == 6) { |
|
|
|
|
inmod6_counter = 0; |
|
|
|
|
} |
|
|
|
|
// slave tick me!
|
|
|
|
|
intick++; |
|
|
|
|
|
|
|
|
|
switch (state) { |
|
|
|
|
|
|
|
|
|
case PAUSED: |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
@ -388,14 +292,9 @@ void uClockClass::handleExternalClock() |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
case STARTED: |
|
|
|
|
if (indiv96th_counter == 2) { |
|
|
|
|
interval = last_interval + external_drift; |
|
|
|
|
} else { |
|
|
|
|
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; |
|
|
|
|
interval = last_interval + slave_drift; |
|
|
|
|
// accumulate interval incomming ticks data for getTempo() smooth reads on slave mode
|
|
|
|
|
ext_interval_buffer[ext_interval_idx++ % EXT_INTERVAL_BUFFER_SIZE] = interval; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -403,41 +302,21 @@ void uClockClass::handleExternalClock() |
|
|
|
|
void uClockClass::handleTimerInt()
|
|
|
|
|
{ |
|
|
|
|
if (counter == 0) { |
|
|
|
|
|
|
|
|
|
counter = interval; |
|
|
|
|
|
|
|
|
|
// need a callback?
|
|
|
|
|
// please, use the polling method with getTick() instead...
|
|
|
|
|
if (onClock96PPQNCallback) { |
|
|
|
|
onClock96PPQNCallback(&div96th_counter); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mod6_counter == 0) { |
|
|
|
|
if (onClock32PPQNCallback) { |
|
|
|
|
onClock32PPQNCallback(&div32th_counter); |
|
|
|
|
}
|
|
|
|
|
if (onClock16PPQNCallback) { |
|
|
|
|
onClock16PPQNCallback(&div16th_counter); |
|
|
|
|
} |
|
|
|
|
div16th_counter++; |
|
|
|
|
div32th_counter++; |
|
|
|
|
onClock96PPQNCallback(&tick); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mod6_counter == 3) { |
|
|
|
|
if (onClock32PPQNCallback) { |
|
|
|
|
onClock32PPQNCallback(&div32th_counter); |
|
|
|
|
} |
|
|
|
|
div32th_counter++; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
div96th_counter++; |
|
|
|
|
mod6_counter++; |
|
|
|
|
|
|
|
|
|
// tick me!
|
|
|
|
|
tick++; |
|
|
|
|
counter = interval; |
|
|
|
|
if (mode == EXTERNAL_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 ((tick < intick) || (tick > (intick + 1))) { |
|
|
|
|
tick = intick; |
|
|
|
|
} |
|
|
|
|
if (div96th_counter <= indiv96th_counter) { |
|
|
|
|
if (tick <= intick) { |
|
|
|
|
counter -= phase_mult(sync_interval); |
|
|
|
|
} else { |
|
|
|
|
if (counter > sync_interval) { |
|
|
|
@ -445,15 +324,9 @@ void uClockClass::handleTimerInt() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mod6_counter == 6) { |
|
|
|
|
mod6_counter = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
counter--; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// elapsed time support
|
|
|
|
@ -514,6 +387,7 @@ volatile uint32_t _timer = 0; |
|
|
|
|
void teensyInterrupt()
|
|
|
|
|
#else |
|
|
|
|
ISR(TIMER1_COMPA_vect)
|
|
|
|
|
//ISR(TIMER2_COMPA_vect)
|
|
|
|
|
#endif |
|
|
|
|
{ |
|
|
|
|
// global timer counter
|
|
|
|
|