/*
 * sm72442.h
 *
 * Created: 21.4.2020 11:35:39
 *  Author: Martin Košút
 */ 


#ifndef SM72442_H_
#define SM72442_H_

#define MPPT_address	0x01	//address of MPPT SM72442(datasheet has swapped I2C0 and I2C2 columns in table)



#define V_REF		5			//reference voltage

#define R_IOUT		47000		//resistor values in schematic (used to calculate true input and output parameters)
#define R_IIN		47000
#define R_SIB		499
#define R_SIA		499
#define R_SH		0.007
#define R_FB1		12700
#define R_FB2		4220
#define R_IN1		31200
#define R_IN2		10000
#define R_OUT1		27000
#define R_OUT2		10000
#define RT1			31200
#define RT2			2000
#define RT3			20000
#define RT4			20000
#define RB1			6490
#define RB2			1000000
#define RB3			220000
#define RB4			330000

typedef enum MPPT_reg
{
	MPPT_reg0 = 0xE0,		//ADC channels info
	MPPT_reg1 = 0xE1,		//input and output voltage and current values
	MPPT_reg3 =	0xE3,		//configuration parameters
	MPPT_reg4 =	0xE4,		//current and voltage offsets
	MPPT_reg5 = 0xE5		//current threshold to start in MPPT 
}T_MPPT_reg;

typedef struct MPPT_data
{
	float V_in;
	float V_out;
	float I_in;
	float I_out;
	float ADC0_V_out_max;
	float ADC2_Operating_mode;
	float ADC4_I_out_max;
	float ADC6_Slew_rate;
	
	bool Override_active;
	float Override_V_out_max;
	float Override_I_out_max;
	
	float set_V_out_max;
	float set_I_out_max;
	uint8_t dead_time_ON;
	uint8_t dead_time_OFF;
	
}T_MPPT_data;

T_MPPT_data MPPT;

uint8_t MPPT_read_register(T_MPPT_reg register);
void parse_data(uint8_t MPPTreg);
uint8_t MPPT_set_parameters(float set_V_out, float set_I_out, uint8_t dead_time_ON, uint8_t dead_time_OFF, bool override);

#endif /* SM72442_H_ */