#include "DS3231.h"

void DS3231_send_time(RTC_time_t *tm)
{
	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_BUSY) != RESET);
	I2C_TransferHandling(DS3231_I2C, DS3231_ADDR, 8, I2C_AutoEnd_Mode, I2C_Generate_Start_Write);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TXIS) == RESET);
	I2C_SendData(DS3231_I2C, 0x00);	//reset pointer

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TXIS) == RESET);
	I2C_SendData(DS3231_I2C, dec2bcd(tm->Second));

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TXIS) == RESET);
	I2C_SendData(DS3231_I2C, dec2bcd(tm->Minute));

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TXIS) == RESET);
	I2C_SendData(DS3231_I2C, dec2bcd(tm->Hour));

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TXIS) == RESET);
	I2C_SendData(DS3231_I2C, dec2bcd(tm->Wday));

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TXIS) == RESET);
	I2C_SendData(DS3231_I2C, dec2bcd(tm->Day));

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TXIS) == RESET);
	I2C_SendData(DS3231_I2C, dec2bcd(tm->Month));

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TXIS) == RESET);
	I2C_SendData(DS3231_I2C, dec2bcd(tm->Year));

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_STOPF) == RESET);
	I2C_ClearFlag(DS3231_I2C, I2C_ICR_STOPCF);
}

void DS3231_read_time(RTC_time_t *tm)
{
	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_BUSY) != RESET);
	I2C_TransferHandling(DS3231_I2C, DS3231_ADDR, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TXIS) == RESET);
	I2C_SendData(DS3231_I2C, 0x00);	//reset pointer

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TC) == RESET);
	I2C_TransferHandling(DS3231_I2C, DS3231_ADDR, 7, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_RXNE) == RESET);
	tm->Second = bcd2dec(I2C_ReceiveData(DS3231_I2C) /*& ~_BV(DS1307_CH)*/);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_RXNE) == RESET);
	tm->Minute = bcd2dec(I2C_ReceiveData(DS3231_I2C));

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_RXNE) == RESET);
	tm->Hour = bcd2dec(I2C_ReceiveData(DS3231_I2C) /*& ~_BV(HR1224)*/);    //assumes 24hr clock

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_RXNE) == RESET);
	tm->Wday = I2C_ReceiveData(DS3231_I2C);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_RXNE) == RESET);
	tm->Day = bcd2dec(I2C_ReceiveData(DS3231_I2C));

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_RXNE) == RESET);
	tm->Month = bcd2dec(I2C_ReceiveData(DS3231_I2C) /*& ~_BV(CENTURY)*/);  //don't use the Century bit

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_RXNE) == RESET);
	tm->Year = bcd2dec(I2C_ReceiveData(DS3231_I2C));

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_STOPF) == RESET);
	I2C_ClearFlag(DS3231_I2C, I2C_ICR_STOPCF);
}
// Convert Decimal to Binary Coded Decimal (BCD)
uint8_t dec2bcd(uint8_t num)
{
	return ((num/10 * 16) + (num % 10));
}

// Convert Binary Coded Decimal (BCD) to Decimal
uint8_t bcd2dec(uint8_t num)
{
	return ((num/16 * 10) + (num % 16));
}

void DS3232_squareWave(void)
{
	uint8_t controlReg;
	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_BUSY) != RESET);
	I2C_TransferHandling(DS3231_I2C, DS3231_ADDR, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TXIS) == RESET);
	I2C_SendData(DS3231_I2C, RTC_CONTROL);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TC) == RESET);
	I2C_TransferHandling(DS3231_I2C, DS3231_ADDR, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_RXNE) == RESET);
	controlReg=I2C_ReceiveData(DS3231_I2C);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_STOPF) == RESET);
	I2C_ClearFlag(DS3231_I2C, I2C_ICR_STOPCF);

	controlReg = 0b00000000;//(controlReg & 0xE3) | (00 << RS1);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_BUSY) != RESET);
	I2C_TransferHandling(DS3231_I2C, DS3231_ADDR, 2, I2C_AutoEnd_Mode, I2C_Generate_Start_Write);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TXIS) == RESET);
	I2C_SendData(DS3231_I2C, RTC_CONTROL);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_TXIS) == RESET);
	I2C_SendData(DS3231_I2C, controlReg);

	while(I2C_GetFlagStatus(DS3231_I2C, I2C_ISR_STOPF) == RESET);
	I2C_ClearFlag(DS3231_I2C, I2C_ICR_STOPCF);
}

void string_2_DateTime(uint8_t *buff, RTC_time_t *time)
{
	uint32_t i=0;
	uint8_t tmp_buff[3];
	tmp_buff[2]=0;
	for(i=0;i<2;i++)
	{
		tmp_buff[i]=*(buff+i);
	}
	time->Hour=atoi(tmp_buff);

	for(i=0;i<2;i++)
	{
		tmp_buff[i]=*(buff+i+2);
	}
	time->Minute=atoi(tmp_buff);

	for(i=0;i<2;i++)
	{
		tmp_buff[i]=*(buff+i+4);
	}
	time->Second=atoi(tmp_buff);

	for(i=0;i<2;i++)
	{
		tmp_buff[i]=*(buff+i+6);
	}
	time->Day=atoi(tmp_buff);

	for(i=0;i<2;i++)
	{
		tmp_buff[i]=*(buff+i+8);
	}
	time->Month=atoi(tmp_buff);

	for(i=0;i<2;i++)
	{
		tmp_buff[i]=*(buff+i+10);
	}
	time->Year=atoi(tmp_buff);

	tmp_buff[0]=*(buff+12);
	tmp_buff[1]=0;
	time->Wday=atoi(tmp_buff);
}

