/*
 * V1.0
 */

#include <Eeprom.h>

/****Private fonction */

HAL_StatusTypeDef readEeprom(uint16_t Add,uint8_t *tab, int len){
	HAL_StatusTypeDef res;
	uint8_t add[2];
	add[0]=Add>>8;
	add[1]=Add;

	if ((res = HAL_I2C_Master_Transmit(EEprom.I2C,EepromADD,add,2,1000)) != HAL_OK){
		return res;
	}
	res = HAL_I2C_Master_Receive(EEprom.I2C,EepromADD,tab,len,1000);
	return res;

}

HAL_StatusTypeDef writeEeprom(uint16_t Add,uint8_t *tab, int len){
	HAL_StatusTypeDef res;
	uint8_t data[2+len];
	data[0]=Add>>8;
	data[1]=Add;

	for(int i =0;i<len;i++)
		data[i+2]=tab[i];

	HAL_GPIO_WritePin(EEprom.WC_PORT,EEprom.WC_PIN,0);
	if ((res = HAL_I2C_Master_Transmit(EEprom.I2C,EepromADD,data,2+len,1000)) != HAL_OK){
		HAL_GPIO_WritePin(EEprom.WC_PORT,EEprom.WC_PIN,1);
		return res;
	}


	//introduire un delay
	for(int i =0;i<5000;i++)
		data[2]=i;
	HAL_GPIO_WritePin(EEprom.WC_PORT,EEprom.WC_PIN,1);
	return res;
}

/** Public Fonction*/

void initEprom(I2C_HandleTypeDef *typ,GPIO_TypeDef *port, uint16_t pin){
	EEprom.I2C = typ;
	EEprom.WC_PORT = port;
	EEprom.WC_PIN = pin;
	HAL_GPIO_WritePin(EEprom.WC_PORT,EEprom.WC_PIN,1);
}

