#include "rf_switch_driver.h"
#include "gd32f10x_gpio.h"


/**! \func	 void vRFSwitchInit(struct rf_switch_config *config)
 *  \brief  Initialize RF Switch
 */
void vRFSwitchInit(struct rf_switch_config *pConfig,
																		uint32_t portA, 
																		uint32_t pinA, 
																		uint32_t portB, 
																		uint32_t pinB, 
																		uint32_t portC, 
																		uint32_t pinC,
																		uint32_t portEn,
																		uint32_t pinEn)
{	
	/* Configure GPIO Output function for RF Switch interface */	
	gpio_init(portA, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ, pinA);
	gpio_init(portB, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ, pinB);
	gpio_init(portC, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ, pinC);
	gpio_init(portEn, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ, pinEn);
	
	vRFSwitchDisable(pConfig);
	pConfig->rfOutState = xRFSwitchSet(pConfig, RF_OUT_01);
	pConfig->portA = portA;
	pConfig->portB = portB;
	pConfig->portC = portC;
	pConfig->portEn = portEn;
	pConfig->pinA = pinA;
	pConfig->pinB = pinB;
	pConfig->pinC = pinC;	
	pConfig->pinEn = pinEn;	
}

/**! \func	 void vRFSwitchEnable(struct rf_switch_config *config)
 *  \brief  Enable RF Switch
 */
void vRFSwitchEnable(struct rf_switch_config *pConfig)
{
	pConfig->enable = 1;
	gpio_bit_set(pConfig->portEn, pConfig->pinEn);
}

/**! \func	 void vRFSwitchDisable(struct rf_switch_config *config)
 *  \brief  Disable RF Switch
 */
void vRFSwitchDisable(struct rf_switch_config *pConfig)
{
	pConfig->enable = 0;
	gpio_bit_reset(pConfig->portEn, pConfig->pinEn);
}

/**! \func	 void xRFSwitchSet(struct rf_switch_config *config, RF_OUT_ENUM state)
 *  \brief  Switch between RF out state
 */
RF_OUT_ENUM xRFSwitchSet(struct rf_switch_config *pConfig, RF_OUT_ENUM state)
{
 	 pConfig->rfOutState = state;	 
	
	 if (state & 0x01)
		 gpio_bit_set(pConfig->portA, pConfig->pinA);
	 else
	   gpio_bit_reset(pConfig->portA, pConfig->pinA);
	 
	 if (state & 0x02)
		 gpio_bit_set(pConfig->portB, pConfig->pinB);
	 else
		 gpio_bit_reset(pConfig->portB, pConfig->pinB);
	 
	 if (state & 0x04)
		 gpio_bit_set(pConfig->portC, pConfig->pinC);
	 else
		 gpio_bit_reset(pConfig->portC, pConfig->pinC);
	 
	 return state;
}

/**! \func	RF_OUT_ENUM xRFSwitchGetState(struct rf_switch_config *config)
 *  \brief  Get RF Switch State
 */
RF_OUT_ENUM xRFSwitchGetState(struct rf_switch_config *pConfig)
{
	return pConfig->rfOutState;
}