/*
 * TLC3335.c
 *
 *  Created on: 8. 8. 2016
 *      Author: Beta
 */

#include "TLC3335.h"

void LTC3335_DefaultConfig(LTC3335_Config* Config)
{
	Config->OutVoltage = OUT_3V3;
	Config->LTC3332_Enable = false;
	Config->Prescaler = 0x0F;
	Config->AlarmThreshold = &(Config->LTC3335_Registers[LTC3335_REG_B][LTC3335_DATA]);
	Config->AccumulatedCharge = &(Config->LTC3335_Registers[LTC3335_REG_C][LTC3335_DATA]);
	Config->Alarms = &(Config->LTC3335_Registers[LTC3335_REG_D][LTC3335_DATA]);
	Config->InterruptRegister = LTC3335_CLEAR_INTERRUPT;

	Config->VoltageSource = InternalVoltage;

	Config->i2cConfig.IIC_instance = 0x00;
	Config->i2cConfig.IIC_LTC3335_Addres = LTC3335_IIC_ADDRESS;
	Config->i2cConfig.i2c_device.baudRate_kbps = 300;
	Config->i2cConfig.i2c_device.address = LTC3335_IIC_ADDRESS;
	Config->i2cConfig.i2c_status = kStatus_I2C_Success;

	Config->LTC3335_Registers[LTC3335_REG_A][LTC3335_COMMAND] = LTC3335_REG_A;
	Config->LTC3335_Registers[LTC3335_REG_A][LTC3335_DATA] = (Config->OutVoltage | Config->Prescaler);

	Config->LTC3335_Registers[LTC3335_REG_B][LTC3335_COMMAND] = LTC3335_REG_B;
	Config->LTC3335_Registers[LTC3335_REG_B][LTC3335_DATA] = 0xFE;

	Config->LTC3335_Registers[LTC3335_REG_C][LTC3335_COMMAND] = LTC3335_REG_C;
	Config->LTC3335_Registers[LTC3335_REG_C][LTC3335_DATA] = 0;

	Config->LTC3335_Registers[LTC3335_REG_D][LTC3335_COMMAND] = LTC3335_REG_D;
	Config->LTC3335_Registers[LTC3335_REG_D][LTC3335_DATA] = 0x00;

	Config->LTC3335_Registers[LTC3335_REG_E][LTC3335_COMMAND] = LTC3335_REG_E;
	Config->LTC3335_Registers[LTC3335_REG_E][LTC3335_DATA] = Config->InterruptRegister;

	return;
}

i2c_status_t LTC3335_Init(LTC3335_Config* Config)
{
	for(uint8_t i = LTC3335_REG_A; i <= LTC3335_REG_E; i++)
	{
		if(i == LTC3335_REG_D)
		{
			continue;
		}

		Config->i2cConfig.i2c_status = 	I2C_DRV_MasterSendDataBlocking(Config->i2cConfig.IIC_instance,
				&Config->i2cConfig.i2c_device,
				&Config->LTC3335_Registers[i][LTC3335_COMMAND],
				1,
				&Config->LTC3335_Registers[i][LTC3335_DATA],
				1,
				100);
	}

	return Config->i2cConfig.i2c_status;
}

i2c_status_t LTC3335_Enable(LTC3335_Config* Config, bool enable)
{
	Config->i2cConfig.i2c_status = LTC3335_SetCounterTest(Config);
	Config->LTC3332_Enable = enable;
	GPIO_DRV_WritePinOutput(LTC3335_EN, enable);

	return Config->i2cConfig.i2c_status;
}


i2c_status_t LTC3335_SelectVoltageSource(LTC3335_Config* Config, LTC3335_VoltageSource VoltageSource)
{
	Config->VoltageSource = VoltageSource;
	Config->LTC3335_Registers[LTC3335_REG_A][LTC3335_DATA] &= 0x7F;
	Config->LTC3335_Registers[LTC3335_REG_A][LTC3335_DATA] |= VoltageSource;

	Config->i2cConfig.i2c_status = I2C_DRV_MasterSendDataBlocking(Config->i2cConfig.IIC_instance,
					&Config->i2cConfig.i2c_device,
					&Config->LTC3335_Registers[LTC3335_REG_A][LTC3335_COMMAND],
					1,
					&Config->LTC3335_Registers[LTC3335_REG_A][LTC3335_DATA],
					1,
					100);
	return Config->i2cConfig.i2c_status;
}

i2c_status_t LTC3335_SetOutVoltage(LTC3335_Config* Config, LTC3335_OutVoltage Voltage)
{
	Config->OutVoltage = Voltage;
	Config->LTC3335_Registers[LTC3335_REG_A][LTC3335_DATA] = (Config->OutVoltage | Config->Prescaler);

	Config->i2cConfig.i2c_status = I2C_DRV_MasterSendDataBlocking(Config->i2cConfig.IIC_instance,
					&Config->i2cConfig.i2c_device,
					&Config->LTC3335_Registers[LTC3335_REG_A][LTC3335_COMMAND],
					1,
					&Config->LTC3335_Registers[LTC3335_REG_A][LTC3335_DATA],
					1,
					100);
	return Config->i2cConfig.i2c_status;
}

i2c_status_t LTC3335_SetPrescaler(LTC3335_Config* Config, uint8_t Prescaler)
{
	Config->Prescaler = (Prescaler & 0x0F);
	Config->LTC3335_Registers[LTC3335_REG_A][LTC3335_DATA] = (Config->OutVoltage | Config->Prescaler);

	Config->i2cConfig.i2c_status = I2C_DRV_MasterSendDataBlocking(Config->i2cConfig.IIC_instance,
					&Config->i2cConfig.i2c_device,
					&Config->LTC3335_Registers[LTC3335_REG_A][LTC3335_COMMAND],
					1,
					&Config->LTC3335_Registers[LTC3335_REG_A][LTC3335_DATA],
					1,
					100);
	return Config->i2cConfig.i2c_status;
}

i2c_status_t LTC3335_SetAccumCharge(LTC3335_Config* Config, uint8_t charge)
{
	Config->LTC3335_Registers[LTC3335_REG_C][LTC3335_DATA] = charge;

	Config->i2cConfig.i2c_status = I2C_DRV_MasterSendDataBlocking(Config->i2cConfig.IIC_instance,
					&Config->i2cConfig.i2c_device,
					&Config->LTC3335_Registers[LTC3335_REG_C][LTC3335_COMMAND],
					1,
					&Config->LTC3335_Registers[LTC3335_REG_C][LTC3335_DATA],
					1,
					100);
	return Config->i2cConfig.i2c_status;
}

i2c_status_t LTC3335_GetAccumCharge(LTC3335_Config* Config)
{
	Config->i2cConfig.i2c_status = I2C_DRV_MasterReceiveDataBlocking(Config->i2cConfig.IIC_instance,
			&Config->i2cConfig.i2c_device,
			&Config->LTC3335_Registers[LTC3335_REG_C][LTC3335_COMMAND],
			1,
			&Config->LTC3335_Registers[LTC3335_REG_C][LTC3335_DATA],
			1,
			100);

	return Config->i2cConfig.i2c_status;
}



i2c_status_t LTC3335_GetAlarms(LTC3335_Config* Config)
{
	Config->i2cConfig.i2c_status = I2C_DRV_MasterReceiveDataBlocking(Config->i2cConfig.IIC_instance,
			&Config->i2cConfig.i2c_device,
			&Config->LTC3335_Registers[LTC3335_REG_C][LTC3335_COMMAND],
			1,
			&Config->LTC3335_Registers[LTC3335_REG_C][LTC3335_DATA],
			1,
			100);

	return Config->i2cConfig.i2c_status;
}

i2c_status_t LTC3335_ClearInterrupt(LTC3335_Config* Config)
{
	Config->InterruptRegister |= LTC3335_CLEAR_INTERRUPT;
	Config->LTC3335_Registers[LTC3335_REG_E][LTC3335_DATA] = Config->InterruptRegister;
	Config->i2cConfig.i2c_status = I2C_DRV_MasterSendDataBlocking(Config->i2cConfig.IIC_instance,
			&Config->i2cConfig.i2c_device,
			&Config->LTC3335_Registers[LTC3335_REG_E][LTC3335_COMMAND],
			1,
			&Config->LTC3335_Registers[LTC3335_REG_E][LTC3335_DATA],
			1,
			100);
	Config->InterruptRegister &= ~LTC3335_CLEAR_INTERRUPT;

	return Config->i2cConfig.i2c_status;
}

i2c_status_t LTC3335_SetCounterTest(LTC3335_Config* Config)
{
	Config->LTC3335_Registers[LTC3335_REG_E][LTC3335_DATA] |= LTC3335_COUNTER_TEST;
	Config->i2cConfig.i2c_status = I2C_DRV_MasterSendDataBlocking(Config->i2cConfig.IIC_instance,
					&Config->i2cConfig.i2c_device,
					&Config->LTC3335_Registers[LTC3335_REG_E][LTC3335_COMMAND],
					1,
					&Config->LTC3335_Registers[LTC3335_REG_E][LTC3335_DATA],
					1,
					100);
	return Config->i2cConfig.i2c_status;
}

i2c_status_t LTC3335_StopCounterTest(LTC3335_Config* Config)
{
	Config->LTC3335_Registers[LTC3335_REG_E][LTC3335_DATA] &= ~LTC3335_COUNTER_TEST;
	Config->i2cConfig.i2c_status = I2C_DRV_MasterSendDataBlocking(Config->i2cConfig.IIC_instance,
					&Config->i2cConfig.i2c_device,
					&Config->LTC3335_Registers[LTC3335_REG_E][LTC3335_COMMAND],
					1,
					&Config->LTC3335_Registers[LTC3335_REG_E][LTC3335_DATA],
					1,
					100);
	return Config->i2cConfig.i2c_status;
}

void LTC3335_GetInVoltage(LTC3335_Config* Config)
{
	Config->InVoltage = (uint16_t) (ADC16_DRV_GetConvValueRAW(adConv1_IDX, 0U) * (3300.0/4096.0));
}






