/**
 ********************************************************************************************************
 * @file F28335Port.cpp
 * @brief Source file for F28335Port.h
 * @copyright 2020 Eaton Corporation. All Rights Reserved.
 *********************************************************************************************************
 */
#include "F28335Port.h"

/*
 *****************************************************************************************
 *		Globals
 *****************************************************************************************
 */
volatile stDSPOutRegister DSPOutRegister;
volatile stDSPInRegister DSPInRegister;

/*
 *****************************************************************************************
 * See header file for function definition.
 *****************************************************************************************
 */
void InitDSP_IORegs( void )
{
	// InitGpio in DSP2833c_Gpio.c has already initialized everything to inputs
	// just change the IOs that we need to outputs and we're done

	// ERIC: Change default I/O pins here..
	DSPOutRegister.GpoA.all = ( IOGOODSET_BIT );	// IO GOOD SET high = IO GOOD low
	// DSPOutRegister.GpoB.all = ( CLOSEPCCBREAKER_BIT | TRIPPCCBREAKER_BIT | CLOSECONTACTORA_BIT);
	// nevermind ... Eugene switched it back on me.
	DSPOutRegister.GpoB.all = 0;
	DSPOutRegister.GpoC.all = 0;

	WriteDSPOutputs_ISR();

	EALLOW;

	GpioCtrlRegs.GPADIR.all = OUTREGA_OUTPUT_MASK;
	GpioCtrlRegs.GPBDIR.all = OUTREGB_OUTPUT_MASK;
	GpioCtrlRegs.GPCDIR.all = OUTREGC_OUTPUT_MASK;

	EDIS;

	DSPInRegister.GpiA.all = 0;
	DSPInRegister.GpiB.all = 0;
	DSPInRegister.GpiC.all = 0;

	InverterPWMOff();
}

/*
 *****************************************************************************************
 * See header file for function definition.
 *****************************************************************************************
 */
#pragma CODE_SECTION( WriteDSPOutputs_ISR, "ramfuncs" );
/*
 *****************************************************************************************
 * See header file for function definition.
 *****************************************************************************************
 */
void WriteDSPOutputs_ISR( void )
{
	//
	// * This function runs from DMA ISR, no BIOS calls allowed
	//

	uint32_t regValue;

	regValue = DSPOutRegister.GpoA.all;
	// regValue &= OUTREGA_OUTPUT_MASK;
	GpioDataRegs.GPASET.all = regValue;
	regValue = ~DSPOutRegister.GpoA.all;
	// regValue &= OUTREGA_OUTPUT_MASK;
	GpioDataRegs.GPACLEAR.all = regValue;

	regValue = DSPOutRegister.GpoB.all;
	// regValue &= OUTREGB_OUTPUT_MASK;
	GpioDataRegs.GPBSET.all = regValue;
	regValue = ~DSPOutRegister.GpoB.all;
	// regValue &= OUTREGB_OUTPUT_MASK;
	GpioDataRegs.GPBCLEAR.all = regValue;

	regValue = DSPOutRegister.GpoC.all;
	// regValue &= OUTREGC_OUTPUT_MASK;
	GpioDataRegs.GPCSET.all = regValue;
	regValue = ~DSPOutRegister.GpoC.all;
	// regValue &= OUTREGC_OUTPUT_MASK;
	GpioDataRegs.GPCCLEAR.all = regValue;
}

/*
 *****************************************************************************************
 * See header file for function definition.
 *****************************************************************************************
 */
#pragma CODE_SECTION( ReadDSPInputs, "ramfuncs" );
/*
 *****************************************************************************************
 * See header file for function definition.
 *****************************************************************************************
 */
void ReadDSPInputs( void )
{
	uint32_t regValue;

	regValue = GpioDataRegs.GPADAT.all;
	// regValue &= INREGA_INPUT_MASK;
	DSPInRegister.GpiA.all = regValue;

	regValue = GpioDataRegs.GPBDAT.all;
	// regValue &= INREGB_INPUT_MASK;
	DSPInRegister.GpiB.all = regValue;

	regValue = GpioDataRegs.GPCDAT.all;
	// regValue &= INREGC_INPUT_MASK;
	DSPInRegister.GpiC.all = regValue;
}

/*
 *****************************************************************************************
 * See header file for function definition.
 *****************************************************************************************
 */
void InverterPWMOn( void )
{
	MasterPWMOn();
	EALLOW;
	GpioCtrlRegs.GPAMUX1.bit.GPIO8 = 1;		// Configure GPIO8 as EPWM5A
	GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1;		// Configure GPIO2 as EPWM2A
	GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 1;		// Configure GPIO4 as ePWM3A
	EDIS;
}

/*
 *****************************************************************************************
 * See header file for function definition.
 *****************************************************************************************
 */
void InverterPWMOff( void )
{
	// SetStack1GateEnable(0);
	// SetStack2GateEnable(0);
	// SetStack3GateEnable(0);

	// MasterPWMOff() was not within the UPS call... thought I should note that for later.
	MasterPWMOff();
	EALLOW;
	GpioCtrlRegs.GPAMUX1.bit.GPIO8 = 0;		// Configure GPIO8 as input
	GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 0;		// Configure GPIO2 as input
	GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 0;		// Configure GPIO4 as input
	EDIS;
}

/*
 *****************************************************************************************
 * See header file for function definition.
 *****************************************************************************************
 */
void DisableUnusedPWMs( void )
{
	EALLOW;

	// Formerly the Rectifier
	GpioCtrlRegs.GPAMUX1.bit.GPIO9 = 0;		// Configure GPIO9 as input
	GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 0;		// Configure GPIO3 as input
	GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 0;		// Configure GPIO5 as input
	EDIS;
}

/*
 *****************************************************************************************
 * See header file for function definition.
 *****************************************************************************************
 */
void SetIOGood( uint16_t good )
{
	if ( good )
	{
		DSPOutRegister.GpoA.bit.IO_Good_Set = 0;
	}
	else
	{
		DSPOutRegister.GpoA.bit.IO_Good_Set = 1;
	}
}
