I answer myself, I was wrong, with correct values the timer interrupt can be triggered at 1MHz.
The problem was that the analogWrite is too slow to go to that frequency, so I digged into the arduino/sam7core sources and found how to use the SAM library directly, the code (much fast) that I wrote is:
// This function to configure a Timer was given some replies above, I changed only the used CLOCK.
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_CLOCK1);
uint32_t rc = VARIANT_MCK/2/frequency; //2 because we selected TIMER_CLOCK1 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);
}
volatile boolean l;
void TC3_Handler()
{
TC_GetStatus(TC1, 0);
dacc_write_conversion_data(DAC0, (l = !l)*4095);
}
void setup() {
Serial.begin(115200);
pinMode(DAC0, OUTPUT);
analogWriteResolution(12);
pmc_enable_periph_clk(DACC_INTERFACE_ID);
dacc_reset(DACC_INTERFACE);
dacc_set_transfer_mode(DACC_INTERFACE, 0);
dacc_set_power_save(DACC_INTERFACE, 0, 0);
dacc_set_timing(DACC_INTERFACE, 0x0, 1, 0x0);
dacc_set_analog_control(DACC_INTERFACE, DACC_ACR_IBCTLCH0(0x02) |
DACC_ACR_IBCTLCH1(0x02) |
DACC_ACR_IBCTLDACCORE(0x01));
dacc_disable_trigger(DACC_INTERFACE);
dacc_set_channel_selection(DACC_INTERFACE, 0);
dacc_enable_channel(DACC_INTERFACE, 0);
// Start timer. Parameters are:
// TC1 : timer counter. Can be TC0, TC1 or TC2
// 0 : channel. Can be 0, 1 or 2
// TC3_IRQn: irq number. See table.
// 40 : frequency (in Hz)
// The interrupt service routine is TC3_Handler. See table.
startTimer(TC1, 0, TC3_IRQn, 1000000);
}
Thanks to all,
Giuseppe Stanghellini