//###########################################################################
//
// FILE:    variables.h
//
// 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.
//###########################################################################


#define SWITCHING_FREQUENCY (100000)


#define TURN_ON_TIME

#define DPWM_PERIOD  (4000000000/SWITCHING_FREQUENCY)


#define DEAD_TIME_NS (100)
#define DPWM_PHASE (DPWM_PERIOD/4)
#define DEAD_TIME_1 (1000)
#define DEAD_TIME_2 (1000)



#define ADC_VALUE_FOR_12V  (2170)
#define ADC_VALUE_FOR_14_8V  (2019)

#define Current_positive_setpoint	(45)			//MAX=47A
#define Current_negative_setpoint	(-40)			//MAX=47A

#define POSITIVE_THRESHOLD 		( (  (Current_positive_setpoint*0.0269+1.25)/2.5 )  * 127)	//ACOMP_B-AD03

#define NOMINAL_BATTERY_VOLTAGE   (36.0)
#define NOMINAL_SYSTEM_VOLTAGE    (38.0)


#define ALL_PHASE

#ifdef MAIN 					// If it is the main routine
	#define EXTERN	 			// If it isn't extern, so define it as nothing
#else 							// If it isn't the main routine
	#define EXTERN extern 		// If it is extern
#endif

typedef void (*FUNC_PTR)(); 	//used for zeroing instruction word.

#if (UCD3138128)

#ifdef MAIN
#pragma DATA_SECTION(program_area, ".ram_pgm_area")
Uint32 program_area[32]; 		// small RAM area for storing programs which modify program flashes
#else
extern Uint32 program_area[32];
#endif
#else
EXTERN Uint32 program_area[32];
#endif


EXTERN volatile Uint32 zoiw_address; //address to zero out integrity double word
EXTERN volatile Uint32 zoiw_flash_key; //flash key to zero out integrity double word - tells it which one to zero out.

EXTERN float PWM0_duty,PWM1_duty;

EXTERN Uint32 I;
EXTERN int32 I_CMD;
EXTERN Uint32 I_OUT_ZERO,I_OUT_ALMOST_ZERO;

typedef struct {
	volatile Uint32 system_voltage;
	volatile Uint32 battery_voltage;
	volatile Uint32 battery_current;         // Battery current or Discharge current
	volatile Uint32 system_current;			// System current or charge current
	volatile Uint32 Current_sum;
	volatile Uint32 temp_reading;
	volatile Uint32 system_voltage_avg_32;
	volatile Uint32 battery_voltage_avg_32;

} ADC_STRUCT;

EXTERN ADC_STRUCT adc_struct;
typedef enum {BOOST_BUCK,PS_OFF,CALIBRATING,ERROR} CONVERTER_STATE;
EXTERN CONVERTER_STATE converter_state;

typedef enum {BOOST_INITIALIZE,BOOST_CC,BOOST_CC_TO_CV,BOOST_CV,BUCK,BUCK_INITIALIZE,WAIT,BOOST_STABILIZE,BUCK_CHECK,BUCK_RECOVER,FALSE_TRIGGER_VALIDATE,POWER_BACK_CHECK} MINI_STATE;

EXTERN MINI_STATE mini_state,mini_state_during_error;

EXTERN int32 eadc_DAC_RAM;

EXTERN int32 MIN_TON;
EXTERN int32 D_T1;
EXTERN int32 D_T2;

EXTERN int32 D_T1_array[33];
EXTERN int32 D_T2_array[33];

typedef enum  {UNDER_BATTERY_VOLTAGE,OVER_BATTERY_VOLTAGE,UNDER_SYSTEM_SUPPLY_VOLTAGE,OVER_SYSTEM_SUPPLY_VOLTAGE,BATTERY_OVER_CURRENT,NO_BATTERY_CONNECTED,POOR_BATTERY_CONNECTION,
	OPAMP_LOCK_UP,FEED_BACK_ERROR,CALIBRATION_ERROR,NO_ERROR} ERROR__STATE;

typedef enum  {CAPTURE_SYSTEM_OVP,CAPTURE_BATTERY_OVP,CAPTURE_CC_to_CV,CAPTURE_BATTERY_NOM,CAPTURE_SYSTEM_NOM,CAPTURE_BATTERY_UVP,CAPTURE_SYSTEM_UVP,CAPTURE_DAC_CC,CAPTURE_BATTERY_OCP,CAPTURE_SYSTEM_OCP,
    CAPTURE_NOTHING} CAPTURE__STATE;
EXTERN ERROR__STATE error_state;
EXTERN int32 test_state;
EXTERN int32 error_value;

EXTERN int32 state_counter;


EXTERN Uint32 store;


struct node
{
	int data;
struct	node * next;
};

typedef struct
{
	int16 CCM_zero_current_adc_readings;
	int16 CCM_zero_current_EADC_ABS_reading;
	int16 CCM_zero_current_EADC_DAC_value;
	int16 DCM_1A_charging_ref;
	int16 DCM_1A_charging_adc_readings;
	int16 CCM_1A_charging_adc_readings;
	int16 CCM_1A_charging_EADC_DAC_value;
	float corresponding_input_voltage;

}CURRENT_READINGS;

EXTERN int current_reading_error[80];



typedef struct {
int32 buck_constant_deadtime;
int32 boost_constant_deadtime;
int32 ADC_Value_for_trigger_point_ON_PS_SIDE;
Uint32 ADC_Value_for_CCtoCV_switch_ON_BATT_SIDE;
CURRENT_READINGS current_readings[45];
int32 OVP_battery;
int32 OVP_system;
int32 UVP_battery;
int32 UVP_system;
int32 OCP_battery;
int32 OCP_system;
int32 ADC_Value_for_nominal_battery_voltage;
int32 ADC_Value_for_nominal_system_voltage;
int32 ADC_value_for_constant_current_charging;
int32 current_scalar;
int32 short_test;
int32 board_mode;
CAPTURE__STATE calibrate_state;
int32 IS_DFLASHED;
int32 integration_calibrated;
Uint32 Min_Vbus_dynamic;
Uint32 Min_Vbus_static;
Uint32 Max_Vbus_dynamic;
Uint32 Max_Vbus_static;
Uint32 Min_Vbus_EADC_DAC;
Uint32 Max_Vbus_EADC_DAC;
Uint32 EADC_DAC_value_for_constant_current;
int32 Vbus_error_diff;
int32 Vbus_EADC_error_threshold;
int32 Vbus_error_diff_margin;
}VAR;

EXTERN Uint32  NUMBER_OF_DFLASH_VARIABLES ;

typedef union  {
	VAR variables;
	Uint32 dflash_array[((sizeof(VAR))/4) +4];

}DFLASH_VARIABLES;

EXTERN DFLASH_VARIABLES dflash_variables;


EXTERN int32 Charge_Ratio,Discharge_Ratio;

EXTERN int32 IDE_V12,IDE_V14_8;


EXTERN Uint32 watch[10];


struct CHKSUMVARS
{
	Uint32 *ptr_main;		// pointer used to read contents of main program
	Uint32 *ptr_bkup;		// pointer used to read contents of backup program
	Uint64 main;			// variable used to store calculated checksum of main program
	Uint64 bkup;			// variable used to store calculated checksum of backup program
};

EXTERN struct CHKSUMVARS Z_chksum;
EXTERN Uint32 restored;

// lookup table for program flash keys
extern const Uint32 pflash_key_lut[4];

EXTERN int32 buck_ide_value;


EXTERN int32 local_state_temp;
EXTERN Uint32 interrupt_clearer;
EXTERN int32 reduce;
EXTERN Uint32 voltage_loop_DAC_value,reduced_voltage_loop_DAC_value;
EXTERN int32 observe_1,observe_2,observe_3;
EXTERN volatile float battery_voltage;
EXTERN volatile float system_voltage;
EXTERN volatile float Inductor_temperature;
EXTERN Uint32 DAC_value;
EXTERN int32 count;
EXTERN int filter_duty_average,p_filter_duty_average;
EXTERN int32 wait_done;
EXTERN int32 Iout_prev_1,Iout_prev_2;
EXTERN int32 power_back_possible_counter;
EXTERN int32 Iout1_ADC_Zero,Iout2_ADC_Zero;
EXTERN int32 reverse_current_sense;
EXTERN int32 current_ref_value;
EXTERN int32 index ;
EXTERN Uint32 battery_reading_before_mode_switch,battery_reading_sampled;
EXTERN int32 battery_reading_sampled_array[10],sum_of_samples_1,sum_of_samples_2;
EXTERN int32 wait_for_steady_state_machine;
EXTERN int32 Min_Vbus_dynamic,Min_Vbus_static,Max_Vbus_dynamic,Max_Vbus_static;
EXTERN Uint32 Vbus_probe_1,Vbus_probe_2;
EXTERN int32 Sensitive;



EXTERN Uint32 dummy;







