Hello Everyone,
I am trying to program the Arduino DUE timer modules. My operation is like this-
- Every 1 microsecond one timer channel is set to generate an interrupt.
- At every interrupt, the CPU reads a 32-bit port and store it.
- then Toggle an output and reset the timers internal counter register.
The problem is -
While the execution point moved to the ISR it's consuming some time to read port and toggle the output.
So my interrupt is not happening exactly after one microsecond but taking 1.14 us because it's taking that 0.14 microsecond to read and toggle ( as I was reseting the counter register value after the read and toggle) .
Now inside the ISR, the code is as below-
###########################################
void TC6_Handler()
{
TC_GetStatus(TC2, 0); //to get the status
//assign the input values to values[]
values[valueIndex]=PIOA->PIO_PDSR;//stroing the vales
//Toggling one output
PIOB->PIO_SODR|=(1<<12);
PIOB->PIO_CODR|=(1<<12);
//Updating value index
valueIndex++;
//Reset Counter 2 channel 0
TC2->TC_CHANNEL[0].TC_CCR|=TC_CCR_SWTRG;
}
######################################################
When I tried to move the reset line immediately after the "TC_GetStatus" function it's taking more time to finish executing the later codes, approx. 1.5us.
##Why this is happening?
I don't have any explanation for this. As my understanding goes the reset will cause the internal counter running while the port operation is occurring so the entire operation should happen within 1 us because at the start of ISR call the counter value is reset( throughout the read and toggle the timer is incrementing so it doesn't account for the time taken for read and toggle).
My timer initialization code is as below-
###########################################################
int TimerStart(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t freq)
{
pmc_set_writeprotect(false); //for 32 bit due process prior to configureing counter channel write protect bits has to be disabled
pmc_enable_periph_clk(irq); // enabling the ISR
TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC |
TC_CMR_TCCLKS_TIMER_CLOCK1); ///////Configuring the timer counter
uint32_t rc = VARIANT_MCK / 2 / freq; /////// calculating the overflow for rc or register c
TC_SetRA(tc, channel, 21000000); // 50% duty cycle square wave
TC_SetRC(tc, channel, rc);
//TC_Start(tc, channel);
tc->TC_CHANNEL[channel].TC_IER= TC_IER_CPCS | TC_IER_CPCS;
tc->TC_CHANNEL[channel].TC_IDR=~(TC_IER_CPCS | TC_IER_CPCS);
NVIC_EnableIRQ(irq);
}
################################################################
And timer start function -
"TimerStart(TC2, 0, TC6_IRQn, 1000000); /// Set timer(microsecond range )frequency here"
Does anybody have any idea about this?
Thank you for reading the long post:).