//###########################################################################
//
// FILE:    store_restore_DFLASH.c
//
// TITLE:
//
// NOTES:
//  1)
//###########################################################################
//
//  Ver  | dd mmm yyyy | Who  		| Description of changes
// ======|=============|============|========================================
//  00 	   05 04 2015 	 HPCS
//
//  Texas Instruments, Inc
//  Copyright Texas Instruments 2008. All rights reserved.
//###########################################################################
#include "system_defines.h"
#include "cyclone_device.h"
#include "variables.h"
#include "function_definitions.h"
#include "software_interrupts.h"
#include "cyclone_defines.h"
#include "pmbus_common.h"
#include "pmbus_topology.h"
#include "pmbus_constants.h"

#define VBAT_GAIN (83.3)
#define VBUS_GAIN (110)

#define VBAT_ADC_SCALAR ( (  VBAT_GAIN *4095  ) /2500 )
#define VBUS_ADC_SCALAR ( (  VBUS_GAIN *4095  ) /2500 )

void store_to_DFLASH()
{

	Uint32  DFLASH_pointer =  DFLASH_START_ADDRESS ;
	int i;



	for(i=0;i<DATA_FLASH_NUM_SEGMENTS;i++)
	{
		erase_data_flash_segment(i);
	}


	for(i=0;i<NUMBER_OF_DFLASH_VARIABLES;i++)
	{
		write_data_flash_word(DFLASH_pointer,dflash_variables.dflash_array[i]);
		DFLASH_pointer=DFLASH_pointer+4;
	}


}

void restore_from_DFLASH()
{

	Uint32  * DFLASH_pointer = (Uint32 *) DFLASH_START_ADDRESS;
		int i;

		for(i=0;i<NUMBER_OF_DFLASH_VARIABLES;i++)
		{
			dflash_variables.dflash_array[i]=*(DFLASH_pointer++) ;
		}

		DFLASH_pointer = (Uint32 *) DFLASH_START_ADDRESS;
		store=*(DFLASH_pointer);
		restored=0;
		if ( store == 0xFFFFFFFF || (dflash_variables.variables.IS_DFLASHED !=0x5A5A) )
		{

			dflash_variables.variables.IS_DFLASHED=0x5A5A;
			dflash_variables.variables.buck_constant_deadtime=100;
			dflash_variables.variables.boost_constant_deadtime=100;

			dflash_variables.variables.ADC_Value_for_CCtoCV_switch_ON_BATT_SIDE=2170;//  /40 V  16.8 V
			dflash_variables.variables.ADC_Value_for_nominal_battery_voltage=1954; //36 V
			dflash_variables.variables.ADC_Value_for_nominal_system_voltage=2069;  //38 V
			dflash_variables.variables.EADC_DAC_value_for_constant_current=2273; //1A
		    dflash_variables.variables.ADC_value_for_constant_current_charging=627;//1A
			dflash_variables.variables.OVP_system			=	2340; 	//43 v //13.85 V
			dflash_variables.variables.OVP_battery			=	2441;	//45 v//16.85 V
			dflash_variables.variables.UVP_system			=	1907;	//35 V //10.2 V
			dflash_variables.variables.ADC_Value_for_trigger_point_ON_PS_SIDE=dflash_variables.variables.UVP_system ;
			dflash_variables.variables.UVP_battery			=	1757;	//32.4 V //9 V
			dflash_variables.variables.OCP_battery          =2640;//4A
			dflash_variables.variables.current_scalar 	=	5572;
			dflash_variables.variables.OCP_system= 1760; //28A
			dflash_variables.variables.short_test=0;
			dflash_variables.variables.board_mode=2;//Do calibration if Dflash was erased.
		    dflash_variables.variables.calibrate_state= CAPTURE_NOTHING;// Start calibration by gathering set points.
		    dflash_variables.variables.Max_Vbus_dynamic = 2025;
		    dflash_variables.variables.Min_Vbus_static = 2048;
		    dflash_variables.variables.Max_Vbus_dynamic = 2079;
		    dflash_variables.variables.Max_Vbus_static = 2077;
		    dflash_variables.variables.Min_Vbus_EADC_DAC = 13023;
		    dflash_variables.variables.Max_Vbus_EADC_DAC = 13023;
		    dflash_variables.variables.Vbus_error_diff = 0;
		    dflash_variables.variables.Vbus_EADC_error_threshold =2;
		    dflash_variables.variables.Vbus_error_diff_margin = 10;
		    store_to_DFLASH();
			restored=1;
		}
		else
		{
		    dflash_variables.variables.board_mode=0;
		}



}


Uint8 store_to_DFLASH_slow()
{

	static Uint32 * DFLASH_pointer=(Uint32 *) DFLASH_START_ADDRESS;
	static int DFLASH_flashing_state=0;
	static int i=0;

	switch (DFLASH_flashing_state)
	{
	case 0:
			if (DecRegs.DFLASHCTRL.bit.BUSY ==0)
			{

				erase_dflash_segment_no_delay(i);
				i++;
				if(i == DATA_FLASH_NUM_SEGMENTS)
				{
					DFLASH_pointer=(Uint32 *)DFLASH_START_ADDRESS;
					DFLASH_flashing_state=1;
					i=0;
				}
			}
			break;
	case 1:
			if (DecRegs.DFLASHCTRL.bit.BUSY ==0)
				{
					write_data_flash_word((Uint32)DFLASH_pointer++,dflash_variables.dflash_array[i]);
					i++;
					if(i == NUMBER_OF_DFLASH_VARIABLES)
					{
						i=0;
						DFLASH_flashing_state=0;
						DFLASH_pointer=(Uint32 *)DFLASH_START_ADDRESS;
						return 1;
					}
				}
			break;
	}

return 0;
}
