I have read manual and I tried to make setup code, it is not working yet.In manual I see:
The channel can also be configured to have an external trigger. In Waveform Mode, an external event can be programmed on one of the following signals: TIOB, XC0, XC1 or XC2. This external event can then be programmed to perform a trigger by setting ENETRG in TC_CMR.
If an external trigger is used, the duration of the pulses must be longer than the master clock period in order to be detected.
In Waveform Mode, TIOA is always configured to be an output and TIOB is an output if it is not selected to be the external triggerMy problem was:
* I want to use TC0, channel 0
* I think I will use Waveform mode, without RC compare
* I want to use interrupts - interrupt should be triggered on external event (signal from other device of FALLING edge, 50Hz)
* I want to use interrupt handler
pinMode(22, INPUT); //I dont know, do I have to set it up just like that, I want to use pin22 as external signal
pmc_set_writeprotect(false); //For being able to set up TC registers (like TC_CMR, TC_CCR etc)
TC_Configure(TC0, 0, 0b00000000000000001001001000000000); // From manual (explained below)
TC0->TC_CHANNEL.TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG; //Start TC or use APIs TC_start function
//TC0->TC_CHANNEL.TC_IER = 0b00000000000000000000000010000001; // IER = interrupt enable register
TC0->TC_CHANNEL.TC_IER = TC_IER_ETRGS | TC_IER_COVFS; //Enable interupt
TC0->TC_CHANNEL.TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG; //Restart timer
So problem here is
This code is never handled in TC0_Handler, even, when counter gets full.
1) Is interrupt handler set up correctly?
2) How do I tell the system that I use pin22 (B.26) as an input for TIOB (Do I have to set up PIO registers, how)?
3) Maybe TC_CMR mode is set up incorrectly?:
TC_Configure(TC0, 0, 0b00000000000000001001001000000000);I got it like this:
TC_CMR register, using Waveform Mode. This mode is used in TC_Configure().
TCCLKS: 000 -> usage of MCK/2 ie 48Mhz
CLKI: 0 ehk counter is incremtented on rising edge of the clock - fine by me
BURST: 00 - the clock is not gated by an external signal - fine by me
CPCSTOP: 0 counter clock is not stopped when counter reaches RC? I think its correct, I dont use RC, in waveform mode 00.
CPCDIS: 0 - counter clock is not disabled when counter reaches RC - I think its OK like this.
EEVTEDG: 10 (2) - on falling egde of external event triggers - This is correct I suppose.
EEVT: 00 - TIOB, signal selected as external event - Must be like that, according to manual.
ENETRG: 1 - the external event restars the counter and starts the coutner clock - Yup.
WAVSEL: 00 - upmode without automatic trigger on RC compare - I think its OK, I cant use here RC compare. 00 is OK.
WAVE: 1 - waveform is enabled
ACPA: 00 - ra compare effect on TIOA - none - as I dont use RA compare
ACPC: 00 - rc compare effect on TIOA - none - as I dont use RC compare
AEEVT: 00 - external event effet on TIOA???? Hmmm, what's that?
ASWTRG: 00 stoftware trigger effect on TIOA - Hmmmm, what's that?
BCPS, BCPC, 00 00 - ???
BEEVT, BSWTRG: 00 00 - ????