timer interrupt

Dear all,

when using time interrupt, what will happen if the TC3_Handler() code takes longer than the startTimer Freq?
I donot understand the function very well. Thank you. ]:slight_smile:

Btw, when I include a small delay i.e. delay(1) in the TC3_Handler() code, I only get one or two serial print output. Any one can explain this? Thank you.

volatile boolean l;

//TC1 ch 0
void TC3_Handler()
{
        TC_GetStatus(TC1, 0);
        digitalWrite(13, l = !l);
        Serial.begin(115200);
        Serial.println(micros());
        uint32_t time1;
        uint32_t time2;
        Serial.println("time1");
        Serial.println(micros());
        for (long int i=0; i<10000000;i++)
        {
        time2 = i;
        }
        delay(1);
        Serial.println("time2");
        Serial.println(time2);
}

void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency) {
        pmc_set_writeprotect(false);
        pmc_enable_periph_clk((uint32_t)irq);
        TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4);
        uint32_t rc = VARIANT_MCK/128/frequency; //128 because we selected TIMER_CLOCK4 above
        TC_SetRA(tc, channel, rc/2); //50% high, 50% low
        TC_SetRC(tc, channel, rc);
        TC_Start(tc, channel);
        tc->TC_CHANNEL[channel].TC_IER=TC_IER_CPCS;
        tc->TC_CHANNEL[channel].TC_IDR=~TC_IER_CPCS;
        NVIC_EnableIRQ(irq);
}

void setup(){
        pinMode(13,OUTPUT);
        startTimer(TC1, 0, TC3_IRQn, 4); //TC1 channel 0, the IRQ for that channel and the desired frequency
}

void loop(){
}

I'm not sure what exactly happens when interrupt routines start conflicting. Normally you keep the interrupt code as short as possible. If you are trying to check the frequency of the timer you need to use faster external equipment (e.g., oscilloscope). Otherwise you really can't print messages at rates that high.

Your example code has a few problems. First you only need to call Serial.begin() once, starting it during every interrupt just wastes time. You also use a timer frequency of: 86000000 Hz / 128 / 4 = 168000 Hz and are trying to send messages using a 156200 baud (i.e. 156200 characters per second). If the interrupt overrides itself then I would only expect to see 1 or 2 characters sent through serial.

Also delay() doesn't disable interrupts, so it could be that TC3_Handler() gets called whenever you use delay(1)?

Other than the delays, and serial output, does this method
give reliable interrupts?

Is the handler routine itself interruptable?

For example:
If the interrupt from the timer comes every 100 us, and the interrupt processing takes 150 us, will the interrupt processing get interrupted
with another instance of the same handler, which causes memory
to be consumed rapidly, and... then, what happens to the Due?