#include "BL210x_port.h"


/****************************************************************************************************************************************** 
* 函数名称: PORT_Init()
* 功能说明:	端口引脚功能选择
* 输    入: PORT_InitTypeDef* PORT_InitStruct
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
void PORT_Init(PORT_InitTypeDef* PORT_InitStruct)
{
	uint32_t temp = 0;
	
//	assert_param(IS_PORT_ALL(PORT_InitStruct->Port));                             //检查输入的参数Port是否合法   
//	
//	assert_param(IS_INPUT_EN(PORT_InitStruct->Input_En));                         //检查输入的参数Input_En是否合法   
//	
//	assert_param(IS_PORT_FUNC(PORT_InitStruct->Func));                            //检查输入的参数Func是否合法 
	
	switch(PORT_InitStruct->Port)
	{
		case PORTA:
			
#ifdef IC_BL2101
  		PORT->PORTA_SEL &= ~(0x03 << ((PORT_InitStruct->Pin)*2));                           
			PORT->PORTA_SEL |= ((PORT_InitStruct->Func) << ((PORT_InitStruct->Pin)*2));          //PORTA IO端口功能配置
#endif
		
#ifdef IC_BL2102
			if(PORT_InitStruct->Pin <= GPIO_PIN7)
			{
				temp = PORT->PORTA_SEL0;
				
				temp &= ~(0x0F << ((PORT_InitStruct->Pin)*4)); 
				temp |= ((PORT_InitStruct->Func) << ((PORT_InitStruct->Pin)*4));
				
				PORT->PORTA_SEL0 = temp;                                          //PORTA0-7 IO端口功能配置
			}
			else if(PORT_InitStruct->Pin <= GPIO_PIN15)
			{
				temp = PORT->PORTA_SEL1;
				
				temp &= ~(0x0F << ((PORT_InitStruct->Pin - GPIO_PIN8)*4)); 
				temp |= ((PORT_InitStruct->Func) << ((PORT_InitStruct->Pin - GPIO_PIN8)*4));

				PORT->PORTA_SEL1 = temp;                                          //PORTA8-15 IO端口功能配置
			}
#endif		
			
			if(PORT_InitStruct->Input_En == ENABLE)                                 
			{
				PORT->PORTA_INEN |=  (1 << (PORT_InitStruct->Pin));            //输入使能打开
			}
			else
			{
				PORT->PORTA_INEN &=  ~(1 << (PORT_InitStruct->Pin));           //输入使能关闭
			}
		break;

#ifdef IC_BL2101			
		case PORTB:
			
			PORT->PORTB_SEL &= ~(0x03 << ((PORT_InitStruct->Pin)*2));
			PORT->PORTB_SEL |= ((PORT_InitStruct->Func) << ((PORT_InitStruct->Pin)*2));        //PORTB IO端口功能配置
			
			if(PORT_InitStruct->Input_En)
			{
				PORT->PORTB_INEN |=  (1 << (PORT_InitStruct->Pin));            //输入使能打开
			}
			else
			{
				PORT->PORTB_INEN &=  ~(1 << (PORT_InitStruct->Pin));           //输入使能关闭
			}
		break;
#endif
			
		default:
		break;
	}
}

/****************************************************************************************************************************************** 
* 函数名称: PORT_CFG_Init()
* 功能说明:	端口配置
* 输    入: PORT_CFGTypeDef* PORT_CFGStruct
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
void PORT_CFG_Init(PORT_CFGTypeDef* PORT_CFGStruct)
{
	uint32_t temp = 0;
	
#ifdef IC_BL2101
	PORT->PORT_CFG &= ~(3 << PORT_CFG_A0_7_DS_POS);
	PORT->PORT_CFG |=  ((PORT_CFGStruct->Out_Cur_A0_7) << PORT_CFG_A0_7_DS_POS);           //配置PORTA0-A7的驱动能力
		
	PORT->PORT_CFG &= ~(3 << PORT_CFG_A8_15_DS_POS);
	PORT->PORT_CFG |=  ((PORT_CFGStruct->Out_Cur_A8_15) << PORT_CFG_A8_15_DS_POS);         //配置PORTA8-A15的驱动能力

	PORT->PORT_CFG &= ~(3 << PORT_CFG_B0_7_DS_POS);
	PORT->PORT_CFG |=  ((PORT_CFGStruct->Out_Cur_B0_7) << PORT_CFG_B0_7_DS_POS);           //配置PORTB0-B7的驱动能力
		
	PORT->PORT_CFG &= ~(3 << PORT_CFG_PUR_POS);
	PORT->PORT_CFG |=  ((PORT_CFGStruct->PullUp_Res) << PORT_CFG_PUR_POS);                 //配置端口PORTA PORTB的上拉电阻
	
	PORT->PORT_CFG &= ~(1 << PORT_CFG_HYS_POS);
	PORT->PORT_CFG |=  ((PORT_CFGStruct->InputHys) << PORT_CFG_HYS_POS);                   //配置PORTA PORTB的输入迟滞等级
	
	PORT->PORT_CFG &= ~(1 << PORT_CFG_WKRF_POS);
	PORT->PORT_CFG |=  ((PORT_CFGStruct->WakeUp_Edge) << PORT_CFG_WKRF_POS);	             //配置PORTA PORTB的唤醒功能沿
#endif

	temp = PORT->PORT_CFG;
	
	temp &= ~(0x03 << PORT_CFG_DS_POS);
	temp |=  ((PORT_CFGStruct->Out_Cur) << PORT_CFG_DS_POS);                     //配置PORTA的驱动能力
	
	temp &= ~(0x03 << PORT_CFG_PUR_POS);
	temp |=  ((PORT_CFGStruct->PullUp_Res) << PORT_CFG_PUR_POS);                 //配置端口PORTA的上拉电阻
	
	temp &= ~(0x01 << PORT_CFG_HYS_POS);
	temp |=  ((PORT_CFGStruct->InputHys) << PORT_CFG_HYS_POS);                   //配置PORTA的输入迟滞等级
	
	temp &= ~(0x01 << PORT_CFG_WKRF_POS);
	temp |=  ((PORT_CFGStruct->WakeUp_Edge) << PORT_CFG_WKRF_POS);	             //配置PORTA的唤醒功能沿
	
	PORT->PORT_CFG = temp;	
}



