//###########################################################################
//
// FILE:    pmbus_topology.c
//
// TITLE:
//
// NOTES:
//  1)
//###########################################################################
//
//  Ver  | dd mmm yyyy | Who  		| Description of changes
// ======|=============|============|========================================
//  00 	   08 28 2015 	 HPCS
//
//  Texas Instruments, Inc
//  Copyright Texas Instruments 2008. All rights reserved.
//###########################################################################

#include "system_defines.h"
#include "Cyclone_Device.h"
#include "cyclone_defines.h"
#include "variables.h"
#include "function_definitions.h"
#include "software_interrupts.h"
#include "pmbus_common.h"
#include "pmbus_topology.h"
#include "pmbus_constants.h"



const Uint8 status_bit_mask[]    = STATUS_BIT_MASK;


/***********************************************************************
 *Start of MFR message
 ***********************************************************************/
const Uint8 setup_id[]          = SETUP_ID;
const Uint8 mfr_model[]			= MFR_MODEL;
const Uint8 mfr_revision[]		= MFR_REVISION;
const Uint8 mfr_date[]			= MFR_DATE;
const Uint8 mfr_location[]		= MFR_LOCATION;
const Uint8 mfr_serial[]		= MFR_SERIAL;
const Uint8 mfr_ic_device[]		= MFR_IC_DEVICE;
const Uint8 mfr_ic_device_rev[]	= MFR_IC_DEVICE_REV;
const Uint8 mfr_id[]			= MFR_ID;

#pragma DATA_SECTION (device_id_string, "FixedDeviceID");
const Uint8  device_id_string[] = DEVICE_ID;


/**************************************
 * Start of Vout command
 **************************************/
Uint8 pmbus_read_write_vout_mode(Uint8 pmbus_read)
{
	return 0;
}


//==========================================================================================
//pmbus_write_vout_transition_rate
//pmbus_read_vout_transition_rate
//==========================================================================================
inline Uint8 pmbus_write_vout_transition_rate(void)
{
	return 0;
}

inline Uint8 pmbus_read_vout_transition_rate(void)
{
	return 0;
}

Uint8 pmbus_read_write_vout_transition_rate(Uint8 pmbus_read)
{
	return 0;
}

//==========================================================================================
//pmbus_write_vout_cmd
//pmbus_read_vout_cmd
//==========================================================================================
inline Uint8 pmbus_write_vout_cmd(void)
{


	return 0;
}

inline Uint8 pmbus_read_vout_cmd(void)
{
	return 0;
}

Uint8 pmbus_read_write_vout_command(Uint8 pmbus_read)
{
	return 0;
}

inline Uint8 pmbus_write_ton_rise(void)
{
	return 0;
}

inline Uint8 pmbus_read_ton_rise(void)
{

	return 0;
}

Uint8 pmbus_read_write_ton_rise(Uint8 pmbus_read)
{
	return 0;
}

/**********************
 * End of vout command
 *********************/

/***********************
 * Start of fault command
 **********************/
inline Uint8 pmbus_write_clear_faults(void)
{
	return 0;
}

Uint8 pmbus_read_write_clear_faults(Uint8 pmbus_read)
{
	return 0;
}


//OV Fault
inline Uint8 pmbus_write_vout_ov_fault_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_vout_ov_fault_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_vout_ov_fault_limit(Uint8 pmbus_read)
{
	return 0;
}

//OV Warn
inline Uint8 pmbus_write_vout_ov_warn_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_vout_ov_warn_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_vout_ov_warn_limit(Uint8 pmbus_read)
{
	return 0;
}
//UV Fault
inline Uint8 pmbus_write_vout_uv_fault_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_vout_uv_fault_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_vout_uv_fault_limit(Uint8 pmbus_read)
{
	return 0;
}

//UV fault response


inline pmbus_read_vout_uv_fault_response(void)
{

}

inline pmbus_write_vout_uv_fault_response(void)
{

}

Uint8 pmbus_read_write_vout_uv_fault_response(Uint8 pmbus_read)
{

	return 0;
}

//UV Warn
inline Uint8 pmbus_write_vout_uv_warn_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_vout_uv_warn_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_vout_uv_warn_limit(Uint8 pmbus_read)
{
	return 0;
}

//oc fault response
inline Uint8 pmbus_write_iout_oc_fault_response(void)
{
	return 0;
}

inline Uint8 pmbus_read_iout_oc_fault_response(void)
{
	return 0;
}

Uint8 pmbus_read_write_oc_fault_response(Uint8 pmbus_read)
{
	return 0;
}

//OC Fault
inline Uint8 pmbus_write_iout_oc_fault_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_iout_oc_fault_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_iout_oc_fault_limit(Uint8 pmbus_read)
{
	return 0;
}

//OC Warn
inline Uint8 pmbus_write_iout_oc_warn_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_iout_oc_warn_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_iout_oc_warn_limit(Uint8 pmbus_read)
{
	return 0;
}


//OT Fault
inline Uint8 pmbus_write_temp_ot_fault_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_temp_ot_fault_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_temp_ot_fault_limit(Uint8 pmbus_read)
{
	return 0;
}

//OT Warn
inline Uint8 pmbus_write_temp_ot_warn_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_temp_ot_warn_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_temp_ot_warn_limit(Uint8 pmbus_read)
{
	return 0;
}

//VIN OV Fault
inline Uint8 pmbus_write_vin_ov_fault_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_vin_ov_fault_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_vin_ov_fault_limit(Uint8 pmbus_read)
{
	return 0;
}

//VIN OV Warn
inline Uint8 pmbus_write_vin_ov_warn_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_vin_ov_warn_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_vin_ov_warn_limit(Uint8 pmbus_read)
{
	return 0;
}
//VIN UV Fault
inline Uint8 pmbus_write_vin_uv_fault_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_vin_uv_fault_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_vin_uv_fault_limit(Uint8 pmbus_read)
{
	return 0;
}

//VIN UV Warn
inline Uint8 pmbus_write_vin_uv_warn_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_vin_uv_warn_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_vin_uv_warn_limit(Uint8 pmbus_read)
{
	return 0;
}
//IIN OC Fault
inline Uint8 pmbus_write_iin_oc_fault_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_iin_oc_fault_limit(void)
{
	return 0;
}


Uint8 pmbus_read_write_iin_oc_fault_limit(Uint8 pmbus_read)
{
	return 0;
}

//IIN OC Warn
inline Uint8 pmbus_write_iin_oc_warn_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_iin_oc_warn_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_iin_oc_warn_limit(Uint8 pmbus_read)
{
	return 0;
}

//PGOOD ON
inline Uint8 pmbus_write_pgood_on_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_pgood_on_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_pgood_on_limit(Uint8 pmbus_read)
{
	return 0;
}


//PGOOD OFF
inline Uint8 pmbus_write_pgood_off_limit(void)
{
	return 0;
}
inline Uint8 pmbus_read_pgood_off_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_pgood_off_limit(Uint8 pmbus_read)
{
	return 0;
}



/*************************************
 * End of fault command
 *************************************/




/*******************************************
 * Start of read param command
 ******************************************/
#define max(a,b)					(((a)>(b)) ? (a):(b))

//==========================================================================================
// pmbus_read_vout()
//==========================================================================================
inline Uint8 pmbus_read_vout(void)
{


	return 0;
}

Uint8 pmbus_read_write_vout(Uint8 pmbus_read)
{
	return 0;
}
/*
The mantissa is calculated by the following relationship:
400V/1.93V
Round(2.5 / 1.93 * 400) = 518.
If the exponent is 0 the available range of VIN that can be sensed is
0V to 518V.
*/
inline Uint8 pmbus_read_vin(void)
{


	return 0;
}

Uint8 pmbus_read_write_vin(Uint8 pmbus_read)
{
	return 0;
}

/*
The mantissa is calculated by the following relationship:
35A/1.6V
Round(2.5 / 1.6 * 35 << 4) = 875. (Experiementally tuned to 900)
If the exponent is 0 the available range of IOUT that can be sensed is
0A to 55A.
0.01733 = 2.5 / 1.6 * 45 / 2^12
*/
inline Uint8 pmbus_read_iout(void)
{


	return 0;
}
Uint8 pmbus_read_write_iout(Uint8 pmbus_read)
{
	return 0;
}

/*
Base temperature equation is 159.6 - VSENSE * 85.5. The ADC12 reading is first converted
into voltage by mulitplying the ADC12 value by 2.5 >> 12 or 5 >> 13. Making 85.5 into an
integer changes this factor to (2 * 85.5 * 5) >> 14 = 855 >> 14.
The exponent "exp" is chosen such that the maximum temperature that can be read is 159.6 C
and the minimum is ~ -54 C. The temperature resolution is: 0.0522 C.
*/
inline pmbus_read_temp_mosfet(void)
{


	return 0;
}
Uint8 pmbus_read_write_temp_mosfet(Uint8 pmbus_read)
{
	return 0;
}

/*
This equation has not been tuned.
(ADC12_Reading*2.5/4096 - 1.9)/0.004 + 25
ADC12_Reading*0.15259 - 450;
*/
inline Uint8 pmbus_read_temp_device(void)
{


	return 0;
}

Uint8 pmbus_read_write_temp_device(Uint8 pmbus_read)
{
	return 0;
}

inline Uint8 pmbus_read_frequency(void)
{

	return 0;

}
Uint8 pmbus_read_write_frequency(Uint8 pmbus_read)
{
	return 0;
}
/*************************************************
 * End of read param command
 *************************************************/

/**************************************************
 * Start of Communication, Memory, and Logic fault
 **************************************************/
inline Uint8 pmbus_write_restore_default_all(void)
{
	return 0;
}

Uint8 pmbus_read_write_restore_default_all(Uint8 pmbus_read)
{
	return 0;
}



//==========================================================================================
// pmbus_write_store_default_all()
//	Store all PMBus-configurable variables from RAM to Data Flash.
//==========================================================================================
inline Uint8 pmbus_write_store_default_all(void)
{
	return 0;
}

Uint8 pmbus_read_write_store_default_all(Uint8 pmbus_read)
{
	return 0;
}
/**************************************************
 * End of Communication, Memory, and Logic fault
 **************************************************/

/**************************************************
 * Start of specific command
 **************************************************/
inline Uint8 pmbus_write_ishare_configure(void)
{
	return 0;
}

inline Uint8 pmbus_read_ishare_configure(void)
{
	return 0;
}

Uint8 pmbus_read_write_ishare_configure(Uint8 pmbus_read)
{
	return 0;
}

//
inline Uint8 pmbus_write_pfc_phase_2_enable(void)
{
	return 0;
}

inline Uint8 pmbus_read_pfc_phase_2_enable(void)
{
	return 0;
}

Uint8 pmbus_read_write_pfc_phase_2_enable(Uint8 pmbus_read)
{
	return 0;
}

//pfc zvs enable
inline Uint8 pmbus_write_pfc_zvs_enable(void)
{
	return 0;
}

inline Uint8 pmbus_read_pfc_zvs_enable(void)
{
	return 0;
}

Uint8 pmbus_read_write_pfc_zvs_enable(Uint8 pmbus_read)
{
	return 0;
}

//pfc_os
inline Uint8 pmbus_write_pfc_os_enable(void)
{
	return 0;
}

inline Uint8 pmbus_read_pfc_os_enable(void)
{
	return 0;
}

Uint8 pmbus_read_write_pfc_os_enable(Uint8 pmbus_read)
{
	return 0;
}

//llc_sr_enable
inline Uint8 pmbus_write_llc_sr_enable(void)
{
	return 0;
}

inline Uint8 pmbus_read_llc_sr_enable(void)
{
	return 0;
}

Uint8 pmbus_read_write_llc_sr_enable(Uint8 pmbus_read)
{
	return 0;
}


inline Uint8 pmbus_write_deadband_config(void)
{
	return 0;
}

inline Uint8 pmbus_read_deadband_config(void)
{
	return 0;
}
Uint8 pmbus_read_write_deadband_config(Uint8 pmbus_read)
{
	return 0;
}

inline Uint8 pmbus_write_mode_switching_config(void)
{
	return 0;
}

inline Uint8 pmbus_read_mode_switching_config(void)
{
	return 0;
}

Uint8 pmbus_read_write_mode_switching_config(Uint8 pmbus_read)
{
	return 0;
}


inline Uint8 pmbus_write_light_load_config(void)
{
	return 0;
}

inline Uint8 pmbus_read_light_load_config(void)
{
	return 0;
}

Uint8 pmbus_read_write_light_load_config(Uint8 pmbus_read)
{
	return 0;
}

inline Uint8 pmbus_write_cpcc_config(void)
{
	return 0;
}

inline Uint8 pmbus_read_cpcc_config(void)
{
	return 0;
}

Uint8 pmbus_read_write_cpcc_config(Uint8 pmbus_read)
{
	return 0;
}

inline Uint8 pmbus_read_filter_select(void)
{
	return 0;
}

inline Uint8 pmbus_write_filter_select(void)
{
	return 0;
}

Uint8 pmbus_read_write_filter_select(Uint8 pmbus_read)
{
	return 0;
}
inline Uint8 pmbus_write_filter_gains(void)
{
	return 0;
}


inline Uint8 pmbus_read_filter_gains(void)
{
	return 0;
}

Uint8 pmbus_read_write_filter_gains(Uint8 pmbus_read)
{
	return 0;
}

/**************************************************
 * End of specific command
 **************************************************/

/**************************************************
 * Start of pmbus status command
 **************************************************/
inline Uint8 pmbus_read_status_bit_mask(void)
{
	return 0;
}

Uint8 pmbus_read_write_status_bit_mask(Uint8 pmbus_read)
{
	return 0;
}

Uint8 pmbus_read_write_status_byte(Uint8 pmbus_read)
{
	return 0;
}

Uint8 pmbus_read_write_status_word(Uint8 pmbus_read)
{
	return 0;
}
/**************************************************
 * End of pmbus status command
 **************************************************/

/***************************************************
 * Start of other commands
 ***************************************************/
//==========================================================================================
inline Uint8 pmbus_write_vin_on_limit(void)
{
	return 0;
}

inline Uint8 pmbus_read_vin_on_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_vin_on_limit(Uint8 pmbus_read)
{
	return 0;
}


//==========================================================================================
inline Uint8 pmbus_write_vin_off_limit(void)
{
	return 0;
}

inline Uint8 pmbus_read_vin_off_limit(void)
{
	return 0;
}

Uint8 pmbus_read_write_vin_off_limit(Uint8 pmbus_read)
{
	return 0;
}
/***************************************************
 * End of other commands
 ***************************************************/

int32 get_pmbus_address(void)
{
	int32 temp_adc_address;
	while(AdcRegs.ADCSTAT.bit.ADC_INT == 0)
	{
		;
	}

	temp_adc_address = AdcRegs.ADCRESULT[7].bit.RESULT;

	if (temp_adc_address > 600)
	{
		temp_adc_address = 0x59; //Module 1
	}
	else
	{
		temp_adc_address = 0x5A; //Module 2
	}
	PMBusRegs.PMBCTRL3.bit.IBIAS_A_EN = 0;

	return temp_adc_address;
}


