/**
  ******************************************************************************
  * @file    lvd.h
  * @author  (C)2015, Qindao ieslab Co., Ltd
  * @version V1.0
  * @date    2015-12-25
  * @brief   the function of the entity of system processor
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/

#ifndef __UWater_CLKTRIM_DRIVER_H
#define __UWater_CLKTRIM_DRIVER_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "ddl.h"
#include "type.h"

#define TRIM_DONE               0xFF         //校准成功
#define TRIM_OVERFLOW           0xEE         //待校准时钟计数器溢出

#define REF_CON                 (32768u)        //参考时钟计数器初值（32768hz，3277约为100ms） 
#define CAL_CON_TARGET          (4000000u)//3840u        //待校准时钟计数器目标值（38400hz，3840为100ms） 
#define ERROR_RANGE             19u          //待校准时钟误差范围值，3840*0.005，约0.5%内

#define TRIM_MAX_NUM            60           //校准模块启动后允许连续校准次数
#define EVERYDAY_TRIM_MAX_NUM   50           //校准模块每天允许校准次数
/*温度差值 单位摄氏度*/
#define TEMPERTURE_DIFF 5.0f
enum clktrim_adjust_direction
{
	DIRECTIONLESS = 0,
	UPWARD,
	DOWNWARD	
};

typedef struct
{
	u8 adjust_direction_flg;   //调整方向的标志
	u32 diff_value;             //差值
}CLKTRIM_INFO;



enum clktrim_machine_state_num //时钟校准状态机状态号
{
	CLKTRIM_MACHINE_S0 = 0,
	CLKTRIM_MACHINE_S1,
	CLKTRIM_MACHINE_S2,
	CLKTRIM_MACHINE_S3
};

/* MACRO Define---------------------------------------------------------------*/
#define SYS_FREQ                4000000

#if(SYS_FREQ == 4000000)
#define SYSCTRL_FREQ            SysctrlRchFreq4MHz
#elif(SYS_FREQ == 8000000)
#define SYSCTRL_FREQ            SysctrlRchFreq8MHz
#elif(SYS_FREQ == 16000000)
#define SYSCTRL_FREQ            SysctrlRchFreq16MHz
#elif(SYS_FREQ == 24000000)
#define SYSCTRL_FREQ            SysctrlRchFreq24MHz
#endif


/* variables Define---------------------------------------------------------------*/


/* Function Declare------------------------------------------------------------*/
extern void ClkTrimInit(void);
u8 ClkTrimIfIdle(void);
void ClktrimMainMachine(void);
#ifdef __cplusplus
}
#endif

#endif /* __UWater_UART_DRIVER_H */
