/**
  **********************************************************************************
  * @file    dma2d_hal.c
  * @author  Product application department
  * @version V1.0
  * @date    2020.05.25
  * @brief   DMA2D模块HAL层驱动.
  *
  @verbatim
  ===================================================================================
                                  ##### 更新 #####
  ===================================================================================
  [..] 更新时间:
  [..] 更新人:
  [..] 更新内容:
        1. nothing.
  @endverbatim
*/

#include "dma2d_hal.h"

/*** 全局变量定义 ***************************************************************/
/**@ volatile
  *@{
  */

/**@
  *@}
  */

/**@ static
  *@{
  */
static DMA2D_HandleTypeDef *g_hdma2d;
/**@
  *@}
  */
/*** 常量定义 *******************************************************************/
/**@ const
  *@{
  */
/**@
  *@}
  */

/*** 函数定义 *******************************************************************/

/**
  *@brief DMA2D:注册寄存器的回调函数
  *
  *@param[in] hdma2d DMA2D句柄
  *@param[in] callback:回调函数实例
  *@param[in] index:回调函数类别
  *@return
  *- 0 :成功;
  *- 1 :失败;
  */
static uint8_t HAL_DMA2D_ConfigCallbackRegistry(DMA2D_HandleTypeDef *hdma2d,
		                                            dma2d_callback_t callback,
                                                uint8_t           index)
{
	(void)index;
	hdma2d->callback = callback;
	return 0;
}

/**
  *@brief DMA2D:中断处理函数
  *
  *@param[in] NONE
  *@param[out] NONE
  *@return
  *- NONE
  */
void __weak HAL_DMA2D_IRQHandler(void)
{
	 
	if(g_hdma2d->callback)
	{
		g_hdma2d->callback((void *)g_hdma2d);
		return;
	}
}

/**
  *@brief DMA2D:中断回调处理函数
  *
  *@param[in] NONE
  *@param[out] NONE
  *@return
  *- NONE
  */
void HAL_DMA2D_Callback(void)
{
	unsigned int data;
	
	data = g_hdma2d->Instance->ISR;
 
	if(data & ISR_TCIF)
		g_hdma2d->Init.G_flg_dma2d_done = 1;
	
	g_hdma2d->Instance->IFCR |= data;
}

/**
  *@brief DMA2D:软复位函数
  *
  *@param[in] NONE
  *@param[out] NONE
  *@return
  *- NONE
  */
void HAL_DMA2D_SoftReset(void)
{
   DRV_DMA2D_SoftReset();
}

/**
  *@brief DMA2D:颜色表初始化函数
  *
  *@param[in] addr : 颜色表地址
  *@param[in] len : 颜色表长度
  @param[out] NONE
  *@return
  *- NONE
  */
void HAL_DMA2D_ClutInit(uint32_t* addr, int32_t len)
{
   DRV_DMA2D_ClutInit(addr, len);
}
 
 /**
  *@brief DMA2D:颜色表导入化函数
  *
  *@param[in] hdma2d : dma2d句柄
  @param[out] NONE
  *@return
  *- NONE
  */
void HAL_DMA2D_FgClutLoad(DMA2D_HandleTypeDef *hdma2d)
{
   DRV_DMA2D_FgClutLoad(hdma2d->Instance , &hdma2d->Init);
}
 
 /**
  *@brief DMA2D:通过PFC的内存到内存的初始化函数
  *
  *@param[in] hdma2d : DMA2D句柄
  @param[out] NONE
  *@return
  *- NONE
  */
HAL_StatusTypeDef HAL_DMA2D_PfcHard(DMA2D_HandleTypeDef *hdma2d)
{
	if(hdma2d->Init.EnableIE == ENABLE)
	{
	  g_hdma2d = hdma2d;
    HAL_DMA2D_ConfigCallbackRegistry(hdma2d,HAL_DMA2D_Callback,0);
	}
	DRV_DMA2D_PfcHard(hdma2d->Instance,&hdma2d->Init);
	
	return(HAL_OK);
	
}

 /**
  *@brief DMA2D:寄存器到内存模式初始化函数
  *
  *@param[in] hdma2d : DMA2D句柄
  @param[out] NONE
  *@return
  *- NONE
  */
 HAL_StatusTypeDef HAL_DMA2D_Reg2Mem(DMA2D_HandleTypeDef *hdma2d)
{
	if(hdma2d->Init.EnableIE == ENABLE)
	{
	  g_hdma2d = hdma2d;
    HAL_DMA2D_ConfigCallbackRegistry(hdma2d,HAL_DMA2D_Callback,0);
	}
	DRV_DMA2D_Reg2Mem(hdma2d->Instance,&hdma2d->Init);
	
	return(HAL_OK);
	
}

 /**
  *@brief DMA2D:内存到内存模式初始化函数
  *
  *@param[in] hdma2d : DMA2D句柄
  @param[out] NONE
  *@return
  *- NONE
  */
 HAL_StatusTypeDef HAL_DMA2D_Mem2Mem(DMA2D_HandleTypeDef *hdma2d)
{
	if(hdma2d->Init.EnableIE == ENABLE)
	{
	  g_hdma2d = hdma2d;
    HAL_DMA2D_ConfigCallbackRegistry(hdma2d, HAL_DMA2D_Callback, 0);
	}
	DRV_DMA2D_Mem2Mem(hdma2d->Instance,&hdma2d->Init);
	
	return(HAL_OK);
	
}

 /**
  *@brief DMA2D:带PFC和混合的内存到内存模式初始化函数
  *
  *@param[in] hdma2d : DMA2D句柄
  @param[out] NONE
  *@return
  *- NONE
  */
  HAL_StatusTypeDef HAL_DMA2D_BlendHard(DMA2D_HandleTypeDef *hdma2d)
{
	if(hdma2d->Init.EnableIE == ENABLE)
	{
	  g_hdma2d = hdma2d;
    HAL_DMA2D_ConfigCallbackRegistry(hdma2d,HAL_DMA2D_Callback,0);
	}
	DRV_DMA2D_BlendHard(hdma2d->Instance,&hdma2d->Init);
	 
	return(HAL_OK);
	
}
