Get counter value before timer reset

Hi LeCrAm,

The TC timers are more limited than the TCC timers, however in this instance is simply a case of routing the TC4's output to port pin PA22, setting the timer to Normal PWM mode and loading the CC0 register with the desired output pulse width:

  ////////////////////////////////////////////////////////////////////////////////////////
  // TC4 Initialisation - generate pulse on reset, clear on CC0 on port pin PA22
  ////////////////////////////////////////////////////////////////////////////////////////

  // Enable the port multiplexer on port pin PA22
  PORT->Group[PORTA].PINCFG[22].bit.PMUXEN = 1;
  // Set-up the pin as a TC4/WO[0] peripheral on PA22
  PORT->Group[PORTA].PMUX[22 >> 1].reg |= PORT_PMUX_PMUXE_E;

  TC4->COUNT16.CTRLA.reg = TC_CTRLA_WAVEGEN_NPWM;    // Set the TC4 timer to normal PWM mode (NPWM)

  TC4->COUNT16.CC[0].reg = 1200;                     // Set the TC4 timer to output a 25us pulse

To set the TCC2 to a 10kHz test frequency:

  ////////////////////////////////////////////////////////////////////////////////////////
  // TCC2 Initialisation - generate pulse every 100us (10kHz) on port pin PA16
  ////////////////////////////////////////////////////////////////////////////////////////

  // Enable the port multiplexer on port pin PA16
  PORT->Group[PORTA].PINCFG[16].bit.PMUXEN = 1;
  // Set-up the pin as a TCC2/WO[0] peripheral on PA16
  PORT->Group[PORTA].PMUX[16 >> 1].reg |= PORT_PMUX_PMUXE_E;

  TCC2->WAVE.reg = TCC_WAVE_WAVEGEN_NPWM;            // Set the TCC2 timer to normal PWM mode (NPWM)
  while(TCC2->SYNCBUSY.bit.WAVE);                    // Wait for synchronization
 
  TCC2->PER.reg = 4799;                              // Set the period (PER) register for a PWM frequency of 10kHz (100us)
  while(TCC2->SYNCBUSY.bit.PER);                     // Wait for synchronization

  TCC2->CC[0].reg = 2400;                            // Set the counter compare 0 (CC0) register for a PWM duty-cycle of 50%
  while(TCC2->SYNCBUSY.bit.CC0);                     // Wait for synchronization

  TCC2->CTRLA.bit.ENABLE = 1;                        // Enable TCC2
  while (TCC2->SYNCBUSY.bit.ENABLE);                 // Wait for synchronization

Here's the output I'm getting from TC4 (light blue), triggered on the falling edge of TCC2 (yellow) at 10kHz:

TC4_DMAC_Read.png

Total latency from the falling edge TCC2 to the rising edge of TC4 is around 700ns.

TC4_DMAC_Read.png