
/***********************************************************************************************************************
 * HEADER FILES
 **********************************************************************************************************************/
#include "power_tools_kit_DirectFET.h"
#include "bldc_scalar_user_interface.h"
/***********************************************************************************************************************
 * LOCAL DATA
 **********************************************************************************************************************/
uint32_t board_temperature;
/**********************************************************************************************************************
* DATA STRUCTURES
**********************************************************************************************************************/
/* GPIO Init handle for Indicator LED Pin */
const XMC_GPIO_CONFIG_t Power_Tools_DirectFET_LedIndicator_Config =
{
 .mode             = XMC_GPIO_MODE_OUTPUT_PUSH_PULL,
 .output_level     = XMC_GPIO_OUTPUT_LEVEL_LOW,
 .input_hysteresis = (XMC_GPIO_INPUT_HYSTERESIS_t)0,
};

/* GPIO Init handle for Direction Pin */
const XMC_GPIO_CONFIG_t Power_Tools_DirectFET_DirectionPin_Config =
{
 .mode             = XMC_GPIO_MODE_INPUT_TRISTATE,
 .output_level     = XMC_GPIO_OUTPUT_LEVEL_LOW,
 .input_hysteresis = (XMC_GPIO_INPUT_HYSTERESIS_t)0,
};

/* GPIO Init handle for ShutDown Pin */
const XMC_GPIO_CONFIG_t Power_Tools_DirectFET_ShutDownPin_Config =
{
 .mode             = XMC_GPIO_MODE_INPUT_TRISTATE,
 .output_level     = XMC_GPIO_OUTPUT_LEVEL_HIGH,
 .input_hysteresis = (XMC_GPIO_INPUT_HYSTERESIS_t)0,
};

const XMC_GPIO_CONFIG_t Power_Tools_Pin_2_2_Config =
{
 .mode             = XMC_GPIO_MODE_INPUT_TRISTATE,
 .output_level     = XMC_GPIO_OUTPUT_LEVEL_LOW,
 .input_hysteresis = (XMC_GPIO_INPUT_HYSTERESIS_t)0,
};

const XMC_GPIO_CONFIG_t Power_Tools_Pin_2_10_Config =
{
 .mode             = XMC_GPIO_MODE_INPUT_TRISTATE,
 .output_level     = XMC_GPIO_OUTPUT_LEVEL_LOW,
 .input_hysteresis = (XMC_GPIO_INPUT_HYSTERESIS_t)0,
};


/***********************************************************************************************************************
 * LOCAL ROUTINES
 **********************************************************************************************************************/

/**********************************************************************************************************************
 * API IMPLEMENTATION
 **********************************************************************************************************************/
void Power_Tools_DirectFET_MotorStart(void)
{
	uint32_t direction;
	direction = XMC_GPIO_GetInput(POWER_TOOL_DIRECTFET_DIRCTION_PIN);
	if(direction==0)
	{
		Motor0_BLDC_SCALAR_SetDirection(BLDC_SCALAR_NEGATIVE_DIR);
	}
	else
	{
		Motor0_BLDC_SCALAR_SetDirection(BLDC_SCALAR_POSITIVE_DIR);
	}
	 Motor0_BLDC_SCALAR_MotorStart();
}
void Power_Tools_DirectFET_MotorStop(void)
{
	 Motor0_BLDC_SCALAR_MotorStop();
}
void Power_Tools_DirectFET_Init(void)
{
	XMC_GPIO_Init(POWER_TOOL_DIRECTFET_LED_INDICATOR, &Power_Tools_DirectFET_LedIndicator_Config);	//init P0.4
	XMC_GPIO_Init(POWER_TOOL_DIRECTFET_DIRCTION_PIN, &Power_Tools_DirectFET_DirectionPin_Config); 	//init P0.11
	XMC_GPIO_Init(POWER_TOOL_DIRECTFET_SHUTDOWN_PIN, &Power_Tools_DirectFET_ShutDownPin_Config);  	//init P0.5
	XMC_GPIO_Init(POWER_TOOL_Pin_2_10, &Power_Tools_Pin_2_10_Config); 	//init P2.10
	XMC_GPIO_Init(POWER_TOOL_Pin_2_2, &Power_Tools_Pin_2_2_Config);  	//init P2.2

	Motor0_BLDC_SCALAR_Init();
}

void Power_Tools_DirectFET_DirectionControl(void)
{
  uint32_t dir;
  dir = XMC_GPIO_GetInput(POWER_TOOL_DIRECTFET_DIRCTION_PIN);    //Read P0.11

  if(dir==0) // reverse direction
  {
    if(Motor0_BLDC_SCALAR.motor_set_direction!=BLDC_SCALAR_NEGATIVE_DIR)
    {
      Motor0_BLDC_SCALAR_MotorStop();
	  Motor0_BLDC_SCALAR_SetDirection(BLDC_SCALAR_NEGATIVE_DIR);
	  Motor0_BLDC_SCALAR_MotorStart();
    }
  }
  else
  {
    if(Motor0_BLDC_SCALAR.motor_set_direction!=BLDC_SCALAR_POSITIVE_DIR)
    {
	  Motor0_BLDC_SCALAR_MotorStop();
	  Motor0_BLDC_SCALAR_SetDirection(BLDC_SCALAR_POSITIVE_DIR);
	  Motor0_BLDC_SCALAR_MotorStart();
    }
  }
}
void Power_Tools_DirectFET_TemperatureControl(void)
{
	uint32_t temperature;

	Motor0_BLDC_SCALAR_GetUserDef1_Result(&temperature);

	board_temperature=(uint32_t) ((((uint16_t) 500)*(temperature - ((uint16_t) 409))) >> 12);

	if(board_temperature>TEMEPRATURE_SHUTDOWN_THRESHOLD)
	{
		Motor0_BLDC_SCALAR.error_status |= (uint32_t)1 << (uint32_t)BLDC_SCALAR_EID_OVER_TEMPERATURE;
		Motor0_BLDC_SCALAR.msm_state = BLDC_SCALAR_MSM_ERROR;
		Power_Tools_DirectFET_MotorStop();
	}
}

void Power_Tools_DirectFET_FaultStatus(void)
{
	volatile uint32_t fault_pin_status;
	fault_pin_status = XMC_GPIO_GetInput(POWER_TOOL_DIRECTFET_SHUTDOWN_PIN);
	 if(fault_pin_status==0)
	 {
		 Motor0_BLDC_SCALAR.error_status |= (uint32_t)1 << (uint32_t)BLDC_SCALAR_EID_BOARD_OVERCURRENT;
		 Motor0_BLDC_SCALAR.msm_state = BLDC_SCALAR_MSM_ERROR;
		 Power_Tools_DirectFET_MotorStop();
	 }
}
