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

#include "pit32_hal.h"


/*** 全局变量定义 ***************************************************************/
/** @
  * @{volatile
  */
 static PIT32_HandleTypeDef *g_hpit32;
/** @ 
  * @}
  */
/** @ 
  * @{static
  */
/** @ 
  * @}
  */
/*** 常量定义 *******************************************************************/
/** @
  * @{const
  */
/** @ 
  * @}
  */

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

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


/**
 * @brief 使能pit32.
 *
 * @param[in] hpit32
 */
void HAL_PIT32_Start(PIT32_HandleTypeDef *hpit32)
{
    DRV_PIT32_Cmd(hpit32->instance,ENABLE);
}

/**
 * @brief 停止pit32.
 *
 * @param[in] hpit32
 */
void HAL_PIT32_Stop(PIT32_HandleTypeDef *hpit32)
{
	DRV_PIT32_Cmd(hpit32->instance,DISABLE);
}

void __weak HAL_PIT32_IRQHandler(void)
{
	_pit32_clr_int_flag(g_hpit32->instance);

	if(g_hpit32->callback)
	{
		g_hpit32->callback((void *)g_hpit32);
		return;
	}
}

void __weak HAL_PIT32_Callback(void *hpit32)
{
}

/**
 * @brief 初始化pit32模块.
 * 
 * @param[in] hpit32
 * @return HAL_StatusTypeDef 
 */
HAL_StatusTypeDef HAL_PIT32_Init(PIT32_HandleTypeDef *hpit32)
{

    DRV_PIT32_Cmd(hpit32->instance,DISABLE);

    DRV_PIT32_SetPrescaler(hpit32->instance,hpit32->init.prescaler);

    DRV_PIT32_ReloadCmd(hpit32->instance,hpit32->init.reLoad);

    DRV_PIT32_ImmediatelyUpdateCNTCmd(hpit32->instance,hpit32->init.updateCNT);

    DRV_PIT32_DozeCmd(hpit32->instance,hpit32->init.runatDoze);

    DRV_PIT32_DebugCmd(hpit32->instance,hpit32->init.runatDebug);
    
    DRV_PIT32_SetCounter(hpit32->instance, hpit32->init.counter);

    HAL_PIT32_ConfigCallbackRegistry(hpit32,HAL_PIT32_Callback,0);

    if(hpit32->init.enableIE == ENABLE)
    {
    	if(hpit32->instance == PIT1)
				NVIC_Init(3, 3, PIT1_IRQn, 2);
			else if(hpit32->instance == PIT2)
				NVIC_Init(3, 3, PIT2_IRQn, 2);
			else if(hpit32->instance == PIT3)
				NVIC_Init(3, 3, PIT3_IRQn, 2);
			else if(hpit32->instance == PIT4)
				NVIC_Init(3, 3, PIT4_IRQn, 2);
			else
				return HAL_ERROR;
				
//			if(hpit32->instance == PIT1)
//			{
//				NVIC_Init(3, 3, PIT1_IRQn, 2);
//			}
//			else
//			{
//			  NVIC_Init(3, 3, PIT2_IRQn, 2);
//			}
    }

    DRV_PIT32_InterruptCmd(hpit32->instance,hpit32->init.enableIE);

    g_hpit32 = hpit32;

    DRV_PIT32_Cmd(hpit32->instance,ENABLE);
    
    return HAL_OK;
}

/**
 * @brief 开始/停止pit32.
 * 
 * @param[in] hpit32
 * @param[in] NewState 
 *  @arg ENABLE
 *  @arg DISABLE
 * @return HAL_StatusTypeDef 
 */
void HAL_PIT32_CMD(PIT32_HandleTypeDef *hpit32, FunctionalState NewState)
{
	DRV_PIT32_Cmd(hpit32->instance,NewState);
}

/**
 * @brief 使能/不使能pit32中断功能.
 * 
 * @param[in] hpit32
 * @param[in] NewState 
 *  @arg ENABLE
 *  @arg DISABLE
 */
void HAL_PIT32_ITConfig(PIT32_HandleTypeDef *hpit32, FunctionalState NewState)
{
   DRV_PIT32_InterruptCmd(hpit32->instance,NewState);
}

/**
 * @brief 重载pit32定时器的reload技术值.
 * 
 * @param[in] hpit32
 * @param[in] Reload 
 * @return HAL_StatusTypeDef 
 */
void HAL_PIT32_SetCounter(PIT32_HandleTypeDef *hpit32, uint16_t cnt)
{
	DRV_PIT32_SetCounter(hpit32->instance,cnt);
}

/**
 * @brief 获得pit32的当前计数值
 * 
 * @param[in] hpit32
 * @param[out] count 返回16bit计数值
 */
uint16_t HAL_PIT32_GetCounter(PIT32_HandleTypeDef *hpit32)
{
	return DRV_PIT32_GetCounter(hpit32->instance);
}

/**
 * @brief 设置pit32的预分频系数.
 * 只能在EN=0时，修改预分频计数器.
 * @pre HAL_PIT32_Stop
 * @param[in] hpit32
 * @param[in] prescaler 
 *  @arg PIT32_CLKDIV
 */
void HAL_PIT32_SetPrescaler(PIT32_HandleTypeDef *hpit32, PIT32_CLKDIV prescaler)
{
	DRV_PIT32_SetPrescaler(hpit32->instance,prescaler);
}

/**
 * @brief 获取pit32的预分频系数.
 * 分频系数有效位低4bit.
 * @param[in] hpit32
 * @return 当前分频值
 */
uint8_t HAL_PIT32_GetPrescaler(PIT32_HandleTypeDef *hpit32)
{
   uint32_t reg;

   reg = _pit32_get_pcsr_all(hpit32->instance);

   return ((uint8_t)((reg>>8)&0x0F));
}

/**
 * @brief 获取pit32中断标志位.
 * 
 * @param[in] hpit32
 * @return
 * 中断产生则返回1, 否则返回0
 */
uint8_t HAL_PIT32_GetITStatus(PIT32_HandleTypeDef *hpit32)
{
   if(_pit32_get_int_flag(hpit32->instance))
   {
	   return 1;
   }
   return 0;
}


/**
 * @brief 清除pit32中断标志位.
 *
 * @param[in] hpit32
 * @return NONE
 */
void HAL_PIT32_CleanITStatus(PIT32_HandleTypeDef *hpit32)
{
	_pit32_clr_int_flag(hpit32->instance);
}

