/*
 * DS3231_RealTime.c
 *
 *  Created on: Jan 27, 2017
 *      Author: User
 */

#include "DS3231_RealTime.h"

void DS3231_GetDefaultConfig(DS3231_realTime* config)
{
	config->i2cConfig.IIC_instance = 0x00;
	config->i2cConfig.i2c_device.baudRate_kbps = 300;
	config->i2cConfig.i2c_device.address = DS3231_I2C_ADDRESS;
	config->i2cConfig.i2c_status = kStatus_I2C_Success;

}

i2c_status_t DS3231_ReadAll(DS3231_realTime* config)
{
	uint8_t cmd = DS3231_ADRRESS_SECOND;

	config->i2cConfig.i2c_status = I2C_DRV_MasterReceiveDataBlocking(config->i2cConfig.IIC_instance,
				&config->i2cConfig.i2c_device,
				&cmd,
				1,
				config->buffer,
				DS3231_SIZE_OF_ALL_DATA,
				I2C_TIMEOUT);


	if(config->i2cConfig.i2c_status != kStatus_I2C_Success)
	{
		return kStatus_I2C_Fail;
	}

	config->seconds = (config->buffer[DS3231_ADRRESS_SECOND] & DS3231_MASK_SECOUNDS_1) +
			((config->buffer[DS3231_ADRRESS_SECOND] & DS3231_MASK_SECOUNDS_10) >> 4) * 10;

	config->minutes = (config->buffer[DS3231_ADRRESS_MINUTES] & DS3231_MASK_MINUTES_1) +
			((config->buffer[DS3231_ADRRESS_MINUTES] & DS3231_MASK_MINUTES_10) >> 4) * 10;

	config->hours = (config->buffer[DS3231_ADRRESS_HOURS] & DS3231_MASK_HOUR_1) +
			((config->buffer[DS3231_ADRRESS_HOURS] & DS3231_MASK_HOUR_10_20) >> 4) * 10;

	config->date = (config->buffer[DS3231_ADRRESS_DATE] & DS3231_MASK_DATE_1) +
			((config->buffer[DS3231_ADRRESS_DATE] & DS3231_MASK_DATE_10) >> 4) * 10;

	config->month = (config->buffer[DS3231_ADRRESS_MONTH] & DS3231_MASK_MONTH_1) +
			((config->buffer[DS3231_ADRRESS_MONTH] & DS3231_MASK_MONTH_10) >> 4) * 10;

	config->year = (config->buffer[DS3231_ADRRESS_YEAR] & DS3231_MASK_YEAR_1) +
			((config->buffer[DS3231_ADRRESS_YEAR] & DS3231_MASK_YEAR_10) >> 4) * 10;

	return kStatus_I2C_Success;
}

i2c_status_t DS3231_WriteAll(DS3231_realTime* config)
{
	uint8_t cmd = DS3231_ADRRESS_SECOND;
	uint8_t temp;

	temp = config->seconds / 10;
	config->buffer[DS3231_ADRRESS_SECOND] = (temp << 4 ) | (config->seconds % 10);

	temp = config->minutes / 10;
	config->buffer[DS3231_ADRRESS_MINUTES] = (temp << 4 ) | (config->minutes % 10);

	temp = config->hours / 10;
	config->buffer[DS3231_ADRRESS_HOURS] = (temp << 4 ) | (config->hours % 10);

	temp = config->date / 10;
	config->buffer[DS3231_ADRRESS_DATE] = (temp << 4 ) | (config->date % 10);

	temp = config->month / 10;
	config->buffer[DS3231_ADRRESS_MONTH] = (temp << 4 ) | (config->month % 10);

	temp = config->year / 10;
	config->buffer[DS3231_ADRRESS_YEAR] = (temp << 4 ) | (config->year % 10);

	config->i2cConfig.i2c_status = I2C_DRV_MasterSendDataBlocking(config->i2cConfig.IIC_instance,
				&config->i2cConfig.i2c_device,
				&cmd,
				1,
				config->buffer,
				DS3231_SIZE_OF_ALL_DATA,
				I2C_TIMEOUT);


	if(config->i2cConfig.i2c_status != kStatus_I2C_Success)
	{
		return kStatus_I2C_Fail;
	}

	return kStatus_I2C_Success;
}

i2c_status_t DS3231_WriteTime(DS3231_realTime* config)
{
	uint8_t cmd = DS3231_ADRRESS_SECOND;

	config->buffer[DS3231_ADRRESS_SECOND] = ((config->seconds / 10) << 4 ) | (config->seconds % 10);
	config->buffer[DS3231_ADRRESS_MINUTES] = ((config->minutes / 10) << 4 ) | (config->minutes % 10);
	config->buffer[DS3231_ADRRESS_HOURS] = ((config->hours / 10) << 4 ) | (config->hours % 10);

	config->i2cConfig.i2c_status = I2C_DRV_MasterSendDataBlocking(config->i2cConfig.IIC_instance,
				&config->i2cConfig.i2c_device,
				&cmd,
				1,
				config->buffer,
				DS3231_SIZE_OF_TIME,
				I2C_TIMEOUT);

	if(config->i2cConfig.i2c_status != kStatus_I2C_Success)
	{
		return kStatus_I2C_Fail;
	}

	return kStatus_I2C_Success;
}

i2c_status_t DS3231_WriteDate(DS3231_realTime* config)
{
	uint8_t cmd = DS3231_ADRRESS_DATE;

	config->buffer[DS3231_ADRRESS_DATE] = ((config->date / 10) << 4 ) | (config->date % 10);
	config->buffer[DS3231_ADRRESS_MONTH] = ((config->month / 10) << 4 ) | (config->month % 10);
	config->buffer[DS3231_ADRRESS_YEAR] = ((config->year / 10) << 4 ) | (config->year % 10);

	config->i2cConfig.i2c_status = I2C_DRV_MasterSendDataBlocking(config->i2cConfig.IIC_instance,
				&config->i2cConfig.i2c_device,
				&cmd,
				1,
				config->buffer,
				DS3231_SIZE_OF_DATE,
				I2C_TIMEOUT);

	if(config->i2cConfig.i2c_status != kStatus_I2C_Success)
	{
		return kStatus_I2C_Fail;
	}

	return kStatus_I2C_Success;
}
