/**
  **********************************************************************************
  * @file    ioctrl_hal.c
  * @author  Product application department
  * @version V1.0
  * @date    2020.02.19
  * @brief   IOCTRL模块HAL层驱动.
  * @note 这个文件提供了hal层ioctrl接口:
  *
  @verbatim
  ===================================================================================
                                  ##### 更新 #####
  ===================================================================================
  [..] 更新时间:
  [..] 更新人:
  [..] 更新内容:
        1. nothing.
  @endverbatim
 */

#include "ioctrl_hal.h"


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

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


/**
 * @brief IO control SPI 初始化
 *
 * @param[in] pIOSPIInit:指向IO control SPI初始化结构体的指针
 * @retval @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef HAL_IOCTRL_SPIInit(IOCTRL_SPIInitTypeDef *pIOSPIInit)
{
    HAL_StatusTypeDef ret = HAL_OK;

    DRV_IOCTRL_SetSPIDS(pIOSPIInit->ds);				
    DRV_IOCTRL_SetSPISR(pIOSPIInit->sr);				
    DRV_IOCTRL_SetSPIIS(pIOSPIInit->is);

    switch (pIOSPIInit->id)
    {
        case IOCTRL_SPI1:
        	DRV_IOCTRL_SetSPI1PS(pIOSPIInit->pins, pIOSPIInit->ps);	
        	DRV_IOCTRL_SetSPI1IE(pIOSPIInit->pins, pIOSPIInit->ie);
            break;
        case IOCTRL_SPI2:
        	DRV_IOCTRL_SetSPI2PS(pIOSPIInit->pins, pIOSPIInit->ps);	
        	DRV_IOCTRL_SetSPI2IE(pIOSPIInit->pins, pIOSPIInit->ie);
            break;
        case IOCTRL_SPI3:
        	DRV_IOCTRL_SetSPI3PS(pIOSPIInit->pins, pIOSPIInit->ps);	
        	DRV_IOCTRL_SetSPI3IE(pIOSPIInit->pins, pIOSPIInit->ie);
            break;
        default:
            break;
    }

    return HAL_OK;
    
}

/**
 * @brief IO control USI 初始化
 *
 * @param[in] pIOUSIInit:指向IO control USI初始化结构体的指针
 * @retval @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef HAL_IOCTRL_USIInit(IOCTRL_USIInitTypeDef *pIOUSIInit)
{
    HAL_StatusTypeDef ret = HAL_OK;
    
    DRV_IOCTRL_SetUSIDS(pIOUSIInit->ds);				
    DRV_IOCTRL_SetUSISR(pIOUSIInit->sr);				
    DRV_IOCTRL_SetUSIIS(pIOUSIInit->is);
	
	switch (pIOUSIInit->id)
    {
        case IOCTRL_USI1:
			DRV_IOCTRL_SetUSI1PS(pIOUSIInit->pins, pIOUSIInit->ps);	
            break;
        case IOCTRL_USI2:
			DRV_IOCTRL_SetUSI2DIEN(pIOUSIInit->pins, pIOUSIInit->dien_status);
			DRV_IOCTRL_SetUSI2DREN( pIOUSIInit->dren_status);
            break;
		case IOCTRL_USI3:
			DRV_IOCTRL_SetUSI3PUEN(pIOUSIInit->pins, pIOUSIInit->puen_status);	
			DRV_IOCTRL_SetUSI3DIEN(pIOUSIInit->pins, pIOUSIInit->dien_status);
			DRV_IOCTRL_SetUSI3DREN( pIOUSIInit->dren_status);
            break;
        default:
            break;
    }

	return HAL_OK;
    
}

/**
 * @brief IO control I2C 初始化
 *
 * @param[in] pIOI2CInit:指向IO control I2C初始化结构体的指针
 * @retval @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef HAL_IOCTRL_I2CInit(IOCTRL_I2CInitTypeDef *pIOI2CInit)
{
    HAL_StatusTypeDef ret = HAL_OK;
    
    DRV_IOCTRL_SetI2CDS(pIOI2CInit->ds);				
    DRV_IOCTRL_SetI2CSR(pIOI2CInit->sr);				
    DRV_IOCTRL_SetI2CIS(pIOI2CInit->is);

    switch (pIOI2CInit->id)
    {
        case IOCTRL_I2C1:
        	DRV_IOCTRL_SetI2C1PS(pIOI2CInit->pins, pIOI2CInit->ps);	
        	DRV_IOCTRL_SetI2C1IE(pIOI2CInit->pins, pIOI2CInit->ie);
            break;
        case IOCTRL_I2C2:
        	DRV_IOCTRL_SetI2C2PS(pIOI2CInit->pins, pIOI2CInit->ps);	
        	DRV_IOCTRL_SetI2C2IE(pIOI2CInit->pins, pIOI2CInit->ie);
            break;
        case IOCTRL_I2C3:
        	DRV_IOCTRL_SetI2C3PS(pIOI2CInit->pins, pIOI2CInit->ps);	
        	DRV_IOCTRL_SetI2C3IE(pIOI2CInit->pins, pIOI2CInit->ie);
            break;
        default:
            break;
    }

    return HAL_OK;
}

/**
 * @brief IO control UART 初始化
 *
 * @param[in] pIOUARTInit:指向IO control UART初始化结构体的指针
 * @retval @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef HAL_IOCTRL_UARTInit(IOCTRL_UARTInitTypeDef *pIOUARTInit)
{
    HAL_StatusTypeDef ret = HAL_OK;
    
    DRV_IOCTRL_SetUARTDS(pIOUARTInit->ds);				
    DRV_IOCTRL_SetUARTSR(pIOUARTInit->sr);				
    DRV_IOCTRL_SetUARTIS(pIOUARTInit->is);

    switch (pIOUARTInit->id)
    {
        case IOCTRL_UART1:
        	DRV_IOCTRL_SetUART1PS(pIOUARTInit->pins, pIOUARTInit->ps);
            break;
        case IOCTRL_UART2:
        	DRV_IOCTRL_SetUART2PS(pIOUARTInit->pins, pIOUARTInit->ps);
            break;
        default:
            break;
    }

    return HAL_OK;
}



/**
 * @brief IO control GINT初始化
 *
 * @param[in] pIOGINTInit:指向IO control GINT初始化结构体的指针
 * @retval @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef HAL_IOCTRL_GINTIint(IOCTRL_GINTInitTypeDef *pIOGINTInit)
{
    HAL_StatusTypeDef ret = HAL_OK;

    switch (pIOGINTInit->id)
    {
        case IOCTRL_GINTL:  /**< gint[7:0] */
            DRV_IOCTRL_SetGINTLDS(pIOGINTInit->ds);//负载能力		
            DRV_IOCTRL_SetGINTLSR(pIOGINTInit->sr);//翻转速率		
            DRV_IOCTRL_SetGINTLIS(pIOGINTInit->is);//输入方式
            DRV_IOCTRL_SetGINTLPS(pIOGINTInit->pins, pIOGINTInit->ps);//上拉使能
            DRV_IOCTRL_SetGINTLIE(pIOGINTInit->pins, pIOGINTInit->ie);//输入使能
            break;
        case IOCTRL_GINTH:  /**< gint[15:8] */
            DRV_IOCTRL_SetGINTHDS(pIOGINTInit->ds);				
            DRV_IOCTRL_SetGINTHSR(pIOGINTInit->sr);				
            DRV_IOCTRL_SetGINTHIS(pIOGINTInit->is);
            DRV_IOCTRL_SetGINTHPS(pIOGINTInit->pins, pIOGINTInit->ps);
            DRV_IOCTRL_SetGINTHIE(pIOGINTInit->pins, pIOGINTInit->ie);
            break;

        default:
            break;
    }

    return HAL_OK;
}

/**
 * @brief IO control SSI初始化
 *
 * @param[in] pIOGINTInit:指向IO control SSI初始化结构体的指针
 * @retval @ref HAL_StatusTypeDef
*/
HAL_StatusTypeDef HAL_IOCTRL_SSIIint(IOCTRL_SSIInitTypeDef *pIOSSIInit)
{
    HAL_StatusTypeDef ret = HAL_OK;

    switch (pIOSSIInit->id)
    {
        case IOCTRL_SSI1:
            DRV_IOCTRL_SetSSI1DS(pIOSSIInit->ds);				
            DRV_IOCTRL_SetSSI1SR(pIOSSIInit->sr);				
            DRV_IOCTRL_SetSSI1IS(pIOSSIInit->is);			
            DRV_IOCTRL_SetSSI1PUE(pIOSSIInit->pue);			
            DRV_IOCTRL_SetSSI1ODE(pIOSSIInit->ode);
            DRV_IOCTRL_SetSSI1PS(pIOSSIInit->pins, pIOSSIInit->ps);
            DRV_IOCTRL_SetSSI1IE(pIOSSIInit->pins, pIOSSIInit->ie);
            break;
        case IOCTRL_SSI2:
            DRV_IOCTRL_SetSSI2DS(pIOSSIInit->ds);				
            DRV_IOCTRL_SetSSI2SR(pIOSSIInit->sr);				
            DRV_IOCTRL_SetSSI2IS(pIOSSIInit->is);		
            DRV_IOCTRL_SetSSI2PUE(pIOSSIInit->pue);			
            DRV_IOCTRL_SetSSI2ODE(pIOSSIInit->ode);
            DRV_IOCTRL_SetSSI2PS(pIOSSIInit->pins, pIOSSIInit->ps);
            DRV_IOCTRL_SetSSI2IE(pIOSSIInit->pins, pIOSSIInit->ie);
            break;
        default:
            break;
    }

    return HAL_OK;
}

/**
 * @brief IO control 复用管脚切换
 *
 * @param[in] pSwap:切换位
 * @param[in] status:切换位执行状态
 * @retval @ref HAL_StatusTypeDef
*/
void HAL_IOCTRL_Swap(IOCTRL_SwapTypeDef pSwap, FunctionalState status)
{
    if(ENABLE == status)
    {
        DRV_IOCTRL_SwapEn(pSwap);
    }
    else
    {
        DRV_IOCTRL_SwapDis(pSwap);
    }
}

/**
 * @brief IO control Swapint复用管脚切换
 *
 * @param[in] pSwap:切换位
 * @param[in] status:切换位执行状态
 * @retval @ref HAL_StatusTypeDef
*/
void HAL_IOCTRL_Swapint(IOCTRL_SwapTypeDef pSwap,FunctionalState status)
{
    if(ENABLE == status)
    {
        DRV_IOCTRL_SwapintEn(pSwap);
    }
    else
    {
        DRV_IOCTRL_SwapintDisable(pSwap);
    }
}

/**
 * @brief IO control Swapintcr寄存器初始化全置0
 *
 * @param[in] NONE
 * @param[in] NONE
 * @retval NONE
*/
void HAL_IOCTRL_SwapintDIS(void)
{
	int pSwap;
	for(pSwap=0;pSwap<32;pSwap++)
	{
	 DRV_IOCTRL_SwapintDisable(pSwap);
	}
}



