/*
 * conf_brd.h
 *
 * Created: 22.2.2016 22:50:12
 *  Author: Chenda
 */ 


#ifndef CONF_BRD_H_
#define CONF_BRD_H_
#include "../lib_mcu/ADC/ADC_drv.h"
/************************************************************************/
/* General constant definitions                                         */
/************************************************************************/
#define F_CPU						16000000UL

#define TIMER_PRESCALER				64

#define UP_VCC						5.58//4.85
#define AVCC_VOLTAGE				UP_VCC

// Power supply max parameters
#define PS_INST_PAR					"DC:0.9-40V;0.1-5A"	//"DC:0.5-35V;0.1-10A"
#define PS_MIN_VOLTAGE				0.9
#define PS_MIN_CURRENT				0.1
#define PS_MAX_VOLTAGE				40.0
#define PS_MAX_CURRENT				5.2//10.0
#define PS_TERMINAL_MEASURING		true
#define PS_OUTPUT_SWITCH_PRESENT	true

// ADC computing data
#define ADC_RESOLUTION				1024.0
#define ADC_BIT_VOLTAGE				AVCC_VOLTAGE/ADC_RESOLUTION			// zkontrolovat jestli vypocet nezaokrouhluje na cela cisla
//#define SNS_LINEARITY_KOEF			0.00906
#define SNS_CURR_RESISTOR			0.02
#define SNS_CURRENT_GAIN			23.9		// will be higher than 1
#define SNS_TERM_VOLTAGE_GAIN		0.12265		// will be lower than 1
#define SNS_POST_REG_VOLTAGE_GAIN	0.12344		// will be lower than 1
#define SNS_PRE_REG_VOLTAGE_GAIN	0.09039		// will be lower than 1
#define AVE_FLOAT_WINDOW_LEN		10

// regulator timer settings
#define TIMER4_PERIOD				8000		// 1/16M * 1 * 8000 = 0.0005 => 2kHz
#define TIMER4_PROTECTION_VALUE		20			// interval in front of TEMER4_PERIOD, protecting for delayed interrupt execution - it defines the maximum value to 

// trafo winding
#define TRAFO_SWITCH_HIGH_VOLTAGE	22.0
#define TRAFO_SWITCH_LOW_VOLTAGE	17.0
//#define TRAFO_SWITCH_HIGH_VOLTAGE	7.0
//#define TRAFO_SWITCH_LOW_VOLTAGE	4.0
#define TRAFO_LOW_WINDING_VOLTAGE	25.0
#define TRAFO_HIGH_WINDING_VOLTAGE	44.0

// regulation mode decision
#define VOLTAGE_TOL					0.05		// 50mV
#define CURRENT_TOL					0.05		// 50mA
#define REG_U_VAL_RANGE				150			// regulation value tolerance
#define REG_I_VAL_RANGE				300			// regulation value tolerance

//#define VOLT_REG_ADC_BIT_PREC		1

// Stabilization gaps
#define STAB_GAP_TRAFO_SW_HIGH		20
#define STAB_GAP_TRAFO_SW_LOW		20
#define STAB_GAP_SUSPEND_MODE		20
#define STAB_GAP_REGULATION_MODE	20
#define STAB_GAP_VOLTAGE_PREC_MODE	40
#define STAB_GAP_CURRENT_PREC_MODE	20

/************************************************************************/
/* Macros and definitions                                               */
/************************************************************************/

// UART0 bus RS-485 direction, connect to both DE & /RE
#define mU0BusRs485Dir_Enab()		DDRC|=(1<<PC5)
#define mU1BusRs485Dir_Out()		PORTC&=~(1<<PC5)	// logic "0"
#define mU0BusRs485Dir_In()			PORTC|=(1<<PC5)		// logic "1"

// Status LED
#define mStatusLed_Enab()			DDRC|=(1<<PC4)
#define mStatusLed_Off()			PORTC|=(1<<PC4)
#define mStatusLed_On()				PORTC&=~(1<<PC4)
#define mStatusLed_Toggle()			PORTC^=(1<<PC4)

// compensation
#define mCompensation1_Enab()		DDRD|=(1<<PD6)
#define mCompensation1_On()			PORTD&=~(1<<PD6)
#define mCompensation1_Off()		PORTD|=(1<<PD6)
#define mCompensation2_Enab()		DDRD|=(1<<PD7)
#define mCompensation2_On()			PORTD&=~(1<<PD7)
#define mCompensation2_Off()		PORTD|=(1<<PD7)
#define mCompensation3_Enab()		DDRB|=(1<<PB0)
#define mCompensation3_On()			PORTB&=~(1<<PB0)
#define mCompensation3_Off()		PORTB|=(1<<PB0)
#define mCompensation4_Enab()		DDRB|=(1<<PB1)
#define mCompensation4_On()			PORTB&=~(1<<PB1)
#define mCompensation4_Off()		PORTB|=(1<<PB1)

// regulator control PWM output
#define mUReg_Enab()				PORTE&=~(1<<PE1);DDRE|=(1<<PE1)
#define mUReg_Clr()					PORTE&=~(1<<PE1)
#define mUReg_Set()					PORTE|=(1<<PE1)
#define mIReg_Enab()				PORTE&=~(1<<PE3);DDRE|=(1<<PE3)
#define mIReg_Clr()					PORTE&=~(1<<PE3)
#define mIReg_Set()					PORTE|=(1<<PE3)

// True switch driving
#define mVoltRegInDrv_Enab()		DDRE|=(1<<PE2)
#define mVoltRegInDrv_20V()			PORTE|=(1<<PE2)
#define mVoltRegInDrv_40V()			PORTE&=~(1<<PE2)
#define mIsVoltRegInDrv_20V()		PINE&(1<<PE2)
#define mVoltOutDrv_Enab()			DDRE|=(1<<PE0)
#define mVoltOutDrv_Off()			PORTE|=(1<<PE0)
#define mVoltOutDrv_On()			PORTE&=~(1<<PE0)
#define mIsVoltOutDrv_Off()			PINE&(1<<PE0)

// ADC Measurement
#define SNS_CURRENT					ADC_CH_3
#define SNS_TERM_VOLTAGE			ADC_CH_2
#define SNS_POST_REG_VOLTAGE		ADC_CH_1
#define SNS_PRE_REG_VOLTAGE			ADC_CH_0

// Device address selector
#define mGetDevAddress()			((PIND&(0b1111<<PD2))>>2)
#define mDevAddCfg()				DDRD&=~((1<<PD2)|(1<<PD3)|(1<<PD4)|(1<<PD5));PORTD|=(1<<PD2)|(1<<PD3)|(1<<PD4)|(1<<PD5)
#define mGetDevAdd_2_2()			(PIND&(1<<PD2))
#define mGetDevAdd_2_1()			(PIND&(1<<PD3))
#define mGetDevAdd_2_3()			(PIND&(1<<PD4))
#define mGetDevAdd_2_0()			(PIND&(1<<PD5))

#define mAdc0PullUp_Dis()			PORTC&=~(1<<PC0)
#define mAdc1PullUp_Dis()			PORTC&=~(1<<PC1)
#define mAdc2PullUp_Dis()			PORTC&=~(1<<PC2)
#define mAdc3PullUp_Dis()			PORTC&=~(1<<PC3)

#define mAdc3PullUp_Enab()			PORTC|=(1<<PC3)

#endif /* CONF_BRD_H_ */