/**
  **********************************************************************************
  * @file    clcd_hal.c
  * @author  Product application department
  * @version V1.0
  * @date    2020.07.13
  * @brief   CLCD模块HAL层驱动.
  *
  @verbatim
  ===================================================================================
                                  ##### 更新 #####
  ===================================================================================
  [..] 更新时间:
  [..] 更新人:
  [..] 更新内容:
        1. nothing.
  @endverbatim
  */
	
#include "type.h"
#include "clcd_drv.h"
#include "clcd_hal.h"


/*** 全局变量定义 ***************************************************************/
/** @
  * @{volatile
  */
 volatile uint32_t clcd_frame_count = 0;
/** @ 
  * @}
  */
/** @ 
  * @{static
  */
 static CLCD_HandleTypeDef *g_hclcd;
/** @ 
  * @}
  */
/*** 常量定义 *******************************************************************/
/** @
  * @{const
  */
/** @ 
  * @}
  */

/*** 函数定义 *******************************************************************/
extern void DelayMS(__IO uint32_t delaymS);
static uint8_t flag=0;
void HAL_CLCD_CheckState(void)
{
	u8 tcon_timeout=0;
	flag=0;
	while(1)
	{
		DelayMS(1);
		tcon_timeout++;
		if((flag==1)||(tcon_timeout>20))
    break;
	}
}
/**
  * @brief CLCD中断服务函数
  * 
  * @param[in]  hclcd  clcd句柄
	* @param[out] NONE
  * @return NONE
  */
//void CLCD_IRQHandler(void)
//{
//  if (_clcd_get_int_status(g_hclcd->instance) & VCOMP)
//	{
//		flag=1;
//		printf("int\r\n");
//	}
//	_clcd_clr_int(g_hclcd->instance, MBERR|VCOMP|LNBU|FUF);//clear all interrupt status.
//}

/**
 * @brief 恢复CLCD为默认设置.
 * 
 * @param[in] hclcd CLCD句柄
 * @return HAL_StatusTypeDef 
 */
HAL_StatusTypeDef HAL_CLCD_DeInit(CLCD_HandleTypeDef *hclcd)
{
	DRV_CLCD_Deinit(hclcd->instance);
	return HAL_OK;
}


/**
 * @brief 初始化CLCD模块.
 * 
 * @param[in] hclcd CLCD句柄
 * @return HAL_StatusTypeDef 
 */
HAL_StatusTypeDef HAL_CLCD_Init(CLCD_HandleTypeDef *hclcd)
{
	if(hclcd->init.enableIE == ENABLE)
	{
		_clcd_enable_int(hclcd->instance, VCOMP);
	  NVIC_Init(3, 3, CLCD_IRQn, 2);//enable LCDC interrupt.
	}
	DRV_CLCD_SetClkDiv(hclcd->instance, hclcd->init.clcd_clkdiv);
	
	DRV_CLCD_SetTiming0(hclcd->instance, hclcd->init.clcd_timeing.hbp,
                                       hclcd->init.clcd_timeing.hfp,
	                                     hclcd->init.clcd_timeing.hsync,
	                                     hclcd->init.clcd_timeing.width/16);
	
	DRV_CLCD_SetTiming1(hclcd->instance, hclcd->init.clcd_timeing.vbp,
	                                     hclcd->init.clcd_timeing.vfp,
	                                     hclcd->init.clcd_timeing.vsync,
	                                     hclcd->init.clcd_timeing.height);
	
	
	DRV_CLCD_SetTiming2(hclcd->instance, hclcd->init.clcd_timeing.width, hclcd->init.clcd_timeing.flags &(IEO | IPC | IHS | IVS));
	DRV_CLCD_SetTiming3(hclcd->instance, 0, FALSE);
	DRV_CLCD_SetControlRegister(hclcd->instance, hclcd->init.clcd_cfg.control);
	
	g_hclcd = hclcd;
	
	return HAL_OK;
}

/**
 *@brief 使能或禁止CLCD.
 *
 *@param[in] hclcd CLCD句柄;
 *@param[in] NewState 新的状态
 * -ENABLE;
 * -DISABLE;
 *@return 无;
 */
void HLA_CLCD_Cmd(CLCD_HandleTypeDef *hclcd, FunctionalStateTypeDef NewState)
{
    DRV_CLCD_Cmd(hclcd->instance, NewState);
}

/**
 *@brief 设置Upper Pannel Frame base address.
 *
 *@param[in] hclcd CLCD句柄;
 *@param[in] addr 地址;
 *@return NONE;
 */
void HLA_CLCD_UpuerPannelBaseAddr(CLCD_HandleTypeDef *hclcd, uint32_t addr)
{
    _clcd_set_upbase(hclcd->instance, addr);
}

