////////////////////////////////////////////////////////////////////////////////
////                          Lib_Int_EEProm.c                              ////
////////////////////////////////////////////////////////////////////////////////
////                                                                        ////
////   void write_int1_eeprom(address, int8 bitPosition, int1 data)         ////
////     Call to write one bit of data                                      ////
////                                                                        ////
////   int1 read_int1_eeprom(address, int8 bitPosition)                     ////
////     Call to read one bit of data                                       ////
////                                                                        ////
////                                                                        ////
////   void write_int16_eeprom(address, int16 data)                         ////
////     Call to write a 16 bit integer                                     ////
////                                                                        ////
////   void write_int16_eeprom(address, int16 data)                         ////
////     Call to read a 16 bit integer                                      ////
////                                                                        ////
////                                                                        ////
////   void write_int32_eeprom(address, int32 data)                         ////
////     Call to write a 32 bit integer                                     ////
////                                                                        ////
////   int16 read_int32_eeprom(address)                                     ////
////     Call to read a 32 bit integer                                      ////
////                                                                        ////
////                                                                        ////
////   void write_float_eeprom(address, float data)                         ////
////     Call to write a floating point number                              ////
////                                                                        ////
////   float read_float_eeprom(address)                                     ////
////     Call to read a floating point number                               ////
////                                                                        ////
////////////////////////////////////////////////////////////////////////////////

#ifndef INTERNAL_EEPROM_UTILITIES
#define INTERNAL_EEPROM_UTILITIES

// Used to adjust the address range
#ifndef INT_EEPROM_ADDRESS
#define INT_EEPROM_ADDRESS  int8
#endif

////////////////////////////////////////////////////////////////////////////////
//// Internal EEPROM Functions
////////////////////////////////////////////////////////////////////////////////

// Purpose:    Write one bit to internal eeprom
// Inputs:     1) An eeprom address
//             2) The bit position (LSB == 0)
//             3) The bit to write
// Outputs:    None
void write_int1_eeprom(INT_EEPROM_ADDRESS address, int8 bitPosition, int1 data)
{
   int8 stored_data;

   stored_data = read_eeprom(address);

   if(data)
   {
      bit_set(stored_data, bitPosition);
   }
   else
   {
      bit_clear(stored_data, bitPosition);
   }

   write_eeprom(address, stored_data);
}


// Purpose:    Read one bit from internal eeprom
// Inputs:     1) An eeprom address
//             2) The bit position (LSB == 0)
// Outputs:    The bit read from internal eeprom
int1 read_int1_eeprom(INT_EEPROM_ADDRESS address, int8 bitPosition)
{
   return bit_test(read_eeprom(address), bitPosition);
}


// Purpose:    Write a 16 bit number to internal eeprom
// Inputs:     1) An eeprom address
//             2) The 16 bit number to write to internal eeprom
// Outputs:    None
void write_int16_eeprom(INT_EEPROM_ADDRESS address, int16 data)
{
   int8 i;

   for(i = 0; i < 2; ++i)
   {
     write_eeprom(address + i, *(&data + i));
   }
}


// Purpose:    Read a 16 bit number from internal eeprom
// Inputs:     An eeprom address
// Outputs:    The 16 bit number read from internal eeprom
int16 read_int16_eeprom(INT_EEPROM_ADDRESS address)
{
   int8  i;
   int16 data;

   for(i = 0; i < 2; ++i)
   {
     *(&data + i) = read_eeprom(address + i);
   }

   return(data);
}


// Purpose:    Write a 32 bit integer to internal eeprom
// Inputs:     1) An eeprom address
//             2) The 32 bit number to write to internal eeprom
// Outputs:    None
void write_int32_eeprom(INT_EEPROM_ADDRESS address, int32 data)
{
   int8 i;

   for(i = 0; i < 4; ++i)
   {
     write_eeprom(address + i, *(&data + i));
   }
}


// Purpose:    Read a 32 bit integer from internal eeprom
// Inputs:     An eeprom address
// Outputs:    The 32 bit integer read from internal eeprom
int32 read_int32_eeprom(INT_EEPROM_ADDRESS address)
{
   int8  i;
   int32 data;

   for(i = 0; i < 4; ++i)
   {
     *(&data + i) = read_eeprom(address + i);
   }

   return data;
}


// Purpose:    Write a floating point number to internal eeprom
// Inputs:     1) An eeprom address. Four eeprom locations will be used.
//             2) The floating point number to write to internal eeprom
// Outputs:    None
//#separate
void write_float_eeprom(INT_EEPROM_ADDRESS address, float data)
{
   int8 i;

   for(i = 0; i < 4; ++i)
   {
     write_eeprom(address + i, *(&data + i));
   }
}


// Purpose:    Read a floating point number from internal eeprom
// Inputs:     An eeprom address
// Outputs:    The floating point number read from the internal eeprom
//#separate
float read_float_eeprom(INT_EEPROM_ADDRESS address)
{
   int8 i;
   float data;

   for(i = 0; i < 4; ++i)
   {
     *(&data + i) = read_eeprom(address + i);
   }

   return data;
}

#endif


/***************************************************************************
 *    Funciones de lectura y escritura de la EEPROM de datos.              *
 *    Pewrmite almacenar los valores del usuario.                          *
 *    La funcion NUMGRAN almacena su segmentos desde posicion 0 a 63       *
 *    por ello la memoria de usuario empieza en 64 (40 Hex)                *
 ***************************************************************************/

 enum Mapa_Memoria
 {
    CTE_CALIB = 0x40,                        //es una Float, ocupa 4 lugares
    LIMITE_INF_PRES = (CTE_CALIB + 4),       //es una Int16, ocupa 2 lugares
    LIMITE_SUP_PRES = (LIMITE_INF_PRES + 2), //es una Int16, ocupa 2 lugares
    LIMITE_MAX_TEMP = (LIMITE_SUP_PRES + 2), //es una Int16, ocupa 2 lugares
    eeOFFSET = (LIMITE_MAX_TEMP + 2),        //es una Int16, ocupa 2 lugares
    eeSPAN = (eeOFFSET + 2),                 //es una Int16, ocupa 2 lugares
    LIMITE_CALIB = (eeSPAN + 2),             //es una Int16, ocupa 2 lugares
    Numero_Conv_AD = (LIMITE_CALIB + 2)      //es una Int8, ocupa 1 lugar
 };

//  CteCal >> 46100
#define CTE_CALIB_DEFAULT 98.88666
//#define CTE_CALIB_LSB_DEFAULT 0xb4

/*   Devuelve el valor del factor de calibración */
float Carga_CteKal()
{
  return (read_float_eeprom(CTE_CALIB));
}

/*   Devuelve el valor del OffSet de calibración */
unsigned long Carga_OffSet()
{
  return (read_int16_eeprom(eeOFFSET));
}

/*   Devuelve el valor del limite inferior de presion */
unsigned long Carga_LIP()
{
  return (read_int16_eeprom(LIMITE_INF_PRES));
}

/*   Devuelve el valor del limite superior de presion */
unsigned long Carga_LSP()
{
  return (read_int16_eeprom(LIMITE_SUP_PRES));
}

//   Lee el valor de conversiones del AD en la EEPROM.
Int8 Lee_NumConv ()
{
    return read_eeprom(Numero_Conv_AD);
}

/*   Inicializa la constante de calibracion. */
//#separate
boolean Inicio_eeprom()
{
    if ((read_eeprom(CTE_CALIB)==0xFF) & (read_eeprom(CTE_CALIB+1)==0xFF) &
          (read_eeprom(CTE_CALIB+2)==0xFF) & (read_eeprom(CTE_CALIB+3)==0xFF))
    return true;
     else
    return false;
}

/*   Guarda el valor de la Constante de Calibracion en la EEPROM.
 *   @parametro CteKal es float.      */
void Guarda_CteKal (float CKal)
{
    if (read_float_eeprom(CTE_CALIB) == CKal)  return;
    write_float_eeprom(CTE_CALIB, CKal);
}

/*   Guarda el valor del OffSEt en la EEPROM.
 *   @parametro OffSet en valor convertidor.      */
void Guarda_OffSet (unsigned long OffSet)
{
    if (read_int16_eeprom(eeOFFSET) == OffSet)  return;
    write_int16_eeprom(eeOFFSET, OffSet);
}

/*   Guarda el valor del limite inferior de presion en la EEPROM.
 *   @parametro LIP en mm de columna de agua.      */
void Guarda_LIP (unsigned long LIP)
{
    if (read_int16_eeprom(LIMITE_INF_PRES) == LIP)  return;
    write_int16_eeprom(LIMITE_INF_PRES, LIP);
}

/*   Guarda el valor del limite superior de presion en la EEPROM.
 *   @parametro LSP en mm de columna de agua.      */
void Guarda_LSP (unsigned long LSP)
{
    if (read_int16_eeprom(LIMITE_SUP_PRES) == LSP)  return;
    write_int16_eeprom(LIMITE_SUP_PRES, LSP);
}

//   Guarda el valor de conversiones del AD en la EEPROM.
void Guarda_NumConv (Int8 NConv)
{
    if (read_eeprom(Numero_Conv_AD) == NConv)  return;
    write_eeprom(Numero_Conv_AD, NConv);
}

/* Funcion de escritura en EEPROM de valores de un Byte */
void Actualiza_dato_EEPROM (unsigned int indice, unsigned int valor)
{
    if (read_eeprom (indice) == valor)
        return;

    write_eeprom (indice, valor);
}
