Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use (CLK_PER / 2) instead of CLKTCA for millis / micros clock #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions cores/arduino/wiring.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ uint16_t fract_inc;

// whole number of microseconds per timer tick

volatile uint8_t timer_counter = 0;
volatile uint32_t timer_overflow_count = 0;
volatile uint32_t timer_millis = 0;
static uint16_t timer_fract = 0;
Expand Down Expand Up @@ -85,22 +86,28 @@ ISR(TCB3_INT_vect)
ISR(TCB0_INT_vect)
#endif
{
// copy these to local variables so they can be stored in registers
// (volatile variables must be read from memory on every access)
uint32_t m = timer_millis;
uint16_t f = timer_fract;

m += millis_inc;
f += fract_inc;
if (f >= FRACT_MAX) {

f -= FRACT_MAX;
m += 1;
uint8_t c = timer_counter;
if (c % 32 == 0) {
// copy these to local variables so they can be stored in registers
// (volatile variables must be read from memory on every access)
uint32_t m = timer_millis;
uint16_t f = timer_fract;

m += millis_inc;
f += fract_inc;
if (f >= FRACT_MAX) {

f -= FRACT_MAX;
m += 1;
}

timer_fract = f;
timer_millis = m;
timer_overflow_count++;
}

timer_fract = f;
timer_millis = m;
timer_overflow_count++;

c++;
timer_counter = c;

/* Clear flag */
_timer->INTFLAGS = TCB_CAPT_bm;
Expand Down Expand Up @@ -396,8 +403,8 @@ void init()
/* Enable timer interrupt */
_timer->INTCTRL |= TCB_CAPT_bm;

/* Clock selection -> same as TCA (F_CPU/64 -- 250kHz) */
_timer->CTRLA = TCB_CLKSEL_CLKTCA_gc;
/* Clock selection -> F_CPU / 2, then we only run the ISR every 32 interrupts. (F_CPU/2/32 = 250kHz) */
_timer->CTRLA = TCB_CLKSEL_CLKDIV2_gc;

/* Enable & start */
_timer->CTRLA |= TCB_ENABLE_bm; /* Keep this last before enabling interrupts to ensure tracking as accurate as possible */
Expand Down