Timer: run and read manually

I just want to set up one timer and with a rising/falling edge on one pin want to save the value out of the timer register.... sounds simple, but my timer doesn't run for some reason...

void setup() {
// put your setup code here, to run once:
Serial.begin(19200);
pmc_set_writeprotect(false); //
pmc_enable_periph_clk(ID_TC1); //"power on" the timer1
pmc_set_writeprotect(false);
TC_Configure(TC1,0,TC_CMR_TCCLKS_TIMER_CLOCK5); // set up somehow
TC_Start(TC1,0); // starts that the timer can run continously
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println(TC_ReadCV(TC1,0)); // get the latest value
delay(2000);
}

Any hint ?

Thomas

ok, I tried a lot, looked into the tc.c/tc.h which comes with the original IDE, looked into the DueTimer library from ivanseidel and SomeRandomGuy. I mixed everything together and get the following working code:

void setup() {
// put your setup code here, to run once:
startTimer(TC0, 0, TC0_IRQn, 1);
Serial.begin(115200);

}

void loop() {
// put your main code here, to run repeatedly:
Serial.println(TC_ReadCV(TC0,0));
delay(100);

}

void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency)
{
uint32_t rc = 0;
uint8_t clock;
pmc_set_writeprotect(false);
pmc_enable_periph_clk((uint32_t)irq);
clock = pickClock(frequency, rc);

TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | clock);
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);
}
uint8_t pickClock(uint32_t frequency, uint32_t& retRC)
{
/*
Timer Definition
TIMER_CLOCK1 MCK/2
TIMER_CLOCK2 MCK/8
TIMER_CLOCK3 MCK/32
TIMER_CLOCK4 MCK/128
*/
struct {
uint8_t flag;
uint8_t divisor;
} clockConfig[] = {
{ TC_CMR_TCCLKS_TIMER_CLOCK1, 2 },
{ TC_CMR_TCCLKS_TIMER_CLOCK2, 8 },
{ TC_CMR_TCCLKS_TIMER_CLOCK3, 32 },
{ TC_CMR_TCCLKS_TIMER_CLOCK4, 128 }
};
float ticks;
float error;
int clkId = 3;
int bestClock = 3;
float bestError = 1.0;
do
{
ticks = (float) VARIANT_MCK / (float) frequency / (float) clockConfig[clkId].divisor;
error = abs(ticks - round(ticks));
if (abs(error) < bestError)
{
bestClock = clkId;
bestError = error;
}
} while (clkId-- > 0);
ticks = (float) VARIANT_MCK / (float) frequency / (float) clockConfig[bestClock].divisor;
retRC = (uint32_t) round(ticks);
return clockConfig[bestClock].flag;
}

which gives on the console:

131206
196831
262456
328081
393706
459331
524956
590581
656206
65581
131206
196831
262456
328081

and seems to work like I want. A running timer in the background (without any firing IR at first) where I can fetch the raw timer value if needed ...