/*
 * RTC.c
 *
 *  Created on: 15 janv. 2019
 *      Author: e_rcluze
 */

#include "RTC.h"
#include "RTC_ADD.h"
#include "i2c.h"

void readRTC(uint8_t add, uint8_t *tab, int len){
	uint8_t ad[1];
	ad[0]=add;
	HAL_StatusTypeDef res;
	res = HAL_I2C_Master_Transmit(&hi2c3,RTC_ADD,ad,1,1000);
	res = HAL_I2C_Master_Receive(&hi2c3,RTC_ADD,tab,len,1000);
}

void writeRTC(uint8_t add, uint8_t *tab, int len){
	uint8_t ad[len+1];
	ad[0]=add;
	for (int i = 0; i< len;i++){
		ad[i+1]=tab[i];
	}
	HAL_StatusTypeDef res;
	res = HAL_I2C_Master_Transmit(&hi2c3,RTC_ADD,ad,len+1,1000);
}

void WriteDate(Date *date){
	uint8_t tab[7];

	tab[0]=((date->second/10)<<4) + (date->second%10);
	tab[1]=((date->minute/10)<<4) + (date->minute%10);
	tab[2]=((date->heure/10)<<4) + (date->heure%10);
	tab[3]=date->day;
	tab[4]=((date->jour/10)<<4) + (date->jour%10);
	tab[5]=((date->mois/10)<<4) + (date->mois%10);
	tab[6]=((date->annee/10)<<4) + (date->annee%10);

	writeRTC(SECOND,tab,7);
}

void redemareRTC(){
	uint8_t pt[1]={0X40};
	writeRTC(ALARM_HOURS,pt,1);
}
