/**
 * Copyright 2016, yichip Semiconductor(shenzhen office)
 * All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Yichip Semiconductor;
 * the contents of this file may not be disclosed to third parties, copied
 * or duplicated in any form, in whole or in part, without the prior
 * written permission of Yichip Semiconductor.
 */

/** 
  *@file timer.h
  *@brief timer support for application.
  */
#ifndef DRIVERS_TIMER_YC_TIMER_H_
#define DRIVERS_TIMER_YC_TIMER_H_

#include <stdbool.h>
#include <stdio.h>  
#include "yc11xx.h"
#include "ycdef.h"  

typedef struct SYSTEM_TIME_HANDLE
{
	void (*init)(void);	
	unsigned int (*get_run_ticks)(void);
	unsigned int (*get_run_tickms)(void);

}sys_time_handle_t;

extern sys_time_handle_t sys_time_handle;
/**
  *@brief system tick.
  */
//#define SYSTEM_CLOCK    (48000000UL)
#define SYSTEM_CLOCK    (24000000UL)
#define CLOCK_DIVISOR		1000
	
#define CLK_SUBSLOT_10_MS    (32)
#define SYSTEM_TIMER_UNIT_SUBSLOTS    (CLK_SUBSLOT_10_MS)
	
extern uint32_t SYStick_count;

typedef void (*Timer_Expire_CB)(int params);

/**
  *@brief Timer_state.
  */
typedef enum
{
	TIMER_STOP,
	TIMER_START,
}TIMER_STATUS;

/**
  *@brief Timer_type.
  */
typedef enum
{
	TIMER_SINGLE,		/*!< */
	TIMER_CYCLE,		/*!< */
	TIMER_TYPE_BIT=0x80,
}TIMER_TYPE;

/**
  *@brief System timer type.
  */
typedef struct sTimerType
{
	uint32_t mTimerValue;		
	uint32_t mTick;	
	TIMER_STATUS mTimerStatus;
	TIMER_TYPE mIsCycle;						
	Timer_Expire_CB pfExpireCb;		
	struct sTimerType *pNextTimer;	
	int32_t cbParams;
}SYS_TIMER_TYPE;

/**
  *@brief Timer_number.
  */
typedef enum
{
	SYS_TIMER_0,
	SYS_TIMER_1,
	SYS_TIMER_2,
	SYS_TIMER_3,
	SYS_TIMER_4,
	SYS_TIMER_5,
	SYS_TIMER_6,
	SYS_TIMER_7,
	SYS_TIMER_8,
}SYS_TIMER_INDEX;


#define OS_ENTER_CRITICAL() __disable_irq() 
#define OS_EXIT_CRITICAL() __enable_irq() 

/**
  *@brief Timer initialization.
  *@param None.
  *@return None.
  */
void SYS_TimerInit();

/**
  *@brief This function can set a timer.
  *@param pTimer 
  *@param tick
  *@param type
  *@param pfExpire_CB
  *@retval true sucess.
  *@retval false failure.
  */
bool SYS_SetTimer(SYS_TIMER_TYPE *pTimer, int tick,TIMER_TYPE type,Timer_Expire_CB pfExpire_CB);

/**
  *@brief Timer polling.
  */
void SYS_timerPolling();

/**
  *@brief release timer.
  *@param pTimer .@ref SYS_TIMER_TYPE
  *@retval true success.
  *@retval false failure.
  */
bool SYS_ReleaseTimer(SYS_TIMER_TYPE *pTimer);

/**
  *@brief Timer expire default handle.
  *@param None.
  *@return None.
  */
void SYS_TimerExpireDefaultHandle();

/**
  *@brief release all timer.
  *@param None.
  *@return None.
  */
void SYS_ReleaseAllTimer();

/**
  *@brief  Check timer exist.
  *@param pTimer will be check exist or not.@ref SYS_TIMER_TYPE   
  *@retval true this timer is exist.
  *@retval false the timer is not exist.  
  */
bool SYS_TimerisExist(SYS_TIMER_TYPE *pTimer);

bool SYS_ResetTimer(SYS_TIMER_TYPE *pTimer);
void SYS_delay_us(uint32_t nus);
void SYS_delay_ms(uint32_t nms);
void SYStick_handle();
void SYS_ClkTicks(void);

#endif