#ifndef __LTC3335_H__
#define	__LTC3335_H__

#include "Cpu.h"
#include "Events.h"
#include "clockMan1.h"
#include "pin_init.h"
#include "osa1.h"
#include "i2cCom1.h"
#include "DbgCs1.h"

/*********************************
 * registers
 **********************************/

#define LTC3335_REG_A	0x01	// [W] Vout and prescaler selection
#define LTC3335_REG_B	0x02	// [W] Alarm threshold
#define LTC3335_REG_C	0x03	// [R/W]Accumulated charge
#define LTC3335_REG_D	0x04	// [R] Alarms
#define LTC3335_REG_E	0x05	// [W] Interrupt register

#define LTC3335_COMMAND	0x00
#define LTC3335_DATA	0x01

#define LTC3335_EN_IDX	1u

#define LTC3335_CLEAR_INTERRUPT 0x01
#define LTC3335_COUNTER_TEST	0x02
#define LTC3335_PRESCALER_MAX	0x0F

#define LTC3335_IIC_ADDRESS	0x64

typedef enum LTC3335_OutVoltage
{
	OUT_1V8	= 0x80,
	OUT_2V5	= 0x90,
	OUT_2V8	= 0xA0,
	OUT_3V0	= 0xB0,
	OUT_3V3	= 0xC0,
	OUT_3V6	= 0xD0,
	OUT_4V5	= 0xE0,
	OUT_5V0	= 0xF0,
} LTC3335_OutVoltage;

typedef enum LTC3335_VoltageSource
{
	InternalVoltage = 0x80,
	ExternalVoltage = 0x00,

} LTC3335_VoltageSource;

typedef struct LTC3335_IIC
{
	i2c_device_t i2c_device;
	i2c_status_t i2c_status;

	uint32_t IIC_instance;
	uint8_t IIC_LTC3335_Addres;

}  LTC3335_IIC;

typedef struct LTC3335_Config
{
	LTC3335_OutVoltage OutVoltage;
	uint16_t InVoltage;
	bool LTC3332_Enable;
	uint8_t Prescaler;
	uint8_t *AlarmThreshold;
	uint8_t *AccumulatedCharge;
	uint8_t *Alarms;
	uint8_t InterruptRegister;

	LTC3335_VoltageSource VoltageSource;

	LTC3335_IIC i2cConfig;

	uint8_t LTC3335_Registers[6][2];

} LTC3335_Config;

// set default config
void LTC3335_DefaultConfig(LTC3335_Config* Config);

// initialize coulomb meter via IIC
i2c_status_t LTC3335_Init(LTC3335_Config* Config);

// turn on/off buck boost converter
i2c_status_t LTC3335_Enable(LTC3335_Config* Config, bool enable);

// select voltage source, internal (via IIC) or external (jumpers)
i2c_status_t LTC3335_SelectVoltageSource(LTC3335_Config* Config, LTC3335_VoltageSource VoltageSource);

// select output voltage {1V8, 2V5, 2V8, 3V0, 3V3, 3V6, 4V5, 5V0}
i2c_status_t LTC3335_SetOutVoltage(LTC3335_Config* Config, LTC3335_OutVoltage Voltage);

// select prescaler M factor, for smallest battery M = 15, for largest batery M = 0, range <0,15>
i2c_status_t LTC3335_SetPrescaler(LTC3335_Config* Config, uint8_t Prescaler);

// set actual accumulated charge
i2c_status_t LTC3335_SetAccumCharge(LTC3335_Config* Config, uint8_t charge);

// get actual accumulated charge
i2c_status_t LTC3335_GetAccumCharge(LTC3335_Config* Config);

// get alarms
i2c_status_t LTC3335_GetAlarms(LTC3335_Config* Config);

// clear interrupt
i2c_status_t LTC3335_ClearInterrupt(LTC3335_Config* Config);

// set counter test via /IRQ
i2c_status_t LTC3335_SetCounterTest(LTC3335_Config* Config);

// stop counter test via /IRQ
i2c_status_t LTC3335_StopCounterTest(LTC3335_Config* Config);

// get voltage
void LTC3335_GetInVoltage(LTC3335_Config* Config);

#endif
