#include "gtimer_drv.h"
#include "dw_apb_timers.h"
#include "inter_vector.h"
#include "ucp_param.h"
#include "ucp_utility.h"
#include "ucp_drv_common.h"


int32_t gtimer2_init(uint8_t tmrId)
{
	do_write((TIMER2_TIMER1BASE + TIMERCONTROLREGOFF+0x14*tmrId), 0); 		// stop timer
	do_write((TIMER2_TIMER1BASE + TIMERCONTROLREGOFF+0x14*tmrId), (BIT1|BIT2)); 	// user defined mode, int disable
	do_write((TIMER2_TIMER1BASE + TIMERLOADCOUNTOFF+0x14*tmrId), 10000); 	// reload count

	return 0;
}

void gtimer2_start(uint8_t tmrId)
{
	uint32_t val = do_read_volatile(TIMER2_TIMER1BASE + TIMERCONTROLREGOFF+0x14*tmrId);
	val |= BIT0;	// start timer
	do_write((TIMER2_TIMER1BASE + TIMERCONTROLREGOFF+0x14*tmrId), val);
}

void gtimer2_stop(uint8_t tmrId)
{
	uint32_t val = do_read_volatile(TIMER2_TIMER1BASE + TIMERCONTROLREGOFF+0x14*tmrId);
	val &= (~BIT0);	// stop timer
	do_write((TIMER2_TIMER1BASE + TIMERCONTROLREGOFF+0x14*tmrId), val);
}

int32_t gtimer2_int_enable(uint8_t tmrId)
{
	if (8 <= tmrId)
	{
		return -1;
	}
	
	uint32_t val = do_read_volatile(TIMER2_TIMER1BASE + TIMERCONTROLREGOFF+0x14*tmrId);
	val &= (~BIT2);	// int enable
	do_write((TIMER2_TIMER1BASE + TIMERCONTROLREGOFF+0x14*tmrId), val);
	
	return 0;
}

int32_t gtimer2_int_disable(uint8_t tmrId)
{
	if (8 <= tmrId)
	{
		return -1;
	}
	
	uint32_t val = do_read_volatile(TIMER2_TIMER1BASE + TIMERCONTROLREGOFF+0x14*tmrId);
	val |= (BIT2);	// int disable
	do_write((TIMER2_TIMER1BASE + TIMERCONTROLREGOFF+0x14*tmrId), val);
	
	return 0;
}

