/***
 *** This Example has no copyright and can be used by anyone.
 *** The following example is based on Device File Package
 *** required to compile the macro definitions used.
 *** The Device File Package is avaiblable by downloading Atmel Studio 7.
***/

/*** GCLK TC0 Peripheral Muxing Definition ***/
#define GCLK_TC0          14

/*** Curent 32KHz Clock Source Definition ***/
#define CURRENT_32KOSC    GCLK_GENCTRL_SRC_XOSC32K

void init_TC0_1KHz(void)
{
 /*** Enable XOSC32K (32KHZ input clock for TC) ***/
 OSC32KCTRL->XOSC32K.reg |= (OSC32KCTRL_XOSC32K_ENABLE  |
                             OSC32KCTRL_XOSC32K_XTALEN  |
                             OSC32KCTRL_XOSC32K_EN1K    |
                             OSC32KCTRL_XOSC32K_EN32K   |
                             OSC32KCTRL_XOSC32K_RUNSTDBY|
                             OSC32KCTRL_XOSC32K_STARTUP(5));

 /*** Wait until XOSC32K is ready ***/
 while(OSC32KCTRL->STATUS.bit.XOSC32KRDY==0);

 /*** Enable GCLK2 with XOSC32K 
  *** as clock peripheral clock 
    *** generator for TC0 
    ***/
 GCLK->GENCTRL[2].reg = GCLK_GENCTRL_DIV(1) | 
                        CURRENT_32KOSC|
                        GCLK_GENCTRL_GENEN;

 /*** Wait for Clocks synchronzation(GENCTRL register is write synchronized) ***/
 while((GCLK->SYNCBUSY.reg & GCLK_SYNCBUSY_GENCTRL2));    

 /*** Link the Generic Clock Generator 2 to the TC0 Peripheral ***/
 GCLK->PCHCTRL[GCLK_TC0].reg = (GCLK_PCHCTRL_CHEN|GCLK_PCHCTRL_GEN_GCLK2);   

 /*** Disable TC0 before configuring it... 
  *** (COUNT8 register is Enable Protected and write synchronized) 
  ***/
 TC0->COUNT8.CTRLA.bit.ENABLE = 0;

 /*** Wait for Clocks synchronzation(COUNT8 register is write synchronized) ***/
 while(TC0->COUNT8.SYNCBUSY.reg & TC_SYNCBUSY_ENABLE);

 /*** TC0 configuration...
  *** 8-bit Timer counter mode,
  *** Run in SDBY
  *** On Demand enabled
  *** Prescaler = 1 (32KHz Divided by 1 gives 32KHz.)
  ***/
 TC0->COUNT8.CTRLA.reg |= (TC_CTRLA_MODE(TC_CTRLA_MODE_COUNT8_Val)|    
                           TC_CTRLA_RUNSTDBY                      |
                           TC_CTRLA_ONDEMAND                      |
                           TC_CTRLA_PRESCALER(TC_CTRLA_PRESCALER_DIV1_Val));

 /*** TC0 Wave generator is used as Match frequency generator (MFRQ) ***/
 TC0->COUNT8.WAVE.reg    =    (TC_WAVE_WAVEGEN_MFRQ);

 /*** TC0 is an event generator based on compare value 
  *** These bit enables the generation of an event 
  *** for every match or capture on channel 0.
  ***/
 TC0->COUNT8.EVCTRL.reg =    (TC_EVCTRL_MCEO0);        

 /*** 32 is the number to get an event @ 1KHz frequency ***/
 TC0->COUNT8.CC[0].reg    =    32;                    

 /*** Wait for Clocks synchronzation(COUNT8 register is write synchronized) ***/
 while(TC0->COUNT8.SYNCBUSY.reg & TC_SYNCBUSY_CC0);

 /*** Ensable TC0 once it is configuring... 
  *** (COUNT8 register is Enable Protected and write synchronized) 
  ***/
 TC0->COUNT8.CTRLA.bit.ENABLE = 1;

 /*** Wait for Clocks synchronzation(COUNT8 register is write synchronized) ***/
 while(TC0->COUNT8.SYNCBUSY.reg & TC_SYNCBUSY_ENABLE);        

}