/*
 * systick.c
 *
 * Created: 31/05/2018 12:07:08
 *  Author: Ledo
 */ 
#include "asf.h"


//Global variables
volatile uint32_t tickCount = 0;        //Global tick count since boot

//Initialize 
void SysTick_init(void)
{
	//Configure System Tick Timer for 1ms interrupts
	SysTick_Config(sysclk_get_cpu_hz() / 1000);
}

//System Timer ISR
ISR(SysTick_Handler)
{
	tickCount++;            //ms counter - useful for timestamps
}


//Calculate count value for 3s (3,000,000 us) Watchdog
void Wdt_init (uint32_t us)
{
    //Calculate count value for 3s (3,000,000 us) Watchdog
    uint32_t wdtCount = wdt_get_timeout_value(3000000UL, BOARD_FREQ_SLCK_XTAL);
	//Initialize Watchdog Timer
	wdt_init(WDT, WDT_MR_WDRSTEN |  //Watchdog fault or underflow causes reset,
	WDT_MR_WDDBGHLT,  //Debug pauses watchdog
	wdtCount,         //Restart value of Watchdog Counter
	wdtCount);        //Delta value, used to prevent reset loops, unused here
}
