//###########################################################################
//
// FILE:    init_filter.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"

void init_filter(void)
{


			Filter0Regs.FILTERKPCOEF0.bit.KP_COEF_0 = 1000;

			Filter0Regs.FILTERKICOEF0.bit.KI_COEF_0 = 10;

			Filter0Regs.FILTERKDCOEF0.bit.KD_COEF_0 = 1;

			Filter0Regs.FILTERKDALPHA.bit.KD_ALPHA_0 = -1;

			Filter0Regs.FILTERKICLPHI.bit.KI_CLAMP_HIGH = 0x7FFFFF;
			Filter0Regs.FILTERKICLPLO.bit.KI_CLAMP_LOW = 0;

			Filter0Regs.FILTEROCLPHI.bit.OUTPUT_CLAMP_HIGH = DPWM_PERIOD * 0.95;// for 45 v on battery side
			Filter0Regs.FILTEROCLPLO.bit.OUTPUT_CLAMP_LOW = 0;// DPWM_PERIOD * 0.6821; // for 32.4 v on battery side.

			Filter0Regs.FILTERCTRL.bit.FILTER_EN = 1;

			LoopMuxRegs.FILTERKCOMPA.bit.KCOMP0 = DPWM_PERIOD;
			Filter0Regs.FILTERCTRL.bit.OUTPUT_MULT_SEL = 1;




			Filter1Regs.COEFCONFIG.bit.BIN0_CONFIG = 0; //coefficient set A
			Filter1Regs.COEFCONFIG.bit.BIN1_CONFIG = 1; //coefficient set B
			Filter1Regs.COEFCONFIG.bit.BIN2_CONFIG = 2; //coefficient set C
			Filter1Regs.COEFCONFIG.bit.BIN3_CONFIG = 2; //coefficient set D
			Filter1Regs.COEFCONFIG.bit.BIN4_CONFIG = 2; //coefficient set E
			Filter1Regs.COEFCONFIG.bit.BIN5_CONFIG = 2; //coefficient set F
			Filter1Regs.COEFCONFIG.bit.BIN6_CONFIG = 2; //coefficient set G

			Filter1Regs.FILTERCTRL.bit.NL_MODE =1;

			Filter1Regs.FILTERNL2.bit.LIMIT5 = 218; //symmetric values
			Filter1Regs.FILTERNL2.bit.LIMIT4 = 182;
			Filter1Regs.FILTERNL1.bit.LIMIT3 = 145;
			Filter1Regs.FILTERNL1.bit.LIMIT2 = 109;
			Filter1Regs.FILTERNL0.bit.LIMIT1 = 72;
			Filter1Regs.FILTERNL0.bit.LIMIT0 = 30;

			Filter1Regs.FILTERKPCOEF0.bit.KP_COEF_0 =1000;
			Filter1Regs.FILTERKPCOEF0.bit.KP_COEF_1 =10000;
			Filter1Regs.FILTERKPCOEF1.bit.KP_COEF_2 =20000;

			Filter1Regs.FILTERKICOEF0.bit.KI_COEF_0 = 10;
			Filter1Regs.FILTERKICOEF0.bit.KI_COEF_1 = 200;
			Filter1Regs.FILTERKICOEF1.bit.KI_COEF_2= 200;
			Filter1Regs.FILTERKICOEF1.bit.KI_COEF_3= 200;


			Filter1Regs.FILTERKDCOEF0.bit.KD_COEF_0 =10;
			Filter1Regs.FILTERKDCOEF0.bit.KD_COEF_1 =20;
			Filter1Regs.FILTERKDCOEF1.bit.KD_COEF_2 =20;

			Filter1Regs.FILTERKDALPHA.bit.KD_ALPHA_0 = -1;

			Filter1Regs.FILTERKICLPHI.bit.KI_CLAMP_HIGH = 0x7FFFFF;
			Filter1Regs.FILTERKICLPLO.bit.KI_CLAMP_LOW = 0;  // 2B, 3B not to be turn on at the same time

			Filter1Regs.FILTEROCLPHI.bit.OUTPUT_CLAMP_HIGH = 7000;
			Filter1Regs.FILTEROCLPLO.bit.OUTPUT_CLAMP_LOW = 0;

			Filter1Regs.FILTERCTRL.bit.FILTER_EN = 1;

			LoopMuxRegs.FILTERKCOMPA.bit.KCOMP0 = DPWM_PERIOD;
			Filter1Regs.FILTERCTRL.bit.OUTPUT_MULT_SEL = 1;



			Filter2Regs.FILTERKPCOEF0.bit.KP_COEF_0 =10000;

			Filter2Regs.FILTERKICOEF0.bit.KI_COEF_0 = 10;

			Filter2Regs.FILTERKDCOEF0.bit.KD_COEF_0 =10;

			Filter2Regs.FILTERKDALPHA.bit.KD_ALPHA_0 = -1;

			Filter2Regs.FILTERKICLPHI.bit.KI_CLAMP_HIGH = 0x7FFFFF;
			Filter2Regs.FILTERKICLPLO.bit.KI_CLAMP_LOW = 0;

			Filter2Regs.FILTEROCLPHI.bit.OUTPUT_CLAMP_HIGH = 600;//5500;
			Filter2Regs.FILTEROCLPLO.bit.OUTPUT_CLAMP_LOW = 0;

			Filter2Regs.FILTERCTRL.bit.FILTER_EN = 1;

			LoopMuxRegs.FILTERKCOMPA.bit.KCOMP0 = DPWM_PERIOD;
			Filter2Regs.FILTERCTRL.bit.OUTPUT_MULT_SEL = 1;




}
